app.py
CHANGED
|
@@ -3,19 +3,28 @@ import os
|
|
| 3 |
import gradio as gr
|
| 4 |
import json
|
| 5 |
import time
|
|
|
|
| 6 |
|
|
|
|
| 7 |
HOST = os.environ.get("host")
|
| 8 |
PORT = int(os.environ.get("port"))
|
| 9 |
USERNAME = os.environ.get("username")
|
| 10 |
PASSWORD = os.environ.get("password")
|
| 11 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
client = None
|
| 13 |
-
|
| 14 |
-
nozzle_temp = gr.Number()
|
| 15 |
-
status = gr.Textbox()
|
| 16 |
-
update_time = gr.Textbox()
|
| 17 |
|
| 18 |
def create_client(host, port, username, password):
|
|
|
|
| 19 |
client = mqtt.Client()
|
| 20 |
client.username_pw_set(username, password)
|
| 21 |
client.tls_set(tls_version=mqtt.ssl.PROTOCOL_TLS)
|
|
@@ -23,48 +32,58 @@ def create_client(host, port, username, password):
|
|
| 23 |
client.on_message = on_message
|
| 24 |
client.connect(host, port)
|
| 25 |
client.loop_start()
|
| 26 |
-
return client
|
| 27 |
|
| 28 |
def on_connect(client, userdata, flags, rc):
|
| 29 |
-
print("Connected with result code "
|
| 30 |
-
|
| 31 |
def on_message(client, userdata, message):
|
|
|
|
| 32 |
print("Received message")
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
|
|
|
|
|
|
| 41 |
def get_data(serial):
|
| 42 |
-
|
| 43 |
-
global client
|
|
|
|
| 44 |
if client is None:
|
| 45 |
-
|
| 46 |
-
request = f"bambu_a1_mini/request/{serial}"
|
| 47 |
-
response = f"bambu_a1_mini/response/{serial}"
|
| 48 |
-
client.subscribe(response)
|
| 49 |
-
client.publish(request, json.dumps("HI"))
|
| 50 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
with gr.Blocks() as blocks:
|
| 52 |
serial = gr.Textbox("0309CA471800852", label="Serial Number")
|
| 53 |
-
send_button = gr.Button(value="
|
| 54 |
-
|
| 55 |
status_text = gr.Textbox(label="Status", interactive=False)
|
| 56 |
bed_temp_text = gr.Textbox(label="Bed Temperature", interactive=False)
|
| 57 |
nozzle_temp_text = gr.Textbox(label="Nozzle Temperature", interactive=False)
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
)
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
bed_temp.change(lambda: bed_temp, bed_temp_text, bed_temp_text)
|
| 67 |
-
nozzle_temp.change(lambda: nozzle_temp, nozzle_temp_text, nozzle_temp_text)
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
blocks.launch()
|
|
|
|
| 3 |
import gradio as gr
|
| 4 |
import json
|
| 5 |
import time
|
| 6 |
+
import threading
|
| 7 |
|
| 8 |
+
# Environment Variables
|
| 9 |
HOST = os.environ.get("host")
|
| 10 |
PORT = int(os.environ.get("port"))
|
| 11 |
USERNAME = os.environ.get("username")
|
| 12 |
PASSWORD = os.environ.get("password")
|
| 13 |
|
| 14 |
+
# Global variables to store received data
|
| 15 |
+
latest_data = {
|
| 16 |
+
"bed_temperature": "N/A",
|
| 17 |
+
"nozzle_temperature": "N/A",
|
| 18 |
+
"status": "N/A",
|
| 19 |
+
"update_time": "Waiting for data...",
|
| 20 |
+
}
|
| 21 |
+
|
| 22 |
+
# MQTT Client setup
|
| 23 |
client = None
|
| 24 |
+
response_topic = None # Will be set dynamically
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
def create_client(host, port, username, password):
|
| 27 |
+
global client
|
| 28 |
client = mqtt.Client()
|
| 29 |
client.username_pw_set(username, password)
|
| 30 |
client.tls_set(tls_version=mqtt.ssl.PROTOCOL_TLS)
|
|
|
|
| 32 |
client.on_message = on_message
|
| 33 |
client.connect(host, port)
|
| 34 |
client.loop_start()
|
|
|
|
| 35 |
|
| 36 |
def on_connect(client, userdata, flags, rc):
|
| 37 |
+
print(f"Connected with result code {rc}")
|
| 38 |
+
|
| 39 |
def on_message(client, userdata, message):
|
| 40 |
+
global latest_data
|
| 41 |
print("Received message")
|
| 42 |
+
try:
|
| 43 |
+
data = json.loads(message.payload)
|
| 44 |
+
latest_data["bed_temperature"] = data.get("bed_temperature", "N/A")
|
| 45 |
+
latest_data["nozzle_temperature"] = data.get("nozzle_temperature", "N/A")
|
| 46 |
+
latest_data["status"] = data.get("status", "N/A")
|
| 47 |
+
latest_data["update_time"] = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
|
| 48 |
+
|
| 49 |
+
except Exception as e:
|
| 50 |
+
print(f"Error parsing MQTT message: {e}")
|
| 51 |
+
|
| 52 |
def get_data(serial):
|
| 53 |
+
"""Request data from the MQTT broker."""
|
| 54 |
+
global client, response_topic
|
| 55 |
+
|
| 56 |
if client is None:
|
| 57 |
+
create_client(HOST, PORT, USERNAME, PASSWORD)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
|
| 59 |
+
request_topic = f"bambu_a1_mini/request/{serial}"
|
| 60 |
+
response_topic = f"bambu_a1_mini/response/{serial}"
|
| 61 |
+
|
| 62 |
+
print(f"Subscribing to {response_topic}")
|
| 63 |
+
client.subscribe(response_topic)
|
| 64 |
+
|
| 65 |
+
# Send a request to get data
|
| 66 |
+
client.publish(request_topic, json.dumps("HI"))
|
| 67 |
+
|
| 68 |
+
return "Request sent. Waiting for response..."
|
| 69 |
+
|
| 70 |
+
def display_data():
|
| 71 |
+
"""Function to return the latest received data."""
|
| 72 |
+
return latest_data["status"], latest_data["bed_temperature"], latest_data["nozzle_temperature"], latest_data["update_time"]
|
| 73 |
+
|
| 74 |
+
# Gradio UI
|
| 75 |
with gr.Blocks() as blocks:
|
| 76 |
serial = gr.Textbox("0309CA471800852", label="Serial Number")
|
| 77 |
+
send_button = gr.Button(value="Request Data")
|
| 78 |
+
|
| 79 |
status_text = gr.Textbox(label="Status", interactive=False)
|
| 80 |
bed_temp_text = gr.Textbox(label="Bed Temperature", interactive=False)
|
| 81 |
nozzle_temp_text = gr.Textbox(label="Nozzle Temperature", interactive=False)
|
| 82 |
+
update_time_text = gr.Textbox(label="Last Update", interactive=False)
|
| 83 |
+
|
| 84 |
+
send_button.click(fn=get_data, inputs=[serial], outputs=[])
|
| 85 |
+
|
| 86 |
+
# Poll the latest received data
|
| 87 |
+
blocks.load(fn=display_data, inputs=[], outputs=[status_text, bed_temp_text, nozzle_temp_text, update_time_text], every=2)
|
| 88 |
+
|
| 89 |
+
blocks.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|