mqtt: use new improved default namer to avoid API conflicts

Signed-off-by: deadprogram <ron@hybridgroup.com>
This commit is contained in:
deadprogram 2017-02-02 15:29:56 +01:00
parent 11e1a1376c
commit 5a418d6131
4 changed files with 17 additions and 4 deletions

View File

@ -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,

View File

@ -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
}

View File

@ -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)

View File

@ -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)))
}