Skip to content

Instantly share code, notes, and snippets.

@topnotcher
Created March 28, 2015 03:20
Show Gist options
  • Save topnotcher/06cb4528d103b5be63d5 to your computer and use it in GitHub Desktop.
Save topnotcher/06cb4528d103b5be63d5 to your computer and use it in GitHub Desktop.
require 'net/http'
require 'json'
class Hue
@@key = '9da762a1b709cc5979778f37c623b452'
@@hue = 'http://192.168.1.152/api'
def initialize
end
def request(path,data)
url = URI.parse(api_url(path))
if data
req = Net::HTTP::Put.new(url.path.to_s, initheader = {'Content-Type' => 'text/json'})
req.body = data
else
req = Net::HTTP::Get.new(url.path.to_s)
end
res = Net::HTTP.start(url.host, url.port).request(req)
return res.body
end
def get(path)
request(path,nil)
end
def put(path,data)
request(path,data)
end
def lights
res = get('lights')
lights = {}
JSON::parse(res).each do |k,v|
lights[k] = v["name"]
end
return lights
end
def on(id,on)
put('lights/'+id.to_s+'/state',{'on' => on}.to_json)
end
def on_all(on)
lights.each do |k,v|
next if v == 'lux'
on(k,on)
end
end
def alert(id)
put('lights/'+id.to_s+'/state',{'alert' => 'select'}.to_json)
end
def color(id,hue,sat,bri)
data = {'on' => true, 'sat' => sat.to_i, 'bri' => bri.to_i, 'hue' => hue.to_i}.to_json
put('lights/'+id.to_s+'/state',data)
end
def color_all(hue,sat,bri)
lights.each do |k,v|
next if v == 'lux'
color(k,hue,sat,bri)
end
end
def api_url(cmd)
@@hue + '/' + @@key + '/' + cmd
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment