-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.go
156 lines (144 loc) · 3.18 KB
/
helpers.go
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package wx
import (
"database/sql"
"log"
"math"
"strings"
_ "github.com/mattn/go-sqlite3"
"github.com/westphae/caliban/tempest"
)
const (
dbFile string = "tempest.db"
createObs string = `
CREATE TABLE IF NOT EXISTS observations (
deviceId INTEGER NOT NULL,
timestamp INTEGER NOT NULL,
windLull REAL,
windAvg REAL,
windGust REAL,
windDirection INTEGER,
windSampleInterval INTEGER,
pressure REAL,
airTemperature REAL,
relativeHumidity INTEGER,
illuminance INTEGER,
uv REAL,
solarRadiation INTEGER,
rainAccumulation INTEGER,
precipitationType INTEGER,
averageStrikeDistance INTEGER,
strikeCount INTEGER,
batteryVolts REAL,
reportInterval INTEGER,
localDayRainAccumulation INTEGER,
nCRainAccumulation INTEGER,
localDayNCRainAccumulation INTEGER,
precipitationAnalysisType INTEGER,
PRIMARY KEY (deviceId, timestamp)
);`
insertObs string = `
INSERT INTO observations VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?
);`
getObs string = `SELECT * FROM observations WHERE deviceId = ? AND timestamp>= ? AND timestamp < ? ORDER BY timestamp;`
)
var (
db *sql.DB
)
func Dewpoint(rh, t float64) (td float64) {
rr := (17.625 * t) / (243.04 + t)
lrh := math.Log(rh / 100)
return 243.04 * (lrh + rr) / (17.625 - lrh - rr)
}
func init() {
// Set up database
var err error
if db, err = sql.Open("sqlite3", dbFile); err != nil {
panic(err)
}
if _, err := db.Exec(createObs); err != nil {
panic(err)
}
}
func SaveTempestDataToDb(deviceId int, obs tempest.Observation) (err error) {
res, err := db.Exec(insertObs,
deviceId,
obs.Timestamp,
obs.WindLull,
obs.WindAvg,
obs.WindGust,
obs.WindDirection,
obs.WindSampleInterval,
obs.Pressure,
obs.AirTemperature,
obs.RelativeHumidity,
obs.Illuminance,
obs.UV,
obs.SolarRadiation,
obs.RainAccumulation,
obs.PrecipitationType,
obs.AverageStrikeDistance,
obs.StrikeCount,
obs.BatteryVolts,
obs.ReportInterval,
obs.LocalDayRainAccumulation,
obs.NCRainAccumulation,
obs.LocalDayNCRainAccumulation,
obs.PrecipitationAnalysisType,
)
switch {
case err == nil:
log.Println("saved tempest data to sqlite db")
case strings.HasPrefix(err.Error(), "UNIQUE constraint failed"):
log.Println("observation already in sqlite db")
return nil
default:
return err
}
if _, err = res.LastInsertId(); err != nil {
return err
}
return nil
}
func GetTempestDataFromDb(deviceId int, tsStart, tsEnd int64) (obs []tempest.Observation, err error) {
var (
d int
)
rows, err := db.Query(getObs, deviceId, tsStart, tsEnd)
if err != nil {
return nil, err
}
defer rows.Close()
o := tempest.Observation{}
for rows.Next() {
err = rows.Scan(
&d,
&o.Timestamp,
&o.WindLull,
&o.WindAvg,
&o.WindGust,
&o.WindDirection,
&o.WindSampleInterval,
&o.Pressure,
&o.AirTemperature,
&o.RelativeHumidity,
&o.Illuminance,
&o.UV,
&o.SolarRadiation,
&o.RainAccumulation,
&o.PrecipitationType,
&o.AverageStrikeDistance,
&o.StrikeCount,
&o.BatteryVolts,
&o.ReportInterval,
&o.LocalDayRainAccumulation,
&o.NCRainAccumulation,
&o.LocalDayNCRainAccumulation,
&o.PrecipitationAnalysisType,
)
if err != nil {
return nil, err
}
obs = append(obs, o)
}
return obs, nil
}