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



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_hubsObject



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

#registerObject 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_listObject



70
71
72
# File 'lib/lights.rb', line 70

def request_bulb_list
  get "lights"
end

#request_configObject



58
59
60
# File 'lib/lights.rb', line 58

def request_config
  get "config"
end

#request_datastoreObject



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_listObject



101
102
103
# File 'lib/lights.rb', line 101

def request_group_list
  get "groups"
end

#request_new_bulb_listObject



74
75
76
# File 'lib/lights.rb', line 74

def request_new_bulb_list
  get "lights/new"
end

#request_new_sensor_listObject



78
79
80
# File 'lib/lights.rb', line 78

def request_new_sensor_list
  get "sensors/new"
end

#request_rulesObject



117
118
119
# File 'lib/lights.rb', line 117

def request_rules
  get "rules"
end

#request_scene_listObject



109
110
111
# File 'lib/lights.rb', line 109

def request_scene_list
  get "scenes"
end

#request_schedule_listObject



105
106
107
# File 'lib/lights.rb', line 105

def request_schedule_list
  get "schedules"
end

#request_schedulesObject



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_listObject



97
98
99
# File 'lib/lights.rb', line 97

def request_sensor_list
  get "sensors"
end

#search_newObject



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