2014-11-03 18:30:56 -08:00
|
|
|
package mqtt
|
|
|
|
|
2016-11-07 19:52:28 +01:00
|
|
|
import (
|
|
|
|
paho "github.com/eclipse/paho.mqtt.golang"
|
|
|
|
multierror "github.com/hashicorp/go-multierror"
|
|
|
|
)
|
2014-11-03 18:30:56 -08:00
|
|
|
|
2016-11-26 18:02:17 +01:00
|
|
|
// Adaptor is the Gobot Adaptor for MQTT
|
2016-09-26 10:13:37 +02:00
|
|
|
type Adaptor struct {
|
2017-01-26 16:21:28 +01:00
|
|
|
name string
|
|
|
|
Host string
|
|
|
|
clientID string
|
|
|
|
username string
|
|
|
|
password string
|
|
|
|
autoReconnect bool
|
|
|
|
client paho.Client
|
2014-11-03 18:30:56 -08:00
|
|
|
}
|
|
|
|
|
2016-09-26 10:13:37 +02:00
|
|
|
// NewAdaptor creates a new mqtt adaptor with specified host and client id
|
|
|
|
func NewAdaptor(host string, clientID string) *Adaptor {
|
|
|
|
return &Adaptor{
|
2017-01-26 16:21:28 +01:00
|
|
|
name: "MQTT",
|
|
|
|
Host: host,
|
|
|
|
autoReconnect: false,
|
|
|
|
clientID: clientID,
|
2014-11-03 18:56:33 -08:00
|
|
|
}
|
2014-11-03 18:30:56 -08:00
|
|
|
}
|
2016-03-23 17:58:13 +03:00
|
|
|
|
2016-11-26 18:02:17 +01:00
|
|
|
// NewAdaptorWithAuth creates a new mqtt adaptor with specified host, client id, username, and password.
|
2016-09-26 10:13:37 +02:00
|
|
|
func NewAdaptorWithAuth(host, clientID, username, password string) *Adaptor {
|
|
|
|
return &Adaptor{
|
2017-01-26 16:21:28 +01:00
|
|
|
name: "MQTT",
|
|
|
|
Host: host,
|
|
|
|
autoReconnect: false,
|
|
|
|
clientID: clientID,
|
|
|
|
username: username,
|
|
|
|
password: password,
|
2016-03-23 17:58:13 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-26 18:02:17 +01:00
|
|
|
// Name returns the MQTT Adaptor's name
|
|
|
|
func (a *Adaptor) Name() string { return a.name }
|
|
|
|
|
|
|
|
// SetName sets the MQTT Adaptor's name
|
2016-09-26 10:13:37 +02:00
|
|
|
func (a *Adaptor) SetName(n string) { a.name = n }
|
2014-11-03 18:30:56 -08:00
|
|
|
|
2017-01-25 20:19:15 +01:00
|
|
|
// Port returns the Host name
|
|
|
|
func (a *Adaptor) Port() string { return a.Host }
|
|
|
|
|
2017-01-26 16:21:28 +01:00
|
|
|
// AutoReconnect returns the MQTT AutoReconnect setting
|
|
|
|
func (a *Adaptor) AutoReconnect() bool { return a.autoReconnect }
|
|
|
|
|
|
|
|
// SetAutoReconnect sets the MQTT AutoReconnect setting
|
|
|
|
func (a *Adaptor) SetAutoReconnect(val bool) { a.autoReconnect = val }
|
|
|
|
|
2014-11-03 21:34:46 -08:00
|
|
|
// Connect returns true if connection to mqtt is established
|
2016-11-07 19:52:28 +01:00
|
|
|
func (a *Adaptor) Connect() (err error) {
|
2017-01-26 16:21:28 +01:00
|
|
|
a.client = paho.NewClient(a.createClientOptions())
|
2015-04-06 15:35:11 -07:00
|
|
|
if token := a.client.Connect(); token.Wait() && token.Error() != nil {
|
2016-11-07 19:52:28 +01:00
|
|
|
err = multierror.Append(err, token.Error())
|
2015-04-06 15:35:11 -07:00
|
|
|
}
|
2016-02-08 15:20:25 -08:00
|
|
|
|
2014-11-19 23:21:19 -08:00
|
|
|
return
|
2014-11-03 18:30:56 -08:00
|
|
|
}
|
|
|
|
|
2014-11-03 21:34:46 -08:00
|
|
|
// Disconnect returns true if connection to mqtt is closed
|
2016-09-26 10:13:37 +02:00
|
|
|
func (a *Adaptor) Disconnect() (err error) {
|
2014-11-03 21:34:46 -08:00
|
|
|
if a.client != nil {
|
|
|
|
a.client.Disconnect(500)
|
2014-11-03 19:47:41 -08:00
|
|
|
}
|
2014-11-19 23:21:19 -08:00
|
|
|
return
|
2014-11-03 18:30:56 -08:00
|
|
|
}
|
|
|
|
|
2016-07-13 10:44:47 -06:00
|
|
|
// Finalize returns true if connection to mqtt is finalized successfully
|
2016-11-07 19:52:28 +01:00
|
|
|
func (a *Adaptor) Finalize() (err error) {
|
2014-11-03 19:47:41 -08:00
|
|
|
a.Disconnect()
|
2014-11-19 23:21:19 -08:00
|
|
|
return
|
2014-11-03 18:30:56 -08:00
|
|
|
}
|
|
|
|
|
2014-11-04 08:41:56 -08:00
|
|
|
// Publish a message under a specific topic
|
2016-09-26 10:13:37 +02:00
|
|
|
func (a *Adaptor) Publish(topic string, message []byte) bool {
|
2014-11-04 08:33:05 -08:00
|
|
|
if a.client == nil {
|
|
|
|
return false
|
|
|
|
}
|
2015-04-06 15:35:11 -07:00
|
|
|
a.client.Publish(topic, 0, false, message)
|
2014-11-04 08:33:05 -08:00
|
|
|
return true
|
2014-11-03 18:30:56 -08:00
|
|
|
}
|
|
|
|
|
2016-11-26 18:02:17 +01:00
|
|
|
// On subscribes to a topic, and then calls the message handler function when data is received
|
2016-09-26 10:13:37 +02:00
|
|
|
func (a *Adaptor) On(event string, f func(s []byte)) bool {
|
2014-11-04 08:33:05 -08:00
|
|
|
if a.client == nil {
|
|
|
|
return false
|
|
|
|
}
|
2016-09-26 10:13:37 +02:00
|
|
|
a.client.Subscribe(event, 0, func(client paho.Client, msg paho.Message) {
|
2014-11-03 22:54:33 -08:00
|
|
|
f(msg.Payload())
|
2015-04-06 15:35:11 -07:00
|
|
|
})
|
2014-11-04 08:33:05 -08:00
|
|
|
return true
|
2014-11-03 18:56:33 -08:00
|
|
|
}
|
|
|
|
|
2017-01-26 16:21:28 +01:00
|
|
|
func (a *Adaptor) createClientOptions() *paho.ClientOptions {
|
2016-09-26 10:13:37 +02:00
|
|
|
opts := paho.NewClientOptions()
|
2017-01-26 16:21:28 +01:00
|
|
|
opts.AddBroker(a.Host)
|
|
|
|
opts.SetClientID(a.clientID)
|
|
|
|
if a.username != "" && a.password != "" {
|
|
|
|
opts.SetPassword(a.password)
|
|
|
|
opts.SetUsername(a.username)
|
2016-03-23 17:58:13 +03:00
|
|
|
}
|
2017-01-26 16:21:28 +01:00
|
|
|
opts.AutoReconnect = a.autoReconnect
|
2014-11-03 18:56:33 -08:00
|
|
|
return opts
|
2014-11-03 18:30:56 -08:00
|
|
|
}
|