tag_parser.py 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. """Parse Bambu Lab MIFARE Classic tag data blocks into structured metadata."""
  2. import logging
  3. logger = logging.getLogger(__name__)
  4. # Bambu tag block layout (MIFARE Classic 1K):
  5. # Block 1: material type (bytes 0-7), color info (bytes 8-15)
  6. # Block 2: temperatures, weights
  7. # Block 4: detailed filament type ("PLA Matte")
  8. # Block 9: tray UUID (16 bytes), same on both tags, equals the AMS tray_uuid
  9. def parse_bambu_blocks(blocks: dict[int, bytes]) -> dict:
  10. """Parse raw Bambu MIFARE Classic blocks into metadata dict.
  11. Args:
  12. blocks: Dict mapping block number -> 16 bytes
  13. Returns:
  14. Dict with tray_uuid, material_type, color, etc.
  15. """
  16. result = {}
  17. # Extract tray UUID from block 9
  18. if 9 in blocks:
  19. result["tray_uuid"] = blocks[9][:16].hex().upper()
  20. # Extract material info from block 1
  21. if 1 in blocks:
  22. data = blocks[1]
  23. # Material type is typically in the first few bytes
  24. material_bytes = data[:8]
  25. result["material_raw"] = material_bytes.hex().upper()
  26. # Extract block 2 data (temperatures, weights)
  27. if 2 in blocks:
  28. data = blocks[2]
  29. result["block2_raw"] = data.hex().upper()
  30. return result