linx5o commited on
Commit
3bea791
·
1 Parent(s): 96e746e
Files changed (1) hide show
  1. app.py +27 -20
app.py CHANGED
@@ -3,6 +3,7 @@ import os
3
  import gradio as gr
4
  import json
5
  import time
 
6
 
7
  # Environment Variables
8
  HOST = os.environ.get("host")
@@ -10,13 +11,19 @@ PORT = int(os.environ.get("port"))
10
  USERNAME = os.environ.get("username")
11
  PASSWORD = os.environ.get("password")
12
 
13
- # MQTT Variables
 
 
 
 
 
 
 
 
14
  client = None
15
- response_topic = None
16
- latest_data = {"status": "N/A", "bed_temperature": "N/A", "nozzle_temperature": "N/A", "update_time": "Waiting for data"}
17
 
18
  def create_client(host, port, username, password):
19
- """Initialize the MQTT client."""
20
  global client
21
  client = mqtt.Client()
22
  client.username_pw_set(username, password)
@@ -27,11 +34,9 @@ def create_client(host, port, username, password):
27
  client.loop_start()
28
 
29
  def on_connect(client, userdata, flags, rc):
30
- """Handle MQTT connection."""
31
  print(f"Connected with result code {rc}")
32
 
33
  def on_message(client, userdata, message):
34
- """Handle incoming MQTT messages and update Gradio UI dynamically."""
35
  global latest_data
36
  print("Received message")
37
  try:
@@ -40,7 +45,7 @@ def on_message(client, userdata, message):
40
  latest_data["nozzle_temperature"] = data.get("nozzle_temperature", "N/A")
41
  latest_data["status"] = data.get("status", "N/A")
42
  latest_data["update_time"] = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
43
-
44
  except Exception as e:
45
  print(f"Error parsing MQTT message: {e}")
46
 
@@ -53,15 +58,25 @@ def get_data(serial):
53
 
54
  request_topic = f"bambu_a1_mini/request/{serial}"
55
  response_topic = f"bambu_a1_mini/response/{serial}"
56
-
57
  print(f"Subscribing to {response_topic}")
58
  client.subscribe(response_topic)
59
-
60
  # Send a request to get data
61
  client.publish(request_topic, json.dumps("HI"))
 
 
 
 
 
 
 
 
 
62
 
63
- return "Request sent. Waiting for response..."
64
-
 
65
 
66
  # Gradio UI
67
  with gr.Blocks() as blocks:
@@ -73,14 +88,6 @@ with gr.Blocks() as blocks:
73
  nozzle_temp_text = gr.Textbox(label="Nozzle Temperature", interactive=False)
74
  update_time_text = gr.Textbox(label="Last Update", interactive=False)
75
 
76
- send_button.click(fn=get_data, inputs=[serial], outputs=[])
77
-
78
- @gr.render(inputs=[])
79
- def display_data():
80
- """Function to return the latest received data dynamically."""
81
- return latest_data["status"], latest_data["bed_temperature"], latest_data["nozzle_temperature"], latest_data["update_time"]
82
-
83
- # Dynamic rendering using @gr.render
84
- display_data() >> [status_text, bed_temp_text, nozzle_temp_text, update_time_text]
85
 
86
  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")
 
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)
 
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:
 
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
 
 
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
+ global latest_data
69
+ latest_data = None
70
+ timeout = 10
71
+ while latest_data is None and timeout > 0:
72
+ time.sleep(1)
73
+ timeout -= 1
74
+
75
+ return latest_data["status"], latest_data["bed_temperature"], latest_data["nozzle_temperature"], latest_data["update_time"]
76
 
77
+ def display_data():
78
+ """Function to return the latest received data."""
79
+ return latest_data["status"], latest_data["bed_temperature"], latest_data["nozzle_temperature"], latest_data["update_time"]
80
 
81
  # Gradio UI
82
  with gr.Blocks() as blocks:
 
88
  nozzle_temp_text = gr.Textbox(label="Nozzle Temperature", interactive=False)
89
  update_time_text = gr.Textbox(label="Last Update", interactive=False)
90
 
91
+ send_button.click(fn=get_data, inputs=[serial], outputs=[status_text, bed_temp_text, nozzle_temp_text, update_time_text])
 
 
 
 
 
 
 
 
92
 
93
  blocks.launch()