2019-12-27 14:38:12 +00:00
package main
import (
"flag"
"github.com/cyrilix/robocar-base/cli"
2021-10-12 19:57:35 +00:00
"github.com/cyrilix/robocar-throttle/pkg/part"
2021-10-12 19:56:42 +00:00
"go.uber.org/zap"
2019-12-27 14:38:12 +00:00
"log"
"os"
)
const (
2020-01-01 18:36:22 +00:00
DefaultClientId = "robocar-throttle"
2019-12-27 14:38:12 +00:00
DefaultThrottleMin = 0.3
)
func main ( ) {
var mqttBroker , username , password , clientId string
2019-12-27 16:42:10 +00:00
var throttleTopic , driveModeTopic , rcThrottleTopic string
2019-12-27 14:38:12 +00:00
var minThrottle , maxThrottle float64
err := cli . SetFloat64DefaultValueFromEnv ( & minThrottle , "THROTTLE_MIN" , DefaultThrottleMin )
if err != nil {
2021-10-12 19:56:42 +00:00
zap . S ( ) . Errorf ( "unable to parse min throttle value arg: %v" , err )
2019-12-27 14:38:12 +00:00
}
err = cli . SetFloat64DefaultValueFromEnv ( & maxThrottle , "THROTTLE_MAX" , minThrottle )
if err != nil {
2021-10-12 19:56:42 +00:00
zap . S ( ) . Errorf ( "unable to parse max throttle value arg: %v" , err )
2019-12-27 14:38:12 +00:00
}
mqttQos := cli . InitIntFlag ( "MQTT_QOS" , 0 )
_ , mqttRetain := os . LookupEnv ( "MQTT_RETAIN" )
cli . InitMqttFlags ( DefaultClientId , & mqttBroker , & username , & password , & clientId , & mqttQos , & mqttRetain )
flag . StringVar ( & throttleTopic , "mqtt-topic-throttle" , os . Getenv ( "MQTT_TOPIC_THROTTLE" ) , "Mqtt topic to publish throttle result, use MQTT_TOPIC_THROTTLE if args not set" )
2019-12-27 16:42:10 +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 ( & rcThrottleTopic , "mqtt-topic-rc-throttle" , os . Getenv ( "MQTT_TOPIC_RC_THROTTLE" ) , "Mqtt topic that contains RC Throttle value, use MQTT_TOPIC_RC_THROTTLE if args not set" )
2019-12-27 14:38:12 +00:00
flag . Float64Var ( & minThrottle , "throttle-min" , minThrottle , "Minimum throttle value, use THROTTLE_MIN if args not set" )
flag . Float64Var ( & maxThrottle , "throttle-max" , maxThrottle , "Minimum throttle value, use THROTTLE_MAX if args not set" )
2022-06-16 09:58:43 +00:00
logLevel := zap . LevelFlag ( "log" , zap . InfoLevel , "log level" )
2019-12-27 14:38:12 +00:00
flag . Parse ( )
if len ( os . Args ) <= 1 {
flag . PrintDefaults ( )
os . Exit ( 1 )
}
2021-10-12 19:56:42 +00:00
config := zap . NewDevelopmentConfig ( )
2022-06-16 09:58:43 +00:00
config . Level = zap . NewAtomicLevelAt ( * logLevel )
2021-10-12 19:56:42 +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-27 14:38:12 +00:00
client , err := cli . Connect ( mqttBroker , username , password , clientId )
if err != nil {
2021-10-12 19:56:42 +00:00
zap . S ( ) . Fatalf ( "unable to connect to mqtt bus: %v" , err )
2019-12-27 14:38:12 +00:00
}
defer client . Disconnect ( 50 )
2020-01-01 19:33:41 +00:00
p := part . NewPart ( client , throttleTopic , driveModeTopic , rcThrottleTopic , float32 ( minThrottle ) , float32 ( maxThrottle ) , 2 )
2019-12-27 14:38:12 +00:00
defer p . Stop ( )
cli . HandleExit ( p )
err = p . Start ( )
if err != nil {
2021-10-12 19:56:42 +00:00
zap . S ( ) . Fatalf ( "unable to start service: %v" , err )
2019-12-27 14:38:12 +00:00
}
}