2014-11-04 10:30:56 +08:00
|
|
|
package mqtt
|
|
|
|
|
2016-11-08 02:52:28 +08:00
|
|
|
import (
|
2017-01-27 02:03:32 +08:00
|
|
|
"crypto/tls"
|
|
|
|
"crypto/x509"
|
|
|
|
"io/ioutil"
|
|
|
|
|
2017-02-02 22:29:56 +08:00
|
|
|
"gobot.io/x/gobot"
|
|
|
|
|
2016-11-08 02:52:28 +08:00
|
|
|
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 {
|
2017-01-26 23:21:28 +08:00
|
|
|
name string
|
|
|
|
Host string
|
|
|
|
clientID string
|
|
|
|
username string
|
|
|
|
password string
|
2017-01-27 02:14:08 +08:00
|
|
|
useSSL bool
|
2017-01-27 02:03:32 +08:00
|
|
|
serverCert string
|
|
|
|
clientCert string
|
|
|
|
clientKey string
|
2017-01-26 23:21:28 +08:00
|
|
|
autoReconnect bool
|
|
|
|
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{
|
2017-02-02 22:29:56 +08:00
|
|
|
name: gobot.DefaultName("MQTT"),
|
2017-01-26 23:21:28 +08:00
|
|
|
Host: host,
|
|
|
|
autoReconnect: false,
|
2017-01-27 02:14:08 +08:00
|
|
|
useSSL: false,
|
2017-01-26 23:21:28 +08:00
|
|
|
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{
|
2017-01-26 23:21:28 +08:00
|
|
|
name: "MQTT",
|
|
|
|
Host: host,
|
|
|
|
autoReconnect: false,
|
2017-01-27 02:14:08 +08:00
|
|
|
useSSL: false,
|
2017-01-26 23:21:28 +08:00
|
|
|
clientID: clientID,
|
|
|
|
username: username,
|
|
|
|
password: password,
|
2016-03-23 22:58:13 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2017-01-26 03:19:15 +08:00
|
|
|
// Port returns the Host name
|
|
|
|
func (a *Adaptor) Port() string { return a.Host }
|
|
|
|
|
2017-01-26 23:21:28 +08: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 }
|
|
|
|
|
2017-01-27 02:14:08 +08:00
|
|
|
// UseSSL returns the MQTT server SSL preference
|
|
|
|
func (a *Adaptor) UseSSL() bool { return a.useSSL }
|
|
|
|
|
|
|
|
// SetUseSSL sets the MQTT server SSL preference
|
|
|
|
func (a *Adaptor) SetUseSSL(val bool) { a.useSSL = val }
|
|
|
|
|
2017-01-27 02:03:32 +08:00
|
|
|
// ServerCert returns the MQTT server SSL cert file
|
|
|
|
func (a *Adaptor) ServerCert() string { return a.serverCert }
|
|
|
|
|
|
|
|
// SetServerCert sets the MQTT server SSL cert file
|
|
|
|
func (a *Adaptor) SetServerCert(val string) { a.serverCert = val }
|
|
|
|
|
|
|
|
// ClientCert returns the MQTT client SSL cert file
|
|
|
|
func (a *Adaptor) ClientCert() string { return a.clientCert }
|
|
|
|
|
|
|
|
// SetClientCert sets the MQTT server SSL cert file
|
|
|
|
func (a *Adaptor) SetClientCert(val string) { a.clientCert = val }
|
|
|
|
|
|
|
|
// ClientKey returns the MQTT client SSL key file
|
|
|
|
func (a *Adaptor) ClientKey() string { return a.clientKey }
|
|
|
|
|
|
|
|
// SetClientCert sets the MQTT server SSL key file
|
|
|
|
func (a *Adaptor) SetClientKey(val string) { a.clientKey = val }
|
|
|
|
|
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) {
|
2017-01-26 23:21:28 +08:00
|
|
|
a.client = paho.NewClient(a.createClientOptions())
|
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
|
|
|
}
|
|
|
|
|
2017-01-26 23:21:28 +08:00
|
|
|
func (a *Adaptor) createClientOptions() *paho.ClientOptions {
|
2016-09-26 16:13:37 +08:00
|
|
|
opts := paho.NewClientOptions()
|
2017-01-26 23:21:28 +08: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 22:58:13 +08:00
|
|
|
}
|
2017-01-26 23:21:28 +08:00
|
|
|
opts.AutoReconnect = a.autoReconnect
|
2017-01-27 02:03:32 +08:00
|
|
|
|
2017-01-27 02:14:08 +08:00
|
|
|
if a.UseSSL() {
|
2017-01-27 02:03:32 +08:00
|
|
|
opts.SetTLSConfig(a.newTLSConfig())
|
|
|
|
}
|
2014-11-04 10:56:33 +08:00
|
|
|
return opts
|
2014-11-04 10:30:56 +08:00
|
|
|
}
|
2017-01-27 02:03:32 +08:00
|
|
|
|
|
|
|
// newTLSConfig sets the TLS config in the case that we are using
|
|
|
|
// an MQTT broker with TLS
|
|
|
|
func (a *Adaptor) newTLSConfig() *tls.Config {
|
|
|
|
// Import server certificate
|
|
|
|
var certpool *x509.CertPool
|
|
|
|
if len(a.ServerCert()) > 0 {
|
|
|
|
certpool = x509.NewCertPool()
|
|
|
|
pemCerts, err := ioutil.ReadFile(a.ServerCert())
|
|
|
|
if err == nil {
|
|
|
|
certpool.AppendCertsFromPEM(pemCerts)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Import client certificate/key pair
|
|
|
|
var certs []tls.Certificate
|
|
|
|
if len(a.ClientCert()) > 0 && len(a.ClientKey()) > 0 {
|
|
|
|
cert, err := tls.LoadX509KeyPair(a.ClientCert(), a.ClientKey())
|
|
|
|
if err != nil {
|
|
|
|
// TODO: proper error handling
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
certs = append(certs, cert)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create tls.Config with desired tls properties
|
|
|
|
return &tls.Config{
|
|
|
|
// RootCAs = certs used to verify server cert.
|
|
|
|
RootCAs: certpool,
|
|
|
|
// ClientAuth = whether to request cert from server.
|
|
|
|
// Since the server is set up for SSL, this happens
|
|
|
|
// anyways.
|
|
|
|
ClientAuth: tls.NoClientCert,
|
|
|
|
// ClientCAs = certs used to validate client cert.
|
|
|
|
ClientCAs: nil,
|
|
|
|
// InsecureSkipVerify = verify that cert contents
|
|
|
|
// match server. IP matches what is in cert etc.
|
|
|
|
InsecureSkipVerify: true,
|
|
|
|
// Certificates = list of certs client sends to server.
|
|
|
|
Certificates: certs,
|
|
|
|
}
|
|
|
|
}
|