Class: Lights
- Inherits:
-
Object
- Object
- Lights
- Defined in:
- lib/lights.rb
Instance Attribute Summary collapse
-
#bulbs ⇒ Object
readonly
Returns the value of attribute bulbs.
-
#username ⇒ Object
readonly
Returns the value of attribute username.
Instance Method Summary collapse
- #add_bulb(id, bulb_data) ⇒ Object
- #create_group(group) ⇒ Object
- #create_scene(scene) ⇒ Object
- #delete_group(id) ⇒ Object
- #delete_user(username) ⇒ Object
- #discover_hubs ⇒ Object
- #edit_bulb(bulb) ⇒ Object
- #edit_group(group) ⇒ Object
-
#initialize(ip, username = nil) ⇒ Lights
constructor
A new instance of Lights.
- #register ⇒ Object (also: #register_username)
- #request_bulb_info(id) ⇒ Object
- #request_bulb_list ⇒ Object
- #request_config ⇒ Object
- #request_datastore ⇒ Object
- #request_group_info(id) ⇒ Object
- #request_group_list ⇒ Object
- #request_new_bulb_list ⇒ Object
- #request_new_sensor_list ⇒ Object
- #request_rules ⇒ Object
- #request_scene_list ⇒ Object
- #request_schedule_list ⇒ Object
- #request_schedules ⇒ Object
- #request_sensor_info(id) ⇒ Object
- #request_sensor_list ⇒ Object
- #search_new ⇒ Object
- #set_bulb_state(id, state) ⇒ Object
- #set_group_state(id, state) ⇒ Object
Constructor Details
#initialize(ip, username = nil) ⇒ Lights
Returns a new instance of Lights.
16 17 18 19 20 21 22 23 24 25 |
# File 'lib/lights.rb', line 16 def initialize(ip,username=nil) @ip = ip @username = username @http = Net::HTTP.new(ip,80) @bulbs = [] @groups = [] @bridges = [] @logger = Logger.new(STDERR) @logger.level = LoggerConfig::LIGHTS_LEVEL end |
Instance Attribute Details
#bulbs ⇒ Object (readonly)
Returns the value of attribute bulbs.
15 16 17 |
# File 'lib/lights.rb', line 15 def bulbs @bulbs end |
#username ⇒ Object (readonly)
Returns the value of attribute username.
15 16 17 |
# File 'lib/lights.rb', line 15 def username @username end |
Instance Method Details
#add_bulb(id, bulb_data) ⇒ Object
62 63 64 |
# File 'lib/lights.rb', line 62 def add_bulb(id,bulb_data) @bulbs << Bulb.new( id, bulb_data ) end |
#create_group(group) ⇒ Object
137 138 139 |
# File 'lib/lights.rb', line 137 def create_group( group ) post "groups", group end |
#create_scene(scene) ⇒ Object
141 142 143 |
# File 'lib/lights.rb', line 141 def create_scene( scene ) put "scenes/#{scene.id}", scene end |
#delete_group(id) ⇒ Object
145 146 147 |
# File 'lib/lights.rb', line 145 def delete_group( id ) delete "groups/#{id}" end |
#delete_user(username) ⇒ Object
157 158 159 |
# File 'lib/lights.rb', line 157 def delete_user( username ) delete "config/whitelist/#{username}" end |
#discover_hubs ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/lights.rb', line 27 def discover_hubs http = Net::HTTP.new("www.meethue.com",443) http.use_ssl = true request = Net::HTTP::Get.new( "/api/nupnp" ) response = http.request request case response.code.to_i when 200 result = JSON.parse( response.body ) result.each { |b| @bridges << Bridge.new(b) } else raise "Unknown error" end @bridges end |
#edit_bulb(bulb) ⇒ Object
149 150 151 |
# File 'lib/lights.rb', line 149 def edit_bulb( bulb ) put "lights/#{bulb.id}", bulb end |
#edit_group(group) ⇒ Object
153 154 155 |
# File 'lib/lights.rb', line 153 def edit_group( group ) put "groups/#{group.id}", group end |
#register ⇒ Object Also known as: register_username
43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/lights.rb', line 43 def register data = { "devicetype"=>"lights" } response = @http.post "/api", data.to_json result = JSON.parse(response.body).first if result.has_key? "error" process_error result elsif result["success"] @username = result["success"]["username"] end result end |
#request_bulb_info(id) ⇒ Object
82 83 84 85 |
# File 'lib/lights.rb', line 82 def request_bulb_info( id ) response = get "lights/#{id}" Bulb.new(id,response) end |
#request_bulb_list ⇒ Object
70 71 72 |
# File 'lib/lights.rb', line 70 def request_bulb_list get "lights" end |
#request_config ⇒ Object
58 59 60 |
# File 'lib/lights.rb', line 58 def request_config get "config" end |
#request_datastore ⇒ Object
125 126 127 |
# File 'lib/lights.rb', line 125 def request_datastore get "" end |
#request_group_info(id) ⇒ Object
87 88 89 90 |
# File 'lib/lights.rb', line 87 def request_group_info( id ) response = get "groups/#{id}" Group.new(id,response) end |
#request_group_list ⇒ Object
101 102 103 |
# File 'lib/lights.rb', line 101 def request_group_list get "groups" end |
#request_new_bulb_list ⇒ Object
74 75 76 |
# File 'lib/lights.rb', line 74 def request_new_bulb_list get "lights/new" end |
#request_new_sensor_list ⇒ Object
78 79 80 |
# File 'lib/lights.rb', line 78 def request_new_sensor_list get "sensors/new" end |
#request_rules ⇒ Object
117 118 119 |
# File 'lib/lights.rb', line 117 def request_rules get "rules" end |
#request_scene_list ⇒ Object
109 110 111 |
# File 'lib/lights.rb', line 109 def request_scene_list get "scenes" end |
#request_schedule_list ⇒ Object
105 106 107 |
# File 'lib/lights.rb', line 105 def request_schedule_list get "schedules" end |
#request_schedules ⇒ Object
121 122 123 |
# File 'lib/lights.rb', line 121 def request_schedules get "schedules" end |
#request_sensor_info(id) ⇒ Object
92 93 94 95 |
# File 'lib/lights.rb', line 92 def request_sensor_info( id ) response = get "sensors/#{id}" Sensor.new(id,response) end |
#request_sensor_list ⇒ Object
97 98 99 |
# File 'lib/lights.rb', line 97 def request_sensor_list get "sensors" end |
#search_new ⇒ Object
66 67 68 |
# File 'lib/lights.rb', line 66 def search_new post "lights" end |
#set_bulb_state(id, state) ⇒ Object
129 130 131 |
# File 'lib/lights.rb', line 129 def set_bulb_state( id, state ) put "lights/#{id}/state", state end |
#set_group_state(id, state) ⇒ Object
133 134 135 |
# File 'lib/lights.rb', line 133 def set_group_state( id, state ) put "groups/#{id}/action", state end |