-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
72 lines (58 loc) · 1.47 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
package main
import (
"flag"
MQTT "github.com/eclipse/paho.mqtt.golang"
log "github.com/sirupsen/logrus"
"os"
)
var cfg config
//define a function for the default message handler
var f MQTT.MessageHandler = func(client MQTT.Client, msg MQTT.Message) {
log.Warn("TOPIC: %s\n", msg.Topic())
log.Warn("MSG: %s\n", msg.Payload())
}
func main() {
var loglevel = flag.String("loglevel", "info", "log level [trace,debug,info,warn,error,fatal,panic]")
var configFile = flag.String("config", "config.yaml", "config file")
flag.Parse()
// configure log formatter
setLogLevel(*loglevel)
log.SetFormatter(&formatter{})
err := parseYaml(*configFile, &cfg)
if err != nil {
log.Error(err.Error())
os.Exit(-1)
}
opts := MQTT.NewClientOptions().AddBroker(cfg.MqttServer)
opts.SetClientID(cfg.Name)
opts.SetDefaultPublishHandler(f)
//create and start a client using the above ClientOptions
c := MQTT.NewClient(opts)
if token := c.Connect(); token.Wait() && token.Error() != nil {
panic(token.Error())
}
// allocate coulometer
cm := Coulombmeter{
port: cfg.SerialPort,
}
// register sensors to Home Assistant
if cfg.HaRegister {
cm.register(c)
}
// init serial port
err = cm.init()
if err != nil {
log.Errorln(err)
os.Exit(-1)
}
// allocate channel to store frame
ch := make(chan uint8, 64)
// start frame parser goroutine
go cm.parseFrame(ch, c)
// infinite loop reading on serial port
err = cm.poll(ch)
if err != nil {
log.Errorln(err)
os.Exit(-2)
}
}