print_queue.py 4.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. # Required filament types for model-based assignment (JSON array, e.g., '["PLA", "PETG"]')
  15. # Used by scheduler to validate printer has compatible filaments loaded
  16. required_filament_types: Mapped[str | None] = mapped_column(Text, nullable=True)
  17. # Waiting reason - explains why a model-based job hasn't started yet
  18. # Set by scheduler when no matching printer is available
  19. waiting_reason: Mapped[str | None] = mapped_column(Text, nullable=True)
  20. # Either archive_id OR library_file_id must be set (archive created at print start from library file)
  21. archive_id: Mapped[int | None] = mapped_column(ForeignKey("print_archives.id", ondelete="CASCADE"), nullable=True)
  22. library_file_id: Mapped[int | None] = mapped_column(
  23. ForeignKey("library_files.id", ondelete="CASCADE"), nullable=True
  24. )
  25. project_id: Mapped[int | None] = mapped_column(ForeignKey("projects.id", ondelete="SET NULL"), nullable=True)
  26. # Scheduling
  27. position: Mapped[int] = mapped_column(Integer, default=0) # Queue order
  28. scheduled_time: Mapped[datetime | None] = mapped_column(DateTime, nullable=True) # None = ASAP
  29. manual_start: Mapped[bool] = mapped_column(Boolean, default=False) # Requires manual trigger to start
  30. # Conditions
  31. require_previous_success: Mapped[bool] = mapped_column(Boolean, default=False)
  32. # Power management
  33. auto_off_after: Mapped[bool] = mapped_column(Boolean, default=False) # Power off printer after print
  34. # AMS mapping: JSON array of global tray IDs for each filament slot
  35. # Format: "[5, -1, 2, -1]" where position = slot_id-1, value = global tray ID (-1 = unused)
  36. ams_mapping: Mapped[str | None] = mapped_column(Text, nullable=True)
  37. # Plate ID for multi-plate 3MF files (1-indexed, None = auto-detect/plate 1)
  38. plate_id: Mapped[int | None] = mapped_column(Integer, nullable=True)
  39. # Print options
  40. bed_levelling: Mapped[bool] = mapped_column(Boolean, default=True)
  41. flow_cali: Mapped[bool] = mapped_column(Boolean, default=False)
  42. vibration_cali: Mapped[bool] = mapped_column(Boolean, default=True)
  43. layer_inspect: Mapped[bool] = mapped_column(Boolean, default=False)
  44. timelapse: Mapped[bool] = mapped_column(Boolean, default=False)
  45. use_ams: Mapped[bool] = mapped_column(Boolean, default=True)
  46. # Status: pending, printing, completed, failed, skipped, cancelled
  47. status: Mapped[str] = mapped_column(String(20), default="pending")
  48. # Tracking
  49. started_at: Mapped[datetime | None] = mapped_column(DateTime, nullable=True)
  50. completed_at: Mapped[datetime | None] = mapped_column(DateTime, nullable=True)
  51. error_message: Mapped[str | None] = mapped_column(Text, nullable=True)
  52. # Timestamps
  53. created_at: Mapped[datetime] = mapped_column(DateTime, server_default=func.now())
  54. # User tracking (who added this to the queue)
  55. created_by_id: Mapped[int | None] = mapped_column(ForeignKey("users.id", ondelete="SET NULL"), nullable=True)
  56. # Relationships
  57. printer: Mapped["Printer"] = relationship()
  58. archive: Mapped["PrintArchive | None"] = relationship()
  59. library_file: Mapped["LibraryFile | None"] = relationship()
  60. project: Mapped["Project | None"] = relationship(back_populates="queue_items")
  61. created_by: Mapped["User | None"] = relationship()
  62. from backend.app.models.archive import PrintArchive # noqa: E402
  63. from backend.app.models.library import LibraryFile # noqa: E402
  64. from backend.app.models.printer import Printer # noqa: E402
  65. from backend.app.models.project import Project # noqa: E402
  66. from backend.app.models.user import User # noqa: E402