Update robocar-base dependencies

This commit is contained in:
2019-12-27 16:51:39 +01:00
parent a4c228ed94
commit 1f5f36a9cf
10 changed files with 101 additions and 47 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"
@ -107,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
}
}
@ -128,12 +133,12 @@ func (m *MqttValue) Float64Value() (float64, error) {
func (m *MqttValue) StringValue() (string, error) {
return string(*m), nil
}
func (m *MqttValue) DriveModeValue() (mode.DriveMode, error) {
func (m *MqttValue) DriveModeValue() (types.DriveMode, error) {
val, err := m.IntValue()
if err != nil {
return mode.DriveModeInvalid, err
return types.DriveModeInvalid, err
}
return mode.DriveMode(val), nil
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,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