-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
137 lines (105 loc) · 2.78 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
package main
import (
"flag"
log "github.com/sirupsen/logrus"
"os"
"os/signal"
"sort"
"strings"
"syscall"
)
const (
BuildVersion = "0.0.1"
)
type Initializer func(container *Container)
var initializers = make(map[int][]Initializer, 0)
func RegisterInitializer(priority int, initializer Initializer) {
if _, ok := initializers[priority]; ok {
initializers[priority] = append(initializers[priority], initializer)
} else {
initializers[priority] = []Initializer{initializer}
}
}
func runInitializers(container *Container) {
priorities := make([]int, len(initializers))
i := 0
for priority := range initializers {
priorities[i] = priority
i++
}
sort.Ints(priorities)
for i := len(priorities) - 1; i >= 0; i-- {
for _, initializer := range initializers[priorities[i]] {
initializer(container)
}
}
}
func main() {
var dataDir string
flag.StringVar(&dataDir, "data-dir", "/data/cec2mqtt/", "Sets the directory where the data, including config, files are stored")
var logLevel string
flag.StringVar(&logLevel, "log-level", "info", "Sets the log level. Options are panic, fatal, error, warning, info, debug, trace")
var logCecMessages bool
flag.BoolVar(&logCecMessages, "log-cec-messages", false, "Enables logging of the libcec log")
flag.Parse()
switch logLevel {
case "panic":
log.SetLevel(log.PanicLevel)
case "fatal":
log.SetLevel(log.FatalLevel)
case "error":
log.SetLevel(log.ErrorLevel)
case "warning":
log.SetLevel(log.WarnLevel)
case "info":
log.SetLevel(log.InfoLevel)
case "debug":
log.SetLevel(log.DebugLevel)
case "trace":
log.SetLevel(log.TraceLevel)
default:
log.SetLevel(log.InfoLevel)
}
log.WithField("version", BuildVersion).Info("Starting Cec2Mqtt")
dataDir = strings.TrimRight(dataDir, "/") + "/"
container := NewContainer()
config, err := ParseConfig(dataDir)
if nil != err {
log.WithFields(log.Fields{
"error": err,
}).Fatal("Error reading configuration")
}
container.Register("config", config)
devices := NewDeviceRegistry(dataDir)
container.Register("devices", devices)
mqtt, err := ConnectMqtt(config)
if nil != err {
log.WithFields(log.Fields{
"config": config.Mqtt,
"error": err,
}).Fatal("Failed to connect to MQTT broker")
}
container.Register("mqtt", mqtt)
cec, err := InitialiseCec(devices, "")
cec.LibCecLoggingEnabled = logCecMessages
if nil != err {
log.WithFields(log.Fields{
"error": err,
}).Fatal("Failed to setup CEC connection")
}
container.Register("cec", cec)
runInitializers(container)
cec.Start()
signals := make(chan os.Signal, 1)
done := make(chan bool, 1)
signal.Notify(signals, syscall.SIGINT, syscall.SIGTERM)
go func() {
<-signals
done <- true
}()
log.Info("Cec2Mqtt started")
<-done
log.Info("Exiting")
config.Save(dataDir)
devices.Save(dataDir)
}