print_queue.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. from datetime import datetime
  2. from typing import Annotated, Literal
  3. from pydantic import BaseModel, PlainSerializer
  4. # Custom serializer to ensure UTC datetimes have Z suffix
  5. def serialize_utc_datetime(dt: datetime | None) -> str | None:
  6. if dt is None:
  7. return None
  8. # Add Z suffix to indicate UTC
  9. return dt.isoformat() + "Z"
  10. UTCDatetime = Annotated[datetime | None, PlainSerializer(serialize_utc_datetime)]
  11. class PrintQueueItemCreate(BaseModel):
  12. printer_id: int | None = None # None = unassigned, user assigns later
  13. target_model: str | None = None # Target printer model (mutually exclusive with printer_id)
  14. target_location: str | None = None # Target location filter (only used with target_model)
  15. required_filament_types: list[str] | None = None # Required filament types for model-based assignment
  16. # Either archive_id OR library_file_id must be provided
  17. archive_id: int | None = None
  18. library_file_id: int | None = None
  19. scheduled_time: datetime | None = None # None = ASAP (next when idle)
  20. require_previous_success: bool = False
  21. auto_off_after: bool = False # Power off printer after print completes
  22. manual_start: bool = False # Requires manual trigger to start (staged)
  23. # AMS mapping: list of global tray IDs for each filament slot
  24. # Format: [5, -1, 2, -1] where position = slot_id-1, value = global tray ID (-1 = unused)
  25. ams_mapping: list[int] | None = None
  26. # Plate ID for multi-plate 3MF files (1-indexed, None = auto-detect/plate 1)
  27. plate_id: int | None = None
  28. # Print options
  29. bed_levelling: bool = True
  30. flow_cali: bool = False
  31. vibration_cali: bool = True
  32. layer_inspect: bool = False
  33. timelapse: bool = False
  34. use_ams: bool = True
  35. class PrintQueueItemUpdate(BaseModel):
  36. printer_id: int | None = None
  37. target_model: str | None = None # Target printer model (mutually exclusive with printer_id)
  38. target_location: str | None = None # Target location filter (only used with target_model)
  39. position: int | None = None
  40. scheduled_time: datetime | None = None
  41. require_previous_success: bool | None = None
  42. auto_off_after: bool | None = None
  43. manual_start: bool | None = None
  44. ams_mapping: list[int] | None = None
  45. plate_id: int | None = None
  46. # Print options
  47. bed_levelling: bool | None = None
  48. flow_cali: bool | None = None
  49. vibration_cali: bool | None = None
  50. layer_inspect: bool | None = None
  51. timelapse: bool | None = None
  52. use_ams: bool | None = None
  53. class PrintQueueItemResponse(BaseModel):
  54. id: int
  55. printer_id: int | None # None = unassigned
  56. target_model: str | None = None # Target printer model for model-based assignment
  57. target_location: str | None = None # Target location filter for model-based assignment
  58. required_filament_types: list[str] | None = None # Required filament types for model-based assignment
  59. waiting_reason: str | None = None # Why a model-based job hasn't started yet
  60. archive_id: int | None # None if library_file_id is set (archive created at print start)
  61. library_file_id: int | None # For queue items from library files
  62. position: int
  63. scheduled_time: UTCDatetime
  64. require_previous_success: bool
  65. auto_off_after: bool
  66. manual_start: bool
  67. ams_mapping: list[int] | None = None
  68. plate_id: int | None = None # Plate ID for multi-plate 3MF files
  69. # Print options
  70. bed_levelling: bool = True
  71. flow_cali: bool = False
  72. vibration_cali: bool = True
  73. layer_inspect: bool = False
  74. timelapse: bool = False
  75. use_ams: bool = True
  76. status: Literal["pending", "printing", "completed", "failed", "skipped", "cancelled"]
  77. started_at: UTCDatetime
  78. completed_at: UTCDatetime
  79. error_message: str | None
  80. created_at: UTCDatetime
  81. # Nested info for UI (populated in route)
  82. archive_name: str | None = None
  83. archive_thumbnail: str | None = None
  84. library_file_name: str | None = None # Name of library file (if library_file_id is set)
  85. library_file_thumbnail: str | None = None # Thumbnail of library file
  86. printer_name: str | None = None
  87. print_time_seconds: int | None = None # Estimated print time from archive or library file
  88. filament_used_grams: float | None = None # Estimated print weight from archive or library file
  89. # User tracking (Issue #206)
  90. created_by_id: int | None = None
  91. created_by_username: str | None = None
  92. class Config:
  93. from_attributes = True
  94. class PrintQueueReorderItem(BaseModel):
  95. id: int
  96. position: int
  97. class PrintQueueReorder(BaseModel):
  98. items: list[PrintQueueReorderItem]
  99. class PrintQueueBulkUpdate(BaseModel):
  100. """Bulk update multiple queue items with the same values."""
  101. item_ids: list[int]
  102. # Fields to update (all optional - only set fields are applied)
  103. printer_id: int | None = None
  104. scheduled_time: datetime | None = None
  105. require_previous_success: bool | None = None
  106. auto_off_after: bool | None = None
  107. manual_start: bool | None = None
  108. # Print options
  109. bed_levelling: bool | None = None
  110. flow_cali: bool | None = None
  111. vibration_cali: bool | None = None
  112. layer_inspect: bool | None = None
  113. timelapse: bool | None = None
  114. use_ams: bool | None = None
  115. class PrintQueueBulkUpdateResponse(BaseModel):
  116. """Response for bulk update operation."""
  117. updated_count: int
  118. skipped_count: int # Items that were not pending
  119. message: str