Class: Lights

Inherits:
Object
  • Object
show all
Defined in:
lib/lights.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#bulbsObject (readonly)

Returns the value of attribute bulbs.



15
16
17
# File 'lib/lights.rb', line 15

def bulbs
  @bulbs
end

#usernameObject (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



64
65
66
# File 'lib/lights.rb', line 64

def add_bulb(id,bulb_data)
  @bulbs << Bulb.new( id, bulb_data )
end

#create_group(group) ⇒ Object



139
140
141
# File 'lib/lights.rb', line 139

def create_group( group )
  post "groups", group
end

#create_scene(scene) ⇒ Object



143
144
145
# File 'lib/lights.rb', line 143

def create_scene( scene )
  post "scenes/#{scene.id}", scene
end

#delete_group(id) ⇒ Object



151
152
153
# File 'lib/lights.rb', line 151

def delete_group( id )
  delete "groups/#{id}"
end

#delete_scene(id) ⇒ Object



147
148
149
# File 'lib/lights.rb', line 147

def delete_scene( id )
  delete "scenes/#{id}"
end

#delete_user(username) ⇒ Object



163
164
165
# File 'lib/lights.rb', line 163

def delete_user( username )
  delete "config/whitelist/#{username}"
end

#discover_hubsObject



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/lights.rb', line 27

def discover_hubs
  http = Net::HTTP.new("discovery.meethue.com",443)
  http.use_ssl = true
  request = Net::HTTP::Get.new( "/" )
  response = http.request request
  
  case response.code.to_i
  when 200
    result = JSON.parse( response.body )
    result.each { |b| @bridges << Bridge.new(b) } 
  else
    @logger.fatal "Could not discover bridge. HTTP error #{response.code}"
    @logger.fatal "Response: #{response.body}"
    raise "Unknown error" 
  end
  @bridges
end

#edit_bulb(bulb) ⇒ Object



155
156
157
# File 'lib/lights.rb', line 155

def edit_bulb( bulb )
  put "lights/#{bulb.id}", bulb
end

#edit_group(group) ⇒ Object



159
160
161
# File 'lib/lights.rb', line 159

def edit_group( group )
  put "groups/#{group.id}", group
end

#registerObject Also known as: register_username



45
46
47
48
49
50
51
52
53
54
55
# File 'lib/lights.rb', line 45

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



84
85
86
87
# File 'lib/lights.rb', line 84

def request_bulb_info( id )
  response = get "lights/#{id}"
  Bulb.new(id,response)
end

#request_bulb_listObject



72
73
74
# File 'lib/lights.rb', line 72

def request_bulb_list
  get "lights"
end

#request_configObject



60
61
62
# File 'lib/lights.rb', line 60

def request_config
  get "config"
end

#request_datastoreObject



127
128
129
# File 'lib/lights.rb', line 127

def request_datastore
  get ""
end

#request_group_info(id) ⇒ Object



89
90
91
92
# File 'lib/lights.rb', line 89

def request_group_info( id )
  response = get "groups/#{id}"
  Group.new(id,response)
end

#request_group_listObject



103
104
105
# File 'lib/lights.rb', line 103

def request_group_list
  get "groups"
end

#request_new_bulb_listObject



76
77
78
# File 'lib/lights.rb', line 76

def request_new_bulb_list
  get "lights/new"
end

#request_new_sensor_listObject



80
81
82
# File 'lib/lights.rb', line 80

def request_new_sensor_list
  get "sensors/new"
end

#request_rulesObject



119
120
121
# File 'lib/lights.rb', line 119

def request_rules
  get "rules"
end

#request_scene_listObject



111
112
113
# File 'lib/lights.rb', line 111

def request_scene_list
  get "scenes"
end

#request_schedule_listObject



107
108
109
# File 'lib/lights.rb', line 107

def request_schedule_list
  get "schedules"
end

#request_schedulesObject



123
124
125
# File 'lib/lights.rb', line 123

def request_schedules
  get "schedules"
end

#request_sensor_info(id) ⇒ Object



94
95
96
97
# File 'lib/lights.rb', line 94

def request_sensor_info( id )
  response = get "sensors/#{id}"
  Sensor.new(id,response)
end

#request_sensor_listObject



99
100
101
# File 'lib/lights.rb', line 99

def request_sensor_list
  get "sensors"
end

#search_newObject



68
69
70
# File 'lib/lights.rb', line 68

def search_new
  post "lights"
end

#set_bulb_state(id, state) ⇒ Object



131
132
133
# File 'lib/lights.rb', line 131

def set_bulb_state( id, state )
  put "lights/#{id}/state", state
end

#set_group_state(id, state) ⇒ Object



135
136
137
# File 'lib/lights.rb', line 135

def set_group_state( id, state )
  put "groups/#{id}/action", state
end