From dc93c4e8a9512533197b58e73f1f1d274908b5cd Mon Sep 17 00:00:00 2001 From: Joe Robinson Date: Tue, 19 Feb 2019 23:52:50 +0000 Subject: Fix kitchen --- scarecrow.rb | 42 ++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 40 insertions(+), 2 deletions(-) (limited to 'scarecrow.rb') diff --git a/scarecrow.rb b/scarecrow.rb index 2fbc2ab..fff656d 100644 --- a/scarecrow.rb +++ b/scarecrow.rb @@ -30,13 +30,21 @@ class Humidity property :value, Float end +class Moisture + include DataMapper::Resource + property :id, Serial + property :device, String + property :timestamp, DateTime + property :value, Float +end + def init DataMapper.auto_migrate! DataMapper.auto_upgrade! # sleep 10 Thread.new do MQTT::Client.connect('192.168.0.59') do |c| - c.subscribe('+/temperature', '+/humidity') + c.subscribe('+/temperature', '+/humidity', '+/moisture') loop do while !c.queue_empty? do topic, message = c.get @@ -57,6 +65,8 @@ def handle_msg(topic, msg) handle_humidity_msg(device_id, msg) elsif type == "temperature" handle_temp_message(device_id, msg) + elsif type == "moisture" + handle_moisture_message(device_id, msg) end end @@ -74,16 +84,28 @@ def handle_temp_message(device_id, msg) end end +def handle_moisture_message(device_id, msg) + moisture = Moisture.create(timestamp: DateTime.now, device: device_id, value: msg) + moisture.errors.each do |error| + puts error + end +end + get '/' do # send_file File.join(settings.public_folder, 'index.html') @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] + @temp_kitchen = Temperature.last(device: "mercury")[:value] + @humidity_kitchen = Humidity.last(device: "cricket1")[:value] + @moisture_kitchen = Moisture.last(device: "cricket1")[:value] time_prop = Temperature.last(device: "mercury")[:timestamp] time_air = Temperature.last(device: "cricket0")[:timestamp] + time_kitchen = Temperature.last(device: "cricket1")[:timestamp] @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") + @time_formatted_kitchen = time_kitchen.strftime("%A, %d %b %Y %l:%M %p") erb :index # return "#{temp_val}°C and #{humidity_val}% humidity at #{time_formatted}" end @@ -91,6 +113,7 @@ end get '/temperature/:device_id' do device_id = "mercury" if (params[:device_id] == "air" || params[:device_id] == "kitchen") device_id = "cricket0" if (params[:device_id] == "propagator") + device_id = "cricket1" if (params[:device_id] == "kitchen") device_id ||= params[:device_id] temp = Temperature.last(device: device_id)[:value] return "#{temp}" @@ -99,20 +122,35 @@ end get '/humidity/:device_id' do device_id = "mercury" if (device_id == "air" || device_id == "basement") device_id = "cricket0" if (device_id == "propagator") + device_id = "cricket1" if (device_id == "kitchen") humidity = Humidity.last(device: device_id)[:value] "#{humidity}" end +get '/moisture/:device_id' do + device_id = "mercury" if (device_id == "air" || device_id == "basement") + device_id = "cricket0" if (device_id == "propagator") + device_id = "cricket1" if (device_id == "kitchen") + moisture = Moisture.last(device: device_id)[:value] + "#{moisture}" +end + get '/metrics' do content_type :text 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] + temp_kitchen = Temperature.last(device: "cricket1")[:value] + humidity_kitchen = Humidity.last(device: "cricket1")[:value] + moisture_kitchen = Moisture.last(device: "cricket1")[:value] "humidity_basement #{humidity_air} humidity_propagator #{humidity_prop} +humidity_kitchen#{humidity_kitchen} temperature_basement #{temp_air} -temperature_propagator #{temp_prop}" +temperature_propagator #{temp_prop} +temperature_kitchen #{temp_kitchen} +moisture_kitchen #{moisture_kitchen}" end init -- cgit v1.2.3