summaryrefslogtreecommitdiff
path: root/weather-db-from-old-log
diff options
context:
space:
mode:
authorpks <pks@pks.rocks>2021-05-02 21:56:23 +0200
committerpks <pks@pks.rocks>2021-05-02 21:56:23 +0200
commit69e11599c2194187e1c172ebe6b7b51945a7f306 (patch)
tree99da60b9b4eca13ed9f6d851e3c127ed55990db6 /weather-db-from-old-log
parentd99fb40baf87b484b25311b2c5e8170bd2224a7b (diff)
v2
Diffstat (limited to 'weather-db-from-old-log')
-rwxr-xr-xweather-db-from-old-log81
1 files changed, 81 insertions, 0 deletions
diff --git a/weather-db-from-old-log b/weather-db-from-old-log
new file mode 100755
index 0000000..3efa4b1
--- /dev/null
+++ b/weather-db-from-old-log
@@ -0,0 +1,81 @@
+#!/usr/bin/env ruby
+
+require 'sqlite3'
+
+# Berlin
+#$device_id_to_handle = {
+# "b7:01" => "outside",
+# "ca:01" => "outside",
+# "d5:01" => "outside",
+# "f4:01" => "outside",
+# "1d:01" => "inside",
+# "1f:01" => "inside",
+# "37:01" => "inside",
+# "38:01" => "inside",
+# "9c:01" => "inside",
+# "cb:01" => "inside"
+#}
+
+# Heidenheim
+$device_id_to_handle = {
+ "58:02" => "outside",
+ "62:02" => "outside",
+ "07:01" => "inside"
+}
+
+def db_execute s, d
+ begin
+ $db.execute(s, d)
+ rescue SQLite3::BusyException
+ sleep 3
+ begin
+ $db.execute(s, d)
+ rescue SQLite3::BusyException
+ STDERR.write "DB busy, skipping data point '#{d.to_s}'\n"
+ end
+ end
+end
+
+def db_insert timestamp, handle, temperature, humidity
+ db_execute("INSERT INTO weather(timestamp, handle, temperature, humidity) VALUES(?,?,?,?)",
+ [timestamp, handle, temperature, humidity])
+end
+
+
+$db = SQLite3::Database.new("weather.db")
+
+$db.execute <<-SQL
+ create table weather(
+ id INTEGER PRIMARY KEY,
+ timestamp DATETIME,
+ handle TEXT,
+ temperature FLOAT,
+ humidity FLOAT
+ );
+ SQL
+
+while line = STDIN.gets
+ parts = line.split "\t"
+ log_entry_type = parts.first.split("::")[1]
+ if log_entry_type == "temp"
+ data = {}
+ parts.each { |i|
+ key, value = i.split "::"
+ data[key] = value
+ }
+ data["timestamp"] = data["timestamp"].to_i
+ data["temperature"] = data["temperature"].to_f
+ data["humidity"] = data["humidity"].to_f
+
+ handle = $device_id_to_handle[data["device_id"]]
+ if handle
+ db_insert \
+ data["timestamp"], \
+ handle, \
+ data["temperature"], \
+ data["humidity"]
+ end
+ end
+end
+
+$db.close