spoolman_slot_assignment.py 1.8 KB

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