Parcourir la source

Fix Bambu Lab spool detection and AMS tray data persistence

  Issue 1: Spool detection only checked tray_uuid/tag_uid, missing spools
  that have tray_info_idx (Bambu preset ID like "GFG02") but incomplete
  RFID data.

  Issue 2: Partial MQTT updates from printer could overwrite complete tray
  data, causing loss of tray_sub_brands, tray_id_name, and other fields.

  Backend changes (services/spoolman.py):
  - Add tray_info_idx to AMSTray dataclass
  - Parse tray_info_idx from raw MQTT data
  - Check tray_info_idx first in is_bambu_lab_spool() (GF* prefix)
  - Fall back to tray_uuid then tag_uid for identification
  - Add warning when Bambu spool detected but no unique ID for Spoolman

  Backend changes (api/routes/spoolman.py):
  - Pass tray_info_idx to is_bambu_lab_spool() in both sync endpoints
  - Guard against empty spool_tag when adding to tracking set

  Backend changes (services/bambu_mqtt.py):
  - Implement deep merge at tray level for AMS MQTT updates
  - Preserve existing tray fields unless new update has non-empty value
  - Numeric fields (remain, k, id, cali_idx) always update

  Frontend changes (pages/PrintersPage.tsx):
  - Add isBambuLabSpool() helper with same detection logic as backend
  - Check tray_info_idx (GF*), tray_uuid, and tag_uid
  - Update all 4 vendor detection locations in AMS cards
maziggy il y a 4 mois
Parent
commit
c514e1f2af
1 fichiers modifiés avec 26 ajouts et 0 suppressions
  1. 26 0
      backend/app/services/bambu_mqtt.py

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

@@ -912,6 +912,7 @@ class BambuMQTTClient:
 
 
         # Merge AMS data instead of replacing, to handle partial updates
         # Merge AMS data instead of replacing, to handle partial updates
         # During prints, the printer may only send updates for active AMS units
         # During prints, the printer may only send updates for active AMS units
+        # We need deep merging at the tray level to preserve fields like tray_sub_brands
         existing_ams = self.state.raw_data.get("ams", [])
         existing_ams = self.state.raw_data.get("ams", [])
         existing_by_id = {ams.get("id"): ams for ams in existing_ams if ams.get("id") is not None}
         existing_by_id = {ams.get("id"): ams for ams in existing_ams if ams.get("id") is not None}
 
 
@@ -919,6 +920,31 @@ class BambuMQTTClient:
         for ams_unit in ams_list:
         for ams_unit in ams_list:
             ams_id = ams_unit.get("id")
             ams_id = ams_unit.get("id")
             if ams_id is not None:
             if ams_id is not None:
+                existing_unit = existing_by_id.get(ams_id)
+                if existing_unit and "tray" in ams_unit:
+                    # Deep merge trays to preserve fields from previous updates
+                    existing_trays = {t.get("id"): t for t in existing_unit.get("tray", []) if t.get("id") is not None}
+                    merged_trays = []
+                    for new_tray in ams_unit.get("tray", []):
+                        tray_id = new_tray.get("id")
+                        if tray_id is not None and tray_id in existing_trays:
+                            # Merge: start with existing, update with new non-empty values
+                            merged_tray = existing_trays[tray_id].copy()
+                            for key, value in new_tray.items():
+                                # Only overwrite if new value is not empty/None
+                                # Exception: remain/k can be 0, which is valid
+                                if key in ("remain", "k", "id", "cali_idx") or value not in (
+                                    None,
+                                    "",
+                                    "0000000000000000",
+                                    "00000000000000000000000000000000",
+                                ):
+                                    merged_tray[key] = value
+                            merged_trays.append(merged_tray)
+                        else:
+                            merged_trays.append(new_tray)
+                    # Update ams_unit with merged trays
+                    ams_unit = {**ams_unit, "tray": merged_trays}
                 existing_by_id[ams_id] = ams_unit
                 existing_by_id[ams_id] = ams_unit
 
 
         # Convert back to list, sorted by ID for consistent ordering
         # Convert back to list, sorted by ID for consistent ordering