print_queue.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. from datetime import datetime
  2. from sqlalchemy import Boolean, DateTime, ForeignKey, Integer, String, Text, func
  3. from sqlalchemy.orm import Mapped, mapped_column, relationship
  4. from backend.app.core.database import Base
  5. class PrintQueueItem(Base):
  6. """Print queue item for scheduled/queued prints."""
  7. __tablename__ = "print_queue"
  8. id: Mapped[int] = mapped_column(primary_key=True)
  9. # Links
  10. printer_id: Mapped[int | None] = mapped_column(ForeignKey("printers.id", ondelete="CASCADE"), nullable=True)
  11. # Target printer model for model-based assignment (mutually exclusive with printer_id)
  12. # When set, scheduler assigns to any idle printer of matching model
  13. target_model: Mapped[str | None] = mapped_column(String(50), nullable=True)
  14. # Either archive_id OR library_file_id must be set (archive created at print start from library file)
  15. archive_id: Mapped[int | None] = mapped_column(ForeignKey("print_archives.id", ondelete="CASCADE"), nullable=True)
  16. library_file_id: Mapped[int | None] = mapped_column(
  17. ForeignKey("library_files.id", ondelete="CASCADE"), nullable=True
  18. )
  19. project_id: Mapped[int | None] = mapped_column(ForeignKey("projects.id", ondelete="SET NULL"), nullable=True)
  20. # Scheduling
  21. position: Mapped[int] = mapped_column(Integer, default=0) # Queue order
  22. scheduled_time: Mapped[datetime | None] = mapped_column(DateTime, nullable=True) # None = ASAP
  23. manual_start: Mapped[bool] = mapped_column(Boolean, default=False) # Requires manual trigger to start
  24. # Conditions
  25. require_previous_success: Mapped[bool] = mapped_column(Boolean, default=False)
  26. # Power management
  27. auto_off_after: Mapped[bool] = mapped_column(Boolean, default=False) # Power off printer after print
  28. # AMS mapping: JSON array of global tray IDs for each filament slot
  29. # Format: "[5, -1, 2, -1]" where position = slot_id-1, value = global tray ID (-1 = unused)
  30. ams_mapping: Mapped[str | None] = mapped_column(Text, nullable=True)
  31. # Plate ID for multi-plate 3MF files (1-indexed, None = auto-detect/plate 1)
  32. plate_id: Mapped[int | None] = mapped_column(Integer, nullable=True)
  33. # Print options
  34. bed_levelling: Mapped[bool] = mapped_column(Boolean, default=True)
  35. flow_cali: Mapped[bool] = mapped_column(Boolean, default=False)
  36. vibration_cali: Mapped[bool] = mapped_column(Boolean, default=True)
  37. layer_inspect: Mapped[bool] = mapped_column(Boolean, default=False)
  38. timelapse: Mapped[bool] = mapped_column(Boolean, default=False)
  39. use_ams: Mapped[bool] = mapped_column(Boolean, default=True)
  40. # Status: pending, printing, completed, failed, skipped, cancelled
  41. status: Mapped[str] = mapped_column(String(20), default="pending")
  42. # Tracking
  43. started_at: Mapped[datetime | None] = mapped_column(DateTime, nullable=True)
  44. completed_at: Mapped[datetime | None] = mapped_column(DateTime, nullable=True)
  45. error_message: Mapped[str | None] = mapped_column(Text, nullable=True)
  46. # Timestamps
  47. created_at: Mapped[datetime] = mapped_column(DateTime, server_default=func.now())
  48. # Relationships
  49. printer: Mapped["Printer"] = relationship()
  50. archive: Mapped["PrintArchive | None"] = relationship()
  51. library_file: Mapped["LibraryFile | None"] = relationship()
  52. project: Mapped["Project | None"] = relationship(back_populates="queue_items")
  53. from backend.app.models.archive import PrintArchive # noqa: E402
  54. from backend.app.models.library import LibraryFile # noqa: E402
  55. from backend.app.models.printer import Printer # noqa: E402
  56. from backend.app.models.project import Project # noqa: E402