From 17cc57d9d1844a3fc97234fd8ae2304552cb9e12 Mon Sep 17 00:00:00 2001 From: deadprogram Date: Mon, 26 Sep 2016 10:13:37 +0200 Subject: [PATCH] core: Refactor MQTT platform for new Adaptor creation signature Signed-off-by: deadprogram --- platforms/mqtt/README.md | 2 +- platforms/mqtt/mqtt_adaptor.go | 41 +++++++++++++---------------- platforms/mqtt/mqtt_adaptor_test.go | 6 ++--- 3 files changed, 23 insertions(+), 26 deletions(-) diff --git a/platforms/mqtt/README.md b/platforms/mqtt/README.md index f84d0214..e5d7edd7 100644 --- a/platforms/mqtt/README.md +++ b/platforms/mqtt/README.md @@ -31,7 +31,7 @@ import ( func main() { 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() { mqttAdaptor.On("hello", func(data []byte) { diff --git a/platforms/mqtt/mqtt_adaptor.go b/platforms/mqtt/mqtt_adaptor.go index 6c13025b..1fd4b3d4 100644 --- a/platforms/mqtt/mqtt_adaptor.go +++ b/platforms/mqtt/mqtt_adaptor.go @@ -1,30 +1,26 @@ package mqtt -import ( - "git.eclipse.org/gitroot/paho/org.eclipse.paho.mqtt.golang.git" -) +import paho "github.com/eclipse/paho.mqtt.golang" -type MqttAdaptor struct { +type Adaptor struct { name string Host string clientID string username string password string - client *mqtt.Client + client paho.Client } -// NewMqttAdaptor creates a new mqtt adaptor with specified name, host and client id -func NewMqttAdaptor(name string, host string, clientID string) *MqttAdaptor { - return &MqttAdaptor{ - name: name, +// NewAdaptor creates a new mqtt adaptor with specified host and client id +func NewAdaptor(host string, clientID string) *Adaptor { + return &Adaptor{ Host: host, clientID: clientID, } } -func NewMqttAdaptorWithAuth(name, host, clientID, username, password string) *MqttAdaptor { - return &MqttAdaptor{ - name: name, +func NewAdaptorWithAuth(host, clientID, username, password string) *Adaptor { + return &Adaptor{ Host: host, clientID: clientID, 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 -func (a *MqttAdaptor) Connect() (errs []error) { - a.client = mqtt.NewClient(createClientOptions(a.clientID, a.Host, a.username, a.password)) +func (a *Adaptor) Connect() (errs []error) { + a.client = paho.NewClient(createClientOptions(a.clientID, a.Host, a.username, a.password)) if token := a.client.Connect(); token.Wait() && token.Error() != nil { errs = append(errs, token.Error()) } @@ -45,7 +42,7 @@ func (a *MqttAdaptor) Connect() (errs []error) { } // 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 { a.client.Disconnect(500) } @@ -53,13 +50,13 @@ func (a *MqttAdaptor) Disconnect() (err error) { } // Finalize returns true if connection to mqtt is finalized successfully -func (a *MqttAdaptor) Finalize() (errs []error) { +func (a *Adaptor) Finalize() (errs []error) { a.Disconnect() return } // 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 { 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 -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 { 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()) }) return true } -func createClientOptions(clientId, raw, username, password string) *mqtt.ClientOptions { - opts := mqtt.NewClientOptions() +func createClientOptions(clientId, raw, username, password string) *paho.ClientOptions { + opts := paho.NewClientOptions() opts.AddBroker(raw) opts.SetClientID(clientId) if username != "" && password != "" { diff --git a/platforms/mqtt/mqtt_adaptor_test.go b/platforms/mqtt/mqtt_adaptor_test.go index 24d5dfa6..5e275f9d 100644 --- a/platforms/mqtt/mqtt_adaptor_test.go +++ b/platforms/mqtt/mqtt_adaptor_test.go @@ -8,10 +8,10 @@ import ( "github.com/hybridgroup/gobot/gobottest" ) -var _ gobot.Adaptor = (*MqttAdaptor)(nil) +var _ gobot.Adaptor = (*Adaptor)(nil) -func initTestMqttAdaptor() *MqttAdaptor { - return NewMqttAdaptor("mqtt", "localhost:1883", "client") +func initTestMqttAdaptor() *Adaptor { + return NewAdaptor("localhost:1883", "client") } func TestMqttAdaptorConnect(t *testing.T) {