-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmodels.go
210 lines (180 loc) · 6.42 KB
/
models.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
package weatherflow
import (
"encoding/json"
"fmt"
)
type Message interface {
GetType() string
GetDeviceID() (int, bool)
}
type MessageObsSt struct {
Status ObsStStatus `json:"status"`
DeviceID int `json:"device_id"`
Type string `json:"type"`
Source string `json:"source"`
Summary ObsStSummary `json:"summary"`
Obs []ObsStData `json:"obs"`
}
type MessageRapidWind struct {
DeviceID int `json:"device_id"`
SerialNumber string `json:"serial_number"`
Type string `json:"type"`
HubSN string `json:"hub_sn"`
Ob RapidWindData `json:"ob"`
}
type MessageConnectionOpened struct {
Type string `json:"type"`
}
type MessageAck struct {
ID string `json:"id"`
Type string `json:"type"`
}
type ObsStStatus struct {
StatusCode int `json:"status_code"`
StatusMessage string `json:"status_message"`
}
type ObsStSummary struct {
PressureTrend string `json:"pressure_trend"`
StrikeCount1h int `json:"strike_count_1h"`
StrikeCount3h int `json:"strike_count_3h"`
PrecipTotal1h float64 `json:"precip_total_1h"`
StrikeLastDist int `json:"strike_last_dist"`
StrikeLastEpoch int `json:"strike_last_epoch"`
PrecipAccumLocalYesterday float64 `json:"precip_accum_local_yesterday"`
PrecipAccumLocalYesterdayFinal float64 `json:"precip_accum_local_yesterday_final"`
PrecipAnalysisTypeYesterday int `json:"precip_analysis_type_yesterday"`
RainingMinutes []int `json:"raining_minutes"`
PrecipMinutesLocalDay int `json:"precip_minutes_local_day"`
PrecipMinutesLocalYesterday int `json:"precip_minutes_local_yesterday"`
}
type ObsStData struct {
TimeEpoch int `json:"time_epoch"`
WindLull float64 `json:"wind_lull"`
WindAvg float64 `json:"wind_avg"`
WindGust float64 `json:"wind_gust"`
WindDirection int `json:"wind_direction"`
WindSampleInterval int `json:"wind_sample_interval"`
StationPressure float64 `json:"station_pressure"`
AirTemperature *float64 `json:"air_temperature"`
RelativeHumidity *float64 `json:"relative_humidity"`
Illuminance int `json:"illuminance"`
UV int `json:"uv"`
SolarRadiation int `json:"solar_radiation"`
RainAccumulated float64 `json:"rain_accumulated"`
PrecipitationType int `json:"precipitation_type"`
LightningStrikeAvgDistance int `json:"lightning_strike_avg_distance"`
LightningStrikeCount int `json:"lightning_strike_count"`
Battery float64 `json:"battery"`
ReportInterval int `json:"report_interval"`
LocalDailyRainAccumulation float64 `json:"local_daily_rain_accumulation"`
RainAccumulatedFinal float64 `json:"rain_accumulated_final"`
LocalDailyRainAccumulationFinal float64 `json:"local_daily_rain_accumulation_final"`
PrecipitationAnalysisType int `json:"precipitation_analysis_type"`
}
type RapidWindData struct {
TimeEpoch int `json:"time_epoch"`
WindSpeed float64 `json:"wind_speed"`
WindDirection int `json:"wind_direction"`
}
func (obs *ObsStData) UnmarshalJSON(data []byte) error {
var obsArray []interface{}
err := json.Unmarshal(data, &obsArray)
if err != nil {
return err
}
obs.TimeEpoch = int(obsArray[0].(float64))
obs.WindLull = obsArray[1].(float64)
obs.WindAvg = obsArray[2].(float64)
obs.WindGust = obsArray[3].(float64)
obs.WindDirection = int(obsArray[4].(float64))
obs.WindSampleInterval = int(obsArray[5].(float64))
obs.StationPressure = obsArray[6].(float64)
if obsArray[7] != nil {
airTemp := obsArray[7].(float64)
obs.AirTemperature = &airTemp
}
if obsArray[8] != nil {
relHumidity := obsArray[8].(float64)
obs.RelativeHumidity = &relHumidity
}
obs.Illuminance = int(obsArray[9].(float64))
obs.UV = int(obsArray[10].(float64))
obs.SolarRadiation = int(obsArray[11].(float64))
obs.RainAccumulated = obsArray[12].(float64)
obs.PrecipitationType = int(obsArray[13].(float64))
obs.LightningStrikeAvgDistance = int(obsArray[14].(float64))
obs.LightningStrikeCount = int(obsArray[15].(float64))
obs.Battery = obsArray[16].(float64)
obs.ReportInterval = int(obsArray[17].(float64))
obs.LocalDailyRainAccumulation = obsArray[18].(float64)
obs.RainAccumulatedFinal = obsArray[19].(float64)
obs.LocalDailyRainAccumulationFinal = obsArray[20].(float64)
obs.PrecipitationAnalysisType = int(obsArray[21].(float64))
return nil
}
func (rw *RapidWindData) UnmarshalJSON(data []byte) error {
var rwArray []interface{}
err := json.Unmarshal(data, &rwArray)
if err != nil {
return err
}
rw.TimeEpoch = int(rwArray[0].(float64))
rw.WindSpeed = rwArray[1].(float64)
rw.WindDirection = int(rwArray[2].(float64))
return nil
}
func (w *MessageObsSt) GetType() string {
return w.Type
}
func (w *MessageRapidWind) GetType() string {
return w.Type
}
func (w *MessageConnectionOpened) GetType() string {
return w.Type
}
func (w *MessageAck) GetType() string {
return w.Type
}
func (w *MessageObsSt) GetDeviceID() (int, bool) {
return w.DeviceID, true
}
func (w *MessageRapidWind) GetDeviceID() (int, bool) {
return w.DeviceID, true
}
func (w *MessageConnectionOpened) GetDeviceID() (int, bool) {
return -1, false
}
func (w *MessageAck) GetDeviceID() (int, bool) {
return -1, false
}
func UnmarshalMessage(data []byte) (Message, error) {
var rawMessage map[string]interface{}
err := json.Unmarshal(data, &rawMessage)
if err != nil {
return nil, err
}
messageType, ok := rawMessage["type"].(string)
if !ok {
return nil, fmt.Errorf("missing 'type' field in message")
}
switch messageType {
case "obs_st":
var message MessageObsSt
err := json.Unmarshal(data, &message)
return &message, err
case "rapid_wind":
var message MessageRapidWind
err := json.Unmarshal(data, &message)
return &message, err
case "connection_opened":
var message MessageConnectionOpened
err := json.Unmarshal(data, &message)
return &message, err
case "ack":
var message MessageAck
err := json.Unmarshal(data, &message)
return &message, err
default:
return nil, fmt.Errorf("unsupported message type: %s", messageType)
}
}