Fix retries for creating MQTT clients
#10
by
j-woo
- opened
app.py
CHANGED
|
@@ -107,8 +107,23 @@ def rpi_on_message(client, userdata, message):
|
|
| 107 |
def get_data(serial=DEFAULT_SERIAL):
|
| 108 |
global bambu_client, response_topic
|
| 109 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 110 |
if bambu_client is None:
|
| 111 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 112 |
|
| 113 |
request_topic = f"bambu_a1_mini/request/{serial}"
|
| 114 |
response_topic = f"bambu_a1_mini/response/{serial}"
|
|
@@ -567,12 +582,14 @@ if __name__ == "__main__":
|
|
| 567 |
logger.info("Initializing Bambu MQTT client")
|
| 568 |
create_client("bambu")
|
| 569 |
except Exception as e:
|
|
|
|
| 570 |
logger.error(f"Failed to initialize Bambu MQTT: {e}")
|
| 571 |
|
| 572 |
try:
|
| 573 |
logger.info("Initializing RPI MQTT client")
|
| 574 |
create_client("rpi")
|
| 575 |
except Exception as e:
|
|
|
|
| 576 |
logger.error(f"Failed to initialize RPI MQTT: {e}")
|
| 577 |
|
| 578 |
demo.queue().launch(
|
|
|
|
| 107 |
def get_data(serial=DEFAULT_SERIAL):
|
| 108 |
global bambu_client, response_topic
|
| 109 |
|
| 110 |
+
retries = 5
|
| 111 |
+
while bambu_client is None and retries > 0:
|
| 112 |
+
try:
|
| 113 |
+
create_client("bambu")
|
| 114 |
+
except Exception as e:
|
| 115 |
+
bambu_client = None
|
| 116 |
+
logger.error(f"Failed to initialize Bambu MQTT: {e}")
|
| 117 |
+
|
| 118 |
+
retries -= 1
|
| 119 |
+
|
| 120 |
if bambu_client is None:
|
| 121 |
+
return (
|
| 122 |
+
latest_data["status"],
|
| 123 |
+
latest_data["bed_temperature"],
|
| 124 |
+
latest_data["nozzle_temperature"],
|
| 125 |
+
latest_data["update_time"],
|
| 126 |
+
)
|
| 127 |
|
| 128 |
request_topic = f"bambu_a1_mini/request/{serial}"
|
| 129 |
response_topic = f"bambu_a1_mini/response/{serial}"
|
|
|
|
| 582 |
logger.info("Initializing Bambu MQTT client")
|
| 583 |
create_client("bambu")
|
| 584 |
except Exception as e:
|
| 585 |
+
bambu_client = None
|
| 586 |
logger.error(f"Failed to initialize Bambu MQTT: {e}")
|
| 587 |
|
| 588 |
try:
|
| 589 |
logger.info("Initializing RPI MQTT client")
|
| 590 |
create_client("rpi")
|
| 591 |
except Exception as e:
|
| 592 |
+
rpi_client = None
|
| 593 |
logger.error(f"Failed to initialize RPI MQTT: {e}")
|
| 594 |
|
| 595 |
demo.queue().launch(
|