Class: LIFX::LightCollection
- Inherits:
-
Object
- Object
- LIFX::LightCollection
- Extended by:
- Forwardable
- Includes:
- Enumerable, LightTarget
- Defined in:
- lib/lifx/light_collection.rb
Overview
LightCollection represents a collection of Lights, which can either refer to all lights on a NetworkContext, or lights
Defined Under Namespace
Classes: TagNotFound
Constant Summary collapse
- DEFAULT_ALIVE_THRESHOLD =
seconds
30
Constants included from LightTarget
LIFX::LightTarget::MSEC_PER_SEC, LIFX::LightTarget::NSEC_IN_SEC
Instance Attribute Summary collapse
-
#context ⇒ NetworkContext
readonly
Refers to NetworkContext the instance belongs to.
-
#tag ⇒ String
readonly
Tag of the collection.
Instance Method Summary collapse
-
#alive(threshold: DEFAULT_ALIVE_THRESHOLD) ⇒ Array<Light>
Returns an Array of Lights considered alive.
-
#initialize(context:, tag: nil) ⇒ LightCollection
constructor
private
Creates a LightCollection instance.
-
#lights ⇒ Array<Light>
Returns an Array of Lights.
-
#send_message(payload, acknowledge: false) ⇒ LightCollection
private
Queues a Protocol::Payload to be sent to bulbs in the collection.
-
#stale(threshold: DEFAULT_ALIVE_THRESHOLD) ⇒ Array<Light>
Returns an Array of Lights considered stale.
-
#to_s ⇒ String
(also: #inspect)
Returns a nice string representation of itself.
-
#with_id(id) ⇒ Light
Returns a Light with device id matching
id
. -
#with_label(label) ⇒ Light
Returns a Light with its label matching
label
. -
#with_tag(tag) ⇒ LightCollection
Returns a LightCollection of Lights tagged with
tag
.
Methods included from LightTarget
#half_sine, #pulse, #reboot!, #refresh, #saw, #set_color, #set_power, #set_site_id, #set_time, #set_waveform, #sine, #triangle, #turn_off, #turn_on
Constructor Details
#initialize(context:, tag: nil) ⇒ LightCollection
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Creates a LIFX::LightCollection instance. Should not be used directly.
25 26 27 28 |
# File 'lib/lifx/light_collection.rb', line 25 def initialize(context:, tag: nil) @context = context @tag = tag end |
Instance Attribute Details
#context ⇒ NetworkContext (readonly)
Refers to NetworkContext the instance belongs to
15 16 17 |
# File 'lib/lifx/light_collection.rb', line 15 def context @context end |
#tag ⇒ String (readonly)
Tag of the collection. nil
represents all lights
19 20 21 |
# File 'lib/lifx/light_collection.rb', line 19 def tag @tag end |
Instance Method Details
#alive(threshold: DEFAULT_ALIVE_THRESHOLD) ⇒ Array<Light>
Returns an Array of LIFX::Lights considered alive
87 88 89 |
# File 'lib/lifx/light_collection.rb', line 87 def alive(threshold: DEFAULT_ALIVE_THRESHOLD) lights.select { |l| l.seconds_since_seen <= threshold } end |
#lights ⇒ Array<Light>
Returns an Array of LIFX::Lights
75 76 77 78 79 80 81 |
# File 'lib/lifx/light_collection.rb', line 75 def lights if tag context.all_lights.select { |l| l..include?(tag) } else context.all_lights end end |
#send_message(payload, acknowledge: false) ⇒ LightCollection
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Queues a Protocol::Payload to be sent to bulbs in the collection
35 36 37 38 39 40 41 42 |
# File 'lib/lifx/light_collection.rb', line 35 def (payload, acknowledge: false) if tag context.(target: Target.new(tag: tag), payload: payload, acknowledge: acknowledge) else context.(target: Target.new(broadcast: true), payload: payload, acknowledge: acknowledge) end self end |
#stale(threshold: DEFAULT_ALIVE_THRESHOLD) ⇒ Array<Light>
Returns an Array of LIFX::Lights considered stale
94 95 96 |
# File 'lib/lifx/light_collection.rb', line 94 def stale(threshold: DEFAULT_ALIVE_THRESHOLD) lights.select { |l| l.seconds_since_seen > threshold } end |
#to_s ⇒ String Also known as: inspect
Returns a nice string representation of itself
100 101 102 |
# File 'lib/lifx/light_collection.rb', line 100 def to_s %Q{#<#{self.class.name} lights=#{lights}#{tag ? " tag=#{tag}" : ''}>} end |
#with_id(id) ⇒ Light
Returns a LIFX::Light with device id matching id
47 48 49 |
# File 'lib/lifx/light_collection.rb', line 47 def with_id(id) lights.find { |l| l.id == id} end |
#with_label(label) ⇒ Light
Returns a LIFX::Light with its label matching label
54 55 56 57 58 59 60 |
# File 'lib/lifx/light_collection.rb', line 54 def with_label(label) if label.is_a?(Regexp) lights.find { |l| l.label(fetch: false) =~ label } else lights.find { |l| l.label(fetch: false) == label } end end |
#with_tag(tag) ⇒ LightCollection
Returns a LIFX::LightCollection of LIFX::Lights tagged with tag
65 66 67 68 69 70 71 |
# File 'lib/lifx/light_collection.rb', line 65 def with_tag(tag) if context..include?(tag) self.class.new(context: context, tag: tag) else raise TagNotFound.new("No such tag '#{tag}'") end end |