Skip to content

Commit

Permalink
Allow setting QoS on MTT adaptor
Browse files Browse the repository at this point in the history
  • Loading branch information
trevrosen authored and deadprogram committed May 22, 2019
1 parent ae9bd2c commit 6c243af
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
8 changes: 6 additions & 2 deletions platforms/mqtt/mqtt_adaptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ type Adaptor struct {
autoReconnect bool
cleanSession bool
client paho.Client
qos int
}

// NewAdaptor creates a new mqtt adaptor with specified host and client id
Expand Down Expand Up @@ -86,6 +87,9 @@ func (a *Adaptor) SetUseSSL(val bool) { a.useSSL = val }
// ServerCert returns the MQTT server SSL cert file
func (a *Adaptor) ServerCert() string { return a.serverCert }

// SetQoS sets the QoS value passed into the MTT client on Publish/Subscribe events
func (a *Adaptor) SetQoS(qos int) { a.qos = qos }

// SetServerCert sets the MQTT server SSL cert file
func (a *Adaptor) SetServerCert(val string) { a.serverCert = val }

Expand Down Expand Up @@ -130,7 +134,7 @@ func (a *Adaptor) Publish(topic string, message []byte) bool {
if a.client == nil {
return false
}
a.client.Publish(topic, 0, false, message)
a.client.Publish(topic, byte(a.qos), false, message)
return true
}

Expand All @@ -139,7 +143,7 @@ func (a *Adaptor) On(event string, f func(msg Message)) bool {
if a.client == nil {
return false
}
a.client.Subscribe(event, 0, func(client paho.Client, msg paho.Message) {
a.client.Subscribe(event, byte(a.qos), func(client paho.Client, msg paho.Message) {
f(msg)
})
return true
Expand Down
6 changes: 6 additions & 0 deletions platforms/mqtt/mqtt_adaptor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,3 +125,9 @@ func TestMqttAdaptorOnWhenConnected(t *testing.T) {
fmt.Println("hola")
}), true)
}

func TestMqttAdaptorQoS(t *testing.T) {
a := initTestMqttAdaptor()
a.SetQoS(1)
gobottest.Assert(t, 1, a.qos)
}

0 comments on commit 6c243af

Please sign in to comment.