mqtt: use new improved default namer to avoid API conflicts
Signed-off-by: deadprogram <ron@hybridgroup.com>
This commit is contained in:
parent
11e1a1376c
commit
5a418d6131
|
@ -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,
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
||||
|
|
|
@ -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)
|
||||
|
|
7
utils.go
7
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)))
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue