Allow setting QoS on MTT adaptor

Fixes #644
This commit is contained in:
Trevor Rosen 2018-12-17 11:06:34 -06:00 committed by Ron Evans
parent ae9bd2c997
commit 6c243af895
2 changed files with 12 additions and 2 deletions

View File

@ -28,6 +28,7 @@ type Adaptor struct {
autoReconnect bool autoReconnect bool
cleanSession bool cleanSession bool
client paho.Client client paho.Client
qos int
} }
// NewAdaptor creates a new mqtt adaptor with specified host and client id // NewAdaptor creates a new mqtt adaptor with specified host and client id
@ -86,6 +87,9 @@ func (a *Adaptor) SetUseSSL(val bool) { a.useSSL = val }
// ServerCert returns the MQTT server SSL cert file // ServerCert returns the MQTT server SSL cert file
func (a *Adaptor) ServerCert() string { return a.serverCert } 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 // SetServerCert sets the MQTT server SSL cert file
func (a *Adaptor) SetServerCert(val string) { a.serverCert = val } func (a *Adaptor) SetServerCert(val string) { a.serverCert = val }
@ -130,7 +134,7 @@ func (a *Adaptor) Publish(topic string, message []byte) bool {
if a.client == nil { if a.client == nil {
return false return false
} }
a.client.Publish(topic, 0, false, message) a.client.Publish(topic, byte(a.qos), false, message)
return true return true
} }
@ -139,7 +143,7 @@ func (a *Adaptor) On(event string, f func(msg Message)) bool {
if a.client == nil { if a.client == nil {
return false 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) f(msg)
}) })
return true return true

View File

@ -125,3 +125,9 @@ func TestMqttAdaptorOnWhenConnected(t *testing.T) {
fmt.Println("hola") fmt.Println("hola")
}), true) }), true)
} }
func TestMqttAdaptorQoS(t *testing.T) {
a := initTestMqttAdaptor()
a.SetQoS(1)
gobottest.Assert(t, 1, a.qos)
}