-
Notifications
You must be signed in to change notification settings - Fork 1
/
types.go
203 lines (165 loc) · 5.01 KB
/
types.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
package main
/*****************************************************************************
* Tempest UDP json objects
* Built from documentation of the format here:
* https://weatherflow.github.io/Tempest/api/udp/v143/
****************************************************************************/
import (
"errors"
"time"
)
type TempestMessage struct {
SerialNumber string `json:"serial_number"`
MessageType string `json:"type"`
}
type RapidWind struct {
TempestMessage
HubSN string `json:"hub_sn"`
Observation []float64 `json:"ob"`
}
func (w *RapidWind) IsValid() bool {
return len(w.Observation) >= 2
}
func (w *RapidWind) Speed() (float64, error) {
return w.GetField(1)
}
func (w *RapidWind) Direction() (float64, error) {
return w.GetField(2)
}
func (w *RapidWind) GetField(n int) (float64, error) {
if len(w.Observation) < n {
return 0, errors.New("No such field")
}
return w.Observation[n], nil
}
type HubStatus struct {
TempestMessage
SerialNumber string `json:"serial_number"`
FirmwareRevision string `json:"firmware_revision"`
Uptime int64 `json:"uptime"`
RSSI float64 `json:"rssi"`
Timestamp uint64 `json:"timestamp"`
ResetFlags string `json:"reset_flags"`
Seq int64 `json:"seq"`
RadioStats []int64 `json:"radio_stats"`
// the 'fs' and 'mqtt_stats' fields are for internal use only and is not decoded.
}
type DeviceStatus struct {
TempestMessage
SerialNumber string `json:"serial_number"`
Timestamp int64 `json:"timestamp"`
Uptime int64 `json:"uptime"`
Voltage float64 `json:"voltage"`
FirmwareRevision int32 `json:"firmware_revision"`
RSSI float64 `json:"rssi"`
HubRSSI float64 `json:"hub_rssi"`
SensorStatus int32 `json:"sensor_status"`
}
type LightningStrikeEvent struct {
TempestMessage
Evt []float64 `json:"evt"`
}
func (l *LightningStrikeEvent) GetTime() *time.Time {
if l == nil || len(l.Evt) < 1 {
return nil
}
fv := l.Evt[0]
u := int64(fv)
t := time.Unix(u, 0)
return &t
}
func (l *LightningStrikeEvent) GetDistanceKm() *float64 {
if l == nil || len(l.Evt) < 2 {
return nil
}
return &l.Evt[1]
}
func (l *LightningStrikeEvent) GetStrikeEnergy() *float64 {
if l == nil || len(l.Evt) < 3 {
return nil
}
return &l.Evt[2]
}
/*
* The station observation struct is a header plus an array of arrays of numbers. Each
* one of these reports can contain multiple observations (though I have never seen it
* actually happen). If thought of as a table, the rows are individual observations
* and the columns are individual fields like wind speed, temperature, etc.
*
* Users of this object can pull the individual rows apart using the helper functions
* below.
*/
type StationObservation struct {
TempestMessage
SerialNumber string `json:"serial_number"`
Observations [][]float64 `json:"obs"`
}
func (o *StationObservation) GetField(obs int, n int) (float64, error) {
if len(o.Observations[obs]) < n {
return 0, errors.New("No such field")
}
return o.Observations[obs][n], nil
}
func (o *StationObservation) NumObservations() int {
return len(o.Observations)
}
func (o *StationObservation) Time(obs int) (*time.Time, error) {
fv, err := o.GetField(obs, 0)
if err != nil {
return nil, err
}
u := int64(fv)
t := time.Unix(u, 0)
return &t, nil
}
func (o *StationObservation) WindLull(obs int) (float64, error) {
return o.GetField(obs, 1)
}
func (o *StationObservation) WindAvg(obs int) (float64, error) {
return o.GetField(obs, 2)
}
func (o *StationObservation) WindGust(obs int) (float64, error) {
return o.GetField(obs, 3)
}
func (o *StationObservation) WindDir(obs int) (float64, error) {
return o.GetField(obs, 4)
}
func (o *StationObservation) WindSampleInterval(obs int) (float64, error) {
return o.GetField(obs, 5)
}
func (o *StationObservation) AirTemp(obs int) (float64, error) {
return o.GetField(obs, 7)
}
func (o *StationObservation) StationPressure(obs int) (float64, error) {
return o.GetField(obs, 6)
}
func (o *StationObservation) RelativeHumidity(obs int) (float64, error) {
return o.GetField(obs, 8)
}
func (o *StationObservation) Illuminance(obs int) (float64, error) {
return o.GetField(obs, 9)
}
func (o *StationObservation) UV(obs int) (float64, error) {
return o.GetField(obs, 10)
}
func (o *StationObservation) SolarRadiation(obs int) (float64, error) {
return o.GetField(obs, 11)
}
func (o *StationObservation) RainPreviousMinute(obs int) (float64, error) {
return o.GetField(obs, 12)
}
func (o *StationObservation) PrecipitationType(obs int) (float64, error) {
return o.GetField(obs, 13)
}
func (o *StationObservation) LightningStrikeAverageDistance(obs int) (float64, error) {
return o.GetField(obs, 14)
}
func (o *StationObservation) LightningStrikeCount(obs int) (float64, error) {
return o.GetField(obs, 15)
}
func (o *StationObservation) Battery(obs int) (float64, error) {
return o.GetField(obs, 16)
}
func (o *StationObservation) ReportInterval(obs int) (float64, error) {
return o.GetField(obs, 17)
}