Erweitere die Verarbeitung von MQTT-Nachrichten in BambuVirtualPrinter um Schicht-, Lüfter-, Geschwindigkeits- und Dateiinformationen; verbessere die Fehlerbehandlung.
This commit is contained in:
parent
ba43df279d
commit
c7c089ef68
@ -304,8 +304,7 @@ class BambuVirtualPrinter:
|
|||||||
print_data = payload['print']
|
print_data = payload['print']
|
||||||
self._log.info(f"Processing print data with keys: {list(print_data.keys())}")
|
self._log.info(f"Processing print data with keys: {list(print_data.keys())}")
|
||||||
|
|
||||||
# Temperaturdaten direkt verarbeiten, ohne auf "temperature" als Schlüssel zu warten
|
# Temperaturdaten direkt verarbeiten
|
||||||
# Bambu-Drucker verwenden Schlüssel wie "nozzle_temper" direkt im print-Objekt
|
|
||||||
self._process_direct_temperature_data(print_data)
|
self._process_direct_temperature_data(print_data)
|
||||||
|
|
||||||
# Status verarbeiten
|
# Status verarbeiten
|
||||||
@ -316,6 +315,18 @@ class BambuVirtualPrinter:
|
|||||||
if 'mc_percent' in print_data:
|
if 'mc_percent' in print_data:
|
||||||
self._process_progress_data(print_data)
|
self._process_progress_data(print_data)
|
||||||
|
|
||||||
|
# Schicht-Informationen verarbeiten
|
||||||
|
self._process_layer_data(print_data)
|
||||||
|
|
||||||
|
# Lüfter-Informationen verarbeiten
|
||||||
|
self._process_fan_data(print_data)
|
||||||
|
|
||||||
|
# Geschwindigkeit verarbeiten
|
||||||
|
self._process_speed_data(print_data)
|
||||||
|
|
||||||
|
# Datei-Informationen verarbeiten
|
||||||
|
self._process_file_data(print_data)
|
||||||
|
|
||||||
# Trigger update
|
# Trigger update
|
||||||
self.new_update("event_printer_data_update")
|
self.new_update("event_printer_data_update")
|
||||||
|
|
||||||
@ -331,6 +342,161 @@ class BambuVirtualPrinter:
|
|||||||
except Exception as e:
|
except Exception as e:
|
||||||
self._log.error(f"Error processing MQTT payload: {e}", exc_info=True)
|
self._log.error(f"Error processing MQTT payload: {e}", exc_info=True)
|
||||||
|
|
||||||
|
def _process_layer_data(self, print_data):
|
||||||
|
"""Verarbeitet Schicht-Informationen aus MQTT-Nachrichten"""
|
||||||
|
try:
|
||||||
|
current_layer = None
|
||||||
|
total_layers = None
|
||||||
|
|
||||||
|
if 'layer_num' in print_data:
|
||||||
|
current_layer = int(print_data['layer_num'])
|
||||||
|
self._log.debug(f"Current layer: {current_layer}")
|
||||||
|
|
||||||
|
if 'total_layer_num' in print_data:
|
||||||
|
total_layers = int(print_data['total_layer_num'])
|
||||||
|
self._log.debug(f"Total layers: {total_layers}")
|
||||||
|
|
||||||
|
# Aktualisiere den PrintJob, wenn einer existiert
|
||||||
|
if self.current_print_job is not None:
|
||||||
|
if current_layer is not None:
|
||||||
|
self.current_print_job.current_layer = current_layer
|
||||||
|
if total_layers is not None:
|
||||||
|
self.current_print_job.total_layers = total_layers
|
||||||
|
|
||||||
|
# Aktualisiere auch die pybambu-Datenstruktur
|
||||||
|
if hasattr(self._bambu_client, 'device') and hasattr(self._bambu_client.device, 'print_job'):
|
||||||
|
if current_layer is not None:
|
||||||
|
self._bambu_client.device.print_job.current_layer = current_layer
|
||||||
|
if total_layers is not None:
|
||||||
|
self._bambu_client.device.print_job.total_layers = total_layers
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
self._log.error(f"Error processing layer data: {e}", exc_info=True)
|
||||||
|
|
||||||
|
def _process_fan_data(self, print_data):
|
||||||
|
"""Verarbeitet Lüfterdaten aus MQTT-Nachrichten"""
|
||||||
|
try:
|
||||||
|
# Verschiedene Lüfter-Typen
|
||||||
|
fan_data = {}
|
||||||
|
|
||||||
|
if 'heatbreak_fan_speed' in print_data:
|
||||||
|
fan_data['heatbreak'] = int(print_data['heatbreak_fan_speed'])
|
||||||
|
|
||||||
|
if 'cooling_fan_speed' in print_data:
|
||||||
|
fan_data['cooling'] = int(print_data['cooling_fan_speed'])
|
||||||
|
|
||||||
|
if 'big_fan1_speed' in print_data:
|
||||||
|
fan_data['chamber1'] = int(print_data['big_fan1_speed'])
|
||||||
|
|
||||||
|
if 'big_fan2_speed' in print_data:
|
||||||
|
fan_data['chamber2'] = int(print_data['big_fan2_speed'])
|
||||||
|
|
||||||
|
if fan_data:
|
||||||
|
self._log.debug(f"Fan speeds: {fan_data}")
|
||||||
|
|
||||||
|
# Aktualisiere die pybambu-Struktur, wenn vorhanden
|
||||||
|
if hasattr(self._bambu_client, 'device') and hasattr(self._bambu_client.device, 'fan_speeds'):
|
||||||
|
try:
|
||||||
|
for fan_type, speed in fan_data.items():
|
||||||
|
setattr(self._bambu_client.device.fan_speeds, fan_type, speed)
|
||||||
|
except:
|
||||||
|
# Wenn fan_speeds nicht die erwarteten Attribute hat, erstellen wir sie
|
||||||
|
self._bambu_client.device.fan_speeds = type('', (), fan_data)()
|
||||||
|
except Exception as e:
|
||||||
|
self._log.error(f"Error processing fan data: {e}", exc_info=True)
|
||||||
|
|
||||||
|
def _process_speed_data(self, print_data):
|
||||||
|
"""Verarbeitet Geschwindigkeitsdaten aus MQTT-Nachrichten"""
|
||||||
|
try:
|
||||||
|
if 'spd_mag' in print_data:
|
||||||
|
speed_magnitude = int(print_data['spd_mag'])
|
||||||
|
self._log.debug(f"Speed magnitude: {speed_magnitude}%")
|
||||||
|
|
||||||
|
if 'spd_lvl' in print_data:
|
||||||
|
speed_level = int(print_data['spd_lvl'])
|
||||||
|
self._log.debug(f"Speed level: {speed_level}")
|
||||||
|
|
||||||
|
# Aktualisiere die pybambu-Struktur, wenn vorhanden
|
||||||
|
if hasattr(self._bambu_client, 'device') and not hasattr(self._bambu_client.device, 'speed'):
|
||||||
|
self._bambu_client.device.speed = type('', (), {})()
|
||||||
|
|
||||||
|
if hasattr(self._bambu_client, 'device') and hasattr(self._bambu_client.device, 'speed'):
|
||||||
|
if 'spd_mag' in print_data:
|
||||||
|
self._bambu_client.device.speed.magnitude = int(print_data['spd_mag'])
|
||||||
|
if 'spd_lvl' in print_data:
|
||||||
|
self._bambu_client.device.speed.level = int(print_data['spd_lvl'])
|
||||||
|
except Exception as e:
|
||||||
|
self._log.error(f"Error processing speed data: {e}", exc_info=True)
|
||||||
|
|
||||||
|
def _process_file_data(self, print_data):
|
||||||
|
"""Verarbeitet Dateiinformationen aus MQTT-Nachrichten"""
|
||||||
|
try:
|
||||||
|
# Dateiname
|
||||||
|
if 'gcode_file' in print_data and print_data['gcode_file']:
|
||||||
|
filename = print_data['gcode_file']
|
||||||
|
self._log.debug(f"Print file: {filename}")
|
||||||
|
|
||||||
|
# Aktualisiere den PrintJob, wenn einer existiert
|
||||||
|
if self.current_print_job is not None:
|
||||||
|
self.current_print_job.gcode_file = filename
|
||||||
|
|
||||||
|
# Aktualisiere auch die pybambu-Datenstruktur
|
||||||
|
if hasattr(self._bambu_client, 'device') and hasattr(self._bambu_client.device, 'print_job'):
|
||||||
|
self._bambu_client.device.print_job.gcode_file = filename
|
||||||
|
|
||||||
|
# Subtask Name (oft der Projektname)
|
||||||
|
if 'subtask_name' in print_data and print_data['subtask_name']:
|
||||||
|
subtask_name = print_data['subtask_name']
|
||||||
|
self._log.debug(f"Subtask name: {subtask_name}")
|
||||||
|
|
||||||
|
# Aktualisiere den PrintJob, wenn einer existiert
|
||||||
|
if self.current_print_job is not None:
|
||||||
|
self.current_print_job.subtask_name = subtask_name
|
||||||
|
|
||||||
|
# Aktualisiere auch die pybambu-Datenstruktur
|
||||||
|
if hasattr(self._bambu_client, 'device') and hasattr(self._bambu_client.device, 'print_job'):
|
||||||
|
self._bambu_client.device.print_job.subtask_name = subtask_name
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
self._log.error(f"Error processing file data: {e}", exc_info=True)
|
||||||
|
|
||||||
|
def _process_print_state(self, print_job_state):
|
||||||
|
"""Verarbeitet den Druckerstatus aus MQTT-Nachrichten"""
|
||||||
|
try:
|
||||||
|
self._log.debug(f"Received printer state update: {print_job_state}")
|
||||||
|
|
||||||
|
# Normalisieren des Status, falls er 'unknown' ist
|
||||||
|
if print_job_state == "unknown":
|
||||||
|
# Wenn der Status unbekannt ist, versuchen wir, ihn aus anderen Informationen abzuleiten
|
||||||
|
if hasattr(self._bambu_client, 'device') and hasattr(self._bambu_client.device, 'print_job'):
|
||||||
|
# Prüfe ob Druckfortschritt vorhanden ist
|
||||||
|
if hasattr(self._bambu_client.device.print_job, 'mc_percent') and self._bambu_client.device.print_job.mc_percent > 0:
|
||||||
|
print_job_state = "RUNNING"
|
||||||
|
self._log.debug(f"Changed unknown state to RUNNING based on print progress")
|
||||||
|
|
||||||
|
# Status im PrintJob aktualisieren
|
||||||
|
if self.current_print_job is not None:
|
||||||
|
self.current_print_job.gcode_state = print_job_state
|
||||||
|
|
||||||
|
# Statusänderung in den Zustandsautomaten übertragen
|
||||||
|
if print_job_state in ["IDLE", "FINISH", "FAILED"]:
|
||||||
|
self.change_state(self._state_idle)
|
||||||
|
elif print_job_state in ["RUNNING", "PREPARE"]:
|
||||||
|
self.change_state(self._state_printing)
|
||||||
|
elif print_job_state == "PAUSE":
|
||||||
|
self.change_state(self._state_paused)
|
||||||
|
elif print_job_state == "unknown":
|
||||||
|
# Wenn wir keine bessere Information haben, behalten wir den aktuellen Zustand bei
|
||||||
|
self._log.debug("Keeping current state due to unknown printer state")
|
||||||
|
else:
|
||||||
|
self._log.warn(f"Unknown print job state: {print_job_state}")
|
||||||
|
|
||||||
|
# Aktualisiere auch die pybambu-Datenstruktur
|
||||||
|
if hasattr(self._bambu_client, 'device') and hasattr(self._bambu_client.device, 'print_job'):
|
||||||
|
self._bambu_client.device.print_job.gcode_state = print_job_state
|
||||||
|
except Exception as e:
|
||||||
|
self._log.error(f"Error processing print state: {e}", exc_info=True)
|
||||||
|
|
||||||
def _process_direct_temperature_data(self, print_data):
|
def _process_direct_temperature_data(self, print_data):
|
||||||
"""Verarbeitet Temperaturdaten direkt aus dem print-Objekt"""
|
"""Verarbeitet Temperaturdaten direkt aus dem print-Objekt"""
|
||||||
try:
|
try:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user