2020-09-20 21:16:06 +00:00
package main
import (
"flag"
"github.com/cyrilix/robocar-base/cli"
2021-01-23 11:30:59 +00:00
events2 "github.com/cyrilix/robocar-protobuf/go/events"
"github.com/cyrilix/robocar-simulator/pkg/events"
2021-01-15 10:12:21 +00:00
"github.com/cyrilix/robocar-simulator/pkg/gateway"
2021-01-23 11:30:59 +00:00
mqtt "github.com/eclipse/paho.mqtt.golang"
"github.com/golang/protobuf/proto"
2021-01-15 10:31:37 +00:00
log "github.com/sirupsen/logrus"
2020-09-20 21:16:06 +00:00
"os"
)
2021-01-15 10:12:21 +00:00
const DefaultClientId = "robocar-simulator"
2020-09-20 21:16:06 +00:00
func main ( ) {
2021-01-23 11:30:59 +00:00
var mqttBroker , username , password , clientId , topicFrame , topicSteering , topicThrottle string
var topicCtrlSteering , topicCtrlThrottle string
2020-09-20 21:16:06 +00:00
var address string
2021-01-15 10:12:21 +00:00
var debug bool
2020-09-20 21:16:06 +00:00
mqttQos := cli . InitIntFlag ( "MQTT_QOS" , 0 )
_ , mqttRetain := os . LookupEnv ( "MQTT_RETAIN" )
cli . InitMqttFlags ( DefaultClientId , & mqttBroker , & username , & password , & clientId , & mqttQos , & mqttRetain )
2021-02-01 16:04:42 +00:00
flag . StringVar ( & topicFrame , "events-topic-camera" , os . Getenv ( "MQTT_TOPIC_CAMERA" ) , "Mqtt topic to events gateway frames, use MQTT_TOPIC_CAMERA if args not set" )
flag . StringVar ( & topicSteering , "events-topic-steering" , os . Getenv ( "MQTT_TOPIC_STEERING" ) , "Mqtt topic to events gateway steering, use MQTT_TOPIC_STEERING if args not set" )
flag . StringVar ( & topicThrottle , "events-topic-throttle" , os . Getenv ( "MQTT_TOPIC_THROTTLE" ) , "Mqtt topic to events gateway throttle, use MQTT_TOPIC_THROTTLE if args not set" )
flag . StringVar ( & topicCtrlSteering , "topic-steering-ctrl" , os . Getenv ( "MQTT_TOPIC_STEERING_CTRL" ) , "Mqtt topic to send steering instructions, use MQTT_TOPIC_STEERING_CTRL if args not set" )
flag . StringVar ( & topicCtrlThrottle , "topic-throttle-ctrl" , os . Getenv ( "MQTT_TOPIC_THROTTLE_CTRL" ) , "Mqtt topic to send throttle instructions, use MQTT_TOPIC_THROTTLE_CTRL if args not set" )
2020-09-20 21:16:06 +00:00
flag . StringVar ( & address , "simulator-address" , "127.0.0.1:9091" , "Simulator address" )
2021-01-15 10:12:21 +00:00
flag . BoolVar ( & debug , "debug" , false , "Debug logs" )
2020-09-20 21:16:06 +00:00
flag . Parse ( )
if len ( os . Args ) <= 1 {
flag . PrintDefaults ( )
os . Exit ( 1 )
}
2021-01-15 10:12:21 +00:00
if debug {
log . SetLevel ( log . DebugLevel )
}
2020-09-20 21:16:06 +00:00
client , err := cli . Connect ( mqttBroker , username , password , clientId )
if err != nil {
2021-01-21 22:39:52 +00:00
log . Fatalf ( "unable to connect to events broker: %v" , err )
2020-09-20 21:16:06 +00:00
}
defer client . Disconnect ( 10 )
2021-01-23 11:30:59 +00:00
gtw := gateway . New ( address )
defer gtw . Stop ( )
2020-09-20 21:16:06 +00:00
2021-01-23 11:30:59 +00:00
msgPub := events . NewMsgPublisher (
gtw ,
events . NewMqttPublisher ( client ) ,
topicFrame ,
topicSteering ,
topicThrottle ,
)
defer msgPub . Stop ( )
2021-02-01 16:04:42 +00:00
msgPub . Start ( )
2020-09-20 21:16:06 +00:00
2021-01-23 11:30:59 +00:00
cli . HandleExit ( gtw )
err = gtw . Start ( )
2020-09-20 21:16:06 +00:00
if err != nil {
log . Fatalf ( "unable to start service: %v" , err )
}
2021-02-01 16:04:42 +00:00
2021-01-23 11:30:59 +00:00
if topicCtrlSteering != "" {
log . Infof ( "configure mqtt route on steering command" )
client . AddRoute ( topicCtrlSteering , func ( client mqtt . Client , message mqtt . Message ) {
onSteeringCommand ( gtw , message )
} )
}
if topicCtrlThrottle != "" {
log . Infof ( "configure mqtt route on throttle command" )
client . AddRoute ( topicCtrlThrottle , func ( client mqtt . Client , message mqtt . Message ) {
onThrottleCommand ( gtw , message )
} )
}
}
func onSteeringCommand ( c * gateway . Gateway , message mqtt . Message ) {
var steeringMsg * events2 . SteeringMessage
err := proto . Unmarshal ( message . Payload ( ) , steeringMsg )
if err != nil {
log . Errorf ( "unable to unmarshal steering msg: %v" , err )
return
}
c . WriteSteering ( steeringMsg )
}
func onThrottleCommand ( c * gateway . Gateway , message mqtt . Message ) {
var throttleMsg * events2 . ThrottleMessage
err := proto . Unmarshal ( message . Payload ( ) , throttleMsg )
if err != nil {
log . Errorf ( "unable to unmarshal throttle msg: %v" , err )
return
}
c . WriteThrottle ( throttleMsg )
2020-09-20 21:16:06 +00:00
}