diff --git a/platforms/mqtt/mqtt_adaptor.go b/platforms/mqtt/mqtt_adaptor.go index 19d9d39f..604c022d 100644 --- a/platforms/mqtt/mqtt_adaptor.go +++ b/platforms/mqtt/mqtt_adaptor.go @@ -5,6 +5,8 @@ import ( "crypto/x509" "io/ioutil" + "gobot.io/x/gobot" + paho "github.com/eclipse/paho.mqtt.golang" multierror "github.com/hashicorp/go-multierror" ) @@ -27,7 +29,7 @@ type Adaptor struct { // NewAdaptor creates a new mqtt adaptor with specified host and client id func NewAdaptor(host string, clientID string) *Adaptor { return &Adaptor{ - name: "MQTT", + name: gobot.DefaultName("MQTT"), Host: host, autoReconnect: false, useSSL: false, diff --git a/platforms/mqtt/mqtt_driver.go b/platforms/mqtt/mqtt_driver.go index 2389921c..5eab88d4 100644 --- a/platforms/mqtt/mqtt_driver.go +++ b/platforms/mqtt/mqtt_driver.go @@ -22,13 +22,16 @@ type Driver struct { // NewDriver returns a new Gobot MQTT Driver func NewDriver(a *Adaptor, topic string) *Driver { m := &Driver{ - name: "MQTT", + name: gobot.DefaultName("MQTT"), topic: topic, connection: a, Eventer: gobot.NewEventer(), Commander: gobot.NewCommander(), } + m.AddEvent(Data) + m.AddEvent(Error) + return m } diff --git a/platforms/mqtt/mqtt_driver_test.go b/platforms/mqtt/mqtt_driver_test.go index 27d07e30..e118f336 100644 --- a/platforms/mqtt/mqtt_driver_test.go +++ b/platforms/mqtt/mqtt_driver_test.go @@ -1,6 +1,7 @@ package mqtt import ( + "strings" "testing" "gobot.io/x/gobot" @@ -12,8 +13,8 @@ var _ gobot.Driver = (*Driver)(nil) func TestMqttDriver(t *testing.T) { d := NewDriver(initTestMqttAdaptor(), "/test/topic") - gobottest.Assert(t, d.Name(), "MQTT") - gobottest.Assert(t, d.Connection().Name(), "MQTT") + gobottest.Assert(t, strings.HasPrefix(d.Name(), "MQTT"), true) + gobottest.Assert(t, strings.HasPrefix(d.Connection().Name(), "MQTT"), true) gobottest.Assert(t, d.Start(), nil) gobottest.Assert(t, d.Halt(), nil) diff --git a/utils.go b/utils.go index 19729a40..d5830a46 100644 --- a/utils.go +++ b/utils.go @@ -2,6 +2,7 @@ package gobot import ( "crypto/rand" + "fmt" "math" "math/big" "time" @@ -55,3 +56,9 @@ func ToScale(input, min, max float64) float64 { return i } } + +// DefaultName returns a sensible random default name +// for a robot, adaptor or driver +func DefaultName(name string) string { + return fmt.Sprintf("%s-%X", name, Rand(int(^uint(0)>>1))) +}