forked from AaronH/RubyHue
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbridge.rb
92 lines (72 loc) · 2.08 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
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)
if response and response.code.to_s != '200'
puts "Response #{response.code} #{response.message}: #{JSON.parse(response.body).first}"
false
else
true
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(url, settings = {})
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
def delete(url)
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
end
end # Hue