Class: MQTT::Homie::Device

Inherits:
HomieObject show all
Extended by:
Network
Defined in:
lib/mqtt/homie/device.rb

Defined Under Namespace

Classes: Firmware, Statistics

Constant Summary collapse

HOMIE_VERSION =
"3.0.1"
DEFAULT_STAT_REFRESH =

seconds

60
DEFAULT_IMPLEMENTATION =
"mqtt-homie-#{VERSION}"

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Network

default_interface, default_localip, default_mac, interfaces, usable_address?

Methods inherited from HomieObject

#topic

Methods included from HomieAttribute

included

Constructor Details

#initialize(options = {}) ⇒ Device

Returns a new instance of Device.



62
63
64
65
66
67
68
69
# File 'lib/mqtt/homie/device.rb', line 62

def initialize(options = {})
  super(options)
  @stats = Statistics.new(options)
  @fw = Firmware.new(subhash(options, "fw_"))

  @use_stats = options.include?(:use_stats) ? options[:use_stats] : true
  @use_fw = options.include?(:use_fw) ? options[:use_fw] : true
end

Instance Attribute Details

#fwObject (readonly)

Returns the value of attribute fw.



51
52
53
# File 'lib/mqtt/homie/device.rb', line 51

def fw
  @fw
end

#statsObject (readonly)

Returns the value of attribute stats.



51
52
53
# File 'lib/mqtt/homie/device.rb', line 51

def stats
  @stats
end

#use_fwObject

Returns the value of attribute use_fw.



52
53
54
# File 'lib/mqtt/homie/device.rb', line 52

def use_fw
  @use_fw
end

#use_statsObject

Returns the value of attribute use_stats.



52
53
54
# File 'lib/mqtt/homie/device.rb', line 52

def use_stats
  @use_stats
end

Instance Method Details

#homie_attributesObject

device attributes must be sent when connection to broker is established or re-established homie/device_id/



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/mqtt/homie/device.rb', line 77

def homie_attributes
  data = super.merge({
    "$homie" => HOMIE_VERSION,
  })

  data.merge!({
    "$fw/name" => @fw.name,
    "$fw/version" => @fw.version,
  }) if @use_fw

  @nodes.each do |node|
    node.homie_attributes.each do |k, v|
      data[node.topic + "/" + k] = v
    end
  end
  data
end

#node(id) ⇒ Object



71
72
73
# File 'lib/mqtt/homie/device.rb', line 71

def node(id)
  @nodes.find { |i| i.id == id }
end