print_queue.py 5.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. batch_id: Mapped[int | None] = mapped_column(ForeignKey("print_batches.id", ondelete="SET NULL"), nullable=True)
  30. # Scheduling
  31. position: Mapped[int] = mapped_column(Integer, default=0) # Queue order
  32. scheduled_time: Mapped[datetime | None] = mapped_column(DateTime, nullable=True) # None = ASAP
  33. manual_start: Mapped[bool] = mapped_column(Boolean, default=False) # Requires manual trigger to start
  34. # Conditions
  35. require_previous_success: Mapped[bool] = mapped_column(Boolean, default=False)
  36. # Power management
  37. auto_off_after: Mapped[bool] = mapped_column(Boolean, default=False) # Power off printer after print
  38. # AMS mapping: JSON array of global tray IDs for each filament slot
  39. # Format: "[5, -1, 2, -1]" where position = slot_id-1, value = global tray ID (-1 = unused)
  40. ams_mapping: Mapped[str | None] = mapped_column(Text, nullable=True)
  41. # Filament overrides for model-based assignment: JSON array of override objects
  42. # Format: '[{"slot_id": 1, "type": "PLA", "color": "#FFFFFF"}]'
  43. # Only slots with overrides are included (sparse). null = use original 3MF values.
  44. filament_overrides: Mapped[str | None] = mapped_column(Text, nullable=True)
  45. # Plate ID for multi-plate 3MF files (1-indexed, None = auto-detect/plate 1)
  46. plate_id: Mapped[int | None] = mapped_column(Integer, nullable=True)
  47. # Print options
  48. bed_levelling: Mapped[bool] = mapped_column(Boolean, default=True)
  49. flow_cali: Mapped[bool] = mapped_column(Boolean, default=False)
  50. vibration_cali: Mapped[bool] = mapped_column(Boolean, default=True)
  51. layer_inspect: Mapped[bool] = mapped_column(Boolean, default=False)
  52. timelapse: Mapped[bool] = mapped_column(Boolean, default=False)
  53. use_ams: Mapped[bool] = mapped_column(Boolean, default=True)
  54. # Status: pending, printing, completed, failed, skipped, cancelled
  55. status: Mapped[str] = mapped_column(String(20), default="pending")
  56. # Tracking
  57. started_at: Mapped[datetime | None] = mapped_column(DateTime, nullable=True)
  58. completed_at: Mapped[datetime | None] = mapped_column(DateTime, nullable=True)
  59. error_message: Mapped[str | None] = mapped_column(Text, nullable=True)
  60. # Timestamps
  61. created_at: Mapped[datetime] = mapped_column(DateTime, server_default=func.now())
  62. # User tracking (who added this to the queue)
  63. created_by_id: Mapped[int | None] = mapped_column(ForeignKey("users.id", ondelete="SET NULL"), nullable=True)
  64. # Relationships
  65. printer: Mapped["Printer"] = relationship()
  66. archive: Mapped["PrintArchive | None"] = relationship()
  67. library_file: Mapped["LibraryFile | None"] = relationship()
  68. project: Mapped["Project | None"] = relationship(back_populates="queue_items")
  69. batch: Mapped["PrintBatch | None"] = relationship(back_populates="queue_items")
  70. created_by: Mapped["User | None"] = relationship()
  71. from backend.app.models.archive import PrintArchive # noqa: E402
  72. from backend.app.models.library import LibraryFile # noqa: E402
  73. from backend.app.models.print_batch import PrintBatch # noqa: E402
  74. from backend.app.models.printer import Printer # noqa: E402
  75. from backend.app.models.project import Project # noqa: E402
  76. from backend.app.models.user import User # noqa: E402