Class: MQTT::Homie::Node

Inherits:
Base
  • Object
show all
Defined in:
lib/mqtt/homie/node.rb

Constant Summary

Constants inherited from Base

Base::REGEX

Instance Attribute Summary collapse

Attributes inherited from Base

#id, #name

Instance Method Summary collapse

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

#deviceObject (readonly)

Returns the value of attribute device.



6
7
8
# File 'lib/mqtt/homie/node.rb', line 6

def device
  @device
end

#typeObject (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_nameObject



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

#inspectObject



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

#mqttObject



60
61
62
# File 'lib/mqtt/homie/node.rb', line 60

def mqtt
  device.mqtt
end

#publishObject



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

Returns:

  • (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

#topicObject



26
27
28
# File 'lib/mqtt/homie/node.rb', line 26

def topic
  "#{device.topic}/#{id}"
end

#unpublishObject



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