core: Refactor MQTT platform for new Adaptor creation signature
Signed-off-by: deadprogram <ron@hybridgroup.com>
This commit is contained in:
parent
e8028c4607
commit
17cc57d9d1
|
@ -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) {
|
||||
|
|
|
@ -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 != "" {
|
||||
|
|
|
@ -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) {
|
||||
|
|
Loading…
Reference in New Issue