hue-lib 0.7.3 → 0.7.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 888f312d7fd5cbc97e0420cdfd93c81ce2384dfc
4
- data.tar.gz: b4925347b45be059603fda074dcdbed11311942f
3
+ metadata.gz: cc8c952537596ce90a96a55c442452834e18fe1f
4
+ data.tar.gz: 4de815463960e79adbbc32c898ab9b626dad3788
5
5
  SHA512:
6
- metadata.gz: 565dcd5268b7948a36bd1bb70f0527015a82609cf02b13d3cc5a323ba912a732fe0dab58b35dc9543f1141a2d762f2c5ae40a69a3ef84358138c52f7e5d82bf2
7
- data.tar.gz: 12675f32773730f522c984812f29ba118db9880bd45a9733702478c8d0b9277a5976c38afb6c42be8ed617b3e3ec74a7901beb8c1c6240c2927dbb865ffade1f
6
+ metadata.gz: 40e1127175b0a0d5717f0ec358674e5ea884632183c8e2f43583d68af68c943b23fc75b47fcd730f1f1bd05ea739f34c6893f181e49ed900d15b4c42237b6d58
7
+ data.tar.gz: f88ae6f675905547f30129ce716d8eb82543d69a522d5951cfb9e9a2d023b88765e33f291c530dc9513854419ff7617f855afcdf17c988da9e8cdcb4ef0fab9d
data/hue-lib.gemspec CHANGED
@@ -3,7 +3,7 @@ $:.push File.expand_path("../lib", __FILE__)
3
3
 
4
4
  Gem::Specification.new do |s|
5
5
  s.name = "hue-lib"
6
- s.version = '0.7.3'
6
+ s.version = '0.7.4'
7
7
  s.authors = ["Birkir A. Barkarson", "Aaron Hurley"]
8
8
  s.email = ["birkirb@stoicviking.net"]
9
9
  s.homepage = "https://github.com/birkirb/hue-lib"
@@ -19,6 +19,7 @@ Gem::Specification.new do |s|
19
19
  s.require_paths = ["lib"]
20
20
 
21
21
  s.add_runtime_dependency("json")
22
+ s.add_development_dependency("rake")
22
23
  s.add_development_dependency("rspec", '>= 2.6.0')
23
24
  s.add_development_dependency("mocha", '>= 0.9.0')
24
25
  s.add_development_dependency("webmock", '>= 1.8.0')
data/lib/hue.rb CHANGED
@@ -5,7 +5,7 @@ require 'securerandom'
5
5
  module Hue
6
6
 
7
7
  DEVICE_TYPE = 'hue-lib'
8
- DEFAULT_UDP_TIMEOUT = 2
8
+ DEFAULT_UDP_TIMEOUT = 5
9
9
  ERROR_DEFAULT_EXISTS = 'Default application already registered.'
10
10
  ERROR_NO_BRIDGE_FOUND = 'No bridge found.'
11
11
 
@@ -22,11 +22,9 @@ module Hue
22
22
  raise Hue::Error.new(ERROR_DEFAULT_EXISTS)
23
23
  else
24
24
  bridge_config = register_bridges.values.first # Assuming one bridge for now
25
- secret = Hue.one_time_uuid
26
- app_config = Hue::Config::Application.new(bridge_config.id, secret)
27
- puts "Registering app...(#{secret})"
28
- instance = Hue::Bridge.new(app_config.id, bridge_config.uri)
29
- instance.register
25
+ puts "Registering new app..."
26
+ instance = Hue::Bridge.register(bridge_config.uri)
27
+ app_config = Hue::Config::Application.new(bridge_config.id, instance.application_id)
30
28
  app_config.write
31
29
  instance
32
30
  end
@@ -51,14 +49,21 @@ module Hue
51
49
  true
52
50
  end
53
51
 
54
- def self.discover(timeout = DEFAULT_UDP_TIMEOUT)
52
+ def self.discover
55
53
  bridges = Hash.new
54
+ udp_discover(bridges)
55
+ nupnp_discover(bridges)
56
+ bridges
57
+ end
58
+
59
+ def self.udp_discover(bridges)
60
+ Hue.logger.info("Bridge UDP Discovery")
56
61
  payload = <<-PAYLOAD
57
62
  M-SEARCH * HTTP/1.1
58
- HOST: 239.255.255.250:1900
59
- MAN: ssdp:discover
60
- MX: 10
61
63
  ST: ssdp:all
64
+ MX: 10
65
+ MAN: ssdp:discover
66
+ HOST: 239.255.255.250:1900
62
67
  PAYLOAD
63
68
  broadcast_address = '239.255.255.250'
64
69
  port_number = 1900
@@ -66,7 +71,7 @@ ST: ssdp:all
66
71
  socket = UDPSocket.new(Socket::AF_INET)
67
72
  socket.send(payload, 0, broadcast_address, port_number)
68
73
 
69
- Timeout.timeout(timeout, Hue::Error) do
74
+ Timeout.timeout(DEFAULT_UDP_TIMEOUT, Hue::Error) do
70
75
  loop do
71
76
  message, (address_family, port, hostname, ip_add) = socket.recvfrom(1024)
72
77
  if message =~ /IpBridge/ && location = /LOCATION: (.*)$/.match(message)
@@ -83,10 +88,28 @@ ST: ssdp:all
83
88
  end
84
89
  end
85
90
  end
86
-
87
- rescue Hue::Error
91
+ rescue Hue::Error => err
92
+ Hue.logger.warn(err)
88
93
  logger.info("UDPSocket timed out.")
89
- bridges
94
+ end
95
+
96
+ def self.nupnp_discover(bridges)
97
+ if bridges.size > 0
98
+ return
99
+ end
100
+
101
+ Hue.logger.info("Bridge NUPNP Discovery")
102
+ response = Net::HTTP.get_response(URI("https://www.meethue.com/api/nupnp"))
103
+ json = JSON.parse(response.body) rescue nil
104
+ if !json.nil?
105
+ json.each do |bridge|
106
+ uuid = bridge['id']
107
+ ip_add = bridge['internalipaddress']
108
+ if !uuid.nil? && !ip_add.nil?
109
+ bridges[uuid] = "http://#{ip_add}/api"
110
+ end
111
+ end
112
+ end
90
113
  end
91
114
 
92
115
  def self.register_bridges
data/lib/hue/bridge.rb CHANGED
@@ -53,9 +53,17 @@ module Hue
53
53
  # ids.each{|x| remove_schedule x}
54
54
  # end
55
55
 
56
+ def self.register(bridge_uri)
57
+ instance = self.new("", bridge_uri)
58
+ instance.register
59
+ instance
60
+ end
61
+
56
62
  def register
57
- create(URI.parse(bridge_uri),
58
- {"username" => application_id, "devicetype" => Hue.device_type})
63
+ response = create(URI.parse(bridge_uri), {"devicetype" => "#{Hue.device_type}@#{Socket.gethostname}"})
64
+ if response.first.has_key?('success')
65
+ @application_id = response.first['success']['username']
66
+ end
59
67
  end
60
68
 
61
69
  def unregister
@@ -6,7 +6,6 @@ module Hue
6
6
  class NotFound < Hue::Error; end;
7
7
 
8
8
  class Abstract
9
-
10
9
  public
11
10
 
12
11
  attr_reader :path, :name
@@ -64,7 +63,7 @@ module Hue
64
63
 
65
64
  def self.read_file(config_file)
66
65
  begin
67
- yaml = YAML.load_file(config_file)
66
+ YAML.load_file(config_file)
68
67
  rescue => err
69
68
  raise Error.new("Failed to read configuration file", err)
70
69
  end
@@ -15,7 +15,12 @@ module Hue
15
15
  end
16
16
 
17
17
  def self.named(name)
