robocar-record/cmd/rc-record/rc-record.go

70 lines
2.1 KiB
Go
Raw Normal View History

2020-01-26 18:57:32 +00:00
package main
import (
"flag"
"github.com/cyrilix/robocar-base/cli"
2021-10-12 20:08:55 +00:00
"github.com/cyrilix/robocar-record/pkg/part"
2021-10-12 20:08:07 +00:00
"go.uber.org/zap"
2020-01-26 18:57:32 +00:00
"log"
"os"
)
const (
DefaultClientId = "robocar-record"
)
func main() {
var mqttBroker, username, password, clientId string
var cameraTopic, steeringTopic, recordTopic, switchRecordTopic string
2021-10-12 20:08:07 +00:00
var debug bool
2020-01-26 18:57:32 +00:00
mqttQos := cli.InitIntFlag("MQTT_QOS", 0)
_, mqttRetain := os.LookupEnv("MQTT_RETAIN")
cli.InitMqttFlags(DefaultClientId, &mqttBroker, &username, &password, &clientId, &mqttQos, &mqttRetain)
flag.StringVar(&cameraTopic, "mqtt-topic-frame", os.Getenv("MQTT_TOPIC_CAMERA"), "Mqtt topic that contains camera frame, use MQTT_TOPIC_CAMERA if args not set")
flag.StringVar(&steeringTopic, "mqtt-topic-steering", os.Getenv("MQTT_TOPIC_STEERING"), "Mqtt topic that contains steering raw values, use MQTT_TOPIC_STEERING if args not set")
flag.StringVar(&switchRecordTopic, "mqtt-topic-switch-record", os.Getenv("MQTT_TOPIC_SWITCH_RECORD"), "Mqtt topic that contain switch record state, use MQTT_TOPIC_SWITCH_RECORD if args not set")
flag.StringVar(&recordTopic, "mqtt-topic-records", os.Getenv("MQTT_TOPIC_RECORDS"), "Mqtt topic to publish record messages, use MQTT_TOPIC_RECORDS if args not set")
2021-10-12 20:08:07 +00:00
flag.BoolVar(&debug, "debug", false, "Display raw value to debug")
2020-01-26 18:57:32 +00:00
flag.Parse()
if len(os.Args) <= 1 {
flag.PrintDefaults()
os.Exit(1)
}
2021-10-12 20:08:07 +00:00
config := zap.NewDevelopmentConfig()
if debug {
config.Level = zap.NewAtomicLevelAt(zap.DebugLevel)
} else {
config.Level = zap.NewAtomicLevelAt(zap.InfoLevel)
}
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-01-26 18:57:32 +00:00
client, err := cli.Connect(mqttBroker, username, password, clientId)
if err != nil {
2021-10-12 20:08:07 +00:00
zap.S().Fatalf("unable to connect to mqtt bus: %v", err)
2020-01-26 18:57:32 +00:00
}
defer client.Disconnect(50)
r := part.NewRecorder(client, recordTopic, cameraTopic, steeringTopic, switchRecordTopic)
defer r.Stop()
cli.HandleExit(r)
if err := r.Start(); err != nil {
2021-10-12 20:08:07 +00:00
zap.S().Fatalf("unable to start service: %v", err)
2020-01-26 18:57:32 +00:00
}
}