class LIFX::LAN::Client
{LIFX::LAN::Client} is the top level interface to the library. It mainly maps methods to the backing {NetworkContext} instance.
Constants
- DISCOVERY_DEFAULT_TIMEOUT
Default timeout in seconds for discovery
Attributes
Refers to the client's network context. @return [NetworkContext] Enclosed network context
Public Class Methods
@param transport_manager: [TransportManager] Specify the {TransportManager}
# File lib/lifx/lan/client.rb, line 31 def initialize(transport_manager: required!('transport_manager')) @context = NetworkContext.new(transport_manager: transport_manager) end
Public Instance Methods
This method tells the {NetworkContext} to look for devices asynchronously. @return [Client] self
# File lib/lifx/lan/client.rb, line 40 def discover @context.discover end
This method tells the {NetworkContext} to look for devices, and will block until there's at least one device.
@example Wait until at least three lights have been found
client.discover! { |c| c.lights.count >= 3 }
@param timeout: [Numeric] How long to try to wait for before returning @param condition_interval: [Numeric] Seconds between evaluating the block @yield [Client] This block is evaluated every `condition_interval` seconds. If true, method returns. If no block is supplied, it will block until it finds at least one light. @raise [DiscoveryTimeout] If discovery times out @return [Client] self
# File lib/lifx/lan/client.rb, line 60 def discover!(timeout: DISCOVERY_DEFAULT_TIMEOUT, condition_interval: 0.1, &block) block ||= -> { self.lights.count > 0 } try_until -> { block.arity == 1 ? block.call(self) : block.call }, timeout: timeout, timeout_exception: DiscoveryTimeout, condition_interval: condition_interval, action_interval: 1 do discover refresh end self end
Blocks until all messages have been sent to the gateways @param timeout: [Numeric] When specified, flush will wait `timeout:` seconds before throwing `Timeout::Error` @raise [TimeoutError] if `timeout:` was exceeded while waiting for send queue to flush @return [void]
# File lib/lifx/lan/client.rb, line 139 def flush(timeout: nil) context.flush(timeout: timeout) end
@return [LightCollection] Lights available to the client @see [NetworkContext#lights]
# File lib/lifx/lan/client.rb, line 111 def lights context.lights end
Sends a request to refresh devices and tags. @return [void]
# File lib/lifx/lan/client.rb, line 75 def refresh @context.refresh end
Stops everything and cleans up.
# File lib/lifx/lan/client.rb, line 144 def stop context.stop end
# File lib/lifx/lan/client.rb, line 44 def stop_discovery @context.stop_discovery end
This method takes a block consisting of multiple asynchronous color or power changing targets and it will try to schedule them so they run at the same time.
You cannot nest `sync` calls, nor call synchronous methods inside a `sync` block.
Due to messaging rate constraints, the amount of messages determine the delay before the commands are executed. This method also assumes all the lights have the same time. @example This example sets all the lights to a random colour at the same time.
client.sync do client.lights.each do |light| light.set_color(rand(4) * 90, 1, 1) end end
@note This method is in alpha and might go away. Use tags for better group messaging. @yield Block of commands to synchronize @return [Float] Number of seconds until commands are executed
# File lib/lifx/lan/client.rb, line 96 def sync(**kwargs, &block) @context.sync(**kwargs, &block) end
This is the same as {#sync}, except it will block until the commands have been executed. @see sync
@return [Float] Number of seconds slept
# File lib/lifx/lan/client.rb, line 103 def sync!(&block) sync(&block).tap do |delay| sleep(delay) end end