Module: Hue

Defined in:
lib/hue.rb,
lib/hue/bulb.rb,
lib/hue/bridge.rb,
lib/hue/colors.rb,
lib/hue/colors/xy.rb,
lib/hue/colors/rgb.rb,
lib/hue/colors/color.rb,
lib/hue/config/bridge.rb,
lib/hue/config/abstract.rb,
lib/hue/animations/candle.rb,
lib/hue/animations/sunrise.rb,
lib/hue/config/application.rb,
lib/hue/colors/hue_saturation.rb,
lib/hue/colors/color_temperature.rb

Defined Under Namespace

Modules: API, Animations, Colors, Config Classes: Bridge, Bulb, Error

Constant Summary collapse

DEVICE_TYPE =
'hue-lib'
DEFAULT_UDP_TIMEOUT =
2
ERROR_DEFAULT_EXISTS =
'Default application already registered.'
ERROR_NO_BRIDGE_FOUND =
'No bridge found.'

Class Method Summary collapse

Class Method Details

.applicationObject



35
36
37
38
39
40
41
42
43
44
45
# File 'lib/hue.rb', line 35

def self.application
  application_config = Hue::Config::Application.default
  bridge_config = Hue::Config::Bridge.find(application_config.bridge_id)
  bridge_config ||= register_bridges[application_config.bridge_id]

  if bridge_config.nil?
    raise Error.new("Unable to find bridge: #{application_config.bridge_id}")
  end

  Hue::Bridge.new(application_config.id, bridge_config.uri)
end

.device_typeObject



12
13
14
# File 'lib/hue.rb', line 12

def self.device_type
  DEVICE_TYPE
end

.discover(timeout = DEFAULT_UDP_TIMEOUT) ⇒ Object



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
# File 'lib/hue.rb', line 54

def self.discover(timeout = DEFAULT_UDP_TIMEOUT)
  bridges = Hash.new
  payload = <<-PAYLOAD
M-SEARCH * HTTP/1.1
HOST: 239.255.255.250:1900
MAN: ssdp:discover
MX: 10
ST: ssdp:all
  PAYLOAD
  broadcast_address = '239.255.255.250'
  port_number = 1900

  socket = UDPSocket.new(Socket::AF_INET)
  socket.send(payload, 0, broadcast_address, port_number)

  Timeout.timeout(timeout, Hue::Error) do
    loop do
      message, (address_family, port, hostname, ip_add) = socket.recvfrom(1024)
      if message =~ /IpBridge/ && location = /LOCATION: (.*)$/.match(message)
        if uuid_match = /uuid:(.{36})/.match(message)
          # Assume this is Philips Hue for now.
          uuid = uuid_match.captures.first
          if bridges[uuid].nil?
            logger.info("Found bridge (#{hostname}:#{port}) with uuid: #{uuid}")
          end
          bridges[uuid] = "http://#{ip_add}/api"
        end
      else
        logger.debug("Found #{hostname}:#{port}: #{message}")
      end
    end
  end

rescue Hue::Error
  logger.info("UDPSocket timed out.")
  bridges
end

.loggerObject



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/hue.rb', line 133

def self.logger
  if !defined?(@@logger)
    log_dir_path = File.join('/var', 'log', 'hue')
    begin
      FileUtils.mkdir_p(log_dir_path)
    rescue Errno::EACCES
      log_dir_path = File.join(ENV['HOME'], ".#{device_type}")
      FileUtils.mkdir_p(log_dir_path)
    end

    log_file_path = File.join(log_dir_path, 'hue-lib.log')
    log_file = File.new(log_file_path, File::WRONLY | File::APPEND | File::CREAT)
    @@logger = Logger.new(log_file)
    @@logger.level = Logger::INFO
  end

  @@logger
end

.one_time_uuidObject



16
17
18
# File 'lib/hue.rb', line 16

def self.one_time_uuid
  SecureRandom.hex(16)
end

.percent_to_unit_interval(value) ⇒ Object



152
153
154
155
156
157
158
# File 'lib/hue.rb', line 152

def self.percent_to_unit_interval(value)
  if percent = /(\d+)%/.match(value.to_s)
    percent.captures.first.to_i / 100.0
  else
    nil
  end
end

.register_bridgesObject



92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/hue.rb', line 92

def self.register_bridges
  bridges = self.discover
  if bridges.empty?
    raise Error.new(ERROR_NO_BRIDGE_FOUND)
  else
    bridges.inject(Hash.new) do |hash, (id, ip)|
      config = Hue::Config::Bridge.new(id, ip)
      config.write
      hash[id] = config
      hash
    end
  end
end

.register_defaultObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/hue.rb', line 20

def self.register_default
  if (Hue::Config::Application.default rescue nil)
    raise Hue::Error.new(ERROR_DEFAULT_EXISTS)
  else
    bridge_config = register_bridges.values.first # Assuming one bridge for now
    secret = Hue.one_time_uuid
    app_config = Hue::Config::Application.new(bridge_config.id, secret)
    puts "Registering app...(#{secret})"
    instance = Hue::Bridge.new(app_config.id, bridge_config.uri)
    instance.register
    app_config.write
    instance
  end
end

.remove_defaultObject



47
48
49
50
51
52
# File 'lib/hue.rb', line 47

def self.remove_default
  instance = application
  instance.unregister
  Hue::Config::Application.default.delete
  true
end