summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorpks <pks@pks.rocks>2021-05-01 06:16:19 +0200
committerpks <pks@pks.rocks>2021-05-01 06:16:19 +0200
commit63fbf0f3aadb4b6d037a8164b501fe836d9c361d (patch)
treea33240dfd9ad5c57f283e942514bb8ce67d71085
parent00fdbd489494c917865cf3fbf153e5751052e2d1 (diff)
v1
-rw-r--r--.gitignore3
-rwxr-xr-xcreate-db.rb5
-rwxr-xr-xfill-db-from-log.rb90
-rwxr-xr-xfill-db.rb2
-rwxr-xr-xget-temps.rb9
-rwxr-xr-xget-temps.rb.bak16
-rwxr-xr-xreceive.py2
-rwxr-xr-xweather-logger16
8 files changed, 133 insertions, 10 deletions
diff --git a/.gitignore b/.gitignore
index 0293661..08278ee 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,3 +1,6 @@
+*env/
+db/
+log/
*.db
*.err
*.gz
diff --git a/create-db.rb b/create-db.rb
index 12326c0..2866b7b 100755
--- a/create-db.rb
+++ b/create-db.rb
@@ -42,9 +42,10 @@ db.execute <<-SQL
humidity FLOAT,
humidity_status TEXT,
humidity_status_numeric INTEGER,
- temperature FLOAT
+ temperature FLOAT,
+ forecast TEXT,
+ forecast_numeric INTEGER
);
SQL
db.close
-
diff --git a/fill-db-from-log.rb b/fill-db-from-log.rb
new file mode 100755
index 0000000..1fb2ab3
--- /dev/null
+++ b/fill-db-from-log.rb
@@ -0,0 +1,90 @@
+#!/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
diff --git a/fill-db.rb b/fill-db.rb
index c4fa513..1bfc0f2 100755
--- a/fill-db.rb
+++ b/fill-db.rb
@@ -17,7 +17,7 @@ wind_conv = {"chill"=>float, "temperature"=>float,"average_speed"=>float,"direct
temp_conv = {"humidity"=>float, "humidity_status"=>str,"humidity_status_numeric"=>int,"temperature"=>float}.merge(common_conv)
conv = { "rain" => rain_conv, "wind" => wind_conv, "temp" => temp_conv }
-$db = SQLite3::Database.new("weather.db")
+$db = SQLite3::Database.new("db/weather.db")
def parse_to_hash s, conv
h = {}
diff --git a/get-temps.rb b/get-temps.rb
index ac24051..f25540b 100755
--- a/get-temps.rb
+++ b/get-temps.rb
@@ -1,15 +1,20 @@
#!/usr/bin/env ruby
+$devices = { "f4:01" => "outside", "1f:01" => "inside" }
+
while line = STDIN.gets
a = line.split
d = {}
a.each { |i|
name, val = i.split("::")
+ if name == "timestamp"
+ d["timestamp"] = val.to_f
+ end
if name == "temperature"
- d["temperature"] = val
+ d["temperature"] = val.to_f
end
if name == "device_id"
- d["dev"] = val
+ d["dev"] = devices[val]
end
}
puts d.to_s
diff --git a/get-temps.rb.bak b/get-temps.rb.bak
new file mode 100755
index 0000000..ac24051
--- /dev/null
+++ b/get-temps.rb.bak
@@ -0,0 +1,16 @@
+#!/usr/bin/env ruby
+
+while line = STDIN.gets
+ a = line.split
+ d = {}
+ a.each { |i|
+ name, val = i.split("::")
+ if name == "temperature"
+ d["temperature"] = val
+ end
+ if name == "device_id"
+ d["dev"] = val
+ end
+ }
+ puts d.to_s
+end
diff --git a/receive.py b/receive.py
index a56789f..71d0a67 100755
--- a/receive.py
+++ b/receive.py
@@ -9,7 +9,7 @@ if __name__ == "__main__":
transport.reset()
while True:
- if os.path.isfile('/home/pi/weather/stop'):
+ if os.path.isfile('/home/pks/weather/stop'):
break
recv = transport.receive_blocking()
diff --git a/weather-logger b/weather-logger
index fc1f6a8..84be622 100755
--- a/weather-logger
+++ b/weather-logger
@@ -1,16 +1,24 @@
#!/bin/sh
-# /etc/init.d/weather-logger
-
+### BEGIN INIT INFO
+# Provides: weather-logger
+# Required-Start: $all
+# Required-Stop:
+# Default-Start: 2 3 4 5
+# Default-Stop:
+# Short-Description: weather-logger
+### END INIT INFO
export SUFFIX=$(date +'%Y-%m-%d-%H:%M:%S')
export USER=pks
-export DIR=/home/$USER/weather/
+export DIR=/home/$USER/weather_logger/
+export PYTHONPATH=/home/$USER/.local/lib/python3.7/site-packages/
case "$1" in
start)
echo "Starting weather-logger"
rm -f $DIR/stop
- runuser -l $USER -c "source $DIR/env/bin/activate && python $DIR/receive.py 2>$DIR/weather.$SUFFIX.err > $DIR/weather.$SUFFIX.out" &
+ #runuser -l $USER -c "source $DIR/env/bin/activate && python $DIR/receive.py 2>$DIR/weather.$SUFFIX.err > $DIR/weather.$SUFFIX.out" &
+ runuser -l $USER -c "python3.7 $DIR/receive.py 2>$DIR/log/weather.$SUFFIX.err > $DIR/log/weather.$SUFFIX.out" &
;;
stop)
echo "Stopping weather-logger"