summaryrefslogtreecommitdiff
path: root/scarecrow.rb
diff options
context:
space:
mode:
authorJoe Robinson <joe@lc8n.com>2019-02-25 20:05:46 +0000
committerJoe Robinson <joe@lc8n.com>2019-02-25 20:05:46 +0000
commitc1eafa32f4d832b270d56c0f9c29c59caec99aea (patch)
treed719f24e55e865907b8fc0f409f9ddcb64f067d6 /scarecrow.rb
parentc4777ea1fe0daeef5ba7d8480bbd5a264ed6660d (diff)
Moisture and things
Diffstat (limited to 'scarecrow.rb')
-rw-r--r--scarecrow.rb70
1 files changed, 53 insertions, 17 deletions
diff --git a/scarecrow.rb b/scarecrow.rb
index e6cb303..bd5509f 100644
--- a/scarecrow.rb
+++ b/scarecrow.rb
@@ -91,18 +91,54 @@ def handle_moisture_message(device_id, msg)
end
end
+def get_value(type, device_id)
+ entry = nil
+ value = 0.0
+ if type == :temperature
+ entry = Temperature.last(device: device_id)
+ elsif type == :humidity
+ entry = Humidity.last(device: device_id)
+ elsif type == :moisture
+ entry = Moisture.last(device: device_id)
+ end
+
+ if entry != nil
+ value = entry[:value]
+ end
+
+ return value
+end
+
+def get_time(type, device_id)
+ entry = nil
+ value = Time.at(0)
+ if type == :temperature
+ entry = Temperature.last(device: device_id)
+ elsif type == :humidity
+ entry = Humidity.last(device: device_id)
+ elsif type == :moisture
+ entry = Moisture.last(device: device_id)
+ end
+
+ if entry != nil
+ value = entry[:timestamp]
+ end
+
+ return value
+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: "cricket1")[: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]
+ @temp_prop = get_value(:temperature, "cricket0")
+ @humidity_prop = get_value(:humidity, "cricket0")
+ @temp_air = get_value(:temperature, "mercury")
+ @humidity_air = get_value(:humidity, "mercury")
+ @temp_kitchen = get_value(:temperature, "cricket1")
+ @humidity_kitchen = get_value(:humidity, "cricket1")
+ @moisture_kitchen = (1000-(get_value(:moisture, "cricket1")))/10
+ time_prop = get_time(:temperature, "cricket0")
+ time_air = get_time(:temperature, "mercury")
+ time_kitchen = get_time(:temperature, "cricket1")
@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")
@@ -137,13 +173,13 @@ 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]
+ temp_prop = get_value(:temperature, "cricket0")
+ humidity_prop = get_value(:humidity, "cricket0")
+ temp_air = get_value(:temperature, "mercury")
+ humidity_air = get_value(:humidity, "mercury")
+ temp_kitchen = get_value(:temperature, "cricket1")
+ humidity_kitchen = get_value(:humidity, "cricket1")
+ moisture_kitchen = (1000-(get_value(:moisture, "cricket1")))/10
"humidity_basement #{humidity_air}
humidity_propagator #{humidity_prop}
humidity_kitchen #{humidity_kitchen}