linx5o commited on
Commit
31206fe
·
1 Parent(s): a28614e
Files changed (1) hide show
  1. app.py +14 -18
app.py CHANGED
@@ -3,7 +3,6 @@ import os
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,19 +10,13 @@ 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)
@@ -34,9 +27,11 @@ def create_client(host, port, 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,7 +40,7 @@ def on_message(client, userdata, message):
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,17 +53,18 @@ def get_data(serial):
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
@@ -83,7 +79,7 @@ with gr.Blocks() as blocks:
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])
88
 
89
  blocks.launch()
 
3
  import gradio as gr
4
  import json
5
  import time
 
6
 
7
  # Environment Variables
8
  HOST = os.environ.get("host")
 
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
  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
  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
 
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
+ @gr.render(inputs=[])
66
  def display_data():
67
+ """Function to return the latest received data dynamically."""
68
  return latest_data["status"], latest_data["bed_temperature"], latest_data["nozzle_temperature"], latest_data["update_time"]
69
 
70
  # Gradio UI
 
79
 
80
  send_button.click(fn=get_data, inputs=[serial], outputs=[])
81
 
82
+ # Dynamic rendering using @gr.render
83
+ display_data() >> [status_text, bed_temp_text, nozzle_temp_text, update_time_text]
84
 
85
  blocks.launch()