From 86ac9dd15faec00668bd9622fd5f976702621b8b Mon Sep 17 00:00:00 2001 From: Joe Robinson Date: Sun, 17 Feb 2019 21:35:34 +0000 Subject: Handle multiple devices --- scarecrow.rb | 59 +++++++++++++++++++++++++++++++++++++-------------------- views/index.erb | 3 ++- 2 files changed, 40 insertions(+), 22 deletions(-) diff --git a/scarecrow.rb b/scarecrow.rb index 71adf45..498da9d 100644 --- a/scarecrow.rb +++ b/scarecrow.rb @@ -17,6 +17,7 @@ DataMapper.setup(:default, ENV['DATABASE_URL'] || "sqlite3://#{Dir.pwd}/scarecro class Temperature include DataMapper::Resource property :id, Serial + property :device, String property :timestamp, DateTime property :value, Float end @@ -24,6 +25,7 @@ end class Humidity include DataMapper::Resource property :id, Serial + property :device, String property :timestamp, DateTime property :value, Float end @@ -33,7 +35,7 @@ def init DataMapper.auto_upgrade! Thread.new do MQTT::Client.connect('192.168.0.59') do |c| - c.subscribe('temperature', 'humidity') + c.subscribe('+/temperature', '+/humidity') loop do while !c.queue_empty? do topic, message = c.get @@ -47,22 +49,25 @@ end def handle_msg(topic, msg) puts "#{topic}: #{msg}" - if topic == "humidity" - handle_humidity_msg(msg) - elsif topic == "temperature" - handle_temp_message(msg) + topic_parts = topic.split(",") + device_id = topic_parts[0] + type = topic_parts[-1] + if type == "humidity" + handle_humidity_msg(device_id, msg) + elsif type == "temperature" + handle_temp_message(device_id, msg) end end -def handle_humidity_msg(msg) - humidity = Humidity.create(timestamp: DateTime.now, value: msg) +def handle_humidity_msg(device_id, msg) + humidity = Humidity.create(timestamp: DateTime.now, device: device_id, value: msg) humidity.errors.each do |error| puts error end end -def handle_temp_message(msg) - temp = Temperature.create(timestamp: DateTime.now, value: msg) +def handle_temp_message(device_id, msg) + temp = Temperature.create(timestamp: DateTime.now, device: device_id, value: msg) temp.errors.each do |error| puts error end @@ -70,30 +75,42 @@ end get '/' do # send_file File.join(settings.public_folder, 'index.html') - @temp_val = Temperature.last[:value] - @humidity_val = Humidity.last[:value] - temp_time = Temperature.last[:timestamp] - humidity_time = Humidity.last[:timestamp] - @time_formatted = temp_time.strftime("%A, %d %b %Y %l:%M %p") + @temp_prop = Temperature.last(device: "cricket0")[:value] + @humidity_prop = Humidity.last(device: "cricket0")[:value] + @temp_air = Temperature.last(device: "mercury")[:value] + @humidity_air = Humidity.last(device: "mercury")[:value] + time_prop = Temperature.last(device: "mercury")[:value] + time_air = Temperature.last(device: "cricket0")[:value] + @time_formatted_prop = time_prop.strftime("%A, %d %b %Y %l:%M %p") + @time_formatted_air = time_air.strftime("%A, %d %b %Y %l:%M %p") erb :index # return "#{temp_val}°C and #{humidity_val}% humidity at #{time_formatted}" end -get '/temperature' do - temp = Temperature.last[:value] +get '/temperature/:device_id' do + device_id = "mercury" if (device_id == "air" || device_id == "kitchen") + device_id = "cricket0" if (device_id == "propagator") + temp = Temperature.last(device: device_id)[:value] return "#{temp}" end -get '/humidity' do - humidity = Humidity.last[:value] +get '/humidity/:device_id' do + device_id = "mercury" if (device_id == "air" || device_id == "kitchen") + device_id = "cricket0" if (device_id == "propagator") + humidity = Humidity.last(device: device_id)[:value] "#{humidity}" end get '/metrics' do content_type :text - humidity = Humidity.last[:value] - temp = Temperature.last[:value] - "humidity #{humidity}\ntemperature #{temp}" + temp_prop = Temperature.last(device: "cricket0")[:value] + humidity_prop = Humidity.last(device: "cricket0")[:value] + temp_air = Temperature.last(device: "mercury")[:value] + humidity_air = Humidity.last(device: "mercury")[:value] + "humidity.air #{humidity_air}\n + humidity.propagator #{humidity_prop}\n + temperature.air #{temp_air}\n + temperature.propagator #{temp_prop}" end init diff --git a/views/index.erb b/views/index.erb index 542e589..bbe7b81 100644 --- a/views/index.erb +++ b/views/index.erb @@ -7,7 +7,8 @@

thebluegarden

-

<%= @temp_val %>°C and <%= @humidity_val %>% humidity at <%= @time_formatted %>

+

Kitchen: <%= @temp_air %>°C and <%= @humidity_air %>% humidity at <%= @time_formatted_air %>

+

Propagator: <%= @temp_prop %>°C and <%= @humidity_air %>% humidity at <%= @time_formatted_prop %>

-- cgit v1.2.3