robocar-led/cmd/rc-led/rc-led.go

73 lines
2.2 KiB
Go
Raw Permalink Normal View History

2019-12-14 10:56:22 +00:00
package main
import (
"flag"
"github.com/cyrilix/robocar-base/cli"
"github.com/cyrilix/robocar-led/pkg/part"
2021-10-12 17:00:46 +00:00
"go.uber.org/zap"
2019-12-14 10:56:22 +00:00
"log"
"os"
)
const (
DefaultClientId = "robocar-led"
)
func main() {
var mqttBroker, username, password, clientId string
2023-05-10 17:52:44 +00:00
var driveModeTopic, recordTopic, speedZoneTopic, throttleTopic string
var enableSpeedZoneMode bool
2019-12-14 10:56:22 +00:00
2019-12-17 23:28:15 +00:00
mqttQos := cli.InitIntFlag("MQTT_QOS", 0)
2019-12-14 10:56:22 +00:00
_, mqttRetain := os.LookupEnv("MQTT_RETAIN")
2019-12-17 23:28:15 +00:00
cli.InitMqttFlags(DefaultClientId, &mqttBroker, &username, &password, &clientId, &mqttQos, &mqttRetain)
2019-12-14 10:56:22 +00:00
flag.StringVar(&driveModeTopic, "mqtt-topic-drive-mode", os.Getenv("MQTT_TOPIC_DRIVE_MODE"), "Mqtt topic that contains DriveMode value, use MQTT_TOPIC_DRIVE_MODE if args not set")
flag.StringVar(&recordTopic, "mqtt-topic-record", os.Getenv("MQTT_TOPIC_RECORD"), "Mqtt topic that contains video recording state, use MQTT_TOPIC_RECORD if args not set")
2023-03-09 18:42:47 +00:00
flag.StringVar(&speedZoneTopic, "mqtt-topic-speed-zone", os.Getenv("MQTT_TOPIC_SPEED_ZONE"), "Mqtt topic that contains speed zone, use MQTT_TOPIC_SPEED_ZONE if args not set")
2023-05-10 17:52:44 +00:00
flag.StringVar(&throttleTopic, "mqtt-topic-throttle", os.Getenv("MQTT_TOPIC_THROTTLE"), "Mqtt topic that contains throttle, use MQTT_TOPIC_THROTTLE if args not set")
flag.BoolVar(&enableSpeedZoneMode, "enable-speedzone-mode", false, "Enable speed-zone mode")
2019-12-14 10:56:22 +00:00
2021-12-29 17:21:13 +00:00
logLevel := zap.LevelFlag("log", zap.InfoLevel, "log level")
2019-12-14 10:56:22 +00:00
flag.Parse()
2021-12-29 17:21:13 +00:00
2019-12-14 10:56:22 +00:00
if len(os.Args) <= 1 {
flag.PrintDefaults()
os.Exit(1)
}
2021-10-12 17:00:46 +00:00
config := zap.NewDevelopmentConfig()
2021-12-29 17:21:13 +00:00
config.Level = zap.NewAtomicLevelAt(*logLevel)
2021-10-12 17:00:46 +00:00
lgr, err := config.Build()
if err != nil {
log.Fatalf("unable to init logger: %v", err)
}
defer func() {
if err := lgr.Sync(); err != nil {
log.Printf("unable to Sync logger: %v\n", err)
}
}()
zap.ReplaceGlobals(lgr)
2019-12-17 23:28:15 +00:00
client, err := cli.Connect(mqttBroker, username, password, clientId)
2019-12-14 10:56:22 +00:00
if err != nil {
2021-10-12 17:00:46 +00:00
zap.S().Fatalf("unable to connect to mqtt bus: %v", err)
2019-12-14 10:56:22 +00:00
}
defer client.Disconnect(50)
2023-05-10 17:52:44 +00:00
mode := part.LedModeBrake
if enableSpeedZoneMode {
mode = part.LedModeSpeedZone
}
p := part.NewPart(client, driveModeTopic, recordTopic, speedZoneTopic, throttleTopic, mode)
2019-12-14 10:56:22 +00:00
defer p.Stop()
2019-12-17 23:28:15 +00:00
cli.HandleExit(p)
2019-12-14 10:56:22 +00:00
err = p.Start()
if err != nil {
2021-10-12 17:00:46 +00:00
zap.S().Fatalf("unable to start service: %v", err)
2019-12-14 10:56:22 +00:00
}
}