1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
|
#!/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}.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
h = {}
s.split("\t").map { |i| i=i.split("::"); h[i[0]] = conv[i[0]].call(i[1]) }
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::rain type::PCR800 device_id::eb:00 battery::9 rain_rate::0.0 rain_total::0.9 rssi::6
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::wind type::WGR800 device_id::04:00 battery::9 chill::0.0 rssi::5 temperature::0.0 average_speed::0.0 direction::270gust::0.0
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
#db::temp type::THGR810, THGN800 device_id::10:02 battery::9 humidity::80 humidity_status::wet humidity_status_numeric::3 rssi::5 temperature::2.6
#db::temp type::THGR810, THGN800 device_id::25:01 battery::0 humidity::94 humidity_status::wet humidity_status_numeric::3 rssi::6 temperature::1.1
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
while line = STDIN.gets
break if line.strip=="stop"
db = line.split("\t").first.split("::")[1]
h = parse_to_hash(line, conv[db])
if db == "rain"
insert_rain h
elsif db == "wind"
insert_wind h
elsif db == "temp"
insert_temp h
end
end
$db.close
|