/
report.go
453 lines (396 loc) · 16.3 KB
/
report.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
// Copyright (c) 2020 Tanner Ryan. All rights reserved. Use of this source code
// is governed by a BSD-style license that can be found in the LICENSE file.
package davisweather
import (
"bytes"
"compress/zlib"
"crypto/md5"
"encoding/hex"
"encoding/json"
"errors"
"io/ioutil"
"log"
"sync"
"time"
"github.com/tannerryan/davisweather/parser"
)
// Report is the latest weather report.
type Report struct {
DeviceID string `json:"deviceID"` // DeviceID is unique device ID
Timestamp time.Time `json:"timestamp"` // Timestamp is the time the Report was last modified
Temperature *float64 `json:"temperature"` // Temperature (°F)
Humidity *float64 `json:"humidity"` // Humidity (%RH)
Dewpoint *float64 `json:"dewpoint"` // Dewpoint (°F)
Wetbulb *float64 `json:"wetbulb"` // Wetbulb (°F)
HeatIndex *float64 `json:"heatindex"` // HeatIndex (°F)
WindChill *float64 `json:"windchill"` // WindChill (°F)
THWIndex *float64 `json:"thwIndex"` // THWIndex is "feels like" (°F)
THSWIndex *float64 `json:"thswIndex"` // THWSIndex is "feels like" including solar (°F)
WindSpeedLast *float64 `json:"windSpeedLast"` // WindSpeedLast is most recent wind speed (mph)
WindDirLast *float64 `json:"windDirLast"` // WindDirLast is most recent wind direction (°)
WindSpeedAvgLast1Min *float64 `json:"windSpeedAvg1Min"` // WindSpeedAvgLast1Min is average wind over last minute (mph)
WindDirAvgLast1Min *float64 `json:"windDirAvg1Min"` // WindDirAvgLast1Min is average wind direction over last minute (°)
WindSpeedAvgLast2Min *float64 `json:"windSpeedAvg2Min"` // WindSpeedAvgLast2Min is average wind over last 2 minutes (mph)
WindDirAvgLast2Min *float64 `json:"windDirAvg2Min"` // WindDirAvgLast2Min is average wind direction over last 2 minutes (°)
WindSpeedHighLast2Min *float64 `json:"windGustSpeedLast2Min"` // WindSpeedHighLast2Min is max gust over last 2 minutes (mph)
WindDirAtHighLast2Min *float64 `json:"windGustDirLast2Min"` // WindDirAtHighLast2Min is max gust direction over last 2 minutes (°)
WindSpeedAvgLast10Min *float64 `json:"windSpeedAvg10Min"` // WindSpeedAvgLast10Min is average wind over last 10 minutes (mph)
WindDirAvgLast10Min *float64 `json:"windDirAvg10Min"` // WindDirAvgLast10Min is average wind dir over last 10 minutes (°)
WindSpeedHighLast10Min *float64 `json:"windGustSpeedLast10Min"` // WindSpeedHighLast10Min is max gust over last 10 minutes (mph)
WindDirAtHighLast10Min *float64 `json:"windGustDirLast10Min"` // WindDirAtHighLast10Min is max gust direction over last 10 minutes (°)
RainSize *float64 `json:"rainSize"` // RainSize is size of rain collector (1: 0.01", 2: 0.2mm)
RainRateLast *float64 `json:"rainRateLast"` // RainRateLast is most recent rain rate (count/hour)
RainRateHigh *float64 `json:"rainRateHigh"` // RainRateHigh is highest rain rate over last minute (count/hour)
RainLast15Min *float64 `json:"rainLast15Min"` // RainLast15Min is rain count in last 15 minutes (count)
RainRateHighLast15Min *float64 `json:"rainRateHighLast15Min"` // RainRateHighLast15Min is highest rain count rate over last 15 minutes (count/hour)
RainLast60Min *float64 `json:"rainLast60Min"` // RainLast60Min is rain count over last 60 minutes (count)
RainLast24Hour *float64 `json:"rainLast24Hour"` // RainLast24Hour is rain count over last 24 hours (count)
RainStorm *float64 `json:"rainStorm"` // RainStorm is rain since last 24 hour break in rain (count)
RainStormStartAt *time.Time `json:"rainStormStart"` // RainStormStartAt is time of rain storm start
SolarRad *float64 `json:"solarRad"` // SolarRad is solar radiation (W/m²)
UVIndex *float64 `json:"uvIndex"` // UVIndex is solar UV index
RXState string `json:"signal"` // RXState is ISS receiver status
TransBatteryFlag string `json:"battery"` // TransBatteryFlag is ISS battery status
RainfallDaily *float64 `json:"rainDaily"` // RainfallDaily is total rain since midnight (count)
RainfallMonthly *float64 `json:"rainMonthly"` // RainfallMonthly is total rain since first of month (count)
RainfallYear *float64 `json:"rainYear"` // RainfallYear is total rain since first of year (count)
RainStormLast *float64 `json:"rainStormLast"` // RainStormLast is rain since last 24 hour break in rain (count)
RainStormLastStartAt *time.Time `json:"rainStormLastStart"` // RainStormLastStartAt is time of last rain storm start
RainStormLastEndAt *time.Time `json:"rainStormLastEnd"` // rainStormLastEndAt is time of last rain storm end
BarometerSeaLevel *float64 `json:"barometerSeaLevel"` // BarometerSeaLevel is barometer reading with elevation adjustment (inches)
BarometerTrend *float64 `json:"barometerTrend"` // BarometerTrend is 3 hour barometric trend (inches)
BarometerAbsolute *float64 `json:"barometerAbsolute"` // BarometerAbsolute is barometer reading at current elevation (inches)
TemperatureIndoor *float64 `json:"indoorTemperature"` // TemperatureIndoor is indoor temp (°F)
HumidityIndoor *float64 `json:"indoorHumidity"` // HumidityIndoor is indoor humidity (%)
DewPointIndoor *float64 `json:"indoorDewpoint"` // DewPointIndoor is indoor dewpoint (°F)
HeatIndexIndoor *float64 `json:"indoorHeatIndex"` // HeatIndexIndoor is indoor heat index (°F)
notify chan bool // notify emits a boolean when the Report contents are modified
verbose bool // verbose enables Report logging to stdout
lastChecksum string // lastChecksum is MD5 checksum of the Report state
lastBytes []byte // lastBytes is the JSON representation of the Report state
mutex *sync.Mutex // mutex is for atomic report actions
}
// NewReport returns a new Report state and a notification channel. The channel
// emits a bool when the Report contents have been modified. Verbose enables
// Report logging.
func NewReport(verbose bool) (*Report, chan bool) {
r := &Report{
notify: make(chan bool, 1),
verbose: verbose,
lastChecksum: "",
lastBytes: nil,
mutex: &sync.Mutex{},
}
return r, r.notify
}
// UpdateHTTP atomically updates the Report state using weather conditions
// retrieved over HTTP. It returns an error if the atomic update action fails.
func (r *Report) UpdateHTTP(new *parser.ConditionsHTTP) error {
r.mutex.Lock()
defer r.mutex.Unlock()
// ensure no error in conditions
if new.Error != nil {
return errors.New(new.Error.Message)
}
// set report header
r.DeviceID = new.Data.DeviceID
// iterate over all provided conditions, load conditions into report
for _, c := range new.Data.Conditions {
structure := *c.DataStructureType
switch structure {
case parser.RecordISS:
v := c.Values.(*parser.WeatherISS)
r.processISS(v)
case parser.RecordLSSBarometer:
v := c.Values.(*parser.WeatherLSSBarometer)
r.processLSSBarometer(v)
case parser.RecordLSSTempRh:
v := c.Values.(*parser.WeatherLSSTempRh)
r.processLSSTempRh(v)
}
}
return r.updateHook(parser.UpdateHTTP, new.Data.Timestamp.Time())
}
// UpdateUDP atomically updates the Report state using weather conditions
// received over UDP. It returns an error if the atomic update action fails.
func (r *Report) UpdateUDP(new *parser.ConditionsUDP) error {
r.mutex.Lock()
defer r.mutex.Unlock()
// set report header
r.DeviceID = new.DeviceID
// iterate over all conditions, load conditions into report
for _, c := range new.Conditions {
r.WindSpeedLast = c.WindSpeedLast
r.WindDirLast = c.WindDirLast
r.RainSize = c.RainSize
r.RainRateLast = c.RainRateLast
r.RainLast15Min = c.RainLast15Min
r.RainLast60Min = c.RainLast60Min
r.RainLast24Hour = c.RainLast24Hour
r.RainStorm = c.RainStorm
if c.RainStormStartAt != nil {
t := c.RainStormStartAt.Time()
r.RainStormStartAt = &t
}
r.RainfallDaily = c.RainfallDaily
r.RainfallMonthly = c.RainfallMonthly
r.RainfallYear = c.RainfallYear
r.WindSpeedHighLast10Min = c.WindSpeedHighLast10Min
r.WindDirAtHighLast10Min = c.WindDirAtHighLast10Min
}
return r.updateHook(parser.UpdateUDP, new.Timestamp.Time())
}
// UpdateJSON atomically updates the Report state using a provided JSON payload.
// It returns an error if the JSON does not match the Report structure or if the
// atomic update action fails.
func (r *Report) UpdateJSON(payload []byte) error {
r.mutex.Lock()
defer r.mutex.Unlock()
// attempt parsing
var n Report
err := json.Unmarshal(payload, &n)
if err != nil {
return err
}
// synchronize all data fields
r.DeviceID = n.DeviceID
r.Temperature = n.Temperature
r.Humidity = n.Humidity
r.Dewpoint = n.Dewpoint
r.Wetbulb = n.Wetbulb
r.HeatIndex = n.HeatIndex
r.WindChill = n.WindChill
r.THWIndex = n.THWIndex
r.THSWIndex = n.THSWIndex
r.WindSpeedLast = n.WindSpeedLast
r.WindDirLast = n.WindDirLast
r.WindSpeedAvgLast1Min = n.WindSpeedAvgLast1Min
r.WindDirAvgLast1Min = n.WindDirAvgLast1Min
r.WindSpeedAvgLast2Min = n.WindSpeedAvgLast2Min
r.WindDirAvgLast2Min = n.WindDirAvgLast2Min
r.WindSpeedHighLast2Min = n.WindSpeedHighLast2Min
r.WindDirAtHighLast2Min = n.WindDirAtHighLast2Min
r.WindSpeedAvgLast10Min = n.WindSpeedAvgLast10Min
r.WindDirAvgLast10Min = n.WindDirAvgLast10Min
r.WindSpeedHighLast10Min = n.WindSpeedHighLast10Min
r.WindDirAtHighLast10Min = n.WindDirAtHighLast10Min
r.RainSize = n.RainSize
r.RainRateLast = n.RainRateLast
r.RainRateHigh = n.RainRateHigh
r.RainLast15Min = n.RainLast15Min
r.RainRateHighLast15Min = n.RainRateHighLast15Min
r.RainLast60Min = n.RainLast60Min
r.RainLast24Hour = n.RainLast24Hour
r.RainStorm = n.RainStorm
r.RainStormStartAt = n.RainStormStartAt
r.SolarRad = n.SolarRad
r.UVIndex = n.UVIndex
r.RXState = n.RXState
r.TransBatteryFlag = n.TransBatteryFlag
r.RainfallDaily = n.RainfallDaily
r.RainfallMonthly = n.RainfallMonthly
r.RainfallYear = n.RainfallYear
r.RainStormLast = n.RainStormLast
r.RainStormLastStartAt = n.RainStormLastStartAt
r.RainStormLastEndAt = n.RainStormLastEndAt
r.BarometerSeaLevel = n.BarometerSeaLevel
r.BarometerTrend = n.BarometerTrend
r.BarometerAbsolute = n.BarometerAbsolute
r.TemperatureIndoor = n.TemperatureIndoor
r.HumidityIndoor = n.HumidityIndoor
r.DewPointIndoor = n.DewPointIndoor
r.HeatIndexIndoor = n.HeatIndexIndoor
return r.updateHook(parser.UpdateJSON, time.Now())
}
// Copy generates and returns a new deep copy of the Report state. It returns an
// error if the copy operation fails.
func (r *Report) Copy() (*Report, error) {
r.mutex.Lock()
defer r.mutex.Unlock()
// marshal current report
buff, err := json.Marshal(r)
if err != nil {
return nil, err
}
report, notify := NewReport(r.verbose)
// unmarshal buffer into report
err = json.Unmarshal(buff, &report)
if err != nil {
return nil, err
}
// sync private variables
report.notify = notify
report.lastChecksum = r.lastChecksum
report.lastBytes = r.lastBytes
report.mutex = &sync.Mutex{}
return report, nil
}
// JSON returns the JSON representation of the Report state and the Report was
// last updated.
func (r *Report) JSON() []byte {
r.mutex.Lock()
defer r.mutex.Unlock()
return r.lastBytes
}
// Encode returns a zlib encoded weather report.
func (r *Report) Encode() []byte {
r.mutex.Lock()
defer r.mutex.Unlock()
// stream last bytes into compressed buffer
var buff bytes.Buffer
stream := zlib.NewWriter(&buff)
stream.Write(r.lastBytes)
stream.Close()
// return compressed bytes
return buff.Bytes()
}
// Decode updates the Report using a zlib encoded weather report. It returns an
// error if the provided payload is not valid.
func (r *Report) Decode(payload []byte) error {
// uncompress data
buff := bytes.NewReader(payload)
stream, err := zlib.NewReader(buff)
if err != nil {
return err
}
defer stream.Close()
// read uncompressed data
report, err := ioutil.ReadAll(stream)
if err != nil {
return err
}
return r.UpdateJSON(report)
}
// updateHook is called after UpdateHTTP, UpdateUDP, and UpdateJSON. If the
// Report state contents have changed, it updates the lastBytes, lastChecksum,
// and Timestamp fields, emitting a bool on the notify channel if the channel is
// not full. If the Report state contents are not changed, nothing happens. It
// returns an error if the Report state checksum fails.
func (r *Report) updateHook(method parser.UpdateMethod, timestamp time.Time) error {
// calculate checksum of latest report
newChecksum, _, err := r.checksum()
if err != nil {
return err
}
// ignore if no ISS transmitter or ISS transmitter battery flag is given
if r.RXState == "" || r.TransBatteryFlag == "" {
return nil
}
// only update internals, timestamp, and notify if new content
if newChecksum != r.lastChecksum {
// update last timestamp, last bytes and checksum
r.Timestamp = timestamp
r.lastChecksum, r.lastBytes, _ = r.checksum()
select {
case r.notify <- true: // attempt to notify
if r.verbose {
log.Println("[davisweather report] new data from", r.DeviceID, method)
}
default: // already notified on channel
if r.verbose {
log.Println("[davisweather report] new data from", r.DeviceID, method, "(downstream pressure on Notify)")
}
}
return nil
}
if r.verbose {
log.Println("[davisweather report] no new data from", r.DeviceID, method)
}
return nil
}
// checksum return an MD5 checksum of the Report state and the JSON
// representation of the Report state. It returns an error if the checksum
// fails.
func (r *Report) checksum() (string, []byte, error) {
// marshal current state
buff, err := json.Marshal(r)
if err != nil {
return "", nil, err
}
// perform sum
hash := md5.Sum(buff)
return hex.EncodeToString(hash[:]), buff, nil
}
// processISS synchronizes the Report state with the provided ISS weather
// conditions.
func (r *Report) processISS(v *parser.WeatherISS) {
r.Temperature = v.Temperature
r.Humidity = v.Humidity
r.Dewpoint = v.Dewpoint
r.Wetbulb = v.Wetbulb
r.HeatIndex = v.HeatIndex
r.WindChill = v.WindChill
r.THWIndex = v.THWIndex
r.THSWIndex = v.THSWIndex
r.WindSpeedLast = v.WindSpeedLast
r.WindDirLast = v.WindDirLast
r.WindSpeedAvgLast1Min = v.WindSpeedAvgLast1Min
r.WindDirAvgLast1Min = v.WindDirAvgLast1Min
r.WindSpeedAvgLast2Min = v.WindSpeedAvgLast2Min
r.WindDirAvgLast2Min = v.WindDirAvgLast2Min
r.WindSpeedHighLast2Min = v.WindSpeedHighLast2Min
r.WindDirAtHighLast2Min = v.WindDirAtHighLast2Min
r.WindSpeedAvgLast10Min = v.WindSpeedAvgLast10Min
r.WindDirAvgLast10Min = v.WindDirAvgLast10Min
r.WindSpeedHighLast10Min = v.WindSpeedHighLast10Min
r.WindDirAtHighLast10Min = v.WindDirAtHighLast10Min
r.RainSize = v.RainSize
r.RainRateLast = v.RainRateLast
r.RainRateHigh = v.RainRateHigh
r.RainLast15Min = v.RainLast15Min
r.RainRateHighLast15Min = v.RainRateHighLast15Min
r.RainLast60Min = v.RainLast60Min
r.RainLast24Hour = v.RainLast24Hour
r.RainStorm = v.RainStorm
if v.RainStormStartAt != nil {
t := v.RainStormStartAt.Time()
r.RainStormStartAt = &t
}
r.SolarRad = v.SolarRad
r.UVIndex = v.UVIndex
if v.RXState != nil {
switch *v.RXState {
case parser.SignalSynced:
r.RXState = "Synced"
case parser.SignalRescan:
r.RXState = "Rescan"
case parser.SignalLost:
r.RXState = "Lost"
}
}
if v.TransBatteryFlag != nil {
switch *v.TransBatteryFlag {
case parser.BatteryNominal:
r.TransBatteryFlag = "Nominal"
case parser.BatteryWarning:
r.TransBatteryFlag = "Warning"
}
}
r.RainfallDaily = v.RainfallDaily
r.RainfallMonthly = v.RainfallMonthly
r.RainfallYear = v.RainfallYear
r.RainStormLast = v.RainStormLast
if v.RainStormLastStartAt != nil {
t := v.RainStormLastStartAt.Time()
r.RainStormLastStartAt = &t
}
if v.RainStormLastEndAt != nil {
t := v.RainStormLastEndAt.Time()
r.RainStormLastEndAt = &t
}
}
// processLSSBarometer synchronizes the Report state with the provided LSS
// barometer weather conditions.
func (r *Report) processLSSBarometer(v *parser.WeatherLSSBarometer) {
r.BarometerSeaLevel = v.BarometerSeaLevel
r.BarometerTrend = v.BarometerTrend
r.BarometerAbsolute = v.BarometerAbsolute
}
// processLSSBarometer synchronizes the Report state with the provided LSS
// temperature humidity weather conditions.
func (r *Report) processLSSTempRh(v *parser.WeatherLSSTempRh) {
r.TemperatureIndoor = v.TemperatureIndoor
r.HumidityIndoor = v.HumidityIndoor
r.DewPointIndoor = v.DewPointIndoor
r.HeatIndexIndoor = v.HeatIndexIndoor
}