-
Notifications
You must be signed in to change notification settings - Fork 16
/
bridge.rb
126 lines (101 loc) · 3.02 KB
/
bridge.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
module Hue
class Bridge
attr_accessor :light_id
def self.shared
@shared ||= Bridge.new
end
def self.method_missing(method, *args, &block)
if args.empty?
self.shared.send method
else
self.shared.send method, *args
end
end
def initialize(light_num = nil)
self.light_id = light_num.to_s
end
def uri(*args)
URI [BASE, UUID, args].flatten.reject{|x| x.to_s.strip == ''}.join('/')
end
def status
JSON.parse Net::HTTP.get(Bridge.shared.uri)
end
def lights
status['lights']
end
def identities
Hash[lights.map{|k, v| [k, v['name']] }]
end
def bulbs
@bulbs ||= lights.keys.map{|b| Bulb.new b}
end
def reload
@bulbs = nil
self
end
def schedules
status['schedules']
end
def remove_schedule(schedule_id)
delete uri('schedules', schedule_id)
puts "Removed schedule #{schedule_id}"
end
def remove_all_schedules
ids = schedules.keys.map(&:to_i).sort.reverse
puts "Removing #{ids.size} schedule#{'s' if ids.size != 1}..."
ids.each{|x| remove_schedule x}
end
def display(response = nil, options = {})
if response and response.code.to_s != '200'
puts "Response #{response.code} #{response.message}: #{JSON.parse(response.body).first}"
false
else
if options[:parse]
JSON.parse(response.body)
else
true
end
end
end
def update_state(settings)
update uri('lights', light_id, 'state'), settings if light_id
end
def update_base(settings)
update uri('lights', light_id), settings if light_id
end
def update(*args)
settings = args.pop
url = parse_uri(*args)
request = Net::HTTP::Put.new(url.request_uri, initheader = {'Content-Type' =>'application/json'})
request.body = settings.to_json
display Net::HTTP.new(url.host, url.port).start {|http| http.request(request) }
end
alias :put :update
def delete(*args)
url = parse_uri(*args)
request = Net::HTTP::Delete.new(url.request_uri, initheader = {'Content-Type' =>'application/json'})
display Net::HTTP.new(url.host, url.port).start{|http| http.request(request)}
end
def get(*args)
url = parse_uri(*args)
request = Net::HTTP::Get.new(url.request_uri, initheader = {'Content-Type' =>'application/json'})
display Net::HTTP.new(url.host, url.port).start{|http| http.request(request)}, parse: true
end
def post(*args)
settings = args.pop
url = parse_uri(*args)
request = Net::HTTP::Post.new(url.request_uri, initheader = {'Content-Type' =>'application/json'})
request.body = settings.to_json
display Net::HTTP.new(url.host, url.port).start {|http| http.request(request) }
end
def parse_uri(*args)
[*args].first.is_a?(URI) ? [*args].first : uri(args)
end
def groups
get :groups
end
def group(_id)
get :groups, _id
end
end
end # Hue