summaryrefslogtreecommitdiff
path: root/v1/fill-db-from-log.rb
diff options
context:
space:
mode:
authorpks <pks@pks.rocks>2021-05-10 13:23:21 +0200
committerpks <pks@pks.rocks>2021-05-10 13:23:21 +0200
commit281fa36a24f81d87543a7791252954414e161565 (patch)
treeab45e491aa93924f7310303913f2df2d3f3b069f /v1/fill-db-from-log.rb
parent69e11599c2194187e1c172ebe6b7b51945a7f306 (diff)
rm v0 v1
Diffstat (limited to 'v1/fill-db-from-log.rb')
-rwxr-xr-xv1/fill-db-from-log.rb90
1 files changed, 0 insertions, 90 deletions
diff --git a/v1/fill-db-from-log.rb b/v1/fill-db-from-log.rb
deleted file mode 100755
index 1fb2ab3..0000000
--- a/v1/fill-db-from-log.rb
+++ /dev/null
@@ -1,90 +0,0 @@
-#!/usr/bin/env ruby
-
-require 'sqlite3'
-
-
-int = Proc.new do |x|
- x.to_i
-end
-float = Proc.new do |x|
- x.to_f
-end
-str = Proc.new do |x|
- x.to_s
-end
-common_conv = { "db" => str, "timestamp"=>float, "device_type"=>str, "type"=>str, "device_id"=>str, "battery" => int, "battery_numeric" => int, "rssi" => int, "rssi_numeric" => int }
-rain_conv = {"rain_rate"=>float, "rain_total"=>float}.merge(common_conv)
-wind_conv = {"chill"=>float, "temperature"=>float,"average_speed"=>float,"direction"=>int,"gust"=>float}.merge(common_conv)
-temp_conv = {"humidity"=>float, "humidity_status"=>str,"humidity_status_numeric"=>int,"temperature"=>float,"barometer"=>float,"forecast"=>str,"forecast_numeric"=>int}.merge(common_conv)
-conv = { "rain" => rain_conv, "wind" => wind_conv, "temp" => temp_conv }
-
-$db = SQLite3::Database.new("weather.db")
-
-
-def parse_to_hash s, conv, type
- h = {}
- begin
- s.split("\t").map { |i|
- i=i.split("::")
- if not conv.keys.include? i[0]
- STDERR.write "Unknown key '#{i[0]}' in data point of type '#{type}', ignoring!\n"
- end
- h[i[0]] = conv[i[0]].call(i[1])
- }
- rescue
- STDERR.write "Cannot parse '#{s.strip}', skipping data point!\n"
- return false
- end
- return h
-end
-
-
-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 insert_rain h
- db_execute("INSERT INTO rain(timestamp, device_type, device_id, battery, rssi, rain_rate, rain_total) VALUES(?,?,?,?,?,?,?)", [h["timestamp"], h["device_type"],h["device_id"], h["battery"], h["rssi"], h["rain_rate"], h["rain_total"]])
-end
-
-
-def insert_wind h
- db_execute("INSERT INTO wind(timestamp, device_type, device_id, battery, rssi, chill, temperature, average_speed, direction, gust) VALUES(?,?,?,?,?,?,?,?,?,?)", [h["timestamp"], h["device_type"],h["device_id"], h["battery"], h["rssi"], h["chill"], h["temperature"], h["average_speed"], h["direction"], h["gust"]])
-end
-
-
-def insert_temp h
- if h.keys.include? "forecast"
- db_execute("INSERT INTO temp(timestamp, device_type, device_id, battery, rssi, humidity, humidity_status, humidity_status_numeric, temperature, forecast, forecast_numeric) VALUES(?,?,?,?,?,?,?,?,?,?,?)",[h["timestamp"], h["device_type"],h["device_id"], h["battery"], h["rssi"], h["humidity"], h["humidity_status"], h["humidity_status_numeric"], h["temperature"], h["forecast"], h["forecast_numeric"]])
- else
- db_execute("INSERT INTO temp(timestamp, device_type, device_id, battery, rssi, humidity, humidity_status, humidity_status_numeric, temperature) VALUES(?,?,?,?,?,?,?,?,?)",[h["timestamp"], h["device_type"],h["device_id"], h["battery"], h["rssi"], h["humidity"], h["humidity_status"], h["humidity_status_numeric"], h["temperature"]])
- end
-end
-
-
-while line = STDIN.gets
- break if line.strip=="stop"
- type = line.split("\t").first.split("::")[1]
- h = parse_to_hash(line, conv[type], type)
- if h
- if type == "rain"
- insert_rain h
- elsif type == "wind"
- insert_wind h
- elsif type == "temp"
- insert_temp h
- end
- end
-end
-
-$db.close