-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
517 lines (430 loc) · 10.4 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
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
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
package main
import (
"bufio"
"bytes"
"fmt"
"io"
"log"
"math"
"net"
"os"
"os/exec"
"strconv"
"strings"
"syscall"
"time"
"github.com/fsnotify/fsnotify"
"github.com/spf13/viper"
)
var GrometVersion string
var GitCommit string
const fsErrorCommandDefault = "/usr2/fs/bin/lgerr"
type fsErrorWriter struct {
command string
}
func (f fsErrorWriter) Write(p []byte) (n int, err error) {
cmd := exec.Command(f.command, "lg", "-1", string(p))
err = cmd.Run()
if err != nil {
return 0, err
}
return len(p), nil
}
type windstate struct {
t time.Time
speed float64
direction float64
}
func (ws *windstate) marshal() string {
if (ws.t == time.Time{}) {
return ",,"
}
w := bytes.Buffer{}
if math.IsNaN(ws.speed) {
w.WriteString(",")
} else {
fmt.Fprintf(&w, "%.1f,", ws.speed)
}
if math.IsNaN(ws.direction) {
w.WriteString(",")
} else {
fmt.Fprintf(&w, "%.1f,", ws.direction)
}
return w.String()
}
func openWindConn(addr string) <-chan windstate {
s := windstate{}
ws := make(chan windstate, 1)
go func() {
ConnLoop:
for {
conn, err := net.Dial("tcp", addr)
if err != nil {
log.Printf("error opening wind device connection: %s", err)
time.Sleep(10 * time.Second)
continue
}
for {
b := bufio.NewReader(conn)
resp, err := b.ReadString('\n')
resp = strings.TrimSpace(resp)
if resp == "Selected hunt group busy" {
conn.Close()
continue ConnLoop
}
if err == io.EOF {
conn.Close()
continue ConnLoop
}
if err != nil {
log.Printf("error reading from wind device: %s", err)
conn.Close()
continue ConnLoop
}
if len(resp) <= 1 {
log.Printf("error reading from wind device: device sent no data")
conn.Close()
continue ConnLoop
}
fields := strings.FieldsFunc(resp[1:], func(r rune) bool { return r == ',' })
if len(fields) < 4 {
log.Printf("error reading from wind: unexpected message %q", resp)
conn.Close()
continue ConnLoop
}
var was_err int
s.t = time.Now()
s.speed, err = strconv.ParseFloat(fields[3], 64)
if err != nil {
s.speed = math.NaN()
log.Printf("error decoding message wind device: wind speed given as %q", fields[3])
was_err = 1
log.Printf("wind message: %q", resp)
}
s.direction, err = strconv.ParseFloat(fields[1], 64)
if err != nil {
s.direction = math.NaN()
log.Printf("error decoding message wind device: wind direction given as %q", fields[1])
if was_err == 0 {
log.Printf("wind message: %q", resp)
}
}
ws <- s
}
}
}()
return ws
}
type metstate struct {
t time.Time
pressure float64
temperature float64
humidity float64
fanStatus bool
fanRate float64
}
func (ms *metstate) marshal() string {
if (ms.t == time.Time{}) {
return ",,,"
}
w := bytes.Buffer{}
if math.IsNaN(ms.temperature) {
w.WriteString(",")
} else {
fmt.Fprintf(&w, "%.1f,", ms.temperature)
}
if math.IsNaN(ms.pressure) {
w.WriteString(",")
} else {
// NOTE: met server output expected to be in HPa
fmt.Fprintf(&w, "%.1f,", 1e3*ms.pressure)
}
if math.IsNaN(ms.humidity) {
w.WriteString(",")
} else {
fmt.Fprintf(&w, "%.1f,", ms.humidity)
}
return w.String()
}
const (
MET4AFanStatusCommand = "*0100FS\r\n"
MET4AFanStatusResp = "*0001FS=%v"
MET4AFanRateCommand = "*0100FR\r\n"
MET4AFanRateResp = "*0001FR=%v"
MET4QueryCommand = "*0100P9\r\n"
)
func openMetConn(c *MetConfig, a AlertsConfig) <-chan metstate {
s := metstate{}
ms := make(chan metstate, 1)
go func() {
Conn:
for {
conn, err := net.Dial("tcp", c.Address)
if err != nil {
log.Printf("error opening MET device connection: %s", err)
time.Sleep(10 * time.Second)
continue
}
b := bufio.NewReader(conn)
for {
fmt.Fprintf(conn, MET4QueryCommand)
resp, err := b.ReadString('\n')
resp = strings.TrimSpace(resp)
if resp == "Selected hunt group busy" {
conn.Close()
continue Conn
}
if err == io.EOF {
conn.Close()
continue Conn
}
if err != nil {
log.Printf("error while reading from met device: %s", err)
conn.Close()
time.Sleep(10 * time.Second)
continue Conn
}
fields := strings.FieldsFunc(resp, func(r rune) bool { return r == ',' })
if len(fields) < 11 {
log.Printf("received bad response from met device %q", resp)
conn.Close()
time.Sleep(10 * time.Second)
continue Conn
}
s.t = time.Now()
s.pressure, err = strconv.ParseFloat(fields[2], 64)
if err != nil {
s.pressure = math.NaN()
}
s.temperature, err = strconv.ParseFloat(fields[6], 64)
if err != nil {
s.temperature = math.NaN()
}
s.humidity, err = strconv.ParseFloat(fields[10], 64)
if err != nil {
s.humidity = math.NaN()
}
if c.Type != "MET4A" {
ms <- s
continue
}
fmt.Fprintf(conn, MET4AFanStatusCommand)
resp, err = b.ReadString('\n')
resp = strings.TrimSpace(resp)
if resp == "Selected hunt group busy" {
conn.Close()
time.Sleep(2 * time.Second)
continue Conn
}
if err == io.EOF {
log.Printf("error while reading fan status from met device: unexpected end of input")
conn.Close()
continue Conn
}
if err != nil {
log.Printf("error while reading fan status from met device: %s", err)
conn.Close()
time.Sleep(10 * time.Second)
continue Conn
}
_, err = fmt.Sscanf(resp, MET4AFanStatusResp, &s.fanStatus)
if err != nil {
log.Printf("error parsing fan status from met device: %s in response %q", err, resp)
conn.Close()
time.Sleep(10 * time.Second)
continue Conn
}
fmt.Fprintf(conn, MET4AFanRateCommand)
resp, err = b.ReadString('\n')
resp = strings.TrimSpace(resp)
if resp == "Selected hunt group busy" {
conn.Close()
time.Sleep(2 * time.Second)
continue Conn
}
if err == io.EOF {
log.Printf("error while reading fan rate from met device: unexpected end of input")
conn.Close()
time.Sleep(10 * time.Second)
continue Conn
}
if err != nil {
log.Printf("error while reading fan rate from met device: %s", err)
conn.Close()
time.Sleep(10 * time.Second)
continue Conn
}
_, err = fmt.Sscanf(resp, MET4AFanRateResp, &s.fanRate)
if err != nil {
log.Printf("error parsing fan rate from met device: %s in response %q", err, resp)
conn.Close()
time.Sleep(10 * time.Second)
continue Conn
}
ms <- s
}
}
}()
return ms
}
type MetConfig struct {
Address string
Type string
}
type WindConfig struct {
Address string
}
type AlertsConfig struct {
Mask []string
Fs struct {
Enabled bool
Path string
}
Email struct {
Enabled bool
Addresses []string
}
}
type config struct {
ListenAddress string `mapstructure:"listen_address"`
Met *MetConfig
Wind *WindConfig
Alerts AlertsConfig
}
const metTimeout = 5 * time.Minute
const windTimeout = 60 * time.Second
func openListener(address string) chan net.Conn {
l, err := net.Listen("tcp", address)
if err != nil {
log.Fatal(err)
}
conns := make(chan net.Conn)
go func() {
defer l.Close()
for {
conn, err := l.Accept()
if err != nil {
log.Fatalf("unable to listen on addres %q: %s", address, err)
}
conns <- conn
}
}()
return conns
}
func Restart() {
err := syscall.Exec(os.Args[0], os.Args, os.Environ())
if err != nil {
log.Fatal(err)
}
}
func main() {
defer func() {
if err := recover(); err != nil {
fmt.Fprintln(os.Stderr, "version:", GrometVersion)
fmt.Fprintln(os.Stderr, "commit:", GitCommit)
panic(err)
}
}()
if len(os.Args) > 1 {
switch os.Args[1] {
case "version":
fmt.Println("version:", GrometVersion)
fmt.Println("commit:", GitCommit)
os.Exit(0)
default:
fmt.Fprintf(os.Stderr, "unknown command %q\n", os.Args[1])
os.Exit(1)
}
}
log.SetPrefix("gromet: ")
log.SetFlags(0)
log.Printf("starting...")
viper.SetConfigName("gromet")
viper.AddConfigPath("/etc/gromet/")
viper.AddConfigPath("$HOME/.gromet")
viper.AddConfigPath("/usr2/control")
viper.AddConfigPath(".")
viper.SetDefault("listen_address", "127.0.0.1:50001")
viper.SetDefault("alerts.fs.enabled", true)
viper.SetDefault("alerts.fs.path", fsErrorCommandDefault)
viper.SetDefault("alerts.mask", []string{})
viper.SetDefault("alerts.email.enabled", true)
viper.SetDefault("alerts.email.addresses", []string{"oper"})
viper.WatchConfig()
viper.OnConfigChange(func(in fsnotify.Event) {
log.Println("configuration changed, restarting...")
Restart()
})
log.Printf("loading configuration")
err := viper.ReadInConfig()
if err != nil {
log.Fatalf("error loading config file: %s \n", err)
}
var c config
err = viper.Unmarshal(&c)
if err != nil {
log.Fatalf("error parsing config file: %s \n", err)
}
log.Printf("listening on %s \n", c.ListenAddress)
conns := openListener(c.ListenAddress)
logOutputs := []io.Writer{os.Stderr}
if c.Alerts.Fs.Enabled {
logOutputs = append(logOutputs, fsErrorWriter{fsErrorCommandDefault})
}
log.SetOutput(io.MultiWriter(logOutputs...))
var metStates <-chan metstate
metTimer := new(time.Timer)
var metTimedOut bool
if c.Met != nil {
metStates = openMetConn(c.Met, c.Alerts)
metTimer = time.NewTimer(metTimeout)
}
var windStates <-chan windstate
windTimer := new(time.Timer)
var windTimedOut bool
if c.Wind != nil {
windStates = openWindConn(c.Wind.Address)
windTimer = time.NewTimer(windTimeout)
}
var met metstate
var wind windstate
for {
select {
case <-metTimer.C:
met = metstate{}
log.Println("reading from met device timed out")
metTimedOut = true
case met = <-metStates:
if metTimedOut {
log.Println("met device communication restored")
metTimedOut = false
} else {
if !metTimer.Stop() {
<-metTimer.C
}
}
metTimer.Reset(metTimeout)
case <-windTimer.C:
wind = windstate{}
log.Println("reading from wind device timed out")
windTimedOut = true
case wind = <-windStates:
windTimer.Reset(windTimeout)
if windTimedOut {
log.Println("wind device communication restored")
windTimedOut = false
} else {
if !windTimer.Stop() {
<-windTimer.C
}
}
windTimer.Reset(windTimeout)
case conn := <-conns:
w := bufio.NewWriter(conn)
w.WriteString(met.marshal())
w.WriteString(wind.marshal())
w.Flush()
conn.Close()
}
}
}