lifx 0.4.6.1 → 0.4.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGES.md +5 -0
- data/lib/lifx/client.rb +9 -4
- data/lib/lifx/message.rb +4 -6
- data/lib/lifx/network_context.rb +7 -54
- data/lib/lifx/transport_manager.rb +1 -0
- data/lib/lifx/transport_manager/lan.rb +41 -1
- data/lib/lifx/version.rb +1 -1
- data/spec/message_spec.rb +4 -2
- data/spec/spec_helper.rb +3 -3
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e74701d68bb9de49cf00008a94e46f8721c5d3e6
|
4
|
+
data.tar.gz: 8dd72e9b4f78d2bc70d042142879b3c98df2b172
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8c620c92cf6dd96dac5b2abac78563e3e7276fa7a3a08b17f010eb7fd123320a3cce52d807c6c6eb7cd46d595800e8fba539625cf287cb858888ef812566e148
|
7
|
+
data.tar.gz: 98187dcc6c15b1d53c48ae6cd7ed95dc86c781b3218e2ab79a51d8bf4c56570b1b3152b97091b56f3e41ee3455cde8449c1eed5b408cad1d75b88dc48df227f8
|
data/CHANGES.md
CHANGED
data/lib/lifx/client.rb
CHANGED
@@ -14,7 +14,7 @@ module LIFX
|
|
14
14
|
#
|
15
15
|
# @return [Client] A LAN LIFX::Client
|
16
16
|
def lan
|
17
|
-
@lan ||= new
|
17
|
+
@lan ||= new(transport_manager: TransportManager::LAN.new)
|
18
18
|
end
|
19
19
|
end
|
20
20
|
|
@@ -25,9 +25,9 @@ module LIFX
|
|
25
25
|
# @return [NetworkContext] Enclosed network context
|
26
26
|
attr_reader :context
|
27
27
|
|
28
|
-
# @param
|
29
|
-
def initialize(
|
30
|
-
@context = NetworkContext.new(
|
28
|
+
# @param transport_manager: [TransportManager] Specify the {TransportManager}
|
29
|
+
def initialize(transport_manager: required!('transport_manager'))
|
30
|
+
@context = NetworkContext.new(transport_manager: transport_manager)
|
31
31
|
end
|
32
32
|
|
33
33
|
# Default timeout in seconds for discovery
|
@@ -133,5 +133,10 @@ module LIFX
|
|
133
133
|
def flush(timeout: nil)
|
134
134
|
context.flush(timeout: timeout)
|
135
135
|
end
|
136
|
+
|
137
|
+
# Stops everything and cleans up.
|
138
|
+
def stop
|
139
|
+
context.stop
|
140
|
+
end
|
136
141
|
end
|
137
142
|
end
|
data/lib/lifx/message.rb
CHANGED
@@ -10,7 +10,6 @@ module LIFX
|
|
10
10
|
class MessageError < StandardError; end
|
11
11
|
class UnpackError < MessageError; end
|
12
12
|
class PackError < MessageError; end
|
13
|
-
class NoPath < MessageError; end
|
14
13
|
|
15
14
|
class InvalidFrame < UnpackError; end
|
16
15
|
class UnsupportedProtocolVersion < UnpackError; end
|
@@ -88,13 +87,13 @@ module LIFX
|
|
88
87
|
alias_method :addressable?, :addressable
|
89
88
|
|
90
89
|
def_delegators :path, :device_id, :site_id, :tagged
|
91
|
-
|
90
|
+
|
92
91
|
attr_accessor :path, :payload
|
93
92
|
def initialize(*args)
|
94
|
-
if args.count == 3
|
93
|
+
if args.count == 3
|
95
94
|
@path, @message, @payload = args
|
96
95
|
elsif (hash = args.first).is_a?(Hash)
|
97
|
-
path = hash.delete(:path)
|
96
|
+
path = hash.delete(:path) || ProtocolPath.new
|
98
97
|
payload = hash.delete(:payload)
|
99
98
|
|
100
99
|
check_valid_fields!(hash)
|
@@ -124,7 +123,6 @@ module LIFX
|
|
124
123
|
|
125
124
|
def pack
|
126
125
|
raise NoPayload if !payload
|
127
|
-
raise NoPath if !path
|
128
126
|
@message.raw_site = path.raw_site
|
129
127
|
@message.raw_target = path.raw_target
|
130
128
|
@message.tagged = path.tagged?
|
@@ -150,7 +148,7 @@ module LIFX
|
|
150
148
|
%Q{#<LIFX::Message #{attrs}>}
|
151
149
|
end
|
152
150
|
alias_method :inspect, :to_s
|
153
|
-
|
151
|
+
|
154
152
|
protected
|
155
153
|
|
156
154
|
def check_valid_fields!(hash)
|
data/lib/lifx/network_context.rb
CHANGED
@@ -7,7 +7,6 @@ require 'lifx/protocol_path'
|
|
7
7
|
|
8
8
|
module LIFX
|
9
9
|
class NetworkContext
|
10
|
-
include Timers
|
11
10
|
include Logging
|
12
11
|
include Utilities
|
13
12
|
include RequiredKeywordArguments
|
@@ -15,16 +14,12 @@ module LIFX
|
|
15
14
|
|
16
15
|
# NetworkContext stores lights and ties together TransportManager, TagManager and RoutingManager
|
17
16
|
attr_reader :transport_manager, :tag_manager, :routing_manager
|
18
|
-
|
19
|
-
def initialize(
|
17
|
+
|
18
|
+
def initialize(transport_manager: required!('transport_manager'))
|
20
19
|
@devices = {}
|
21
20
|
|
22
|
-
@transport_manager =
|
23
|
-
|
24
|
-
TransportManager::LAN.new
|
25
|
-
else
|
26
|
-
raise ArgumentError.new("Unknown transport method: #{transport}")
|
27
|
-
end
|
21
|
+
@transport_manager = transport_manager
|
22
|
+
@transport_manager.context = self
|
28
23
|
@transport_manager.add_observer(self) do |message: nil, ip: nil, transport: nil|
|
29
24
|
handle_message(message, ip, transport)
|
30
25
|
end
|
@@ -32,9 +27,6 @@ module LIFX
|
|
32
27
|
reset!
|
33
28
|
|
34
29
|
@threads = []
|
35
|
-
@threads << initialize_timer_thread
|
36
|
-
initialize_periodic_refresh
|
37
|
-
initialize_message_rate_updater
|
38
30
|
end
|
39
31
|
|
40
32
|
def discover
|
@@ -106,7 +98,7 @@ module LIFX
|
|
106
98
|
time = light && light.time
|
107
99
|
end
|
108
100
|
|
109
|
-
delay = (messages.count + 1) * (1.0 / message_rate)
|
101
|
+
delay = (messages.count + 1) * (1.0 / message_rate)
|
110
102
|
at_time = ((time.to_f + delay) * 1_000_000_000).to_i
|
111
103
|
messages.each do |m|
|
112
104
|
m.at_time = at_time
|
@@ -146,16 +138,8 @@ module LIFX
|
|
146
138
|
@routing_manager.tags_for_device_id(device.id)
|
147
139
|
end
|
148
140
|
|
149
|
-
def gateways
|
150
|
-
transport_manager.gateways.map(&:keys).flatten.map { |id| lights.with_id(id) }
|
151
|
-
end
|
152
|
-
|
153
|
-
def gateway_connections
|
154
|
-
transport_manager.gateways.map(&:values).flatten
|
155
|
-
end
|
156
|
-
|
157
141
|
def to_s
|
158
|
-
%Q{#<LIFX::NetworkContext
|
142
|
+
%Q{#<LIFX::NetworkContext transport_manager=#{transport_manager}>}
|
159
143
|
end
|
160
144
|
alias_method :inspect, :to_s
|
161
145
|
|
@@ -166,44 +150,13 @@ module LIFX
|
|
166
150
|
|
167
151
|
@routing_manager.update_from_message(message)
|
168
152
|
if !message.tagged?
|
169
|
-
if @devices[message.device_id].nil?
|
153
|
+
if @devices[message.device_id].nil? && message.payload.is_a?(Protocol::Light::State)
|
170
154
|
device = Light.new(context: self, id: message.device_id, site_id: message.site_id)
|
171
|
-
register_device(device)
|
172
155
|
end
|
173
156
|
device = @devices[message.device_id]
|
174
157
|
return if !device # Virgin bulb
|
175
158
|
device.handle_message(message, ip, transport)
|
176
159
|
end
|
177
160
|
end
|
178
|
-
|
179
|
-
def initialize_periodic_refresh
|
180
|
-
timers.every(10) do
|
181
|
-
refresh
|
182
|
-
end
|
183
|
-
end
|
184
|
-
|
185
|
-
def initialize_message_rate_updater
|
186
|
-
timers.every(5) do
|
187
|
-
missing_mesh_firmware = lights.select { |l| l.mesh_firmware(fetch: false).nil? }
|
188
|
-
if missing_mesh_firmware.count > 10
|
189
|
-
send_message(target: Target.new(broadcast: true), payload: Protocol::Device::GetMeshFirmware.new)
|
190
|
-
elsif missing_mesh_firmware.count > 0
|
191
|
-
missing_mesh_firmware.each { |l| l.send_message(Protocol::Device::GetMeshFirmware.new) }
|
192
|
-
else
|
193
|
-
@message_rate = lights.all? do |light|
|
194
|
-
m = light.mesh_firmware(fetch: false)
|
195
|
-
m && m >= '1.2'
|
196
|
-
end ? 20 : 5
|
197
|
-
gateway_connections.each do |connection|
|
198
|
-
connection.set_message_rate(@message_rate)
|
199
|
-
end
|
200
|
-
end
|
201
|
-
end
|
202
|
-
end
|
203
|
-
|
204
|
-
DEFAULT_MESSAGING_RATE = 5 # per second
|
205
|
-
def message_rate
|
206
|
-
@message_rate || 5
|
207
|
-
end
|
208
161
|
end
|
209
162
|
end
|
@@ -3,15 +3,20 @@ require 'lifx/site'
|
|
3
3
|
module LIFX
|
4
4
|
module TransportManager
|
5
5
|
class LAN < Base
|
6
|
+
include Timers
|
6
7
|
def initialize(bind_ip: '0.0.0.0', send_ip: Config.broadcast_ip, port: 56700, peer_port: 56750)
|
7
8
|
super
|
8
9
|
@bind_ip = bind_ip
|
9
10
|
@send_ip = send_ip
|
10
11
|
@port = port
|
11
12
|
@peer_port = peer_port
|
12
|
-
|
13
|
+
|
13
14
|
@sites = {}
|
15
|
+
@threads = []
|
16
|
+
@threads << initialize_timer_thread
|
14
17
|
initialize_transports
|
18
|
+
initialize_periodic_refresh
|
19
|
+
initialize_message_rate_updater
|
15
20
|
end
|
16
21
|
|
17
22
|
def flush(**options)
|
@@ -49,6 +54,7 @@ module LIFX
|
|
49
54
|
|
50
55
|
def stop
|
51
56
|
stop_discovery
|
57
|
+
@threads.each(&:kill)
|
52
58
|
@transport.close
|
53
59
|
@sites.values.each do |site|
|
54
60
|
site.stop
|
@@ -103,8 +109,42 @@ module LIFX
|
|
103
109
|
@sites.values.map(&:gateways)
|
104
110
|
end
|
105
111
|
|
112
|
+
def gateway_connections
|
113
|
+
gateways.map(&:values).flatten
|
114
|
+
end
|
115
|
+
|
106
116
|
protected
|
107
117
|
|
118
|
+
def initialize_periodic_refresh
|
119
|
+
timers.every(10) do
|
120
|
+
context.refresh
|
121
|
+
end
|
122
|
+
end
|
123
|
+
|
124
|
+
def initialize_message_rate_updater
|
125
|
+
timers.every(5) do
|
126
|
+
missing_mesh_firmware = context.lights.alive.select { |l| l.mesh_firmware(fetch: false).nil? }
|
127
|
+
if missing_mesh_firmware.count > 10
|
128
|
+
context.send_message(target: Target.new(broadcast: true), payload: Protocol::Device::GetMeshFirmware.new)
|
129
|
+
elsif missing_mesh_firmware.count > 0
|
130
|
+
missing_mesh_firmware.each { |l| l.send_message(Protocol::Device::GetMeshFirmware.new) }
|
131
|
+
else
|
132
|
+
@message_rate = context.lights.alive.all? do |light|
|
133
|
+
m = light.mesh_firmware(fetch: false)
|
134
|
+
m && m >= '1.2'
|
135
|
+
end ? 20 : 5
|
136
|
+
gateway_connections.each do |connection|
|
137
|
+
connection.set_message_rate(@message_rate)
|
138
|
+
end
|
139
|
+
end
|
140
|
+
end
|
141
|
+
end
|
142
|
+
|
143
|
+
DEFAULT_MESSAGING_RATE = 5 # per second
|
144
|
+
def message_rate
|
145
|
+
@message_rate || 5
|
146
|
+
end
|
147
|
+
|
108
148
|
def initialize_transports
|
109
149
|
create_broadcast_transport
|
110
150
|
create_peer_transport
|
data/lib/lifx/version.rb
CHANGED
data/spec/message_spec.rb
CHANGED
@@ -69,8 +69,10 @@ describe LIFX::Message do
|
|
69
69
|
context 'no path' do
|
70
70
|
let(:msg) { LIFX::Message.new(payload: LIFX::Protocol::Device::SetPower.new) }
|
71
71
|
|
72
|
-
it '
|
73
|
-
|
72
|
+
it 'defaults to null site and target' do
|
73
|
+
unpacked = LIFX::Message.unpack(msg.pack)
|
74
|
+
expect(unpacked.path.site_id).to eq('000000000000')
|
75
|
+
expect(unpacked.path.device_id).to eq('000000000000')
|
74
76
|
end
|
75
77
|
end
|
76
78
|
|
data/spec/spec_helper.rb
CHANGED
@@ -14,10 +14,10 @@ shared_context 'integration', integration: true do
|
|
14
14
|
c = LIFX::Client.lan
|
15
15
|
begin
|
16
16
|
c.discover! do
|
17
|
-
c.tags.include?('
|
17
|
+
c.tags.include?('_Test') && c.lights.with_tag('_Test').count > 0
|
18
18
|
end
|
19
19
|
rescue Timeout::Error
|
20
|
-
raise "Could not find any lights with tag
|
20
|
+
raise "Could not find any lights with tag _Test in #{c.lights.inspect}"
|
21
21
|
end
|
22
22
|
c
|
23
23
|
end
|
@@ -27,7 +27,7 @@ shared_context 'integration', integration: true do
|
|
27
27
|
lifx.flush
|
28
28
|
end
|
29
29
|
|
30
|
-
let(:lights) { lifx.lights.with_tag('
|
30
|
+
let(:lights) { lifx.lights.with_tag('_Test') }
|
31
31
|
let(:light) { lights.first }
|
32
32
|
end
|
33
33
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: lifx
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.4.
|
4
|
+
version: 0.4.7
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jack Chen (chendo)
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-04-
|
11
|
+
date: 2014-04-16 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bindata
|