72 lines
1.4 KiB
Go
72 lines
1.4 KiB
Go
package main
|
|
|
|
import (
|
|
"flag"
|
|
log "github.com/sirupsen/logrus"
|
|
"os"
|
|
"os/signal"
|
|
"rpiMqttControl/internal/Config"
|
|
"rpiMqttControl/internal/MqttService"
|
|
"rpiMqttControl/internal/PinControlService"
|
|
)
|
|
|
|
var (
|
|
Version string = "0.0.0"
|
|
GitCommit string = "000000"
|
|
)
|
|
|
|
func main() {
|
|
versionStr := Version + "-" + GitCommit
|
|
flagConfig := flag.String("config", "/etc/rpicontrol/rpicontrol.conf", "path to config file")
|
|
flagLogLevel := flag.String("log", "info", "set log level for console output")
|
|
flagVersion := flag.Bool("version", false, "output version and exit")
|
|
|
|
flag.Parse()
|
|
|
|
if *flagVersion {
|
|
print(versionStr)
|
|
os.Exit(0)
|
|
}
|
|
|
|
log.Infof("starting rpicontrol Version %s", versionStr)
|
|
|
|
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)
|
|
}
|
|
}
|
|
|
|
}
|