From a9c130ea402b609c2d0a5abe459fe628dda74cdf Mon Sep 17 00:00:00 2001 From: joek Date: Sun, 24 Sep 2017 13:45:47 +0200 Subject: [PATCH] Added SetCleanSession Signed-off-by: joek --- platforms/mqtt/mqtt_adaptor.go | 10 ++++++++++ platforms/mqtt/mqtt_adaptor_test.go | 7 +++++++ 2 files changed, 17 insertions(+) diff --git a/platforms/mqtt/mqtt_adaptor.go b/platforms/mqtt/mqtt_adaptor.go index faae4bd04..0fe2e8128 100644 --- a/platforms/mqtt/mqtt_adaptor.go +++ b/platforms/mqtt/mqtt_adaptor.go @@ -26,6 +26,7 @@ type Adaptor struct { clientCert string clientKey string autoReconnect bool + cleanSession bool client paho.Client } @@ -35,6 +36,7 @@ func NewAdaptor(host string, clientID string) *Adaptor { name: gobot.DefaultName("MQTT"), Host: host, autoReconnect: false, + cleanSession: true, useSSL: false, clientID: clientID, } @@ -46,6 +48,7 @@ func NewAdaptorWithAuth(host, clientID, username, password string) *Adaptor { name: "MQTT", Host: host, autoReconnect: false, + cleanSession: true, useSSL: false, clientID: clientID, username: username, @@ -68,6 +71,12 @@ func (a *Adaptor) AutoReconnect() bool { return a.autoReconnect } // SetAutoReconnect sets the MQTT AutoReconnect setting 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 func (a *Adaptor) UseSSL() bool { return a.useSSL } @@ -145,6 +154,7 @@ func (a *Adaptor) createClientOptions() *paho.ClientOptions { opts.SetUsername(a.username) } opts.AutoReconnect = a.autoReconnect + opts.CleanSession = a.cleanSession if a.UseSSL() { opts.SetTLSConfig(a.newTLSConfig()) diff --git a/platforms/mqtt/mqtt_adaptor_test.go b/platforms/mqtt/mqtt_adaptor_test.go index 653206b01..b764a4600 100644 --- a/platforms/mqtt/mqtt_adaptor_test.go +++ b/platforms/mqtt/mqtt_adaptor_test.go @@ -36,6 +36,13 @@ func TestMqttAdaptorAutoReconnect(t *testing.T) { 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) { a := initTestMqttAdaptor() gobottest.Assert(t, a.UseSSL(), false)