2019-12-01 21:35:19 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
|
|
|
"github.com/cyrilix/robocar-arduino/arduino"
|
2019-12-18 22:21:52 +00:00
|
|
|
"github.com/cyrilix/robocar-base/cli"
|
2019-12-01 21:35:19 +00:00
|
|
|
"github.com/cyrilix/robocar-base/mqttdevice"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
)
|
|
|
|
|
|
|
|
const DefaultClientId = "robocar-arduino"
|
|
|
|
|
|
|
|
func setDefaultValueFromEnv(value *string, key string, defaultValue string) {
|
|
|
|
if os.Getenv(key) != "" {
|
|
|
|
*value = os.Getenv(key)
|
|
|
|
} else {
|
|
|
|
*value = defaultValue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
2019-12-18 22:21:52 +00:00
|
|
|
var mqttBroker, username, password, clientId, topicBase string
|
2019-12-01 21:35:19 +00:00
|
|
|
var device string
|
|
|
|
var baud int
|
|
|
|
var pubFrequency float64
|
2019-12-18 22:21:52 +00:00
|
|
|
var debug bool
|
2019-12-01 21:35:19 +00:00
|
|
|
|
2019-12-18 22:21:52 +00:00
|
|
|
mqttQos := cli.InitIntFlag("MQTT_QOS", 0)
|
2019-12-01 21:35:19 +00:00
|
|
|
_, mqttRetain := os.LookupEnv("MQTT_RETAIN")
|
|
|
|
|
2019-12-18 22:21:52 +00:00
|
|
|
cli.InitMqttFlags(DefaultClientId, &mqttBroker, &username, &password, &clientId, &mqttQos, &mqttRetain)
|
|
|
|
|
2019-12-01 21:35:19 +00:00
|
|
|
flag.Float64Var(&pubFrequency, "mqtt-pub-frequency", 25., "Number of messages to publish per second")
|
2019-12-18 22:21:52 +00:00
|
|
|
flag.StringVar(&topicBase, "mqtt-topic-base", os.Getenv("MQTT_TOPIC_BASE"), "Mqtt topic prefix, use MQTT_TOPIC_BASE if args not set")
|
2019-12-01 21:35:19 +00:00
|
|
|
flag.StringVar(&device, "device", "/dev/serial0", "Serial device")
|
|
|
|
flag.IntVar(&baud, "baud", 115200, "Serial baud")
|
2019-12-18 22:21:52 +00:00
|
|
|
flag.BoolVar(&debug, "debug", false, "Display raw value to debug")
|
|
|
|
|
|
|
|
flag.Parse()
|
|
|
|
if len(os.Args) <= 1 {
|
|
|
|
flag.PrintDefaults()
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
2019-12-01 21:35:19 +00:00
|
|
|
|
|
|
|
pubSub := mqttdevice.NewPahoMqttPubSub(mqttBroker, username, password, clientId, mqttQos, mqttRetain)
|
2019-12-18 22:21:52 +00:00
|
|
|
defer func() {
|
|
|
|
err := pubSub.Close()
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("unable to close pubsub: %v", err)
|
|
|
|
}
|
|
|
|
}()
|
2019-12-01 21:35:19 +00:00
|
|
|
|
2019-12-18 22:21:52 +00:00
|
|
|
a := arduino.NewPart(device, baud, pubSub, topicBase, pubFrequency, debug)
|
|
|
|
err := a.Start()
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("unable to start service: %v", err)
|
|
|
|
}
|
2019-12-01 21:35:19 +00:00
|
|
|
}
|