robocar-steering/cmd/rc-steering/rc-steering.go

69 lines
2.3 KiB
Go
Raw Normal View History

2020-02-09 11:14:10 +00:00
package main
import (
"flag"
"github.com/cyrilix/robocar-base/cli"
2022-08-20 19:33:45 +00:00
"github.com/cyrilix/robocar-steering/pkg/steering"
2021-10-12 17:47:33 +00:00
"go.uber.org/zap"
2021-10-12 17:49:53 +00:00
"log"
2020-02-09 11:14:10 +00:00
"os"
)
const (
DefaultClientId = "robocar-steering"
)
func main() {
var mqttBroker, username, password, clientId string
2022-08-22 11:12:12 +00:00
var steeringTopic, driveModeTopic, rcSteeringTopic, tfSteeringTopic, objectsTopic string
2020-02-09 11:14:10 +00:00
mqttQos := cli.InitIntFlag("MQTT_QOS", 0)
_, mqttRetain := os.LookupEnv("MQTT_RETAIN")
cli.InitMqttFlags(DefaultClientId, &mqttBroker, &username, &password, &clientId, &mqttQos, &mqttRetain)
flag.StringVar(&steeringTopic, "mqtt-topic-steering", os.Getenv("MQTT_TOPIC_STEERING"), "Mqtt topic to publish steering result, use MQTT_TOPIC_STEERING if args not set")
flag.StringVar(&rcSteeringTopic, "mqtt-topic-rc-steering", os.Getenv("MQTT_TOPIC_RC_STEERING"), "Mqtt topic that contains RC steering value, use MQTT_TOPIC_RC_STEERING if args not set")
flag.StringVar(&tfSteeringTopic, "mqtt-topic-tf-steering", os.Getenv("MQTT_TOPIC_TF_STEERING"), "Mqtt topic that contains tenorflow steering value, use MQTT_TOPIC_TF_STEERING if args not set")
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")
2022-08-22 11:12:12 +00:00
flag.StringVar(&objectsTopic, "mqtt-topic-objects", os.Getenv("MQTT_TOPIC_OBJECTS"), "Mqtt topic that contains Objects from object detection value, use MQTT_TOPIC_OBJECTS if args not set")
2020-02-09 11:14:10 +00:00
logLevel := zap.LevelFlag("log", zap.InfoLevel, "log level")
2021-10-12 17:47:33 +00:00
2020-02-09 11:14:10 +00:00
flag.Parse()
2020-02-09 11:14:10 +00:00
if len(os.Args) <= 1 {
flag.PrintDefaults()
os.Exit(1)
}
2021-10-12 17:47:33 +00:00
config := zap.NewDevelopmentConfig()
config.Level = zap.NewAtomicLevelAt(*logLevel)
2021-10-12 17:47:33 +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)
2020-02-09 11:14:10 +00:00
client, err := cli.Connect(mqttBroker, username, password, clientId)
if err != nil {
log.Fatalf("unable to connect to mqtt bus: %v", err)
}
defer client.Disconnect(50)
2022-08-22 11:12:12 +00:00
p := steering.NewController(client, steeringTopic, driveModeTopic, rcSteeringTopic, tfSteeringTopic, objectsTopic)
2020-02-09 11:14:10 +00:00
defer p.Stop()
cli.HandleExit(p)
err = p.Start()
if err != nil {
2021-10-12 17:47:33 +00:00
zap.S().Fatalf("unable to start service: %v", err)
2020-02-09 11:14:10 +00:00
}
}