spoolman_slot_assignment.py 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. from datetime import datetime
  2. from sqlalchemy import CheckConstraint, DateTime, ForeignKey, Integer, UniqueConstraint, func
  3. from sqlalchemy.orm import Mapped, mapped_column, relationship
  4. from backend.app.core.database import Base
  5. AMS_ID_CHECK = "(ams_id >= 0 AND ams_id <= 7) OR ams_id = 32 OR (ams_id >= 128 AND ams_id <= 191) OR ams_id = 255"
  6. # Slots 0-3; 4-8 only on the AMS 3H (32) and among an H3C's external inputs (255).
  7. TRAY_ID_CHECK = "tray_id >= 0 AND (tray_id <= 3 OR (ams_id IN (32, 255) AND tray_id <= 8))"
  8. class SpoolmanSlotAssignment(Base):
  9. """Assignment of a Spoolman spool to a specific AMS slot on a printer.
  10. Tracks which Spoolman spool ID occupies a given (printer, ams, tray) slot.
  11. This is the source of truth for Spoolman slot assignments — Spoolman's own
  12. ``spool.location`` field is NOT managed by Bambuddy and is left for the user.
  13. """
  14. __tablename__ = "spoolman_slot_assignments"
  15. id: Mapped[int] = mapped_column(primary_key=True)
  16. printer_id: Mapped[int] = mapped_column(ForeignKey("printers.id", ondelete="CASCADE"))
  17. ams_id: Mapped[int] = mapped_column(Integer)
  18. tray_id: Mapped[int] = mapped_column(Integer)
  19. spoolman_spool_id: Mapped[int] = mapped_column(Integer)
  20. assigned_at: Mapped[datetime] = mapped_column(DateTime, server_default=func.now())
  21. printer: Mapped["Printer"] = relationship()
  22. __table_args__ = (
  23. UniqueConstraint("printer_id", "ams_id", "tray_id", name="uq_slot_assignment"),
  24. # 0-7: standard AMS units. 32: the H3C's AMS 3H. 128-191: AMS-HT (each
  25. # unit uses ams_id 128+, single tray). 255: external / VT tray. Matches
  26. # the value range the internal `spool_assignment` table accepts. See
  27. # #1274 — H2C with AMS-HT on the left nozzle reports ams_id=128.
  28. CheckConstraint(AMS_ID_CHECK, name="ck_ams_id_range"),
  29. CheckConstraint(TRAY_ID_CHECK, name="ck_tray_id_range"),
  30. )
  31. from backend.app.models.printer import Printer # noqa: E402, F401