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

context[R]

Refers to the client's network context. @return [NetworkContext] Enclosed network context

Public Class Methods

lan() click to toggle source

Returns a {Client} set up for accessing devices on the LAN

@return [Client] A LAN LIFX::Client

# File lib/lifx/lan/client.rb, line 17
def lan
  @lan ||= new(transport_manager: TransportManager::LAN.new)
end
new(transport_manager: required!('transport_manager')) click to toggle source

@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

discover() click to toggle source

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
discover!(timeout: DISCOVERY_DEFAULT_TIMEOUT, condition_interval: 0.1, &block) click to toggle source

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
flush(timeout: nil) click to toggle source

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
lights() click to toggle source

@return [LightCollection] Lights available to the client @see [NetworkContext#lights]

# File lib/lifx/lan/client.rb, line 111
def lights
  context.lights
end
purge_unused_tags!() click to toggle source

Purges unused tags from the system. Should only use when all devices are on the network, otherwise offline devices using their tags will not be tagged correctly. @return [Array<String>] Tags that were purged

# File lib/lifx/lan/client.rb, line 131
def purge_unused_tags!
  context.purge_unused_tags!
end
refresh() click to toggle source

Sends a request to refresh devices and tags. @return [void]

# File lib/lifx/lan/client.rb, line 75
def refresh
  @context.refresh
end
stop() click to toggle source

Stops everything and cleans up.

# File lib/lifx/lan/client.rb, line 144
def stop
  context.stop
end
stop_discovery() click to toggle source
# File lib/lifx/lan/client.rb, line 44
def stop_discovery
  @context.stop_discovery
end
sync(**kwargs, &block) click to toggle source

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
sync!(&block) click to toggle source

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
tags() click to toggle source

@return [Array<String>] All tags visible to the client @see [NetworkContext#tags]

# File lib/lifx/lan/client.rb, line 117
def tags
  context.tags
end
unused_tags() click to toggle source

@return [Array<String>] Tags that are currently unused by known devices @see [NetworkContext#unused_tags]

# File lib/lifx/lan/client.rb, line 123
def unused_tags
  context.unused_tags
end