Explorar o código

- Fixed bug, where switched off printers still show as active on printer page

maziggy hai 5 meses
pai
achega
ac4fec7de8
Modificáronse 2 ficheiros con 37 adicións e 9 borrados
  1. 21 0
      backend/app/services/bambu_mqtt.py
  2. 16 9
      backend/app/services/printer_manager.py

+ 21 - 0
backend/app/services/bambu_mqtt.py

@@ -278,6 +278,27 @@ class BambuMQTTClient:
     def topic_publish(self) -> str:
         return f"device/{self.serial_number}/request"
 
+    # Maximum time (seconds) without a message before considering connection stale
+    STALE_TIMEOUT = 60.0
+
+    def is_stale(self) -> bool:
+        """Check if the connection is stale (no messages for too long)."""
+        if self._last_message_time == 0:
+            return False  # Never received a message yet
+        time_since_last = time.time() - self._last_message_time
+        return time_since_last > self.STALE_TIMEOUT
+
+    def check_staleness(self) -> bool:
+        """Check staleness and update connected state if stale. Returns True if connected."""
+        if self.state.connected and self.is_stale():
+            logger.warning(
+                f"[{self.serial_number}] Connection stale - no message for {time.time() - self._last_message_time:.1f}s"
+            )
+            self.state.connected = False
+            if self.on_state_change:
+                self.on_state_change(self.state)
+        return self.state.connected
+
     def _on_connect(self, client, userdata, flags, rc, properties=None):
         if rc == 0:
             self.state.connected = True

+ 16 - 9
backend/app/services/printer_manager.py

@@ -106,22 +106,29 @@ class PrinterManager:
             self.disconnect_printer(printer_id)
 
     def get_status(self, printer_id: int) -> PrinterState | None:
-        """Get the current status of a printer."""
+        """Get the current status of a printer (checks for stale connections)."""
         if printer_id in self._clients:
-            return self._clients[printer_id].state
+            client = self._clients[printer_id]
+            # Check staleness and update connected state if needed
+            client.check_staleness()
+            return client.state
         return None
 
     def get_all_statuses(self) -> dict[int, PrinterState]:
-        """Get status of all connected printers."""
-        return {
-            printer_id: client.state
-            for printer_id, client in self._clients.items()
-        }
+        """Get status of all connected printers (checks for stale connections)."""
+        result = {}
+        for printer_id, client in self._clients.items():
+            # Check staleness and update connected state if needed
+            client.check_staleness()
+            result[printer_id] = client.state
+        return result
 
     def is_connected(self, printer_id: int) -> bool:
-        """Check if a printer is connected."""
+        """Check if a printer is connected (checks for stale connections)."""
         if printer_id in self._clients:
-            return self._clients[printer_id].state.connected
+            client = self._clients[printer_id]
+            # Check staleness and update connected state if needed
+            return client.check_staleness()
         return False
 
     def get_client(self, printer_id: int) -> BambuMQTTClient | None: