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 +4 -4
- data/hue-lib.gemspec +2 -1
- data/lib/hue.rb +37 -14
- data/lib/hue/bridge.rb +10 -2
- data/lib/hue/config/abstract.rb +1 -2
- data/lib/hue/config/application.rb +6 -1
- data/spec/hue/bridge_spec.rb +2 -1
- data/spec/hue_spec.rb +2 -1
- data/spec/spec_helper.rb +10 -1
- metadata +16 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: cc8c952537596ce90a96a55c442452834e18fe1f
|
4
|
+
data.tar.gz: 4de815463960e79adbbc32c898ab9b626dad3788
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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.
|
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 =
|
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
|
-
|
26
|
-
|
27
|
-
|
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
|
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(
|
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
|
-
|
91
|
+
rescue Hue::Error => err
|
92
|
+
Hue.logger.warn(err)
|
88
93
|
logger.info("UDPSocket timed out.")
|
89
|
-
|
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
|
-
|
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
|
data/lib/hue/config/abstract.rb
CHANGED
@@ -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
|
-
|
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
|
-
|
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
|
data/spec/hue/bridge_spec.rb
CHANGED
@@ -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
|
-
|
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
|
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 -
|
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.
|
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:
|
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
|