class LIFX::LAN::TagManager
@api private @private
Constants
- VALID_TAG_IDS
Attributes
context[R]
Public Class Methods
new(context: required!(:context), tag_table: required!(:tag_table))
click to toggle source
# File lib/lifx/lan/tag_manager.rb, line 20 def initialize(context: required!(:context), tag_table: required!(:tag_table)) @context = WeakRef.new(context) @tag_table = tag_table end
Public Instance Methods
add_tag_to_device(tag: required!(:tag), device: required!(:device))
click to toggle source
# File lib/lifx/lan/tag_manager.rb, line 35 def add_tag_to_device(tag: required!(:tag), device: required!(:device)) tag_entry = entry_with(label: tag, site_id: device.site_id) if !tag_entry create_tag(label: tag, site_id: device.site_id) tag_entry = entry_with(label: tag, site_id: device.site_id) end device_tags_field = device.tags_field device_tags_field |= id_to_tags_field(tag_entry.tag_id) device.send_message!(Protocol::Device::SetTags.new(tags: device_tags_field), wait_for: Protocol::Device::StateTags) do device.tags.include?(tag) end end
create_tag(label: required!(:label), site_id: required!(:site_id))
click to toggle source
# File lib/lifx/lan/tag_manager.rb, line 25 def create_tag(label: required!(:label), site_id: required!(:site_id)) id = next_unused_id_on_site_id(site_id) raise TagLimitReached if id.nil? # Add the entry for the tag we're about to create to prevent a case where # we don't receive a StateTagLabels before another tag gets created @tag_table.update_table(tag_id: id, label: label, site_id: site_id) context.send_message(target: Target.new(site_id: site_id), payload: Protocol::Device::SetTagLabels.new(tags: id_to_tags_field(id), label: label.encode('utf-8'))) end
remove_tag_from_device(tag: required!(:tag), device: required!(:device))
click to toggle source
# File lib/lifx/lan/tag_manager.rb, line 49 def remove_tag_from_device(tag: required!(:tag), device: required!(:device)) tag_entry = entry_with(label: tag, site_id: device.site_id) return if !tag_entry device_tags_field = device.tags_field device_tags_field &= ~id_to_tags_field(tag_entry.tag_id) device.send_message!(Protocol::Device::SetTags.new(tags: device_tags_field), wait_for: Protocol::Device::StateTags) do !device.tags.include?(tag) end end
Protected Instance Methods
entries_with(**args)
click to toggle source
# File lib/lifx/lan/tag_manager.rb, line 98 def entries_with(**args) @tag_table.entries_with(**args) end
entry_with(**args)
click to toggle source
# File lib/lifx/lan/tag_manager.rb, line 94 def entry_with(**args) entries_with(**args).first end
next_unused_id_on_site_id(site_id)
click to toggle source
# File lib/lifx/lan/tag_manager.rb, line 106 def next_unused_id_on_site_id(site_id) (VALID_TAG_IDS - entries_with(site_id: site_id).map(&:tag_id)).first end