Skip to content

Commit

Permalink
Merge pull request hybridgroup#457 from joek/dev
Browse files Browse the repository at this point in the history
Added SetCleanSession (hybridgroup#451)
  • Loading branch information
deadprogram authored Oct 10, 2017
2 parents a998b0b + a9c130e commit 631e1d1
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
10 changes: 10 additions & 0 deletions platforms/mqtt/mqtt_adaptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ type Adaptor struct {
clientCert string
clientKey string
autoReconnect bool
cleanSession bool
client paho.Client
}

Expand All @@ -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,
}
Expand All @@ -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,
Expand All @@ -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 }

Expand Down Expand Up @@ -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())
Expand Down
7 changes: 7 additions & 0 deletions platforms/mqtt/mqtt_adaptor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 631e1d1

Please sign in to comment.