Class: Transport

Inherits:
Object
  • Object
show all
Defined in:
lib/transport.rb

Instance Method Summary collapse

Instance Method Details

#get_connectionObject



5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/transport.rb', line 5

def get_connection
    puts "MQTT:"
    temperature_topic = 'softwareengineering/thermostat/cfbnik/temperature'
    MQTT::Client.connect('mqtt.labict.be') do |client|
        while(true)
            client.get(temperature_topic) do |topic, message|
                json = message
                hash = JSON.parse(json)
                temp = (hash["temperature"]).to_f
                @on_change_block.call(temp) unless @on_change_block.nil?
            end
        end
    end
end

#on_change(&block) ⇒ Object



20
21
22
# File 'lib/transport.rb', line 20

def on_change &block
    @on_change_block = block
end

#send_color(color) ⇒ Object



24
25
26
27
28
29
# File 'lib/transport.rb', line 24

def send_color(color)
    connection = MQTT::Client.connect('mqtt.labict.be')
    my_hash = {"color" => color}
    payload = JSON.generate(my_hash) # convert my_hash to JSON

    connection.publish('softwareengineering/thermostat/cfbnik/led', payload, retain = false)
end