summaryrefslogtreecommitdiff
path: root/scarecrow.rb
diff options
context:
space:
mode:
authorJoe Robinson <joe@lc8n.com>2019-02-17 21:35:34 +0000
committerJoe Robinson <joe@lc8n.com>2019-02-17 21:35:34 +0000
commit86ac9dd15faec00668bd9622fd5f976702621b8b (patch)
tree605557dda0a7ad98cb9f8e8242d2e7a7df150ebf /scarecrow.rb
parent96d72ed4d7fdd9cfa429e6b17e0a289f37427723 (diff)
Handle multiple devices
Diffstat (limited to 'scarecrow.rb')
-rw-r--r--scarecrow.rb59
1 files changed, 38 insertions, 21 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