Class: MQTT::Homie::Node
Constant Summary
Constants inherited from Base
Instance Attribute Summary collapse
-
#device ⇒ Object
readonly
Returns the value of attribute device.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Attributes inherited from Base
Instance Method Summary collapse
- #[](id) ⇒ Object
-
#batch_update(hash) ⇒ Object
takes a hash with property names as keys, and values as values.
- #each(&block) ⇒ Object
- #full_name ⇒ Object
-
#initialize(device, id, name, type) ⇒ Node
constructor
A new instance of Node.
- #inspect ⇒ Object
- #mqtt ⇒ Object
- #publish ⇒ Object
- #published? ⇒ Boolean
- #remove_property(id) ⇒ Object
- #topic ⇒ Object
- #unpublish ⇒ Object
Constructor Details
#initialize(device, id, name, type) ⇒ Node
Returns a new instance of Node.
8 9 10 11 12 13 14 |
# File 'lib/mqtt/homie/node.rb', line 8 def initialize(device, id, name, type) super(id, name) @device = device @type = type @properties = {} @published = false end |
Instance Attribute Details
#device ⇒ Object (readonly)
Returns the value of attribute device.
6 7 8 |
# File 'lib/mqtt/homie/node.rb', line 6 def device @device end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
6 7 8 |
# File 'lib/mqtt/homie/node.rb', line 6 def type @type end |
Instance Method Details
#[](id) ⇒ Object
52 53 54 |
# File 'lib/mqtt/homie/node.rb', line 52 def [](id) @properties[id] end |
#batch_update(hash) ⇒ Object
takes a hash with property names as keys, and values as values
65 66 67 68 69 70 71 |
# File 'lib/mqtt/homie/node.rb', line 65 def batch_update(hash) mqtt.batch_publish do hash.each do |(k, v)| self[k].value = v end end end |
#each(&block) ⇒ Object
56 57 58 |
# File 'lib/mqtt/homie/node.rb', line 56 def each(&block) @properties.each_value(&block) end |
#full_name ⇒ Object
20 21 22 23 24 |
# File 'lib/mqtt/homie/node.rb', line 20 def full_name return name if device.count == 1 "#{device.name} #{name}" end |
#inspect ⇒ Object
16 17 18 |
# File 'lib/mqtt/homie/node.rb', line 16 def inspect "#<MQTT::Homie::Node #{topic} name=#{full_name.inspect}, type=#{type.inspect}>" end |
#mqtt ⇒ Object
60 61 62 |
# File 'lib/mqtt/homie/node.rb', line 60 def mqtt device.mqtt end |
#publish ⇒ Object
77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/mqtt/homie/node.rb', line 77 def publish mqtt.batch_publish do unless published? mqtt.publish("#{topic}/$name", name, retain: true, qos: 1) mqtt.publish("#{topic}/$type", @type.to_s, retain: true, qos: 1) @published = true end mqtt.publish("#{topic}/$properties", @properties.keys.join(","), retain: true, qos: 1) @properties.each_value(&:publish) end end |
#published? ⇒ Boolean
73 74 75 |
# File 'lib/mqtt/homie/node.rb', line 73 def published? @published end |
#remove_property(id) ⇒ Object
41 42 43 44 45 46 47 48 49 50 |
# File 'lib/mqtt/homie/node.rb', line 41 def remove_property(id) return false unless (property = @properties[id]) init do property.unpublish @properties.delete(id) mqtt.publish("#{topic}/$properties", @properties.keys.join(","), retain: true, qos: 1) if published? end true end |
#topic ⇒ Object
26 27 28 |
# File 'lib/mqtt/homie/node.rb', line 26 def topic "#{device.topic}/#{id}" end |
#unpublish ⇒ Object
90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/mqtt/homie/node.rb', line 90 def unpublish return unless published? @published = false mqtt.publish("#{topic}/$name", retain: true, qos: 0) mqtt.publish("#{topic}/$type", retain: true, qos: 0) mqtt.publish("#{topic}/$properties", retain: true, qos: 0) @properties.each_value(&:unpublish) end |