Merge pull request #457 from joek/dev

Added SetCleanSession (#451)
This commit is contained in:
Ron Evans 2017-10-10 17:32:40 +02:00 committed by GitHub
commit 631e1d1d9a
2 changed files with 17 additions and 0 deletions

View File

@ -26,6 +26,7 @@ type Adaptor struct {
clientCert string clientCert string
clientKey string clientKey string
autoReconnect bool autoReconnect bool
cleanSession bool
client paho.Client client paho.Client
} }
@ -35,6 +36,7 @@ func NewAdaptor(host string, clientID string) *Adaptor {
name: gobot.DefaultName("MQTT"), name: gobot.DefaultName("MQTT"),
Host: host, Host: host,
autoReconnect: false, autoReconnect: false,
cleanSession: true,
useSSL: false, useSSL: false,
clientID: clientID, clientID: clientID,
} }
@ -46,6 +48,7 @@ func NewAdaptorWithAuth(host, clientID, username, password string) *Adaptor {
name: "MQTT", name: "MQTT",
Host: host, Host: host,
autoReconnect: false, autoReconnect: false,
cleanSession: true,
useSSL: false, useSSL: false,
clientID: clientID, clientID: clientID,
username: username, username: username,
@ -68,6 +71,12 @@ func (a *Adaptor) AutoReconnect() bool { return a.autoReconnect }
// SetAutoReconnect sets the MQTT AutoReconnect setting // SetAutoReconnect sets the MQTT AutoReconnect setting
func (a *Adaptor) SetAutoReconnect(val bool) { a.autoReconnect = val } func (a *Adaptor) SetAutoReconnect(val bool) { a.autoReconnect = val }
// CleanSession returns the MQTT CleanSession setting
func (a *Adaptor) CleanSession() bool { return a.cleanSession }
// SetCleanSession sets the MQTT CleanSession setting. Should be false if reconnect is enabled. Otherwise all subscriptions will be lost
func (a *Adaptor) SetCleanSession(val bool) { a.cleanSession = val }
// UseSSL returns the MQTT server SSL preference // UseSSL returns the MQTT server SSL preference
func (a *Adaptor) UseSSL() bool { return a.useSSL } func (a *Adaptor) UseSSL() bool { return a.useSSL }
@ -145,6 +154,7 @@ func (a *Adaptor) createClientOptions() *paho.ClientOptions {
opts.SetUsername(a.username) opts.SetUsername(a.username)
} }
opts.AutoReconnect = a.autoReconnect opts.AutoReconnect = a.autoReconnect
opts.CleanSession = a.cleanSession
if a.UseSSL() { if a.UseSSL() {
opts.SetTLSConfig(a.newTLSConfig()) opts.SetTLSConfig(a.newTLSConfig())

View File

@ -36,6 +36,13 @@ func TestMqttAdaptorAutoReconnect(t *testing.T) {
gobottest.Assert(t, a.AutoReconnect(), true) gobottest.Assert(t, a.AutoReconnect(), true)
} }
func TestMqttAdaptorCleanSession(t *testing.T) {
a := initTestMqttAdaptor()
gobottest.Assert(t, a.CleanSession(), true)
a.SetCleanSession(false)
gobottest.Assert(t, a.CleanSession(), false)
}
func TestMqttAdaptorUseSSL(t *testing.T) { func TestMqttAdaptorUseSSL(t *testing.T) {
a := initTestMqttAdaptor() a := initTestMqttAdaptor()
gobottest.Assert(t, a.UseSSL(), false) gobottest.Assert(t, a.UseSSL(), false)