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



40
41
42
# File 'lib/mqtt/homie/node.rb', line 40

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

#batch_update(hash) ⇒ Object

takes a hash with property names as keys, and values as values



53
54
55
56
57
58
59
# File 'lib/mqtt/homie/node.rb', line 53

def batch_update(hash)
  mqtt.batch_publish do
    hash.each do |(k, v)|
      self[k].value = v
    end
  end
end

#each(&block) ⇒ Object



44
45
46
# File 'lib/mqtt/homie/node.rb', line 44

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

#mqttObject



48
49
50
# File 'lib/mqtt/homie/node.rb', line 48

def mqtt
  device.mqtt
end

#property(*args, **kwargs, &block) ⇒ Object



20
21
22
23
24
25
26
27
28
# File 'lib/mqtt/homie/node.rb', line 20

def property(*args, **kwargs, &block)
  device.init do |prior_state|
    property = Property.new(self, *args, **kwargs, &block)
    raise ArgumentError, "Property '#{property.id}' already exists on '#{id}'" if @properties.key?(property.id)
    @properties[property.id] = property
    property.publish if prior_state == :ready
    property
  end
end

#publishObject



61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/mqtt/homie/node.rb', line 61

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

#remove_property(id) ⇒ Object



30
31
32
33
34
35
36
37
38
# File 'lib/mqtt/homie/node.rb', line 30

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



16
17
18
# File 'lib/mqtt/homie/node.rb', line 16

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

#unpublishObject



74
75
76
77
78
79
80
81
82
83
# File 'lib/mqtt/homie/node.rb', line 74

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