print_queue.py 4.8 KB

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