Add config option to set QOS of published messages

This commit adds a new config option to germqtt to set the QOS level
for messages published. By default it'll use 0, but if operating in
an environment where more guarantees are needed on delivery you can
set this to be higher now.

Change-Id: I7bf07921ce2f94a9a34f468294a1a5f1da2673af
This commit is contained in:
Matthew Treinish 2016-08-02 13:17:32 -04:00
parent 5c2a34d237
commit 71f062b6a1
No known key found for this signature in database
GPG Key ID: FD12A0F214C9E177
3 changed files with 16 additions and 4 deletions

View File

@ -70,6 +70,9 @@ can set:
with.
* **password** - Used to set the auth password to connect to the MQTT broker
with. A username must be set for this option to be used.
* **qos** - Used to set the QOS level for the messages published by germqtt.
For more information on the different QOS levels refer to:
https://mosquitto.org/man/mqtt-7.html
Other Settings
--------------

View File

@ -9,3 +9,4 @@ key=/home/computertreker/.ssh/id_rsa
[mqtt]
hostname=localhost
topic=gerrit
qos = 2

View File

@ -50,7 +50,7 @@ class GerritStream(object):
class PushMQTT(object):
def __init__(self, hostname, port=1883, client_id=None,
keepalive=60, will=None, auth=None, tls=None):
keepalive=60, will=None, auth=None, tls=None, qos=0):
self.hostname = hostname
self.port = port
self.client_id = client_id
@ -58,18 +58,19 @@ class PushMQTT(object):
self.will = will
self.auth = auth
self.tls = tls
self.qos = qos
def publish_single(self, topic, msg):
publish.single(topic, msg, hostname=self.hostname,
port=self.port, client_id=self.client_id,
keepalive=self.keepalive, will=self.will,
auth=self.auth, tls=self.tls)
auth=self.auth, tls=self.tls, qos=self.qos)
def publish_multiple(self, topic, msg):
publish.multiple(topic, msg, hostname=self.hostname,
port=self.port, client_id=self.client_id,
keepalive=self.keepalive, will=self.will,
auth=self.auth, tls=self.tls)
auth=self.auth, tls=self.tls, qos=self.qos)
def get_options():
@ -131,11 +132,18 @@ def _main(args, config):
if mqtt_password:
auth['password'] = mqtt_password
# QOS setting
if config.has_option('mqtt', 'qos'):
mqtt_qos = config.getint('mqtt', 'qos')
else:
mqtt_qos = 0
mqttqueue = PushMQTT(
config.get('mqtt', 'hostname'),
port=mqtt_port,
keepalive=keepalive,
auth=auth)
auth=auth,
qos=mqtt_qos)
base_topic = config.get('mqtt', 'topic')
while True: