update
Browse files
app.py
CHANGED
|
@@ -33,14 +33,13 @@ logging.basicConfig(
|
|
| 33 |
)
|
| 34 |
logger = logging.getLogger(__name__)
|
| 35 |
|
| 36 |
-
# Global variables
|
|
|
|
| 37 |
latest_data = {
|
| 38 |
"bed_temperature": "N/A",
|
| 39 |
"nozzle_temperature": "N/A",
|
| 40 |
-
"status": "
|
| 41 |
-
"update_time": "
|
| 42 |
-
"current_image_url": None,
|
| 43 |
-
"print_progress": 0
|
| 44 |
}
|
| 45 |
|
| 46 |
# Initialize analysis components
|
|
@@ -112,18 +111,63 @@ def create_3mf_package(gcode_content: str, gcode_filename: str = "Metadata/plate
|
|
| 112 |
zip_buffer.seek(0)
|
| 113 |
return zip_buffer
|
| 114 |
|
| 115 |
-
def create_client(host, port, username, password):
|
|
|
|
| 116 |
global client
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 124 |
|
| 125 |
def on_connect(client, userdata, flags, rc):
|
| 126 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 127 |
|
| 128 |
def on_message(client, userdata, message):
|
| 129 |
global latest_data
|
|
@@ -172,8 +216,13 @@ def send_print_parameters(nozzle_temp, bed_temp, print_speed, fan_speed):
|
|
| 172 |
except Exception as e:
|
| 173 |
return f"Error sending parameters: {e}"
|
| 174 |
|
| 175 |
-
# Initialize MQTT client
|
| 176 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 177 |
|
| 178 |
def update_printer_status():
|
| 179 |
"""Get current printer status"""
|
|
|
|
| 33 |
)
|
| 34 |
logger = logging.getLogger(__name__)
|
| 35 |
|
| 36 |
+
# Global variables
|
| 37 |
+
client = None
|
| 38 |
latest_data = {
|
| 39 |
"bed_temperature": "N/A",
|
| 40 |
"nozzle_temperature": "N/A",
|
| 41 |
+
"status": "Disconnected",
|
| 42 |
+
"update_time": "Not connected"
|
|
|
|
|
|
|
| 43 |
}
|
| 44 |
|
| 45 |
# Initialize analysis components
|
|
|
|
| 111 |
zip_buffer.seek(0)
|
| 112 |
return zip_buffer
|
| 113 |
|
| 114 |
+
def create_client(host, port, username, password, max_retries=3):
|
| 115 |
+
"""Create MQTT client with retry mechanism"""
|
| 116 |
global client
|
| 117 |
+
|
| 118 |
+
for attempt in range(max_retries):
|
| 119 |
+
try:
|
| 120 |
+
# Create new client
|
| 121 |
+
client = mqtt.Client(protocol=mqtt.MQTTv311)
|
| 122 |
+
client.username_pw_set(username, password)
|
| 123 |
+
client.tls_set(tls_version=mqtt.ssl.PROTOCOL_TLS)
|
| 124 |
+
client.on_connect = on_connect
|
| 125 |
+
client.on_message = on_message
|
| 126 |
+
client.on_disconnect = on_disconnect
|
| 127 |
+
|
| 128 |
+
# Try to connect
|
| 129 |
+
print(f"Attempting to connect to MQTT broker (attempt {attempt + 1}/{max_retries})")
|
| 130 |
+
client.connect(host, port, keepalive=60)
|
| 131 |
+
client.loop_start()
|
| 132 |
+
return True
|
| 133 |
+
|
| 134 |
+
except Exception as e:
|
| 135 |
+
print(f"Connection attempt {attempt + 1} failed: {e}")
|
| 136 |
+
if client:
|
| 137 |
+
client.loop_stop()
|
| 138 |
+
client = None
|
| 139 |
+
if attempt < max_retries - 1:
|
| 140 |
+
time.sleep(5) # Wait before retry
|
| 141 |
+
|
| 142 |
+
print("Failed to connect to MQTT broker after all attempts")
|
| 143 |
+
return False
|
| 144 |
|
| 145 |
def on_connect(client, userdata, flags, rc):
|
| 146 |
+
"""Callback when connected to MQTT broker"""
|
| 147 |
+
if rc == 0:
|
| 148 |
+
print("Successfully connected to MQTT broker")
|
| 149 |
+
serial = os.getenv("PRINTER_SERIAL", "0309CA471800852")
|
| 150 |
+
response_topic = f"bambu_a1_mini/response/{serial}"
|
| 151 |
+
client.subscribe(response_topic)
|
| 152 |
+
print(f"Subscribed to {response_topic}")
|
| 153 |
+
|
| 154 |
+
# Update status
|
| 155 |
+
global latest_data
|
| 156 |
+
latest_data["status"] = "Connected"
|
| 157 |
+
latest_data["update_time"] = time.strftime("%Y-%m-%d %H:%M:%S")
|
| 158 |
+
else:
|
| 159 |
+
print(f"Failed to connect to MQTT broker with code: {rc}")
|
| 160 |
+
|
| 161 |
+
def on_disconnect(client, userdata, rc):
|
| 162 |
+
"""Callback when disconnected from MQTT broker"""
|
| 163 |
+
print("Disconnected from MQTT broker")
|
| 164 |
+
global latest_data
|
| 165 |
+
latest_data["status"] = "Disconnected"
|
| 166 |
+
latest_data["update_time"] = time.strftime("%Y-%m-%d %H:%M:%S")
|
| 167 |
+
|
| 168 |
+
if rc != 0:
|
| 169 |
+
print("Unexpected disconnection. Attempting to reconnect...")
|
| 170 |
+
create_client(HOST, PORT, USERNAME, PASSWORD)
|
| 171 |
|
| 172 |
def on_message(client, userdata, message):
|
| 173 |
global latest_data
|
|
|
|
| 216 |
except Exception as e:
|
| 217 |
return f"Error sending parameters: {e}"
|
| 218 |
|
| 219 |
+
# Initialize MQTT client (but don't fail if it doesn't connect)
|
| 220 |
+
try:
|
| 221 |
+
mqtt_connected = create_client(HOST, PORT, USERNAME, PASSWORD)
|
| 222 |
+
if not mqtt_connected:
|
| 223 |
+
print("Warning: Starting without MQTT connection")
|
| 224 |
+
except Exception as e:
|
| 225 |
+
print(f"Warning: Failed to initialize MQTT: {e}")
|
| 226 |
|
| 227 |
def update_printer_status():
|
| 228 |
"""Get current printer status"""
|