18
- yaml = read_file(file_path)
18
+ begin
19
+ yaml = read_file(file_path)
20
+ rescue => err
21
+ Hue.logger.error(err)
22
+ end
23
+
19
24
  if yaml && named_yaml = yaml[name]
20
25
  new(named_yaml[STRING_BRIDGE_ID], named_yaml[STRING_ID], name)
21
26
  else
@@ -62,7 +62,8 @@ describe Hue::Bridge do
62
62
  bridge = described_class.new(new_id, TEST_BRIDGE_URI)
63
63
 
64
64
  it 'should allow registering the new config' do
65
- with_fake_post(nil, {:username => new_id, :devicetype => Hue.device_type})
65
+ mock_socket_hostname
66
+ with_fake_post(nil, {:devicetype => "hue-lib@hostname"})
66
67
  bridge.register
67
68
  end
68
69
  end
data/spec/hue_spec.rb CHANGED
@@ -38,6 +38,7 @@ describe Hue do
38
38
  context 'when attempting discover without a bridge on the network' do
39
39
  before(:each) do
40
40
  mock_udp_no_reply
41
+ mock_nupnp_empty_reply
41
42
  end
42
43
 
43
44
  it 'should return an empty list of bridges' do
@@ -73,7 +74,7 @@ describe Hue do
73
74
  it 'should allow a new default if one doesn\'t exist' do
74
75
  with_temp_config_path do
75
76
  with_fake_post(nil, {}, 'post_success', TEST_UDP_BRIDGE_URI)
76
- with_stdout(/Registering app...(.*)$/) do
77
+ with_stdout(/Registering new app...$/) do
77
78
  described_class.register_default
78
79
  end
79
80
  end
data/spec/spec_helper.rb CHANGED
@@ -92,7 +92,7 @@ def mock_bridge_config_path
92
92
  Hue::Config::Bridge.stubs(:file_path).returns(TEST_CONFIG_BRIDGE_PATH)
93
93
  end
94
94
 
95
- # HUE - UDP
95
+ # HUE - DISCOVERY
96
96
 
97
97
  TEST_UDP_BRIDGE_UUID = '09230030-4c1e-0130-8d83-0018de9ecdd0'
98
98
  TEST_UDP_BRIDGE_HOSTNAME = 'upd-host'
@@ -120,6 +120,11 @@ def mock_udp_no_reply
120
120
  UDPSocket.stubs(:new).returns(socket)
121
121
  end
122
122
 
123
+ def mock_nupnp_empty_reply
124
+ stub_request(:get, "https://www.meethue.com/api/nupnp").
125
+ with(:headers => {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'Host'=>'www.meethue.com', 'User-Agent'=>'Ruby'}).
126
+ to_return(:status => 200, :body => "", :headers => {})
127
+ end
123
128
 
124
129
  # BRIDGE - API CALLS
125
130
 
@@ -177,3 +182,7 @@ def with_fake_delete(named, delete_reply = 'delete_success')
177
182
  stub.should have_been_requested
178
183
  end
179
184
  end
185
+
186
+ def mock_socket_hostname
187
+ Socket.stubs(:gethostname).returns("hostname")
188
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hue-lib
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.3
4
+ version: 0.7.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Birkir A. Barkarson
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2015-08-02 00:00:00.000000000 Z
12
+ date: 2017-04-23 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: json
@@ -25,6 +25,20 @@ dependencies:
25
25
  - - '>='
26
26
  - !ruby/object:Gem::Version
27
27
  version: '0'
28
+ - !ruby/object:Gem::Dependency
29
+ name: rake
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - '>='
33
+ - !ruby/object:Gem::Version
34
+ version: '0'
35
+ type: :development
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - '>='
40
+ - !ruby/object:Gem::Version
41
+ version: '0'
28
42
  - !ruby/object:Gem::Dependency
29
43
  name: rspec
30
44
  requirement: !ruby/object:Gem::Requirement