-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
108 lines (89 loc) · 2.66 KB
/
main.go
File metadata and controls
108 lines (89 loc) · 2.66 KB
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
package main
import (
"context"
"log/slog"
"os"
"os/signal"
"syscall"
"time"
"github.com/b4ckspace/ledboard-v2/application"
"github.com/b4ckspace/ledboard-v2/ledboard"
"github.com/b4ckspace/ledboard-v2/mqttclient"
"github.com/b4ckspace/ledboard-v2/utils"
"github.com/kelseyhightower/envconfig"
)
type Config struct {
Debug bool `envconfig:"DEBUG"`
Mode string `envconfig:"MODE" required:"true"`
LedBoardHost string `envconfig:"LEDBOARD_HOST" required:"true"`
TimeZome string `envconfig:"TZ" default:"Europe/Berlin"`
LedBoardPingIntervalSeconds int `envconfig:"LEDBOARD_PING_INTERVAL_SECONDS" default:"5"`
MqttHost string `envconfig:"MQTT_HOST" required:"true"`
}
func main() {
config := Config{}
err := envconfig.Process("", &config)
if err != nil {
slog.Error("unable to parse environment", "error", err)
os.Exit(1)
}
// Set up a default logger
logLevel := slog.LevelInfo
if config.Debug {
logLevel = slog.LevelDebug
}
handler := slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{Level: logLevel})
slog.SetDefault(slog.New(handler))
// Load timezone
location, err := time.LoadLocation(config.TimeZome)
if err != nil {
slog.Error("unable to load timezone", "error", err)
os.Exit(1)
}
// Initialize LED Board Client
ledBoardClient, err := ledboard.NewClient(config.LedBoardHost, 9520)
if err != nil {
slog.Error("failed to connect to ledboard", "error", err)
os.Exit(1)
}
// Initialize MQTT Client
mqttClient := mqttclient.NewClient()
err = mqttClient.Connect(config.MqttHost)
if err != nil {
slog.Error("failed to connect to mqtt broker", "error", err)
os.Exit(1)
}
defer mqttClient.Disconnect()
// Set up context for graceful shutdown
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
// Initialize PingProbe
pingProbe, err := utils.NewPingProbe(config.LedBoardHost, config.LedBoardPingIntervalSeconds)
if err != nil {
slog.Error("unable to start pingprobe", "error", err)
os.Exit(1)
}
// Listen for OS signals to gracefully shut down
sigChan := make(chan os.Signal, 1)
signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM)
go func() {
sig := <-sigChan
slog.Info("received signal, shutting down...", "signal", sig)
cancel()
}()
var app *application.Application
switch config.Mode {
case string(application.DefaultMode):
fallthrough
case string(application.LasercutterMode):
app = application.NewApplication(ledBoardClient, mqttClient, pingProbe, application.Mode(config.Mode), location)
default:
slog.Error("unknown configuration mode", "mode", config.Mode)
os.Exit(1)
}
err = app.Run(ctx)
if err != nil {
slog.Error("Unable to run", "error", err)
os.Exit(1)
}
}