[mqttdevice] Fix driveMode conversion and cyclic dependencies

This commit is contained in:
2019-12-18 21:08:46 +01:00
parent ef6edec672
commit a0dedb056b
5 changed files with 68 additions and 36 deletions

View File

@ -96,8 +96,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")
@ -130,6 +128,13 @@ func (m *MqttValue) Float64Value() (float64, error) {
func (m *MqttValue) StringValue() (string, error) {
return string(*m), nil
}
func (m *MqttValue) DriveModeValue() (mode.DriveMode, error) {
val, err := m.IntValue()
if err != nil {
return mode.DriveModeInvalid, err
}
return mode.DriveMode(val), nil
}
func (m *MqttValue) ByteSliceValue() ([]byte, error) {
return *m, nil
}

View File

@ -1,14 +1,15 @@
package mqttdevice
import (
"github.com/cyrilix/robocar-base/testtools"
"github.com/cyrilix/robocar-base/mode"
"github.com/cyrilix/robocar-base/testtools/docker"
mqtt "github.com/eclipse/paho.mqtt.golang"
"testing"
)
func TestIntegration(t *testing.T) {
ctx, mqttC, mqttUri := testtools.MqttContainer(t)
ctx, mqttC, mqttUri := docker.MqttContainer(t)
defer mqttC.Terminate(ctx)
t.Run("ConnectAndClose", func(t *testing.T) {
@ -187,3 +188,23 @@ func TestMqttValue_StringValue(t *testing.T) {
}
}
}
func TestMqttValue_DriveModeValue(t *testing.T) {
cases := []struct {
value MqttValue
expected mode.DriveMode
}{
{NewMqttValue(mode.DriveModeUser), mode.DriveModeUser},
{NewMqttValue(mode.DriveModePilot), mode.DriveModePilot},
{NewMqttValue(mode.DriveModeInvalid), mode.DriveModeInvalid},
}
for _, c := range cases {
val, err := c.value.DriveModeValue()
if err != nil {
t.Errorf("unexpected conversion error: %v", err)
}
if c.expected != val {
t.Errorf("MqttValue.DriveMode(): %v, wants %v", val, c.expected)
}
}
}