Upgrade robocar-base dependency

This commit is contained in:
2019-12-30 23:35:23 +01:00
parent d1ef22c2a5
commit 6d367d4d6b
609 changed files with 158 additions and 99917 deletions

View File

@ -3,6 +3,7 @@ package cli
import (
"flag"
"fmt"
"github.com/cyrilix/robocar-base/service"
MQTT "github.com/eclipse/paho.mqtt.golang"
"log"
"os"
@ -33,13 +34,23 @@ func SetIntDefaultValueFromEnv(value *int, key string, defaultValue int) error {
}
return nil
}
type Part interface {
Start() error
Stop()
func SetFloat64DefaultValueFromEnv(value *float64, key string, defaultValue float64) error {
var sVal string
if os.Getenv(key) != "" {
sVal = os.Getenv(key)
val, err := strconv.ParseFloat(sVal, 64)
if err != nil {
log.Printf("unable to convert string to float: %v", err)
return err
}
*value = val
} else {
*value = defaultValue
}
return nil
}
func HandleExit(p Part) {
func HandleExit(p service.Part) {
signals := make(chan os.Signal, 1)
signal.Notify(signals, os.Kill, os.Interrupt, syscall.SIGTERM)
@ -71,6 +82,15 @@ func InitIntFlag(key string, defValue int) int {
return value
}
func InitFloat64Flag(key string, defValue float64) float64 {
var value float64
err := SetFloat64DefaultValueFromEnv(&value, key, defValue)
if err != nil {
log.Panicf("invalid 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

View File

@ -1,8 +1,9 @@
package mqttdevice
import (
"encoding/json"
"fmt"
"github.com/cyrilix/robocar-base/mode"
"github.com/cyrilix/robocar-base/types"
MQTT "github.com/eclipse/paho.mqtt.golang"
"io"
"log"
@ -96,8 +97,6 @@ func NewMqttValue(v interface{}) MqttValue {
return MqttValue(fmt.Sprintf("%0.2f", val))
case int, int8, int16, int32, int64:
return MqttValue(fmt.Sprintf("%d", val))
case mode.DriveMode:
return MqttValue(mode.ToString(val))
case bool:
if val {
return []byte("ON")
@ -109,8 +108,12 @@ func NewMqttValue(v interface{}) MqttValue {
case MqttValue:
return val
default:
log.Printf("invalid mqtt value: %v", val)
return nil
jsonValue, err := json.Marshal(v)
if err != nil {
log.Printf("unable to mashall to json value '%v': %v", v, err)
return nil
}
return jsonValue
}
}
@ -130,6 +133,13 @@ func (m *MqttValue) Float64Value() (float64, error) {
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
}

33
vendor/github.com/cyrilix/robocar-base/service/part.go generated vendored Normal file
View File

@ -0,0 +1,33 @@
package service
import (
"fmt"
mqtt "github.com/eclipse/paho.mqtt.golang"
"log"
)
func StopService(name string, client mqtt.Client, topics ...string) {
log.Printf("Stop %s service", name)
token := client.Unsubscribe(topics...)
token.Wait()
if token.Error() != nil {
log.Printf("unable to unsubscribe service: %v", token.Error())
}
client.Disconnect(50)
}
func RegisterCallback(client mqtt.Client, topic string, callback mqtt.MessageHandler) error {
log.Printf("Register callback on topic %v", topic)
token := client.Subscribe(topic, 0, callback)
token.Wait()
if token.Error() != nil {
return fmt.Errorf("unable to register callback on topic %s: %v", topic, token.Error())
}
return nil
}
type Part interface {
Start() error
Stop()
}

View File

@ -1,43 +1,11 @@
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)}

View File

@ -1,4 +1,4 @@
package mode
package types
import (
"log"
@ -32,6 +32,5 @@ func ParseString(val string) DriveMode {
default:
log.Printf("invalid DriveMode: %v", val)
return DriveModeInvalid
}
}

10
vendor/github.com/cyrilix/robocar-base/types/rc.go generated vendored Normal file
View File

@ -0,0 +1,10 @@
package types
/* Radio control value */
type RCValue struct {
Value float64
Confidence float64
}
type Steering RCValue
type Throttle RCValue

View File

@ -0,0 +1,5 @@
package types
type BoundingBox struct {
Left, Top, Right, Bottom int
}