class Mqtttemp
Public Class Methods
new(server)
click to toggle source
# File lib/mqtttemp.rb, line 5 def initialize(server) @server = server; end
Public Instance Methods
enable_thread(mqtt_topic)
click to toggle source
# File lib/mqtttemp.rb, line 13 def enable_thread(mqtt_topic) Thread.new do MQTT::Client.connect(@server) do |c| # If you pass a block to the get method, then it will loop c.get(mqtt_topic) do |topic,message| if(isValidJson(message)) @on_change_block.call(JSON.parse(message)["temperature"]) unless @on_change_block.nil? end end end end end
isValidJson(json)
click to toggle source
# File lib/mqtttemp.rb, line 27 def isValidJson(json) JSON.parse(json) return true rescue JSON::ParserError => e return false end
on_change(&block)
click to toggle source
# File lib/mqtttemp.rb, line 9 def on_change &block @on_change_block = block end
send_led_hex(hex, topic)
click to toggle source
# File lib/mqtttemp.rb, line 34 def send_led_hex(hex, topic) MQTT::Client.connect(@server) do |c| data = '{"color":"'+ hex +'"}' c.publish(topic, data) end end