Upgrade dependencies
This commit is contained in:
41
vendor/github.com/cyrilix/robocar-base/cli/cli.go
generated
vendored
41
vendor/github.com/cyrilix/robocar-base/cli/cli.go
generated
vendored
@ -2,7 +2,7 @@ package cli
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"github.com/cyrilix/robocar-base/mqttdevice"
|
||||
"fmt"
|
||||
"github.com/cyrilix/robocar-base/service"
|
||||
MQTT "github.com/eclipse/paho.mqtt.golang"
|
||||
"log"
|
||||
@ -61,16 +61,20 @@ func HandleExit(p service.Part) {
|
||||
}()
|
||||
}
|
||||
|
||||
func InitMqttFlags(defaultClientId string, mqttBroker, username, password, clientId *string, mqttQos *int, mqttRetain *bool) {
|
||||
func InitMqttFlagSet(flagSet *flag.FlagSet, 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")
|
||||
flagSet.StringVar(mqttBroker, "mqtt-broker", *mqttBroker, "Broker Uri, use MQTT_BROKER env if arg not set")
|
||||
flagSet.StringVar(username, "mqtt-username", os.Getenv("MQTT_USERNAME"), "Broker Username, use MQTT_USERNAME env if arg not set")
|
||||
flagSet.StringVar(password, "mqtt-password", os.Getenv("MQTT_PASSWORD"), "Broker Password, MQTT_PASSWORD env if args not set")
|
||||
flagSet.StringVar(clientId, "mqtt-client-id", *clientId, "Mqtt client id, use MQTT_CLIENT_ID env if args not set")
|
||||
flagSet.IntVar(mqttQos, "mqtt-qos", *mqttQos, "Qos to pusblish message, use MQTT_QOS env if arg not set")
|
||||
flagSet.BoolVar(mqttRetain, "mqtt-retain", *mqttRetain, "Retain mqtt message, if not set, true if MQTT_RETAIN env variable is set")
|
||||
}
|
||||
|
||||
func InitMqttFlags(defaultClientId string, mqttBroker, username, password, clientId *string, mqttQos *int, mqttRetain *bool) {
|
||||
InitMqttFlagSet(flag.CommandLine, defaultClientId, mqttBroker, username, password, clientId, mqttQos, mqttRetain)
|
||||
}
|
||||
|
||||
func InitIntFlag(key string, defValue int) int {
|
||||
@ -92,5 +96,24 @@ func InitFloat64Flag(key string, defValue float64) float64 {
|
||||
}
|
||||
|
||||
func Connect(uri, username, password, clientId string) (MQTT.Client, error) {
|
||||
return mqttdevice.Connect(uri, username, password, clientId)
|
||||
//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
|
||||
}
|
||||
|
141
vendor/github.com/cyrilix/robocar-base/mqttdevice/mqttdevice.go
generated
vendored
141
vendor/github.com/cyrilix/robocar-base/mqttdevice/mqttdevice.go
generated
vendored
@ -1,141 +0,0 @@
|
||||
package mqttdevice
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"github.com/cyrilix/robocar-base/types"
|
||||
MQTT "github.com/eclipse/paho.mqtt.golang"
|
||||
"log"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
type Publisher interface {
|
||||
Publish(topic string, payload MqttValue)
|
||||
}
|
||||
|
||||
type Subscriber interface {
|
||||
Subscribe(topic string, mh MQTT.MessageHandler)
|
||||
}
|
||||
|
||||
type MQTTPubSub interface {
|
||||
Publisher
|
||||
Subscriber
|
||||
}
|
||||
|
||||
type pahoMqttPubSub struct {
|
||||
client MQTT.Client
|
||||
qos int
|
||||
retain bool
|
||||
}
|
||||
|
||||
func NewPahoMqttPubSub(client MQTT.Client, qos int, retain bool) MQTTPubSub {
|
||||
p := pahoMqttPubSub{client: client, qos: qos, retain: retain}
|
||||
return &p
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
// Publish message to broker
|
||||
func (p *pahoMqttPubSub) Publish(topic string, payload MqttValue) {
|
||||
tokenResp := p.client.Publish(topic, byte(p.qos), p.retain, string(payload))
|
||||
if tokenResp.Error() != nil {
|
||||
log.Fatalf("%+v\n", tokenResp.Error())
|
||||
}
|
||||
}
|
||||
|
||||
// Register func to execute on message
|
||||
func (p *pahoMqttPubSub) Subscribe(topic string, callback MQTT.MessageHandler) {
|
||||
tokenResp := p.client.Subscribe(topic, byte(p.qos), callback)
|
||||
if tokenResp.Error() != nil {
|
||||
log.Fatalf("%+v\n", tokenResp.Error())
|
||||
}
|
||||
}
|
||||
|
||||
type MqttValue []byte
|
||||
|
||||
func NewMqttValue(v interface{}) MqttValue {
|
||||
switch val := v.(type) {
|
||||
case string:
|
||||
return MqttValue(val)
|
||||
case float32, float64:
|
||||
return MqttValue(fmt.Sprintf("%0.2f", val))
|
||||
case int, int8, int16, int32, int64:
|
||||
return MqttValue(fmt.Sprintf("%d", val))
|
||||
case bool:
|
||||
if val {
|
||||
return []byte("ON")
|
||||
} else {
|
||||
return []byte("OFF")
|
||||
}
|
||||
case []byte:
|
||||
return val
|
||||
case MqttValue:
|
||||
return val
|
||||
default:
|
||||
jsonValue, err := json.Marshal(v)
|
||||
if err != nil {
|
||||
log.Printf("unable to mashall to json value '%v': %v", v, err)
|
||||
return nil
|
||||
}
|
||||
return jsonValue
|
||||
}
|
||||
}
|
||||
|
||||
func (m *MqttValue) IntValue() (int, error) {
|
||||
return strconv.Atoi(string(*m))
|
||||
}
|
||||
|
||||
func (m *MqttValue) Float32Value() (float32, error) {
|
||||
val := string(*m)
|
||||
r, err := strconv.ParseFloat(val, 32)
|
||||
return float32(r), err
|
||||
}
|
||||
func (m *MqttValue) Float64Value() (float64, error) {
|
||||
val := string(*m)
|
||||
return strconv.ParseFloat(val, 64)
|
||||
}
|
||||
func (m *MqttValue) StringValue() (string, error) {
|
||||
return string(*m), nil
|
||||
}
|
||||
func (m *MqttValue) DriveModeValue() (types.DriveMode, error) {
|
||||
val, err := m.IntValue()
|
||||
if err != nil {
|
||||
return types.DriveModeInvalid, err
|
||||
}
|
||||
return types.DriveMode(val), nil
|
||||
}
|
||||
func (m *MqttValue) ByteSliceValue() ([]byte, error) {
|
||||
return *m, nil
|
||||
}
|
||||
func (m *MqttValue) BoolValue() (bool, error) {
|
||||
val := string(*m)
|
||||
switch val {
|
||||
case "ON":
|
||||
return true, nil
|
||||
case "OFF":
|
||||
return false, nil
|
||||
default:
|
||||
return false, fmt.Errorf("value %v can't be converted to bool", val)
|
||||
}
|
||||
}
|
24
vendor/github.com/cyrilix/robocar-base/testtools/testtools.go
generated
vendored
24
vendor/github.com/cyrilix/robocar-base/testtools/testtools.go
generated
vendored
@ -1,35 +1,11 @@
|
||||
package testtools
|
||||
|
||||
import (
|
||||
"github.com/cyrilix/robocar-base/mqttdevice"
|
||||
mqtt "github.com/eclipse/paho.mqtt.golang"
|
||||
"github.com/golang/protobuf/proto"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"sync"
|
||||
)
|
||||
|
||||
|
||||
func NewFakePublisher() *FakePublisher {
|
||||
return &FakePublisher{msg: make(map[string]mqttdevice.MqttValue)}
|
||||
}
|
||||
|
||||
type FakePublisher struct {
|
||||
muMsg sync.Mutex
|
||||
msg map[string]mqttdevice.MqttValue
|
||||
}
|
||||
|
||||
func (f *FakePublisher) Publish(topic string, payload mqttdevice.MqttValue) {
|
||||
f.muMsg.Lock()
|
||||
defer f.muMsg.Unlock()
|
||||
f.msg[topic] = payload
|
||||
}
|
||||
|
||||
func (f *FakePublisher) PublishedEvent(topic string) mqttdevice.MqttValue {
|
||||
f.muMsg.Lock()
|
||||
defer f.muMsg.Unlock()
|
||||
return f.msg[topic]
|
||||
}
|
||||
|
||||
type fakeMessage struct {
|
||||
qos byte
|
||||
topic string
|
||||
|
36
vendor/github.com/cyrilix/robocar-base/types/mode.go
generated
vendored
36
vendor/github.com/cyrilix/robocar-base/types/mode.go
generated
vendored
@ -1,36 +0,0 @@
|
||||
package types
|
||||
|
||||
import (
|
||||
"log"
|
||||
)
|
||||
|
||||
type DriveMode int
|
||||
|
||||
const (
|
||||
DriveModeInvalid = -1
|
||||
DriveModeUser = iota
|
||||
DriveModePilot
|
||||
)
|
||||
|
||||
func ToString(mode DriveMode) string {
|
||||
switch mode {
|
||||
case DriveModeUser:
|
||||
return "user"
|
||||
case DriveModePilot:
|
||||
return "pilot"
|
||||
default:
|
||||
return ""
|
||||
}
|
||||
}
|
||||
|
||||
func ParseString(val string) DriveMode {
|
||||
switch val {
|
||||
case "user":
|
||||
return DriveModeUser
|
||||
case "pilot":
|
||||
return DriveModePilot
|
||||
default:
|
||||
log.Printf("invalid DriveMode: %v", val)
|
||||
return DriveModeInvalid
|
||||
}
|
||||
}
|
10
vendor/github.com/cyrilix/robocar-base/types/rc.go
generated
vendored
10
vendor/github.com/cyrilix/robocar-base/types/rc.go
generated
vendored
@ -1,10 +0,0 @@
|
||||
package types
|
||||
|
||||
/* Radio control value */
|
||||
type RCValue struct {
|
||||
Value float64
|
||||
Confidence float64
|
||||
}
|
||||
|
||||
type Steering RCValue
|
||||
type Throttle RCValue
|
5
vendor/github.com/cyrilix/robocar-base/types/types.go
generated
vendored
5
vendor/github.com/cyrilix/robocar-base/types/types.go
generated
vendored
@ -1,5 +0,0 @@
|
||||
package types
|
||||
|
||||
type BoundingBox struct {
|
||||
Left, Top, Right, Bottom int
|
||||
}
|
Reference in New Issue
Block a user