-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
114 lines (96 loc) · 2.94 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
package main
import (
"context"
"flag"
"os"
"os/signal"
"strings"
"github.com/aalu1418/rpi-terminal-hub/rpio/infrared"
"github.com/aalu1418/rpi-terminal-hub/services"
"github.com/aalu1418/rpi-terminal-hub/services/alerts"
"github.com/aalu1418/rpi-terminal-hub/services/connectivity"
"github.com/aalu1418/rpi-terminal-hub/services/metrics"
"github.com/aalu1418/rpi-terminal-hub/services/server"
"github.com/aalu1418/rpi-terminal-hub/services/vacuum"
"github.com/aalu1418/rpi-terminal-hub/services/weather"
"github.com/aalu1418/rpi-terminal-hub/types"
log "github.com/sirupsen/logrus"
"github.com/stianeikeland/go-rpio/v4"
)
var (
OWMKey string
IRRecorder bool
IREmitTest bool
)
func init() {
flag.StringVar(&OWMKey, "owm", os.Getenv(types.OWM_ENVVAR), "pass in openweathermap api key")
flag.BoolVar(&IRRecorder, "record-ir", false, "run IR recorder")
flag.BoolVar(&IREmitTest, "emit-ir", false, "run IR emit test")
flag.Parse()
}
func main() {
if IRRecorder {
if err := infrared.NewRecorder(rpio.Pin(types.IR_RECEIVER)); err != nil {
log.Fatalf("[IR] recorder error: %s", err)
}
return
}
if IREmitTest {
if err := infrared.NewEmitter(rpio.Pin(types.IR_EMITTER), vacuum.COMMAND_STARTSTOP); err != nil {
log.Fatalf("[IR] emit test error: %s", err)
}
return
}
if OWMKey == "" {
log.Fatalf("missing openweathermap api key - pass via %s env or --owm flag", types.OWM_ENVVAR)
}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
system := make(chan os.Signal, 1)
signal.Notify(system, os.Interrupt)
// start message queue
messages := types.NewQueue()
defer close(messages)
// start up services
clients := []types.Service{
server.New(messages),
metrics.New(messages),
connectivity.New(messages),
weather.New(messages, OWMKey),
alerts.NewNWS(messages),
vacuum.New(messages),
}
// start up post office for message sorting
postOffice := services.NewPostOffice(messages, clients)
// start postOffice first
startStop := []types.BaseService{postOffice}
for i := range clients {
startStop = append(startStop, clients[i])
}
// start up all services
for i := range startStop {
startCtx, cancel := context.WithTimeout(ctx, types.DEFAULT_TIMEOUT)
if err := startStop[i].Start(startCtx); err != nil {
log.Panicf("service (%s) failed to start: %s", startStop[i].Name(), err)
}
cancel()
}
var names []string
for i := range clients {
names = append(names, clients[i].Name())
}
log.Infof("all services started: %s", strings.Join(names, ","))
// wait for exit system to interrupt
<-system
log.Info("shutting down services")
// stop all clients
// stop post office first to avoid sending on closed channel
for i := range startStop {
stopCtx, cancel := context.WithTimeout(ctx, types.DEFAULT_TIMEOUT)
if err := startStop[i].Stop(stopCtx); err != nil {
log.Warnf("service (%s) failed to stop properly: %s", startStop[i].Name(), err)
}
cancel()
}
log.Info("all services shut down")
}