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.
- #count ⇒ Object
- #each(&block) ⇒ Object
- #empty? ⇒ Boolean
- #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
73 74 75 76 77 78 79 |
# File 'lib/mqtt/homie/node.rb', line 73 def batch_update(hash) mqtt.batch_publish do hash.each do |(k, v)| self[k].value = v end end end |
#count ⇒ Object
60 61 62 |
# File 'lib/mqtt/homie/node.rb', line 60 def count @properties.count end |
#each(&block) ⇒ Object
56 57 58 |
# File 'lib/mqtt/homie/node.rb', line 56 def each(&block) @properties.each_value(&block) end |
#empty? ⇒ Boolean
64 65 66 |
# File 'lib/mqtt/homie/node.rb', line 64 def empty? @properties.empty? 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
68 69 70 |
# File 'lib/mqtt/homie/node.rb', line 68 def mqtt device.mqtt end |
#publish ⇒ Object
85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/mqtt/homie/node.rb', line 85 def publish mqtt.batch_publish do if device. 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) end @properties.each_value(&:publish) end end |
#published? ⇒ Boolean
81 82 83 |
# File 'lib/mqtt/homie/node.rb', line 81 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
100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/mqtt/homie/node.rb', line 100 def unpublish return unless device. 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 |