summaryrefslogtreecommitdiff
path: root/scarecrow.rb
diff options
context:
space:
mode:
Diffstat (limited to 'scarecrow.rb')
-rw-r--r--scarecrow.rb18
1 files changed, 10 insertions, 8 deletions
diff --git a/scarecrow.rb b/scarecrow.rb
index 498da9d..34b8125 100644
--- a/scarecrow.rb
+++ b/scarecrow.rb
@@ -33,6 +33,7 @@ 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')
@@ -49,7 +50,7 @@ end
def handle_msg(topic, msg)
puts "#{topic}: #{msg}"
- topic_parts = topic.split(",")
+ topic_parts = topic.split("/")
device_id = topic_parts[0]
type = topic_parts[-1]
if type == "humidity"
@@ -79,8 +80,8 @@ get '/' do
@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_prop = Temperature.last(device: "mercury")[:timestamp]
+ time_air = Temperature.last(device: "cricket0")[: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")
erb :index
@@ -88,8 +89,9 @@ get '/' do
end
get '/temperature/:device_id' do
- device_id = "mercury" if (device_id == "air" || device_id == "kitchen")
- device_id = "cricket0" if (device_id == "propagator")
+ device_id = "mercury" if (params[:device_id] == "air" || params[:device_id] == "kitchen")
+ device_id = "cricket0" if (params[:device_id] == "propagator")
+ device_id ||= params[:device_id]
temp = Temperature.last(device: device_id)[:value]
return "#{temp}"
end
@@ -108,9 +110,9 @@ get '/metrics' do
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}"
+humidity.propagator #{humidity_prop}\n
+temperature.air #{temp_air}\n
+temperature.propagator #{temp_prop}"
end
init