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-5: tray UUID (32 hex chars across 2 blocks)
  8. def parse_bambu_blocks(blocks: dict[int, bytes]) -> dict:
  9. """Parse raw Bambu MIFARE Classic blocks into metadata dict.
  10. Args:
  11. blocks: Dict mapping block number -> 16 bytes
  12. Returns:
  13. Dict with tray_uuid, material_type, color, etc.
  14. """
  15. result = {}
  16. # Extract tray UUID from blocks 4+5
  17. if 4 in blocks and 5 in blocks:
  18. uuid_raw = blocks[4] + blocks[5]
  19. result["tray_uuid"] = uuid_raw[: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