Füge benutzerdefinierte Verbindungsstatusverfolgung für BambuVirtualPrinter hinzu

This commit is contained in:
Manuel Weiser 2025-03-02 10:44:12 +01:00
parent e9c06bb4b5
commit 3a615cfafe

View File

@ -111,6 +111,7 @@ class BambuVirtualPrinter:
self._mqtt_client = None
self._mqtt_connected = False
self._bambu_client = None
self._custom_connected = False
self._bambu_client: BambuClient = self._create_client_connection_async()
@ -172,6 +173,11 @@ class BambuVirtualPrinter:
def project_files(self):
return self._project_files_view
@property
def is_connected(self):
"""Custom property to track connection status without modifying BambuClient directly"""
return self._custom_connected and self._mqtt_connected
def change_state(self, new_state: APrinterState):
self._state_change_queue.put(new_state)
@ -231,6 +237,7 @@ class BambuVirtualPrinter:
self._log.debug(f"MQTT connected with result code: {rc}")
if rc == 0:
self._mqtt_connected = True
self._custom_connected = True
# Subscribe to the relevant topics for the Bambu printer
device_topic = f"device/{self._settings.get(['serial'])}/report"
@ -242,10 +249,12 @@ class BambuVirtualPrinter:
self.sendOk()
else:
self._mqtt_connected = False
self._custom_connected = False
self._log.error(f"Failed to connect to MQTT broker with result code: {rc}")
def _on_mqtt_disconnect(self, client, userdata, rc):
self._mqtt_connected = False
self._custom_connected = False
self._log.debug(f"MQTT disconnected with result code: {rc}")
def _on_mqtt_message(self, client, userdata, msg):
@ -335,14 +344,16 @@ class BambuVirtualPrinter:
self._log.error(f"Failed to connect to MQTT broker: {e}")
raise
# Inject our MQTT client into the BambuClient
# Inject our MQTT client into the BambuClient without modifying 'connected'
bambu_client._mqtt_client = self._mqtt_client
bambu_client.connected = True
# Instead of modifying bambu_client.connected, we'll use our custom property
self._custom_connected = True
# Store the Bambu client
self._bambu_client = bambu_client
self._log.info(f"Bambu connection status: {bambu_client.connected}")
self._log.info(f"Custom connection status: {self.is_connected}")
self.sendOk()
def publish_mqtt(self, topic, payload):
@ -354,7 +365,7 @@ class BambuVirtualPrinter:
# Override BambuClient's publish method to use our MQTT client
def publish(self, command):
"""Publish a command using our MQTT client"""
if not self._mqtt_connected:
if not self.is_connected:
self._log.error("Cannot publish command: MQTT not connected")
return False
@ -634,10 +645,10 @@ class BambuVirtualPrinter:
return
# post gcode to printer otherwise
if self.bambu_client.connected:
if self.is_connected:
GCODE_COMMAND = commands.SEND_GCODE_TEMPLATE
GCODE_COMMAND["print"]["param"] = full_command + "\n"
if self.bambu_client.publish(GCODE_COMMAND):
if self.publish(GCODE_COMMAND):
self._log.info("command sent successfully")
self.sendOk()
@ -727,7 +738,7 @@ class BambuVirtualPrinter:
def _processTemperatureQuery(self) -> bool:
# includeOk = not self._okBeforeCommandOutput
if self.bambu_client.connected:
if self.is_connected:
output = self._create_temperature_message()
self.sendIO(output)
return True
@ -738,8 +749,11 @@ class BambuVirtualPrinter:
if self._mqtt_client and self._mqtt_connected:
self._mqtt_client.loop_stop()
self._mqtt_client.disconnect()
if self.bambu_client.connected:
self.bambu_client.disconnect()
self._mqtt_connected = False
self._custom_connected = False
# Ändern Sie jeden Verweis auf bambu_client.connected zu self.is_connected
if self._bambu_client:
self._bambu_client.disconnect()
self.change_state(self._state_idle)
self._serial_io.close()
self.stop()