diff --git a/bot.py b/bot.py index dea9f1b..b3aae7b 100755 --- a/bot.py +++ b/bot.py @@ -98,13 +98,21 @@ def on_message(mosq, obj, msg): mqttc = mqtt.Client(config['mqtt']['name']) -mqttc.will_set("system/%s/state" % config['mqtt']['name'], payload='offline', qos=2, retain=True) -mqttc.connect(config['mqtt']['server']) -mqttc.subscribe("door/outer/#") -mqttc.subscribe("bot/outgoing") -mqttc.subscribe("system/alfred_outer/state") -mqttc.on_message = on_message -mqttc.publish("system/%s/state" % config['mqtt']['name'], payload='online', qos=2, retain=True) -send_to_bot("I was restarted for some reason!") - -mqttc.loop_forever() +send_to_bot("Bot was restarted for some reason 😕") + +while True: + mqttc.will_set("system/%s/state" % config['mqtt']['name'], payload='offline', qos=2, retain=True) + mqttc.connect(config['mqtt']['server']) + mqttc.subscribe("door/outer/#") + mqttc.subscribe("bot/outgoing") + mqttc.subscribe("system/alfred_outer/state") + mqttc.on_message = on_message + mqttc.publish("system/%s/state" % config['mqtt']['name'], payload='online', qos=2, retain=True) + + while mqttc.loop(0.1) == 0: + pass + + send_to_bot("Bot MQTT connection lost 😠... I'll just restart myself...") + mqttc.disconnect() + time.sleep(5) +