lib/mqtt/homie/client.rb in mqtt-homie-0.1.0 vs lib/mqtt/homie/client.rb in mqtt-homie-0.1.1
- old
+ new
@@ -10,12 +10,21 @@
def initialize(options = {})
@device = options[:device]
@host = options[:host]
@root_topic = options[:root_topic] || DEFAULT_ROOT_TOPIC
+
raise "device required" unless @device
+ # next version of homie doesn't use stats or firmware details
+ @use_stats = true
+ if options[:develop]
+ @device.use_stats = false
+ @device.use_fw = false
+ @use_stats = false
+ end
+
# observe all node properties so we can publish values when they change
@device.nodes.each do |node|
node.properties.each do |property|
property.add_observer(self)
end
@@ -28,16 +37,16 @@
@device.state = :init
@client = create_mqtt_client
@client.connect
publish(@device, topic)
- publish_statistics
+ publish_statistics if @use_stats
@threads = []
# run a thread to publish statistics
- @threads << Thread.new { run_statistics }
+ @threads << Thread.new { run_statistics } if @use_stats
# run a thread to listen for settings
@threads << Thread.new { run_set_listener }
@device.state = :ready
@@ -104,11 +113,13 @@
end
def run_statistics
while !Thread.current[:done]
publish_statistics
- sleep @device.interval
+
+ # halve interval, if we miss a notification then we will be marked as offline
+ sleep @device.stats.interval / 2
end
debug("statistics thread exiting")
end
def find_property_by_set_topic(set_topic)
@@ -119,10 +130,10 @@
end
nil
end
def publish_statistics
- publish(@device.statistics, topic + "/$stats")
+ publish(@device.stats, topic + "/$stats")
end
def publish_property_value(property)
node = @device.nodes.find { |i| i.properties.include?(property) }
data = {