lifx-lan 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (73) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +17 -0
  3. data/.travis.yml +8 -0
  4. data/.yardopts +3 -0
  5. data/CHANGES.md +45 -0
  6. data/Gemfile +19 -0
  7. data/LICENSE.txt +23 -0
  8. data/README.md +15 -0
  9. data/Rakefile +20 -0
  10. data/bin/lifx-snoop +50 -0
  11. data/examples/auto-off/auto-off.rb +34 -0
  12. data/examples/blink/blink.rb +19 -0
  13. data/examples/identify/identify.rb +69 -0
  14. data/examples/travis-build-light/build-light.rb +57 -0
  15. data/lib/bindata_ext/bool.rb +30 -0
  16. data/lib/bindata_ext/record.rb +11 -0
  17. data/lib/lifx-lan.rb +27 -0
  18. data/lib/lifx/lan/client.rb +149 -0
  19. data/lib/lifx/lan/color.rb +199 -0
  20. data/lib/lifx/lan/config.rb +17 -0
  21. data/lib/lifx/lan/firmware.rb +60 -0
  22. data/lib/lifx/lan/gateway_connection.rb +185 -0
  23. data/lib/lifx/lan/light.rb +440 -0
  24. data/lib/lifx/lan/light_collection.rb +111 -0
  25. data/lib/lifx/lan/light_target.rb +185 -0
  26. data/lib/lifx/lan/logging.rb +14 -0
  27. data/lib/lifx/lan/message.rb +168 -0
  28. data/lib/lifx/lan/network_context.rb +188 -0
  29. data/lib/lifx/lan/observable.rb +66 -0
  30. data/lib/lifx/lan/protocol/address.rb +25 -0
  31. data/lib/lifx/lan/protocol/device.rb +387 -0
  32. data/lib/lifx/lan/protocol/header.rb +24 -0
  33. data/lib/lifx/lan/protocol/light.rb +142 -0
  34. data/lib/lifx/lan/protocol/message.rb +19 -0
  35. data/lib/lifx/lan/protocol/metadata.rb +23 -0
  36. data/lib/lifx/lan/protocol/payload.rb +12 -0
  37. data/lib/lifx/lan/protocol/sensor.rb +31 -0
  38. data/lib/lifx/lan/protocol/type.rb +204 -0
  39. data/lib/lifx/lan/protocol/wan.rb +51 -0
  40. data/lib/lifx/lan/protocol/wifi.rb +102 -0
  41. data/lib/lifx/lan/protocol_path.rb +85 -0
  42. data/lib/lifx/lan/required_keyword_arguments.rb +12 -0
  43. data/lib/lifx/lan/routing_manager.rb +114 -0
  44. data/lib/lifx/lan/routing_table.rb +48 -0
  45. data/lib/lifx/lan/seen.rb +25 -0
  46. data/lib/lifx/lan/site.rb +97 -0
  47. data/lib/lifx/lan/tag_manager.rb +111 -0
  48. data/lib/lifx/lan/tag_table.rb +49 -0
  49. data/lib/lifx/lan/target.rb +24 -0
  50. data/lib/lifx/lan/thread.rb +13 -0
  51. data/lib/lifx/lan/timers.rb +29 -0
  52. data/lib/lifx/lan/transport.rb +46 -0
  53. data/lib/lifx/lan/transport/tcp.rb +91 -0
  54. data/lib/lifx/lan/transport/udp.rb +87 -0
  55. data/lib/lifx/lan/transport_manager.rb +43 -0
  56. data/lib/lifx/lan/transport_manager/lan.rb +169 -0
  57. data/lib/lifx/lan/utilities.rb +36 -0
  58. data/lib/lifx/lan/version.rb +5 -0
  59. data/lifx-lan.gemspec +26 -0
  60. data/spec/color_spec.rb +43 -0
  61. data/spec/gateway_connection_spec.rb +30 -0
  62. data/spec/integration/client_spec.rb +42 -0
  63. data/spec/integration/light_spec.rb +56 -0
  64. data/spec/integration/tags_spec.rb +42 -0
  65. data/spec/light_collection_spec.rb +37 -0
  66. data/spec/message_spec.rb +183 -0
  67. data/spec/protocol_path_spec.rb +109 -0
  68. data/spec/routing_manager_spec.rb +25 -0
  69. data/spec/routing_table_spec.rb +23 -0
  70. data/spec/spec_helper.rb +56 -0
  71. data/spec/transport/udp_spec.rb +44 -0
  72. data/spec/transport_spec.rb +14 -0
  73. metadata +187 -0
