This repository has been archived by the owner on Aug 25, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
352 lines (333 loc) · 9.57 KB
/
main.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
package main
import (
"bytes"
"encoding/json"
"github.com/spf13/viper"
"log"
"net/http"
)
type RainwiseWeather struct {
DeviceTime string `json:"time"`
BatteryLevel float64 `json:"batt"`
Signal int `json:"signal"`
SignalQuality int `json:"quality"`
US struct {
AirTemperature struct {
Current float64 `json:"tic"`
Average float64 `json:"tia"`
TodayHigh float64 `json:"tdh"`
TodayIntervalHigh float64 `json:"tih"`
TodayLow float64 `json:"tdl"`
TodayIntervalLow float64 `json:"til"`
} `json:"atmp"`
RelativeHumidity struct {
Current int `json:"ric"`
Average int `json:"ria"`
TodayHigh int `json:"rdh"`
TodayIntervalHigh int `json:"rih"`
TodayLow int `json:"rdl"`
TodayIntervalLow int `json:"ril"`
} `json:"rh"`
BarometricPressure struct {
Current float64 `json:"bic"`
Average float64 `json:"bia"`
TodayHigh float64 `json:"bdh"`
TodayIntervalHigh float64 `json:"bih"`
TodayLow float64 `json:"bdl"`
TodayIntervalLow float64 `json:"bil"`
} `json:"bp"`
Wind struct {
Current float64 `json:"wic"`
Wict int `json:"wict"`
Average float64 `json:"wia"`
TodayHigh float64 `json:"wdh"`
Wdht int `json:"wdht"`
Wih float64 `json:"wih"`
Wiht int `json:"wiht"`
} `json:"wnd"`
Rainfall struct {
Today float64 `json:"rfd"`
Minute float64 `json:"rfm"`
} `json:"rf"`
/*
SolarRad struct {
Src int `json:"src"`
Srd int `json:"srd"`
Srm int `json:"srm"`
} `json:"sr"`
SolarRad2 struct {
Sr2C int `json:"sr2c"`
Sr2D int `json:"sr2d"`
Sr2M int `json:"sr2m"`
} `json:"sr2"`
UVIndex struct {
Uvc int `json:"uvc"`
Uvd int `json:"uvd"`
Uvm int `json:"uvm"`
} `json:"uv"`
LeafWetness struct {
Lwc int `json:"lwc"`
Lwd int `json:"lwd"`
Lwm int `json:"lwm"`
} `json:"lw"`
Temperature1 struct {
T1Ic float64 `json:"t1ic"`
T1Ia float64 `json:"t1ia"`
T1Dh float64 `json:"t1dh"`
T1Ih float64 `json:"t1ih"`
T1Dl float64 `json:"t1dl"`
T1Il float64 `json:"t1il"`
} `json:"tmp1"`
Temperature2 struct {
T2Ic float64 `json:"t2ic"`
T2Ia float64 `json:"t2ia"`
T2Dh float64 `json:"t2dh"`
T2Ih float64 `json:"t2ih"`
T2Dl float64 `json:"t2dl"`
T2Il float64 `json:"t2il"`
} `json:"tmp2"`
SoilMoisture struct {
Sic int `json:"sic"`
Sia int `json:"sia"`
Sdh int `json:"sdh"`
Sih int `json:"sih"`
Sdl int `json:"sdl"`
Sil int `json:"sil"`
} `json:"sm"`
InsideTemperature struct {
Itic float64 `json:"itic"`
Itia float64 `json:"itia"`
Itdh float64 `json:"itdh"`
Itih float64 `json:"itih"`
Itdl float64 `json:"itdl"`
Itil float64 `json:"itil"`
} `json:"itmp"`
*/
} `json:"us"`
/*
Metric struct {
Atmp struct {
Tic float64 `json:"tic"`
Tia float64 `json:"tia"`
Tdh float64 `json:"tdh"`
Tih float64 `json:"tih"`
Tdl float64 `json:"tdl"`
Til float64 `json:"til"`
} `json:"atmp"`
Rh struct {
Ric int `json:"ric"`
Ria int `json:"ria"`
Rdh int `json:"rdh"`
Rih int `json:"rih"`
Rdl int `json:"rdl"`
Ril int `json:"ril"`
} `json:"rh"`
Bp struct {
Bic float64 `json:"bic"`
Bia float64 `json:"bia"`
Bdh float64 `json:"bdh"`
Bih float64 `json:"bih"`
Bdl float64 `json:"bdl"`
Bil float64 `json:"bil"`
} `json:"bp"`
Wnd struct {
Wic float64 `json:"wic"`
Wict int `json:"wict"`
Wia float64 `json:"wia"`
Wdh float64 `json:"wdh"`
Wdht int `json:"wdht"`
Wih float64 `json:"wih"`
Wiht int `json:"wiht"`
} `json:"wnd"`
Rf struct {
Rfd float64 `json:"rfd"`
Rfm float64 `json:"rfm"`
} `json:"rf"`
Sr struct {
Src int `json:"src"`
Srd int `json:"srd"`
Srm int `json:"srm"`
} `json:"sr"`
Sr2 struct {
Sr2C int `json:"sr2c"`
Sr2D int `json:"sr2d"`
Sr2M int `json:"sr2m"`
} `json:"sr2"`
Uv struct {
Uvc int `json:"uvc"`
Uvd int `json:"uvd"`
Uvm int `json:"uvm"`
} `json:"uv"`
Lw struct {
Lwc int `json:"lwc"`
Lwd int `json:"lwd"`
Lwm int `json:"lwm"`
} `json:"lw"`
Tmp1 struct {
T1Ic float64 `json:"t1ic"`
T1Ia float64 `json:"t1ia"`
T1Dh float64 `json:"t1dh"`
T1Ih float64 `json:"t1ih"`
T1Dl float64 `json:"t1dl"`
T1Il float64 `json:"t1il"`
} `json:"tmp1"`
Tmp2 struct {
T2Ic float64 `json:"t2ic"`
T2Ia float64 `json:"t2ia"`
T2Dh float64 `json:"t2dh"`
T2Ih float64 `json:"t2ih"`
T2Dl float64 `json:"t2dl"`
T2Il float64 `json:"t2il"`
} `json:"tmp2"`
Sm struct {
Sic int `json:"sic"`
Sia int `json:"sia"`
Sdh int `json:"sdh"`
Sih int `json:"sih"`
Sdl int `json:"sdl"`
Sil int `json:"sil"`
} `json:"sm"`
Itmp struct {
Itic float64 `json:"itic"`
Itia float64 `json:"itia"`
Itdh float64 `json:"itdh"`
Itih float64 `json:"itih"`
Itdl float64 `json:"itdl"`
Itil float64 `json:"itil"`
} `json:"itmp"`
} `json:"metric"`
*/
}
type Weather struct {
DeviceTime string `json:"time"`
BatteryLevel float64 `json:"batt"`
Signal int `json:"signal"`
SignalQuality int `json:"quality"`
US struct {
AirTemperature struct {
Current float64 `json:"tic"`
Average float64 `json:"tia"`
TodayHigh float64 `json:"tdh"`
TodayIntervalHigh float64 `json:"tih"`
TodayLow float64 `json:"tdl"`
TodayIntervalLow float64 `json:"til"`
} `json:"atmp"`
RelativeHumidity struct {
Current int `json:"ric"`
Average int `json:"ria"`
TodayHigh int `json:"rdh"`
TodayIntervalHigh int `json:"rih"`
TodayLow int `json:"rdl"`
TodayIntervalLow int `json:"ril"`
} `json:"rh"`
BarometricPressure struct {
Current float64 `json:"bic"`
Average float64 `json:"bia"`
TodayHigh float64 `json:"bdh"`
TodayIntervalHigh float64 `json:"bih"`
TodayLow float64 `json:"bdl"`
TodayIntervalLow float64 `json:"bil"`
} `json:"bp"`
Wind struct {
Current float64 `json:"wic"`
Wict int `json:"wict"`
Average float64 `json:"wia"`
TodayHigh float64 `json:"wdh"`
Wdht int `json:"wdht"`
Wih float64 `json:"wih"`
Wiht int `json:"wiht"`
} `json:"wnd"`
Rainfall struct {
Today float64 `json:"rfd"`
Minute float64 `json:"rfm"`
} `json:"rf"`
} `json:"us"`
}
func (w *Weather) MarshalJSON() ([]byte, error) {
type WeatherAlias struct {
DeviceTime string `json:"deviceTime"`
BatteryLevel float64 `json:"batteryLevel"`
Signal int `json:"signal"`
SignalQuality int `json:"signalQuality"`
US struct {
AirTemperature struct {
Current float64 `json:"current"`
Average float64 `json:"average"`
TodayHigh float64 `json:"todayHigh"`
TodayIntervalHigh float64 `json:"todayIntervalHigh"`
TodayLow float64 `json:"todayLow"`
TodayIntervalLow float64 `json:"todayIntervalLow"`
} `json:"airTemperature"`
RelativeHumidity struct {
Current int `json:"current"`
Average int `json:"average"`
TodayHigh int `json:"todayHigh"`
TodayIntervalHigh int `json:"todayIntervalHigh"`
TodayLow int `json:"todayLow"`
TodayIntervalLow int `json:"todayIntervalLow"`
} `json:"relativeHumidity"`
BarometricPressure struct {
Current float64 `json:"current"`
Average float64 `json:"average"`
TodayHigh float64 `json:"todayHigh"`
TodayIntervalHigh float64 `json:"todayIntervalHigh"`
TodayLow float64 `json:"todayLow"`
TodayIntervalLow float64 `json:"todayIntervalLow"`
} `json:"barometricPressure"`
Wind struct {
Current float64 `json:"current"`
Wict int `json:"currentDirection"`
Average float64 `json:"average"`
TodayHigh float64 `json:"todayHigh"`
Wdht int `json:"todayHighDirection"`
Wih float64 `json:"intervalHigh"`
Wiht int `json:"intervalHighDirection"`
} `json:"wind"`
Rainfall struct {
Today float64 `json:"today"`
Minute float64 `json:"interval"`
} `json:"rainfall"`
} `json:"US"`
}
var a WeatherAlias = WeatherAlias(*w)
return json.Marshal(&a)
}
func main() {
//set up logging
log.SetFlags(log.LstdFlags)
log.SetPrefix("[WeatherGrabber] ")
viper.SetConfigName("weathergrabber-config")
viper.SetConfigType("yaml")
viper.AddConfigPath(".")
err := viper.ReadInConfig() // Find and read the config file
if err != nil { // Handle errors reading the config file
log.Fatalf("Fatal error config file: %s \n", err)
}
ip100_ip := viper.GetString("ip100ip")
ip100_url := "http://" + ip100_ip + "/weather.json"
log.Println("Grabbing weather ")
rsp, err := http.Get(ip100_url)
defer rsp.Body.Close()
if err != nil {
log.Fatal("NewRequest: ", err)
return
}
// Fill the record with the data from the JSON
var weather Weather
// Use json.Decode for reading streams of JSON data
if err := json.NewDecoder(rsp.Body).Decode(&weather); err != nil {
log.Println(err)
}
log.Println("Pushing weather to webhook ")
jsonStr, err := json.Marshal(&weather)
url := viper.GetString("webhook")
req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonStr))
req.Header.Set("api_key", viper.GetString("api_key"))
req.Header.Set("Content-Type", "application/json")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
panic(err)
}
defer resp.Body.Close()
}