From 71f062b6a1e78e7bcfa623bfc06b49b44bdeac39 Mon Sep 17 00:00:00 2001 From: Matthew Treinish Date: Tue, 2 Aug 2016 13:17:32 -0400 Subject: [PATCH] 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 --- README.rst | 3 +++ etc/germqtt.conf | 1 + germqtt/germqtt.py | 16 ++++++++++++---- 3 files changed, 16 insertions(+), 4 deletions(-) diff --git a/README.rst b/README.rst index 01ba299..3b0739f 100644 --- a/README.rst +++ b/README.rst @@ -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 -------------- diff --git a/etc/germqtt.conf b/etc/germqtt.conf index 9342cef..23e6a1b 100644 --- a/etc/germqtt.conf +++ b/etc/germqtt.conf @@ -9,3 +9,4 @@ key=/home/computertreker/.ssh/id_rsa [mqtt] hostname=localhost topic=gerrit +qos = 2 diff --git a/germqtt/germqtt.py b/germqtt/germqtt.py index 7da7435..3d7de59 100644 --- a/germqtt/germqtt.py +++ b/germqtt/germqtt.py @@ -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: