From 128abcb78c7072b09ad246ca49bc398119ed513a Mon Sep 17 00:00:00 2001 From: Andrew Wilkinson Date: Mon, 7 Nov 2022 11:27:52 +0000 Subject: [PATCH] fix: Reconnect if MQTT server is not available at start up. --- glowprom/mqtt.py | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/glowprom/mqtt.py b/glowprom/mqtt.py index 4ab7d3e..8220198 100644 --- a/glowprom/mqtt.py +++ b/glowprom/mqtt.py @@ -15,6 +15,7 @@ # along with this program. If not, see . import functools +import time import paho.mqtt.client as mqtt @@ -25,12 +26,22 @@ def on_connect(topic, client, userdata, flags, rc): client.subscribe(topic) -def connect(args, on_message): +def connect(args, on_message, retry=True): client = mqtt.Client() client.on_connect = functools.partial(on_connect, args.topic) client.on_message = on_message client.username_pw_set(args.user, password=args.passwd) - client.connect("glowmqtt.energyhive.com", 1883, 60) + + while True: + try: + client.connect(args.mqtt, args.port, 60) + except Exception as e: + if not retry: + raise + print(e) + time.sleep(10) + else: + break client.loop_forever()