@@ -0,0 +1,24 @@
1
+ module LIFX
2
+ module LAN
3
+ module Protocol
4
+ module HeaderFields
5
+ def HeaderFields.included(mod)
6
+ mod.instance_eval do
7
+ uint16 :msg_size # Size of entire message in bytes including this field.
8
+ bit12le :protocol, value: 1024 # Protocol number, always 1024 for LIFX Protocol messages
9
+ bool_bit1 :addressable, value: true # Message includes an address, always true for LIFX Protocol messages
10
+ bool_bit1 :tagged # Indicates that the message addresses device tagged with specified tags
11
+ bit2le :origin, value: 0 # Message origin indicator, should be zero
12
+ uint32 :source # Source identifier. Unique 32 bit value sent by the client, used for NAT and responses
13
+ end
14
+ end
15
+ end
16
+
17
+ class Header < BinData::Record
18
+ endian :little
19
+
20
+ include HeaderFields
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,142 @@
1
+ module LIFX
2
+ module LAN
3
+ module Protocol
4
+ # @api private
5
+ module Light
6
+ module Waveform
7
+ SAW = 0
8
+ SINE = 1
9
+ HALF_SINE = 2
10
+ TRIANGLE = 3
11
+ PULSE = 4
12
+ end
13
+
14
+ class Hsbk < Payload
15
+ endian :little
16
+
17
+ uint16 :hue # 0..65_535 scaled to 0° - 360°
18
+ uint16 :saturation # 0..65_535 scaled to 0% - 100%
19
+ uint16 :brightness # 0..65_535 scaled to 0% - 100%
20
+ uint16 :kelvin # Explicit 2_400..9_000
21
+ end
22
+
23
+ # Introspect the light state
24
+ class Get < Payload
25
+ endian :little
26
+ end
27
+
28
+ # class Set < Payload
29
+ # endian :little
30
+
31
+ # uint8 :stream # 0 is no stream.
32
+ # hsbk :color
33
+ # uint32 :duration # Milliseconds.
34
+ # end
35
+
36
+ # Light state including identifying meta data
37
+ class State < Payload
38
+ endian :little
39
+
40
+ hsbk :color
41
+ int16 :dim
42
+ uint16 :power
43
+ string :label, length: 32, trim_padding: true
44
+ uint64 :tags
45
+ end
46
+
47
+ # Simple light color control
48
+ class SetColor < Payload
49
+ endian :little
50
+
51
+ uint8 :stream, value: 0 # Reserved for LIFX use, must be 0 (0 is no stream)
52
+ hsbk :color # Color value
53
+ uint32 :duration # Transition duration in milliseconds
54
+ end
55
+
56
+ # Instrospect light power level on device
57
+ class GetPower < Payload
58
+ endian :little
59
+
60
+ end
61
+
62
+ # Set the power level of a light
63
+ class SetPower < Payload
64
+ endian :little
65
+
66
+ uint16 :level # Power level between 0..65535. Zero implies standby and non-zero sets a corresponding power draw level on device
67
+ uint32 :duration # Duration in milliseconds for the power transition between 0..65535
68
+ end
69
+
70
+ # Current power level of device
71
+ class StatePower < Payload
72
+ endian :little
73
+
74
+ uint16 :level
75
+ end
76
+
77
+ # Does below code work with LIFX20 ?
78
+ # ----------------------------------------------------------------
79
+ class SetWaveform < Payload
80
+ endian :little
81
+
82
+ uint8 :stream # 0 is no stream.
83
+ bool :transient
84
+ hsbk :color
85
+ uint32 :period # Milliseconds per cycle.
86
+ float :cycles
87
+ int16 :skew_ratio
88
+ uint8 :waveform
89
+ end
90
+
91
+ class SetDimAbsolute < Payload
92
+ endian :little
93
+
94
+ int16 :brightness # 0 is no change.
95
+ uint32 :duration # Milliseconds.
96
+ end
97
+
98
+ class SetDimRelative < Payload
99
+ endian :little
100
+
101
+ int32 :brightness # 0 is no change.
102
+ uint32 :duration # Milliseconds.
103
+ end
104
+
105
+ class Rgbw < Payload
106
+ endian :little
107
+
108
+ uint16 :red
109
+ uint16 :green
110
+ uint16 :blue
111
+ uint16 :white
112
+ end
113
+
114
+ class SetRgbw < Payload
115
+ endian :little
116
+
117
+ rgbw :color
118
+ end
119
+
120
+ class GetRailVoltage < Payload
121
+ endian :little
122
+ end
123
+
124
+ class StateRailVoltage < Payload
125
+ endian :little
126
+
127
+ uint32 :voltage
128
+ end
129
+
130
+ class GetTemperature < Payload
131
+ endian :little
132
+ end
133
+
134
+ class StateTemperature < Payload
135
+ endian :little
136
+
137
+ int16 :temperature # Deci-celsius. 25.45 celsius is 2545
138
+ end
139
+ end
140
+ end
141
+ end
142
+ end
@@ -0,0 +1,19 @@
1
+ require 'lifx/lan/protocol/header'
2
+ require 'lifx/lan/protocol/address'
3
+ require 'lifx/lan/protocol/metadata'
4
+
5
+ module LIFX
6
+ module LAN
7
+ module Protocol
8
+ class Message < BinData::Record
9
+ endian :little
10
+
11
+ include HeaderFields
12
+ include AddressFields
13
+ include MetadataFields
14
+
15
+ rest :payload
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,23 @@
1
+ module LIFX
2
+ module LAN
3
+ module Protocol
4
+ module MetadataFields
5
+ def MetadataFields.included(mod)
6
+ mod.instance_eval do
7
+ hide :_reserved3
8
+
9
+ uint64 :at_time
10
+ uint16 :type_
11
+ uint16 :_reserved3
12
+ end
13
+ end
14
+ end
15
+
16
+ class Metadata < BinData::Record
17
+ endian :little
18
+
19
+ include MetadataFields
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,12 @@
1
+ module LIFX
2
+ module LAN
3
+ # @api private
4
+ module Protocol
5
+ class Payload < BinData::Record
6
+ def to_s
7
+ "#<#{self.class} #{super}>"
8
+ end
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,31 @@
1
+ module LIFX
2
+ module LAN
3
+ module Protocol
4
+ # @api private
5
+ module Sensor
6
+ class GetAmbientLight < Payload
7
+ endian :little
8
+
9
+ end
10
+
11
+ class StateAmbientLight < Payload
12
+ endian :little
13
+
14
+ float :lux
15
+ end
16
+
17
+ class GetDimmerVoltage < Payload
18
+ endian :little
19
+
20
+ end
21
+
22
+ class StateDimmerVoltage < Payload
23
+ endian :little
24
+
25
+ uint32 :voltage
26
+ end
27
+
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,204 @@
1
+ # Generated code ahoy!
2
+ module LIFX
3
+ module LAN
4
+ module Protocol
5
+ TYPE_ID_TO_CLASS = {
6
+ # 1 => Device::SetSite,
7
+ # 2 => Device::GetPanGateway,
8
+ # 3 => Device::StatePanGateway,
9
+ 2 => Device::GetService,
10
+ 3 => Device::StateService,
11
+ 4 => Device::GetTime,
12
+ 5 => Device::SetTime,
13
+ 6 => Device::StateTime,
14
+ # 7 => Device::GetResetSwitch,
15
+ # 8 => Device::StateResetSwitch,
16
+ # 12 => Device::GetMeshInfo,
17
+ # 13 => Device::StateMeshInfo,
18
+ # 14 => Device::GetMeshFirmware,
19
+ # 15 => Device::StateMeshFirmware,
20
+ 12 => Device::GetHostInfo,
21
+ 13 => Device::StateHostInfo,
22
+ 14 => Device::GetHostFirmware,
23
+ 15 => Device::StateHostFirmware,
24
+ 16 => Device::GetWifiInfo,
25
+ 17 => Device::StateWifiInfo,
26
+ 18 => Device::GetWifiFirmware,
27
+ 19 => Device::StateWifiFirmware,
28
+ 20 => Device::GetPower,
29
+ 21 => Device::SetPower,
30
+ 22 => Device::StatePower,
31
+ 23 => Device::GetLabel,
32
+ 24 => Device::SetLabel,
33
+ 25 => Device::StateLabel,
34
+ 26 => Device::GetTags,
35
+ 27 => Device::SetTags,
36
+ 28 => Device::StateTags,
37
+ 29 => Device::GetTagLabels,
38
+ 30 => Device::SetTagLabels,
39
+ 31 => Device::StateTagLabels,
40
+ 32 => Device::GetVersion,
41
+ 33 => Device::StateVersion,
42
+ 34 => Device::GetInfo,
43
+ 35 => Device::StateInfo,
44
+ # 36 => Device::GetMcuRailVoltage,
45
+ # 37 => Device::StateMcuRailVoltage,
46
+ # 38 => Device::Reboot,
47
+ 38 => Device::SetReboot,
48
+ 43 => Device::StateReboot,
49
+ 45 => Device::Acknowledgement,
50
+ 46 => Device::SetFactoryReset,
51
+ 47 => Device::StateFactoryReset,
52
+ 48 => Device::GetLocation,
53
+ 49 => Device::SetLocation,
54
+ 50 => Device::StateLocation,
55
+ 51 => Device::GetGroup,
56
+ 52 => Device::SetGroup,
57
+ 53 => Device::StateGroup,
58
+ 54 => Device::GetOwner,
59
+ 55 => Device::SetOwner,
60
+ 56 => Device::StateOwner,
61
+ 58 => Device::EchoRequest,
62
+ 59 => Device::EchoResponse,
63
+
64
+ 101 => Light::Get,
65
+ # 102 => Light::Set,
66
+ 102 => Light::SetColor,
67
+ 103 => Light::SetWaveform,
68
+ # 104 => Light::SetDimAbsolute,
69
+ # 105 => Light::SetDimRelative,
70
+ # 106 => Light::SetRgbw,
71
+ 107 => Light::State,
72
+ # 108 => Light::GetRailVoltage,
73
+ # 109 => Light::StateRailVoltage,
74
+ 110 => Light::GetTemperature,
75
+ 111 => Light::StateTemperature,
76
+ 116 => Light::GetPower,
77
+ 117 => Light::SetPower,
78
+ 118 => Light::StatePower,
79
+
80
+ 201 => Wan::ConnectPlain,
81
+ 202 => Wan::ConnectKey,
82
+ 203 => Wan::StateConnect,
83
+ 204 => Wan::Sub,
84
+ 205 => Wan::Unsub,
85
+ 206 => Wan::StateSub,
86
+
87
+ 301 => Wifi::Get,
88
+ 302 => Wifi::Set,
89
+ 303 => Wifi::State,
90
+ # 304 => Wifi::GetAccessPoint,
91
+ 304 => Wifi::GetAccessPoints,
92
+ 305 => Wifi::SetAccessPoint,
93
+ # 306 => Wifi::StateAccessPoint,
94
+ 306 => Wifi::StateAccessPoints,
95
+ 307 => Wifi::GetAccessPoint,
96
+ 308 => Wifi::StateAccessPoint,
97
+
98
+ 401 => Sensor::GetAmbientLight,
99
+ 402 => Sensor::StateAmbientLight,
100
+ 403 => Sensor::GetDimmerVoltage,
101
+ 404 => Sensor::StateDimmerVoltage,
102
+ }
103
+
104
+ CLASS_TO_TYPE_ID = {
105
+ # Device::SetSite => 1,
106
+ # Device::GetPanGateway => 2,
107
+ # Device::StatePanGateway => 3,
108
+ Device::GetService => 2,
109
+ Device::StateService => 3,
110
+ Device::GetTime => 4,
111
+ Device::SetTime => 5,
112
+ Device::StateTime => 6,
113
+ # Device::GetResetSwitch => 7,
114
+ # Device::StateResetSwitch => 8,
115
+ # Device::GetMeshInfo => 12,
116
+ # Device::StateMeshInfo => 13,
117
+ # Device::GetMeshFirmware => 14,
118
+ # Device::StateMeshFirmware => 15,
119
+ Device::GetHostInfo => 12,
120
+ Device::StateHostInfo => 13,
121
+ Device::GetHostFirmware => 14,
122
+ Device::StateHostFirmware => 15,
123
+ Device::GetWifiInfo => 16,
124
+ Device::StateWifiInfo => 17,
125
+ Device::GetWifiFirmware => 18,
126
+ Device::StateWifiFirmware => 19,
127
+ Device::GetPower => 20,
128
+ Device::SetPower => 21,
129
+ Device::StatePower => 22,
130
+ Device::GetLabel => 23,
131
+ Device::SetLabel => 24,
132
+ Device::StateLabel => 25,
133
+ Device::GetTags => 26,
134
+ Device::SetTags => 27,
135
+ Device::StateTags => 28,
136
+ Device::GetTagLabels => 29,
137
+ Device::SetTagLabels => 30,
138
+ Device::StateTagLabels => 31,
139
+ Device::GetVersion => 32,
140
+ Device::StateVersion => 33,
141
+ Device::GetInfo => 34,
142
+ Device::StateInfo => 35,
143
+ # Device::GetMcuRailVoltage => 36,
144
+ # Device::StateMcuRailVoltage => 37,
145
+ # Device::Reboot => 38,
146
+ Device::SetReboot => 38,
147
+ Device::StateReboot => 43,
148
+ Device::Acknowledgement => 45,
149
+ Device::SetFactoryReset => 46,
150
+ Device::StateFactoryReset => 47,
151
+ Device::GetLocation => 48,
152
+ Device::SetLocation => 49,
153
+ Device::StateLocation => 50,
154
+ Device::GetGroup => 51,
155
+ Device::SetGroup => 52,
156
+ Device::StateGroup => 53,
157
+ Device::GetOwner => 54,
158
+ Device::SetOwner => 55,
159
+ Device::StateOwner => 56,
160
+ Device::EchoRequest => 58,
161
+ Device::EchoResponse => 59,
162
+
163
+ Light::Get => 101,
164
+ # Light::Set => 102,
165
+ Light::SetColor => 102,
166
+ # Light::SetWaveform => 103,
167
+ # Light::SetDimAbsolute => 104,
168
+ # Light::SetDimRelative => 105,
169
+ # Light::SetRgbw => 106,
170
+ Light::State => 107,
171
+ # Light::GetRailVoltage => 108,
172
+ # Light::StateRailVoltage => 109,
173
+ Light::GetTemperature => 110,
174
+ Light::StateTemperature => 111,
175
+ Light::GetPower => 116,
176
+ Light::SetPower => 117,
177
+ Light::StatePower => 118,
178
+
179
+ Wan::ConnectPlain => 201,
180
+ Wan::ConnectKey => 202,
181
+ Wan::StateConnect => 203,
182
+ Wan::Sub => 204,
183
+ Wan::Unsub => 205,
184
+ Wan::StateSub => 206,
185
+
186
+ Wifi::Get => 301,
187
+ Wifi::Set => 302,
188
+ Wifi::State => 303,
189
+ # Wifi::GetAccessPoint => 304,
190
+ Wifi::GetAccessPoints => 304,
191
+ Wifi::SetAccessPoint => 305,
192
+ # Wifi::StateAccessPoint => 306,
193
+ Wifi::StateAccessPoints => 306,
194
+ Wifi::GetAccessPoint => 307,
195
+ Wifi::StateAccessPoint => 308,
196
+
197
+ Sensor::GetAmbientLight => 401,
198
+ Sensor::StateAmbientLight => 402,
199
+ Sensor::GetDimmerVoltage => 403,
200
+ Sensor::StateDimmerVoltage => 404,
201
+ }
202
+ end
203
+ end
204
+ end