rpicontrol/main.go

57 lines
1.2 KiB
Go
Raw Normal View History

2022-12-16 22:27:24 +00:00
package main
import (
"flag"
log "github.com/sirupsen/logrus"
"os"
"os/signal"
"rpiMqttControl/internal/Config"
"rpiMqttControl/internal/MqttService"
"rpiMqttControl/internal/PinControlService"
)
func main() {
flagConfig := flag.String("config", "/etc/rpicontrol/rpicontrol.conf", "path to config file")
flagLogLevel := flag.String("log", "warn", "set log level for console output")
flag.Parse()
if level, err := log.ParseLevel(*flagLogLevel); err != nil {
log.SetLevel(log.WarnLevel)
log.Warnf("could not set log level. %s", err.Error())
} else {
log.SetLevel(level)
}
config, errConfig := Config.NewConfigFromYamlFile(*flagConfig)
if errConfig != nil {
log.Fatal(errConfig)
os.Exit(1)
}
pinControl, err := PinControlService.NewPinControl(&config.PinControlConfig)
if err != nil {
log.Fatal(err)
os.Exit(1)
}
mqttService := MqttService.NewMqttService(config.MqttConfig, pinControl)
signalCh := make(chan os.Signal, 1)
signal.Notify(signalCh, os.Interrupt)
mqttService.Start()
pinControl.Start()
for {
select {
case <-signalCh:
log.Info("attempting to shutdown...")
pinControl.Stop()
mqttService.Stop()
os.Exit(0)
}
}
}