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" "crypto/x509"
"io/ioutil" "io/ioutil"
"gobot.io/x/gobot"
paho "github.com/eclipse/paho.mqtt.golang" paho "github.com/eclipse/paho.mqtt.golang"
multierror "github.com/hashicorp/go-multierror" 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 // NewAdaptor creates a new mqtt adaptor with specified host and client id
func NewAdaptor(host string, clientID string) *Adaptor { func NewAdaptor(host string, clientID string) *Adaptor {
return &Adaptor{ return &Adaptor{
name: "MQTT", name: gobot.DefaultName("MQTT"),
Host: host, Host: host,
autoReconnect: false, autoReconnect: false,
useSSL: false, useSSL: false,

View File

@ -22,13 +22,16 @@ type Driver struct {
// NewDriver returns a new Gobot MQTT Driver // NewDriver returns a new Gobot MQTT Driver
func NewDriver(a *Adaptor, topic string) *Driver { func NewDriver(a *Adaptor, topic string) *Driver {
m := &Driver{ m := &Driver{
name: "MQTT", name: gobot.DefaultName("MQTT"),
topic: topic, topic: topic,
connection: a, connection: a,
Eventer: gobot.NewEventer(), Eventer: gobot.NewEventer(),
Commander: gobot.NewCommander(), Commander: gobot.NewCommander(),
} }
m.AddEvent(Data)
m.AddEvent(Error)
return m return m
} }

View File

@ -1,6 +1,7 @@
package mqtt package mqtt
import ( import (
"strings"
"testing" "testing"
"gobot.io/x/gobot" "gobot.io/x/gobot"
@ -12,8 +13,8 @@ var _ gobot.Driver = (*Driver)(nil)
func TestMqttDriver(t *testing.T) { func TestMqttDriver(t *testing.T) {
d := NewDriver(initTestMqttAdaptor(), "/test/topic") d := NewDriver(initTestMqttAdaptor(), "/test/topic")
gobottest.Assert(t, d.Name(), "MQTT") gobottest.Assert(t, strings.HasPrefix(d.Name(), "MQTT"), true)
gobottest.Assert(t, d.Connection().Name(), "MQTT") gobottest.Assert(t, strings.HasPrefix(d.Connection().Name(), "MQTT"), true)
gobottest.Assert(t, d.Start(), nil) gobottest.Assert(t, d.Start(), nil)
gobottest.Assert(t, d.Halt(), nil) gobottest.Assert(t, d.Halt(), nil)

View File

@ -2,6 +2,7 @@ package gobot
import ( import (
"crypto/rand" "crypto/rand"
"fmt"
"math" "math"
"math/big" "math/big"
"time" "time"
@ -55,3 +56,9 @@ func ToScale(input, min, max float64) float64 {
return i 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)))
}