-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
121 lines (97 loc) · 2.99 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
package main
import (
"embed"
"time"
"github.com/archekb/lsx024b/internal/config"
"github.com/archekb/lsx024b/internal/device"
"github.com/archekb/lsx024b/internal/http"
"github.com/archekb/lsx024b/internal/log"
"github.com/archekb/lsx024b/internal/models"
"github.com/archekb/lsx024b/internal/mqtt"
"os"
"os/signal"
"syscall"
)
//go:embed web/*
var staticFS embed.FS
var version string
func init() {
if version != "" {
log.ProductionMode()
}
}
func main() {
log.Infof("Starting lsx024b v%s", version)
cnf, err := config.New("config.yml")
if err != nil {
log.Fatal("Configuration error: ", err)
}
// connect to Device
dev := device.NewLSB(cnf.Device.Port, cnf.Device.SlaveId)
if err := dev.Connect(); err != nil {
log.Fatalf("Can't connect to %s: %s", cnf.Device.Port, err)
}
log.Infof("Device via %s connected", cnf.Device.Port)
// connect to MQTT
var mqttClient *mqtt.MQTT
if cnf.MQTT.Address != "" {
mqttClient = mqtt.New(cnf.MQTT.Address, cnf.MQTT.User, cnf.MQTT.Password, cnf.Device.Name)
mqttClient.SetDefaultTopic(cnf.MQTT.Topic, cnf.Device.Name)
if cnf.MQTT.HomeAssistant {
mqttClient.HAInit(cnf.Device.Name, cnf.Device.Model, version)
mqttClient.SetOnConnect(func() { mqttClient.HAPublishDevice() })
}
if err := mqttClient.Connect(); err != nil {
log.Fatal("Can't connect to MQTT: ", err)
}
log.Info("Connected to MQTT server")
}
// read data from device
var lastRes *models.ResultLSB
stopReadController := make(chan struct{}, 1)
go func() {
ticker := time.Tick(time.Duration(cnf.Device.UpdateInterval) * time.Second)
for {
select {
case <-stopReadController:
return
case <-ticker:
dataSummary, err := dev.Summary()
if err != nil {
log.Error("Error reading data from controller: ", err)
lastRes = &models.ResultLSB{Updated: time.Now().UTC(), Model: cnf.Device.Model}
if mqttClient != nil && mqttClient.IsConnected() {
mqttClient.PublishToDefault(lastRes)
}
continue
}
lastRes = &models.ResultLSB{Connected: true, Updated: time.Now().UTC(), Interval: cnf.Device.UpdateInterval, Model: cnf.Device.Model, Device: dataSummary}
if mqttClient != nil && mqttClient.IsConnected() {
mqttClient.PublishToDefault(lastRes)
}
}
}
}()
// server HTTP
if cnf.HTTP.Address != "" {
log.Info("Starting HTTP Server")
srvHTTP := http.New(&staticFS, &http.Env{State: &lastRes, Release: version != ""})
if cnf.HTTP.Certificate != "" && cnf.HTTP.Key != "" {
srvHTTP.RunTLS(cnf.HTTP.Address, cnf.HTTP.Certificate, cnf.HTTP.Key)
} else {
srvHTTP.Run(cnf.HTTP.Address)
}
}
// garceful shutdown
quit := make(chan os.Signal, 1)
signal.Notify(quit, os.Interrupt, syscall.SIGINT, syscall.SIGTERM)
<-quit
log.Info("Shutting down server...")
stopReadController <- struct{}{}
if mqttClient != nil && mqttClient.IsConnected() {
mqttClient.PublishToDefault(&models.ResultLSB{Updated: time.Now().UTC(), Model: cnf.Device.Model})
mqttClient.Close()
}
dev.Close()
log.Info("Server exiting")
}