From adfc000b6cd0a246746e7e74fb2829062c472905 Mon Sep 17 00:00:00 2001 From: Masoko Date: Sun, 11 Feb 2024 14:02:52 +0200 Subject: [PATCH] Hotfix (#79) * fixing update again --- src/rpi-cpu2mqtt.py | 41 ++++++++++++++++++++++++++++------------- 1 file changed, 28 insertions(+), 13 deletions(-) diff --git a/src/rpi-cpu2mqtt.py b/src/rpi-cpu2mqtt.py index 3c74db1..f285d33 100644 --- a/src/rpi-cpu2mqtt.py +++ b/src/rpi-cpu2mqtt.py @@ -339,7 +339,7 @@ def on_connect(client, userdata, flags, rc): def create_mqtt_client(): - client = paho.Client(client_id="rpi-mqtt-monitor-" + hostname) + client = paho.Client(client_id="rpi-mqtt-monitor-" + hostname + str(int(time.time()))) client.username_pw_set(config.mqtt_user, config.mqtt_password) client.on_log = on_log client.on_connect = on_connect @@ -355,7 +355,7 @@ def publish_update_status_to_mqtt(git_update): client = create_mqtt_client() if client is None: - return + print("Error: Unable to connect to MQTT broker") client.loop_start() if config.git_update: @@ -367,7 +367,13 @@ def publish_update_status_to_mqtt(git_update): if config.update: if config.discovery_messages: client.publish("homeassistant/update/" + hostname + "/config", - config_json('update'), qos=config.qos) + config_json('update'), qos=1) + + # Wait for all messages to be delivered + while len(client._out_messages) > 0: + time.sleep(0.1) + client.loop() + client.loop_stop() client.disconnect() @@ -446,6 +452,9 @@ def publish_to_mqtt(cpu_load=0, cpu_temp=0, used_space=0, voltage=0, sys_clock_s if config.discovery_messages: client.publish("homeassistant/button/" + config.mqtt_topic_prefix + "/" + hostname + "_restart/config", config_json('restart_button'), qos=config.qos) + while len(client._out_messages) > 0: + time.sleep(0.1) + client.loop() client.loop_stop() # disconnect from mqtt server @@ -467,7 +476,11 @@ def bulk_publish_to_mqtt(cpu_load=0, cpu_temp=0, used_space=0, voltage=0, sys_cl # publish monitored values to MQTT client.publish(config.mqtt_topic_prefix + "/" + hostname, values, qos=config.qos, retain=config.retain) - + while len(client._out_messages) > 0: + time.sleep(0.1) + client.loop() + + client.loop_stop() # disconnect from mqtt server client.disconnect() @@ -601,29 +614,31 @@ if __name__ == '__main__': args = parse_arguments(); if args.service: - client = create_mqtt_client() - if client is None: - print("Error: Unable to connect to MQTT broker") - sys.exit(1) - - client.loop_start() + client = paho.Client() + client.username_pw_set(config.mqtt_user, config.mqtt_password) + client.on_message = on_message + + try: + client.connect(config.mqtt_host, int(config.mqtt_port)) + except Exception as e: + print("Error connecting to MQTT broker:", e) + sys.exit(1) # Exit the script client.subscribe("homeassistant/update/" + hostname + "/command") # Replace with your MQTT topic print("Listening to topic : " + "homeassistant/update/" + hostname + "/command") - + client.loop_start() # Start the MQTT client loop in a new thread # Start the gather_and_send_info function in a new thread thread1 = threading.Thread(target=gather_and_send_info) thread1.daemon = True # Set the daemon attribute to True thread1.start() + if config.update: # Start the update_status function in a new thread thread2 = threading.Thread(target=update_status) thread2.daemon = True # Set the daemon attribute to True thread2.start() - client.loop_start() # Start the MQTT client loop in a new thread - # Check the exit flag in the main thread while True: if exit_flag: