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