class Connection
Public Class Methods
new()
click to toggle source
# File lib/connection.rb, line 8 def initialize() end
Public Instance Methods
connect()
click to toggle source
# File lib/connection.rb, line 28 def connect require "mqtt" temperature_topic = 'softwareengineering/temperature/mbed' MQTT::Client.connect('mqtt.labict.be') do |client| while true client.get(temperature_topic) do |topic,message| json = JSON.parse(message) $temperature = (json["temperature"]).to_f @on_change_block.call($temperature) unless @on_change_block.nil? end end end end
get_temperature(url)
click to toggle source
# File lib/connection.rb, line 11 def get_temperature(url) connection = open(url) return connection.read.to_f end
on_change(&block)
click to toggle source
# File lib/connection.rb, line 16 def on_change &block @on_change_block = block end
send(color)
click to toggle source
# File lib/connection.rb, line 20 def send(color) require "mqtt" client2 = MQTT::Client.connect('mqtt.labict.be') my_hash = {"color" => color} payload = JSON.generate(my_hash) client2.publish('softwareengineering/thermostat/Mykyta/led', payload, retain=false) end