core: Refactor MQTT platform for new Adaptor creation signature

Signed-off-by: deadprogram <ron@hybridgroup.com>
This commit is contained in:
deadprogram 2016-09-26 10:13:37 +02:00
parent e8028c4607
commit 17cc57d9d1
3 changed files with 23 additions and 26 deletions

View File

@ -31,7 +31,7 @@ import (
func main() { func main() {
gbot := gobot.NewGobot() gbot := gobot.NewGobot()
mqttAdaptor := mqtt.NewMqttAdaptor("server", "tcp://0.0.0.0:1883", "pinger") mqttAdaptor := mqtt.NewAdaptor("tcp://0.0.0.0:1883", "pinger")
work := func() { work := func() {
mqttAdaptor.On("hello", func(data []byte) { mqttAdaptor.On("hello", func(data []byte) {

View File

@ -1,30 +1,26 @@
package mqtt package mqtt
import ( import paho "github.com/eclipse/paho.mqtt.golang"
"git.eclipse.org/gitroot/paho/org.eclipse.paho.mqtt.golang.git"
)
type MqttAdaptor struct { type Adaptor struct {
name string name string
Host string Host string
clientID string clientID string
username string username string
password string password string
client *mqtt.Client client paho.Client
} }
// NewMqttAdaptor creates a new mqtt adaptor with specified name, host and client id // NewAdaptor creates a new mqtt adaptor with specified host and client id
func NewMqttAdaptor(name string, host string, clientID string) *MqttAdaptor { func NewAdaptor(host string, clientID string) *Adaptor {
return &MqttAdaptor{ return &Adaptor{
name: name,
Host: host, Host: host,
clientID: clientID, clientID: clientID,
} }
} }
func NewMqttAdaptorWithAuth(name, host, clientID, username, password string) *MqttAdaptor { func NewAdaptorWithAuth(host, clientID, username, password string) *Adaptor {
return &MqttAdaptor{ return &Adaptor{
name: name,
Host: host, Host: host,
clientID: clientID, clientID: clientID,
username: username, username: username,
@ -32,11 +28,12 @@ func NewMqttAdaptorWithAuth(name, host, clientID, username, password string) *Mq
} }
} }
func (a *MqttAdaptor) Name() string { return a.name } func (a *Adaptor) Name() string { return a.name }
func (a *Adaptor) SetName(n string) { a.name = n }
// Connect returns true if connection to mqtt is established // Connect returns true if connection to mqtt is established
func (a *MqttAdaptor) Connect() (errs []error) { func (a *Adaptor) Connect() (errs []error) {
a.client = mqtt.NewClient(createClientOptions(a.clientID, a.Host, a.username, a.password)) a.client = paho.NewClient(createClientOptions(a.clientID, a.Host, a.username, a.password))
if token := a.client.Connect(); token.Wait() && token.Error() != nil { if token := a.client.Connect(); token.Wait() && token.Error() != nil {
errs = append(errs, token.Error()) errs = append(errs, token.Error())
} }
@ -45,7 +42,7 @@ func (a *MqttAdaptor) Connect() (errs []error) {
} }
// Disconnect returns true if connection to mqtt is closed // Disconnect returns true if connection to mqtt is closed
func (a *MqttAdaptor) Disconnect() (err error) { func (a *Adaptor) Disconnect() (err error) {
if a.client != nil { if a.client != nil {
a.client.Disconnect(500) a.client.Disconnect(500)
} }
@ -53,13 +50,13 @@ func (a *MqttAdaptor) Disconnect() (err error) {
} }
// Finalize returns true if connection to mqtt is finalized successfully // Finalize returns true if connection to mqtt is finalized successfully
func (a *MqttAdaptor) Finalize() (errs []error) { func (a *Adaptor) Finalize() (errs []error) {
a.Disconnect() a.Disconnect()
return return
} }
// Publish a message under a specific topic // Publish a message under a specific topic
func (a *MqttAdaptor) Publish(topic string, message []byte) bool { func (a *Adaptor) Publish(topic string, message []byte) bool {
if a.client == nil { if a.client == nil {
return false return false
} }
@ -68,18 +65,18 @@ func (a *MqttAdaptor) Publish(topic string, message []byte) bool {
} }
// Subscribe to a topic, and then call the message handler function when data is received // Subscribe to a topic, and then call the message handler function when data is received
func (a *MqttAdaptor) On(event string, f func(s []byte)) bool { func (a *Adaptor) On(event string, f func(s []byte)) bool {
if a.client == nil { if a.client == nil {
return false return false
} }
a.client.Subscribe(event, 0, func(client *mqtt.Client, msg mqtt.Message) { a.client.Subscribe(event, 0, func(client paho.Client, msg paho.Message) {
f(msg.Payload()) f(msg.Payload())
}) })
return true return true
} }
func createClientOptions(clientId, raw, username, password string) *mqtt.ClientOptions { func createClientOptions(clientId, raw, username, password string) *paho.ClientOptions {
opts := mqtt.NewClientOptions() opts := paho.NewClientOptions()
opts.AddBroker(raw) opts.AddBroker(raw)
opts.SetClientID(clientId) opts.SetClientID(clientId)
if username != "" && password != "" { if username != "" && password != "" {

View File

@ -8,10 +8,10 @@ import (
"github.com/hybridgroup/gobot/gobottest" "github.com/hybridgroup/gobot/gobottest"
) )
var _ gobot.Adaptor = (*MqttAdaptor)(nil) var _ gobot.Adaptor = (*Adaptor)(nil)
func initTestMqttAdaptor() *MqttAdaptor { func initTestMqttAdaptor() *Adaptor {
return NewMqttAdaptor("mqtt", "localhost:1883", "client") return NewAdaptor("localhost:1883", "client")
} }
func TestMqttAdaptorConnect(t *testing.T) { func TestMqttAdaptorConnect(t *testing.T) {