Debug on car
This commit is contained in:
95
vendor/github.com/cyrilix/robocar-base/cli/cli.go
generated
vendored
Normal file
95
vendor/github.com/cyrilix/robocar-base/cli/cli.go
generated
vendored
Normal file
@ -0,0 +1,95 @@
|
||||
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) {
|
||||
if os.Getenv(key) != "" {
|
||||
*value = os.Getenv(key)
|
||||
} else {
|
||||
*value = defaultValue
|
||||
}
|
||||
}
|
||||
func SetIntDefaultValueFromEnv(value *int, key string, defaultValue int) error {
|
||||
var sVal string
|
||||
if os.Getenv(key) != "" {
|
||||
sVal = os.Getenv(key)
|
||||
val, err := strconv.Atoi(sVal)
|
||||
if err != nil {
|
||||
log.Printf("unable to convert string to int: %v", err)
|
||||
return err
|
||||
}
|
||||
*value = val
|
||||
} else {
|
||||
*value = defaultValue
|
||||
}
|
||||
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
|
||||
}
|
42
vendor/github.com/cyrilix/robocar-base/mqttdevice/mqttdevice.go
generated
vendored
42
vendor/github.com/cyrilix/robocar-base/mqttdevice/mqttdevice.go
generated
vendored
@ -6,6 +6,7 @@ import (
|
||||
MQTT "github.com/eclipse/paho.mqtt.golang"
|
||||
"io"
|
||||
"log"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
type Publisher interface {
|
||||
@ -85,7 +86,7 @@ func (p *pahoMqttPubSub) Connect() {
|
||||
}
|
||||
}
|
||||
|
||||
type MqttValue string
|
||||
type MqttValue []byte
|
||||
|
||||
func NewMqttValue(v interface{}) MqttValue {
|
||||
switch val := v.(type) {
|
||||
@ -99,14 +100,47 @@ func NewMqttValue(v interface{}) MqttValue {
|
||||
return MqttValue(mode.ToString(val))
|
||||
case bool:
|
||||
if val {
|
||||
return "ON"
|
||||
return []byte("ON")
|
||||
} else {
|
||||
return "OFF"
|
||||
return []byte("OFF")
|
||||
}
|
||||
case []byte:
|
||||
return val
|
||||
case MqttValue:
|
||||
return val
|
||||
default:
|
||||
log.Printf("invalid mqtt value: %v", val)
|
||||
return ""
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
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) 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)
|
||||
}
|
||||
}
|
||||
|
105
vendor/github.com/cyrilix/robocar-base/testtools/testtools.go
generated
vendored
Normal file
105
vendor/github.com/cyrilix/robocar-base/testtools/testtools.go
generated
vendored
Normal file
@ -0,0 +1,105 @@
|
||||
package testtools
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"github.com/cyrilix/robocar-base/mqttdevice"
|
||||
mqtt "github.com/eclipse/paho.mqtt.golang"
|
||||
"github.com/testcontainers/testcontainers-go"
|
||||
"github.com/testcontainers/testcontainers-go/wait"
|
||||
"sync"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func MqttContainer(t *testing.T) (context.Context, testcontainers.Container, string) {
|
||||
ctx := context.Background()
|
||||
req := testcontainers.ContainerRequest{
|
||||
Image: "eclipse-mosquitto",
|
||||
ExposedPorts: []string{"1883/tcp"},
|
||||
WaitingFor: wait.ForLog("listen socket on port 1883."),
|
||||
}
|
||||
mqttC, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{
|
||||
ContainerRequest: req,
|
||||
Started: true,
|
||||
})
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
ip, err := mqttC.Host(ctx)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
port, err := mqttC.MappedPort(ctx, "1883/tcp")
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
mqttUri := fmt.Sprintf("tcp://%s:%d", ip, port.Int())
|
||||
return ctx, mqttC, mqttUri
|
||||
}
|
||||
|
||||
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
|
||||
payload []byte
|
||||
acked bool
|
||||
}
|
||||
|
||||
func (f *fakeMessage) Duplicate() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func (f *fakeMessage) Qos() byte {
|
||||
return f.qos
|
||||
}
|
||||
|
||||
func (f *fakeMessage) Retained() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func (f *fakeMessage) Topic() string {
|
||||
return f.topic
|
||||
}
|
||||
|
||||
func (f *fakeMessage) MessageID() uint16 {
|
||||
return 1234
|
||||
}
|
||||
|
||||
func (f *fakeMessage) Payload() []byte {
|
||||
return f.payload
|
||||
}
|
||||
|
||||
func (f *fakeMessage) Ack() {
|
||||
f.acked = true
|
||||
}
|
||||
|
||||
func NewFakeMessage(topic string, payload []byte) mqtt.Message {
|
||||
return &fakeMessage{
|
||||
qos: 0,
|
||||
topic: topic,
|
||||
payload: payload,
|
||||
acked: false,
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user