slot_preset.py 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. """Model for storing AMS slot to filament preset mappings.
  2. This stores the user's preferred filament preset for each AMS slot,
  3. similar to how Bambu Studio remembers preset selections.
  4. """
  5. from datetime import datetime
  6. from sqlalchemy import DateTime, ForeignKey, Integer, String, UniqueConstraint, func
  7. from sqlalchemy.orm import Mapped, mapped_column, relationship
  8. from backend.app.core.database import Base
  9. class SlotPresetMapping(Base):
  10. """Maps an AMS slot to a cloud filament preset."""
  11. __tablename__ = "slot_preset_mappings"
  12. __table_args__ = (UniqueConstraint("printer_id", "ams_id", "tray_id", name="uq_slot_preset"),)
  13. id: Mapped[int] = mapped_column(primary_key=True)
  14. printer_id: Mapped[int] = mapped_column(ForeignKey("printers.id", ondelete="CASCADE"))
  15. ams_id: Mapped[int] = mapped_column(Integer) # AMS unit ID (0, 1, 2, 3)
  16. tray_id: Mapped[int] = mapped_column(Integer) # Tray ID within AMS (0-3)
  17. preset_id: Mapped[str] = mapped_column(String(100)) # Cloud preset setting_id
  18. preset_name: Mapped[str] = mapped_column(String(200)) # Preset name for display
  19. preset_source: Mapped[str] = mapped_column(String(20), default="cloud") # cloud or local
  20. # The filament id the slot was configured with alongside this preset. When
  21. # the printer later reports a different one, something else (the slicer's
  22. # Device tab, the printer screen) re-configured the slot and this row no
  23. # longer describes it (#3216). None for rows written before it was recorded
  24. # and by callers that do not know it.
  25. tray_info_idx: Mapped[str | None] = mapped_column(String(32), nullable=True)
  26. created_at: Mapped[datetime] = mapped_column(DateTime, server_default=func.now())
  27. updated_at: Mapped[datetime] = mapped_column(DateTime, server_default=func.now(), onupdate=func.now())
  28. # Relationship
  29. printer: Mapped["Printer"] = relationship()
  30. from backend.app.models.printer import Printer # noqa: E402