Class: MQTT::Homie::Device

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

Constant Summary collapse

VERSION =

the Homie spec version

"4.0.0"

Constants inherited from Base

Base::REGEX

Instance Attribute Summary collapse

Attributes inherited from Base

#id, #name

Instance Method Summary collapse

Constructor Details

#initialize(id, name, root_topic: nil, mqtt: nil, clear_topics: true, &block) ⇒ Device

Returns a new instance of Device.



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/mqtt/homie/device.rb', line 15

def initialize(id, name, root_topic: nil, mqtt: nil, clear_topics: true, &block)
  super(id, name)
  @root_topic = root_topic || "homie"
  @state = :init
  @nodes = {}
  @published = false
  @out_of_band_topic_proc = block
  mqtt = MQTT::Client.new(mqtt) if mqtt.is_a?(String)
  @mqtt = mqtt || MQTT::Client.new
  @mqtt.set_will("#{topic}/$state", "lost", retain: true, qos: 1)

  @mqtt.on_reconnect do
    each do |node|
      node.each(&:subscribe)
    end
    mqtt.publish("#{topic}/$state", :init, retain: true, qos: 1)
    mqtt.publish("#{topic}/$state", state, retain: true, qos: 1) unless state == :init
  end

  @mqtt.connect
  self.clear_topics if clear_topics
end

Instance Attribute Details

#loggerObject

Returns the value of attribute logger.



13
14
15
# File 'lib/mqtt/homie/device.rb', line 13

def logger
  @logger
end

#mqttObject (readonly)

Returns the value of attribute mqtt.



12
13
14
# File 'lib/mqtt/homie/device.rb', line 12

def mqtt
  @mqtt
end

#out_of_band_topic_procObject

Returns the value of attribute out_of_band_topic_proc.



13
14
15
# File 'lib/mqtt/homie/device.rb', line 13

def out_of_band_topic_proc
  @out_of_band_topic_proc
end

#root_topicObject (readonly)

Returns the value of attribute root_topic.



12
13
14
# File 'lib/mqtt/homie/device.rb', line 12

def root_topic
  @root_topic
end

#stateObject (readonly)

Returns the value of attribute state.



12
13
14
# File 'lib/mqtt/homie/device.rb', line 12

def state
  @state
end

Instance Method Details

#[](id) ⇒ Object



77
78
79
# File 'lib/mqtt/homie/device.rb', line 77

def [](id)
  @nodes[id]
end

#clear_topicsObject

Raises:

  • (ArgumentError)


153
154
155
156
157
158
159
160
161
162
163
# File 'lib/mqtt/homie/device.rb', line 153

def clear_topics
  raise ArgumentError, "cannot clear topics once published" if @published

  @mqtt.subscribe("#{topic}/#")
  @mqtt.unsubscribe("#{topic}/#", wait_for_ack: true)
  until @mqtt.queue_empty?
    packet = @mqtt.get
    @mqtt.publish(packet.topic, retain: true, qos: 0)
  end
  true
end

#countObject



85
86
87
# File 'lib/mqtt/homie/device.rb', line 85

def count
  @nodes.count
end

#deviceObject



42
43
44
# File 'lib/mqtt/homie/device.rb', line 42

def device
  self
end

#disconnectObject



127
128
129
130
131
# File 'lib/mqtt/homie/device.rb', line 127

def disconnect
  @published = false
  mqtt.disconnect
  @subscription_thread&.kill
end

#each(&block) ⇒ Object



81
82
83
# File 'lib/mqtt/homie/device.rb', line 81

def each(&block)
  @nodes.each_value(&block)
end

#initObject



140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/mqtt/homie/device.rb', line 140

def init
  return yield state if state == :init

  prior_state = state
  mqtt.publish("#{topic}/$state", (self.state = :init).to_s, retain: true, qos: 1)
  result = nil
  mqtt.batch_publish do
    result = yield prior_state
  end
  mqtt.publish("#{topic}/$state", (self.state = :ready).to_s, retain: true, qos: 1)
  result
end

#inspectObject



38
39
40
# File 'lib/mqtt/homie/device.rb', line 38

def inspect
  "#<MQTT::Homie::Device #{topic} name=#{name.inspect}, $state=#{state.inspect}>"
end

#joinObject



133
134
135
136
137
138
# File 'lib/mqtt/homie/device.rb', line 133

def join
  @subscription_thread&.join
rescue => e
  e.set_backtrace(e.backtrace + ["<from Homie MQTT thread>"] + caller)
  raise e
end

#publishObject



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/mqtt/homie/device.rb', line 89

def publish
  return if @published

  mqtt.batch_publish do
    mqtt.publish("#{topic}/$homie", VERSION, retain: true, qos: 1)
    mqtt.publish("#{topic}/$name", name, retain: true, qos: 1)
    mqtt.publish("#{topic}/$state", @state.to_s, retain: true, qos: 1)

    @subscription_thread = Thread.new do
      # you'll get the exception when you call `join`
      Thread.current.report_on_exception = false

      mqtt.get do |packet|
        logger&.debug("received packet at #{packet.topic} with payload #{packet.payload.inspect}")
        match = packet.topic.match(topic_regex)
        node = @nodes[match[:node]] if match
        property = node[match[:property]] if node

        unless property&.settable?
          @out_of_band_topic_proc&.call(packet.topic, packet.payload)
          next
        end

        property.set(packet.payload)
      end
    end

    mqtt.publish("#{topic}/$nodes", @nodes.keys.join(","), retain: true, qos: 1)
    @nodes.each_value(&:publish)

    yield if block_given?

    mqtt.publish("#{topic}/$state", (@state = :ready).to_s, retain: true, qos: 1)
  end

  @published = true
end

#remove_node(id) ⇒ Object



66
67
68
69
70
71
72
73
74
75
# File 'lib/mqtt/homie/device.rb', line 66

def remove_node(id)
  return false unless (node = @nodes[id])

  init do
    node.unpublish
    @nodes.delete(id)
    mqtt.publish("#{topic}/$nodes", @nodes.keys.join(","), retain: true, qos: 1) if @published
  end
  true
end

#topicObject



46
47
48
# File 'lib/mqtt/homie/device.rb', line 46

def topic
  "#{root_topic}/#{id}"
end