simplify-MQTT-clients
#5
by
j-woo
- opened
app.py
CHANGED
|
@@ -51,23 +51,23 @@ rpi_client = None
|
|
| 51 |
response_topic = None # Will be set dynamically
|
| 52 |
|
| 53 |
|
| 54 |
-
def create_client(client_type
|
| 55 |
global bambu_client, rpi_client
|
| 56 |
if client_type == "bambu":
|
| 57 |
bambu_client = mqtt.Client()
|
| 58 |
-
bambu_client.username_pw_set(
|
| 59 |
bambu_client.tls_set(tls_version=mqtt.ssl.PROTOCOL_TLS)
|
| 60 |
bambu_client.on_connect = on_connect
|
| 61 |
bambu_client.on_message = bambu_on_message
|
| 62 |
-
bambu_client.connect(
|
| 63 |
bambu_client.loop_start()
|
| 64 |
elif client_type == "rpi":
|
| 65 |
rpi_client = mqtt.Client()
|
| 66 |
-
rpi_client.username_pw_set(
|
| 67 |
rpi_client.tls_set(tls_version=mqtt.ssl.PROTOCOL_TLS)
|
| 68 |
rpi_client.on_connect = on_connect
|
| 69 |
rpi_client.on_message = rpi_on_message
|
| 70 |
-
rpi_client.connect(
|
| 71 |
rpi_client.loop_start()
|
| 72 |
|
| 73 |
|
|
@@ -105,7 +105,7 @@ def get_data(serial=DEFAULT_SERIAL):
|
|
| 105 |
global bambu_client, response_topic
|
| 106 |
|
| 107 |
if bambu_client is None:
|
| 108 |
-
create_client("bambu"
|
| 109 |
|
| 110 |
request_topic = f"bambu_a1_mini/request/{serial}"
|
| 111 |
response_topic = f"bambu_a1_mini/response/{serial}"
|
|
@@ -217,7 +217,7 @@ def capture_image(url=None, use_test_image=False, test_image_name=None):
|
|
| 217 |
global rpi_client, latest_data
|
| 218 |
|
| 219 |
if rpi_client is None:
|
| 220 |
-
create_client("rpi"
|
| 221 |
|
| 222 |
serial = DEFAULT_SERIAL
|
| 223 |
request_topic = f"bambu_a1_mini/request/{serial}"
|
|
@@ -562,9 +562,14 @@ if __name__ == "__main__":
|
|
| 562 |
logger.info("Starting Bambu A1 Mini Print Control application")
|
| 563 |
|
| 564 |
try:
|
| 565 |
-
logger.info("Initializing MQTT client")
|
| 566 |
-
create_client("bambu"
|
| 567 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 568 |
except Exception as e:
|
| 569 |
logger.error(f"Failed to initialize MQTT: {e}")
|
| 570 |
|
|
|
|
| 51 |
response_topic = None # Will be set dynamically
|
| 52 |
|
| 53 |
|
| 54 |
+
def create_client(client_type):
|
| 55 |
global bambu_client, rpi_client
|
| 56 |
if client_type == "bambu":
|
| 57 |
bambu_client = mqtt.Client()
|
| 58 |
+
bambu_client.username_pw_set(BAMBU_USERNAME, BAMBU_PASSWORD)
|
| 59 |
bambu_client.tls_set(tls_version=mqtt.ssl.PROTOCOL_TLS)
|
| 60 |
bambu_client.on_connect = on_connect
|
| 61 |
bambu_client.on_message = bambu_on_message
|
| 62 |
+
bambu_client.connect(BAMBU_HOST, BAMBU_PORT)
|
| 63 |
bambu_client.loop_start()
|
| 64 |
elif client_type == "rpi":
|
| 65 |
rpi_client = mqtt.Client()
|
| 66 |
+
rpi_client.username_pw_set(RPI_USERNAME, RPI_PASSWORD)
|
| 67 |
rpi_client.tls_set(tls_version=mqtt.ssl.PROTOCOL_TLS)
|
| 68 |
rpi_client.on_connect = on_connect
|
| 69 |
rpi_client.on_message = rpi_on_message
|
| 70 |
+
rpi_client.connect(RPI_HOST, RPI_PORT)
|
| 71 |
rpi_client.loop_start()
|
| 72 |
|
| 73 |
|
|
|
|
| 105 |
global bambu_client, response_topic
|
| 106 |
|
| 107 |
if bambu_client is None:
|
| 108 |
+
create_client("bambu")
|
| 109 |
|
| 110 |
request_topic = f"bambu_a1_mini/request/{serial}"
|
| 111 |
response_topic = f"bambu_a1_mini/response/{serial}"
|
|
|
|
| 217 |
global rpi_client, latest_data
|
| 218 |
|
| 219 |
if rpi_client is None:
|
| 220 |
+
create_client("rpi")
|
| 221 |
|
| 222 |
serial = DEFAULT_SERIAL
|
| 223 |
request_topic = f"bambu_a1_mini/request/{serial}"
|
|
|
|
| 562 |
logger.info("Starting Bambu A1 Mini Print Control application")
|
| 563 |
|
| 564 |
try:
|
| 565 |
+
logger.info("Initializing Bambu MQTT client")
|
| 566 |
+
create_client("bambu")
|
| 567 |
+
except Exception as e:
|
| 568 |
+
logger.error(f"Failed to initialize Bambu MQTT: {e}")
|
| 569 |
+
|
| 570 |
+
try:
|
| 571 |
+
logger.info("Initializing RPI MQTT client")
|
| 572 |
+
create_client("rpi")
|
| 573 |
except Exception as e:
|
| 574 |
logger.error(f"Failed to initialize MQTT: {e}")
|
| 575 |
|