diff --git a/cmd/rc-led/rc-led.go b/cmd/rc-led/rc-led.go index 30bb558..08b8f04 100644 --- a/cmd/rc-led/rc-led.go +++ b/cmd/rc-led/rc-led.go @@ -2,10 +2,8 @@ package main import ( "flag" - "fmt" "github.com/cyrilix/robocar-base/cli" - part2 "github.com/cyrilix/robocar-led/part" - MQTT "github.com/eclipse/paho.mqtt.golang" + "github.com/cyrilix/robocar-led/part" "log" "os" ) @@ -18,10 +16,10 @@ func main() { var mqttBroker, username, password, clientId string var driveModeTopic, recordTopic string - mqttQos := InitIntFlag("MQTT_QOS", 0) + mqttQos := cli.InitIntFlag("MQTT_QOS", 0) _, mqttRetain := os.LookupEnv("MQTT_RETAIN") - funcInitMqttFlags(DefaultClientId, &mqttBroker, &username, &password, &clientId, &mqttQos, &mqttRetain) + cli.InitMqttFlags(DefaultClientId, &mqttBroker, &username, &password, &clientId, &mqttQos, &mqttRetain) 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") @@ -32,61 +30,19 @@ func main() { os.Exit(1) } - client, err := Connect(mqttBroker, username, password, clientId) + 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) - p := part2.NewPart(client, driveModeTopic, recordTopic) + p := part.NewPart(client, driveModeTopic, recordTopic) defer p.Stop() + cli.HandleExit(p) + err = p.Start() if err != nil { log.Fatalf("unable to start service: %v", err) } } - -func funcInitMqttFlags(defaultClientId string, mqttBroker, username, password, clientId *string, mqttQos *int, mqttRetain *bool) { - cli.SetDefaultValueFromEnv(clientId, "MQTT_CLIENT_ID", defaultClientId) - cli.SetDefaultValueFromEnv(mqttBroker, "MQTT_BROKER", "tcp://127.0.0.1:1883") - - flag.StringVar(mqttBroker, "mqtt-broker", *mqttBroker, "Broker Uri, use MQTT_BROKER env if arg not set") - flag.StringVar(username, "mqtt-username", os.Getenv("MQTT_USERNAME"), "Broker Username, use MQTT_USERNAME env if arg not set") - flag.StringVar(password, "mqtt-password", os.Getenv("MQTT_PASSWORD"), "Broker Password, MQTT_PASSWORD env if args not set") - flag.StringVar(clientId, "mqtt-client-id", *clientId, "Mqtt client id, use MQTT_CLIENT_ID env if args not set") - flag.IntVar(mqttQos, "mqtt-qos", *mqttQos, "Qos to pusblish message, use MQTT_QOS env if arg not set") - flag.BoolVar(mqttRetain, "mqtt-retain", *mqttRetain, "Retain mqtt message, if not set, true if MQTT_RETAIN env variable is set") -} - -func InitIntFlag(key string, defValue int) int { - var value int - err := cli.SetIntDefaultValueFromEnv(&value, key, defValue) - if err != nil { - log.Panicf("invalid int value: %v", err) - } - return value -} - -func Connect(uri, username, password, clientId string) (MQTT.Client, error) { - //create a ClientOptions struct setting the broker address, clientid, turn - //off trace output and set the default message handler - opts := MQTT.NewClientOptions().AddBroker(uri) - opts.SetUsername(username) - opts.SetPassword(password) - opts.SetClientID(clientId) - opts.SetAutoReconnect(true) - opts.SetDefaultPublishHandler( - //define a function for the default message handler - func(client MQTT.Client, msg MQTT.Message) { - fmt.Printf("TOPIC: %s\n", msg.Topic()) - fmt.Printf("MSG: %s\n", msg.Payload()) - }) - - //create and start a client using the above ClientOptions - client := MQTT.NewClient(opts) - if token := client.Connect(); token.Wait() && token.Error() != nil { - return nil, fmt.Errorf("unable to connect to mqtt bus: %v", token.Error()) - } - return client, nil -} diff --git a/go.mod b/go.mod index 0bba2eb..b7c795a 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,8 @@ module github.com/cyrilix/robocar-led go 1.13 require ( - github.com/cyrilix/robocar-base v0.0.0-20191214094302-e5c81519bf31 + github.com/cyrilix/robocar-base v0.0.0-20191217230653-8d6df473d522 github.com/eclipse/paho.mqtt.golang v1.2.0 + golang.org/x/net v0.0.0-20191209160850-c0dbc17a3553 // indirect periph.io/x/periph v3.6.2+incompatible ) diff --git a/go.sum b/go.sum index e638b09..62a0bd8 100644 --- a/go.sum +++ b/go.sum @@ -10,8 +10,8 @@ github.com/cenkalti/backoff v2.2.1+incompatible/go.mod h1:90ReRw6GdpyfrHakVjL/QH github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw= github.com/containerd/continuity v0.0.0-20190426062206-aaeac12a7ffc h1:TP+534wVlf61smEIq1nwLLAjQVEK2EADoW3CX9AuT+8= github.com/containerd/continuity v0.0.0-20190426062206-aaeac12a7ffc/go.mod h1:GL3xCUCBDV3CZiTSEKksMWbLE66hEyuu9qyDOOqM47Y= -github.com/cyrilix/robocar-base v0.0.0-20191214094302-e5c81519bf31 h1:a2YOsGupM+RgItL9iNIEM4LfjCozrZWuDFNs48D3XZs= -github.com/cyrilix/robocar-base v0.0.0-20191214094302-e5c81519bf31/go.mod h1:/KZidG8Y4sKxCCkTcswpKz20oFN3j62tJvamEHcSgLM= +github.com/cyrilix/robocar-base v0.0.0-20191217230653-8d6df473d522 h1:5FRdQAdPSTWCQsU95tFR/CEj/dPC4mnmt3QjA2V8Cw0= +github.com/cyrilix/robocar-base v0.0.0-20191217230653-8d6df473d522/go.mod h1:/KZidG8Y4sKxCCkTcswpKz20oFN3j62tJvamEHcSgLM= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/docker/distribution v2.7.1-0.20190205005809-0d3efadf0154+incompatible h1:dvc1KSkIYTVjZgHf/CTC2diTYC8PzhaA5sFISRfNVrE= @@ -70,6 +70,7 @@ github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I= github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/satori/go.uuid v1.2.0 h1:0uYX9dsZ2yD7q2RtLRtPSdGDWzjeM3TbMJP9utgA0ww= github.com/satori/go.uuid v1.2.0/go.mod h1:dA0hQrYB0VpLJoorglMZABFdXlWrHn1NEOzdhQKdks0= github.com/sirupsen/logrus v1.2.0 h1:juTguoYk5qI21pwyTXY3B3Y5cOTH3ZUyZCg1v/mihuo= github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo= @@ -87,6 +88,8 @@ golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73r golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20191126235420-ef20fe5d7933 h1:e6HwijUxhDe+hPNjZQQn9bA5PW3vNmnN64U2ZW759Lk= golang.org/x/net v0.0.0-20191126235420-ef20fe5d7933/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/net v0.0.0-20191209160850-c0dbc17a3553 h1:efeOvDhwQ29Dj3SdAV/MJf8oukgn+8D8WgaCaRMchF8= +golang.org/x/net v0.0.0-20191209160850-c0dbc17a3553/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U= golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f h1:wMNYb4v58l5UBM7MYRLPG6ZhfOqbKu7X5eyFl8ZhKvA= golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= @@ -98,6 +101,7 @@ golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a h1:1BGLXjeY4akVXGgbC9HugT3Jv golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +golang.org/x/time v0.0.0-20181108054448-85acf8d2951c h1:fqgJT0MGcGpPgpWU7VRdRjuArfcOvC4AoJmILihzhDg= golang.org/x/time v0.0.0-20181108054448-85acf8d2951c/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= golang.org/x/tools v0.0.0-20180810170437-e96c4e24768d/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/tools v0.0.0-20180828015842-6cd1fcedba52/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= @@ -108,6 +112,7 @@ google.golang.org/genproto v0.0.0-20180817151627-c66870c02cf8/go.mod h1:JiN7NxoA google.golang.org/grpc v1.17.0 h1:TRJYBgMclJvGYn2rIMjj+h9KtMt5r1Ij7ODVRIZkwhk= google.golang.org/grpc v1.17.0/go.mod h1:6QZJwpn2B+Zp71q/5VxRsJ6NXXVCE5NRUHRo+f3cWCs= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY= gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/fsnotify.v1 v1.4.7 h1:xOHLXZwVvI9hhs+cLKq5+I5onOuwQLhQwiu63xxlHs4= gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys= diff --git a/part/part.go b/part/part.go index 95f45e3..8d42f01 100644 --- a/part/part.go +++ b/part/part.go @@ -41,13 +41,16 @@ func (p *LedPart) Start() error { if err := p.registerCallbacks(); err != nil { return fmt.Errorf("unable to start service: %v", err) } - defer p.Stop() for { time.Sleep(1 * time.Hour) } } func (p *LedPart) Stop() { + defer p.led.SetBlink(0) + defer p.led.SetGreen(0) + defer p.led.SetBlue(0) + defer p.led.SetRed(0) StopService("led", p.client, p.onDriveModeTopic, p.onRecordTopic) } diff --git a/vendor/github.com/cyrilix/robocar-base/cli/cli.go b/vendor/github.com/cyrilix/robocar-base/cli/cli.go index af84946..9ff38fe 100644 --- a/vendor/github.com/cyrilix/robocar-base/cli/cli.go +++ b/vendor/github.com/cyrilix/robocar-base/cli/cli.go @@ -1,9 +1,14 @@ package cli import ( + "flag" + "fmt" + MQTT "github.com/eclipse/paho.mqtt.golang" "log" "os" + "os/signal" "strconv" + "syscall" ) func SetDefaultValueFromEnv(value *string, key string, defaultValue string) { @@ -28,3 +33,63 @@ func SetIntDefaultValueFromEnv(value *int, key string, defaultValue int) error { } return nil } + +type Part interface { + Start() error + Stop() +} + +func HandleExit(p Part) { + signals := make(chan os.Signal, 1) + signal.Notify(signals, os.Kill, os.Interrupt, syscall.SIGTERM) + + go func() { + <-signals + p.Stop() + os.Exit(0) + }() +} + +func InitMqttFlags(defaultClientId string, mqttBroker, username, password, clientId *string, mqttQos *int, mqttRetain *bool) { + SetDefaultValueFromEnv(clientId, "MQTT_CLIENT_ID", defaultClientId) + SetDefaultValueFromEnv(mqttBroker, "MQTT_BROKER", "tcp://127.0.0.1:1883") + + flag.StringVar(mqttBroker, "mqtt-broker", *mqttBroker, "Broker Uri, use MQTT_BROKER env if arg not set") + flag.StringVar(username, "mqtt-username", os.Getenv("MQTT_USERNAME"), "Broker Username, use MQTT_USERNAME env if arg not set") + flag.StringVar(password, "mqtt-password", os.Getenv("MQTT_PASSWORD"), "Broker Password, MQTT_PASSWORD env if args not set") + flag.StringVar(clientId, "mqtt-client-id", *clientId, "Mqtt client id, use MQTT_CLIENT_ID env if args not set") + flag.IntVar(mqttQos, "mqtt-qos", *mqttQos, "Qos to pusblish message, use MQTT_QOS env if arg not set") + flag.BoolVar(mqttRetain, "mqtt-retain", *mqttRetain, "Retain mqtt message, if not set, true if MQTT_RETAIN env variable is set") +} + +func InitIntFlag(key string, defValue int) int { + var value int + err := SetIntDefaultValueFromEnv(&value, key, defValue) + if err != nil { + log.Panicf("invalid int value: %v", err) + } + return value +} + +func Connect(uri, username, password, clientId string) (MQTT.Client, error) { + //create a ClientOptions struct setting the broker address, clientid, turn + //off trace output and set the default message handler + opts := MQTT.NewClientOptions().AddBroker(uri) + opts.SetUsername(username) + opts.SetPassword(password) + opts.SetClientID(clientId) + opts.SetAutoReconnect(true) + opts.SetDefaultPublishHandler( + //define a function for the default message handler + func(client MQTT.Client, msg MQTT.Message) { + fmt.Printf("TOPIC: %s\n", msg.Topic()) + fmt.Printf("MSG: %s\n", msg.Payload()) + }) + + //create and start a client using the above ClientOptions + client := MQTT.NewClient(opts) + if token := client.Connect(); token.Wait() && token.Error() != nil { + return nil, fmt.Errorf("unable to connect to mqtt bus: %v", token.Error()) + } + return client, nil +} diff --git a/vendor/modules.txt b/vendor/modules.txt index 2449cdb..d1b0e85 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -9,7 +9,7 @@ github.com/containerd/continuity/fs github.com/containerd/continuity/pathdriver github.com/containerd/continuity/syscallx github.com/containerd/continuity/sysx -# github.com/cyrilix/robocar-base v0.0.0-20191214094302-e5c81519bf31 +# github.com/cyrilix/robocar-base v0.0.0-20191217230653-8d6df473d522 github.com/cyrilix/robocar-base/cli github.com/cyrilix/robocar-base/mode github.com/cyrilix/robocar-base/mqttdevice @@ -83,7 +83,7 @@ github.com/testcontainers/testcontainers-go github.com/testcontainers/testcontainers-go/wait # golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2 golang.org/x/crypto/ssh/terminal -# golang.org/x/net v0.0.0-20191126235420-ef20fe5d7933 +# golang.org/x/net v0.0.0-20191209160850-c0dbc17a3553 golang.org/x/net/context golang.org/x/net/internal/socks golang.org/x/net/proxy