Class: LIFX::NetworkContext
- Inherits:
-
Object
- Object
- LIFX::NetworkContext
- Extended by:
- Forwardable
- Defined in:
- lib/lifx/network_context.rb
Instance Attribute Summary collapse
-
#routing_manager ⇒ Object
readonly
NetworkContext stores lights and ties together TransportManager, TagManager and RoutingManager.
-
#tag_manager ⇒ Object
readonly
NetworkContext stores lights and ties together TransportManager, TagManager and RoutingManager.
-
#transport_manager ⇒ Object
readonly
NetworkContext stores lights and ties together TransportManager, TagManager and RoutingManager.
Instance Method Summary collapse
- #all_lights ⇒ Object
- #discover ⇒ Object
- #flush(**options) ⇒ Object
- #gateway_connections ⇒ Object
- #gateways ⇒ Object
-
#initialize(transport: :lan) ⇒ NetworkContext
constructor
A new instance of NetworkContext.
- #lights ⇒ Object
- #refresh ⇒ Object
- #register_device(device) ⇒ Object
- #reset! ⇒ Object
-
#send_message(target:, payload:, acknowledge: false) ⇒ Object
Sends a message to their destination(s).
- #stop ⇒ Object
-
#sync { ... } ⇒ Float
Synchronize asynchronous set_color, set_waveform and set_power messages to multiple devices.
- #tags_for_device(device) ⇒ Object
- #to_s ⇒ Object (also: #inspect)
Constructor Details
#initialize(transport: :lan) ⇒ NetworkContext
Returns a new instance of NetworkContext.
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/lifx/network_context.rb', line 18 def initialize(transport: :lan) @devices = {} @transport_manager = case transport when :lan TransportManager::LAN.new else raise ArgumentError.new("Unknown transport method: #{transport}") end @transport_manager.add_observer(self) do |message:, ip:, transport:| (, ip, transport) end reset! @threads = [] @threads << initialize_timer_thread initialize_periodic_refresh end |
Instance Attribute Details
#routing_manager ⇒ Object (readonly)
NetworkContext stores lights and ties together TransportManager, TagManager and RoutingManager
16 17 18 |
# File 'lib/lifx/network_context.rb', line 16 def routing_manager @routing_manager end |
#tag_manager ⇒ Object (readonly)
NetworkContext stores lights and ties together TransportManager, TagManager and RoutingManager
16 17 18 |
# File 'lib/lifx/network_context.rb', line 16 def tag_manager @tag_manager end |
#transport_manager ⇒ Object (readonly)
NetworkContext stores lights and ties together TransportManager, TagManager and RoutingManager
16 17 18 |
# File 'lib/lifx/network_context.rb', line 16 def transport_manager @transport_manager end |
Instance Method Details
#all_lights ⇒ Object
130 131 132 |
# File 'lib/lifx/network_context.rb', line 130 def all_lights @devices.values end |
#discover ⇒ Object
39 40 41 |
# File 'lib/lifx/network_context.rb', line 39 def discover @transport_manager.discover end |
#flush(**options) ⇒ Object
117 118 119 |
# File 'lib/lifx/network_context.rb', line 117 def flush(**) @transport_manager.flush(**) end |
#gateway_connections ⇒ Object
150 151 152 |
# File 'lib/lifx/network_context.rb', line 150 def gateway_connections transport_manager.gateways.map(&:values).flatten end |
#gateways ⇒ Object
146 147 148 |
# File 'lib/lifx/network_context.rb', line 146 def gateways transport_manager.gateways.map(&:keys).flatten.map { |id| lights.with_id(id) } end |
#lights ⇒ Object
126 127 128 |
# File 'lib/lifx/network_context.rb', line 126 def lights LightCollection.new(context: self) end |
#refresh ⇒ Object
43 44 45 |
# File 'lib/lifx/network_context.rb', line 43 def refresh @routing_manager.refresh end |
#register_device(device) ⇒ Object
121 122 123 124 |
# File 'lib/lifx/network_context.rb', line 121 def register_device(device) device_id = device.id @devices[device_id] = device # What happens when there's already one registered? end |
#reset! ⇒ Object
47 48 49 50 |
# File 'lib/lifx/network_context.rb', line 47 def reset! @routing_manager = RoutingManager.new(context: self) @tag_manager = TagManager.new(context: self, tag_table: @routing_manager.tag_table) end |
#send_message(target:, payload:, acknowledge: false) ⇒ Object
Sends a message to their destination(s)
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/lifx/network_context.rb', line 63 def (target:, payload:, acknowledge: false) paths = @routing_manager.resolve_target(target) = paths.map do |path| Message.new(path: path, payload: payload, acknowledge: acknowledge) end if within_sync? Thread.current[:sync_messages].push(*) return end .each do || @transport_manager.write() end end |
#stop ⇒ Object
52 53 54 55 56 57 |
# File 'lib/lifx/network_context.rb', line 52 def stop @transport_manager.stop @threads.each do |thread| Thread.kill(thread) end end |
#sync { ... } ⇒ Float
This is alpha
Synchronize asynchronous set_color, set_waveform and set_power messages to multiple devices. You cannot use synchronous methods in the block
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/lifx/network_context.rb', line 89 def sync(&block) if within_sync? raise "You cannot nest sync" end = Thread.new do Thread.current[:sync_enabled] = true Thread.current[:sync_messages] = = [] block.call Thread.current[:sync_enabled] = false end.join.value time = nil try_until -> { time } do light = gateways.sample time = light && light.time end delay = (.count + 1) * (1.0 / ) at_time = ((time.to_f + delay) * 1_000_000_000).to_i .each do |m| m.at_time = at_time @transport_manager.write(m) end flush delay end |
#tags_for_device(device) ⇒ Object
142 143 144 |
# File 'lib/lifx/network_context.rb', line 142 def (device) @routing_manager.(device.id) end |
#to_s ⇒ Object Also known as: inspect
154 155 156 |
# File 'lib/lifx/network_context.rb', line 154 def to_s %Q{#<LIFX::NetworkContext connections=#{gateway_connections}>} end |