print_queue.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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. filament_overrides: list[dict] | None = None # Filament overrides for model-based assignment
  17. # Either archive_id OR library_file_id must be provided
  18. archive_id: int | None = None
  19. library_file_id: int | None = None
  20. scheduled_time: datetime | None = None # None = ASAP (next when idle)
  21. require_previous_success: bool = False
  22. auto_off_after: bool = False # Power off printer after print completes
  23. manual_start: bool = False # Requires manual trigger to start (staged)
  24. # Persistent "Print Anyway" acknowledgement (#1698-followup). When set,
  25. # PrintModal already showed the deficit warning and the user confirmed,
  26. # so the scheduler does not re-flag this item on the next tick.
  27. skip_filament_check: bool = False
  28. # AMS mapping: list 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: list[int] | None = None
  31. # Plate ID for multi-plate 3MF files (1-indexed, None = auto-detect/plate 1)
  32. plate_id: int | None = None
  33. # Print options
  34. bed_levelling: bool = True
  35. flow_cali: bool = False
  36. vibration_cali: bool = True
  37. layer_inspect: bool = False
  38. timelapse: bool = False
  39. use_ams: bool = True
  40. # Nozzle offset calibration — dual-nozzle printers only (#1682). Default True
  41. # matches BambuStudio's default; the MQTT layer ignores the flag on
  42. # single-nozzle printers so the wire value stays "skip" there.
  43. nozzle_offset_cali: bool = True
  44. # Auto-print G-code injection
  45. gcode_injection: bool = False
  46. # Batch: create multiple copies (creates a batch if > 1)
  47. quantity: int = 1
  48. # Existing batch to add this item into. When set, the item's batch_id is
  49. # populated on insert so the queue UI groups it with its siblings. Used by
  50. # the multi-plate auto-batch flow and by the "Group as batch" action.
  51. batch_id: int | None = None
  52. # Project to associate the resulting archive with
  53. project_id: int | None = None
  54. class PrintQueueItemUpdate(BaseModel):
  55. printer_id: int | None = None
  56. target_model: str | None = None # Target printer model (mutually exclusive with printer_id)
  57. target_location: str | None = None # Target location filter (only used with target_model)
  58. filament_overrides: list[dict] | None = None # Filament overrides for model-based assignment
  59. position: int | None = None
  60. scheduled_time: datetime | None = None
  61. require_previous_success: bool | None = None
  62. auto_off_after: bool | None = None
  63. manual_start: bool | None = None
  64. ams_mapping: list[int] | None = None
  65. plate_id: int | None = None
  66. # Print options
  67. bed_levelling: bool | None = None
  68. flow_cali: bool | None = None
  69. vibration_cali: bool | None = None
  70. layer_inspect: bool | None = None
  71. timelapse: bool | None = None
  72. use_ams: bool | None = None
  73. nozzle_offset_cali: bool | None = None
  74. # Auto-print G-code injection
  75. gcode_injection: bool | None = None
  76. # H2C dual-nozzle-rack slicer pick (#1780). Both fields are opaque
  77. # JSON-encoded structures BambuStudio sends in its project_file MQTT
  78. # body; sent back to the printer verbatim on dispatch. list[int] for
  79. # nozzle_mapping (per-filament physical nozzle position IDs), list[dict]
  80. # for nozzles_info (per-extruder rack metadata).
  81. nozzle_mapping: list[int] | None = None
  82. nozzles_info: list[dict] | None = None
  83. class PrintQueueItemResponse(BaseModel):
  84. id: int
  85. printer_id: int | None # None = unassigned
  86. target_model: str | None = None # Target printer model for model-based assignment
  87. target_location: str | None = None # Target location filter for model-based assignment
  88. required_filament_types: list[str] | None = None # Required filament types for model-based assignment
  89. filament_overrides: list[dict] | None = None # Filament overrides for model-based assignment
  90. waiting_reason: str | None = None # Why a model-based job hasn't started yet
  91. archive_id: int | None # None if library_file_id is set (archive created at print start)
  92. library_file_id: int | None # For queue items from library files
  93. position: int
  94. scheduled_time: UTCDatetime
  95. require_previous_success: bool
  96. auto_off_after: bool
  97. manual_start: bool
  98. # True when the dispatch scheduler last evaluated this item and the
  99. # assigned spool could not satisfy at least one slot's required grams
  100. # (#1496). Display-only — the ▶ click recomputes deficit against live
  101. # spool state.
  102. filament_short: bool = False
  103. # User has acknowledged "Print Anyway" — scheduler skips the deficit check
  104. # for this item (#1698-followup).
  105. skip_filament_check: bool = False
  106. ams_mapping: list[int] | None = None
  107. plate_id: int | None = None # Plate ID for multi-plate 3MF files
  108. # Print options
  109. bed_levelling: bool = True
  110. flow_cali: bool = False
  111. vibration_cali: bool = True
  112. layer_inspect: bool = False
  113. timelapse: bool = False
  114. use_ams: bool = True
  115. nozzle_offset_cali: bool = True
  116. status: Literal["pending", "printing", "completed", "failed", "skipped", "cancelled"]
  117. started_at: UTCDatetime
  118. completed_at: UTCDatetime
  119. error_message: str | None
  120. created_at: UTCDatetime
  121. # Nested info for UI (populated in route)
  122. archive_name: str | None = None
  123. archive_thumbnail: str | None = None
  124. # True when the linked archive has been soft-deleted (its files are gone
  125. # from disk). In that case the *archive_name* / *archive_thumbnail* /
  126. # downstream metadata fields are intentionally left None so the frontend
  127. # doesn't 404-storm the now-missing thumbnail / plates / plate-thumbnail
  128. # endpoints (#1348 follow-up). Frontends can render a "source deleted"
  129. # badge based on this flag.
  130. archive_deleted: bool = False
  131. library_file_name: str | None = None # Name of library file (if library_file_id is set)
  132. library_file_thumbnail: str | None = None # Thumbnail of library file
  133. printer_name: str | None = None
  134. print_time_seconds: int | None = None # Estimated print time from archive or library file
  135. filament_used_grams: float | None = None # Estimated print weight from archive or library file
  136. filament_type: str | None = None # e.g. "PLA", "PETG" (from archive/library file)
  137. filament_color: str | None = None # e.g. "#FFFFFF" (from archive/library file)
  138. layer_height: float | None = None # e.g. 0.2 (from archive/library file)
  139. nozzle_diameter: float | None = None # e.g. 0.4 (from archive/library file)
  140. sliced_for_model: str | None = None # e.g. "P1S" (from archive/library file)
  141. # Build plate type (e.g. "Textured PEI Plate") so the user knows which
  142. # plate to mount on the printer (#1281). Per-plate accurate on multi-plate
  143. # 3MFs: when `plate_id` is set, the value is the matching plate's
  144. # `curr_bed_type` rather than the archive-level first-plate default.
  145. bed_type: str | None = None
  146. # User tracking (Issue #206)
  147. created_by_id: int | None = None
  148. created_by_username: str | None = None
  149. # Batch grouping
  150. batch_id: int | None = None
  151. batch_name: str | None = None
  152. # Shortest-job-first scheduling
  153. been_jumped: bool = False
  154. # Auto-print G-code injection
  155. gcode_injection: bool = False
  156. # H2C dual-nozzle-rack slicer pick (#1780). Surface for any future
  157. # "edit print → choose nozzle" UI; null on every model except O1C2
  158. # uploads from BambuStudio.
  159. nozzle_mapping: list[int] | None = None
  160. nozzles_info: list[dict] | None = None
  161. class Config:
  162. from_attributes = True
  163. class PrintQueueReorderItem(BaseModel):
  164. id: int
  165. position: int
  166. class PrintQueueReorder(BaseModel):
  167. items: list[PrintQueueReorderItem]
  168. class PrintQueueBulkUpdate(BaseModel):
  169. """Bulk update multiple queue items with the same values."""
  170. item_ids: list[int]
  171. # Fields to update (all optional - only set fields are applied)
  172. printer_id: int | None = None
  173. scheduled_time: datetime | None = None
  174. require_previous_success: bool | None = None
  175. auto_off_after: bool | None = None
  176. manual_start: bool | None = None
  177. # Print options
  178. bed_levelling: bool | None = None
  179. flow_cali: bool | None = None
  180. vibration_cali: bool | None = None
  181. layer_inspect: bool | None = None
  182. timelapse: bool | None = None
  183. use_ams: bool | None = None
  184. nozzle_offset_cali: bool | None = None
  185. # Auto-print G-code injection
  186. gcode_injection: bool | None = None
  187. class PrintQueueBulkUpdateResponse(BaseModel):
  188. """Response for bulk update operation."""
  189. updated_count: int
  190. skipped_count: int # Items that were not pending
  191. message: str
  192. class PrintBatchCreate(BaseModel):
  193. """Create a batch, either empty (multi-plate pre-batch flow) or by
  194. assigning existing pending queue items into it (manual "Group as batch")."""
  195. name: str
  196. archive_id: int | None = None
  197. library_file_id: int | None = None
  198. # Existing pending queue items to assign to this batch. None / empty for
  199. # the empty-batch flow (client passes the returned id on subsequent
  200. # addToQueue calls).
  201. item_ids: list[int] | None = None
  202. class PrintBatchUngroupResponse(BaseModel):
  203. """Response after ungrouping a batch."""
  204. ungrouped_count: int
  205. message: str
  206. class PrintBatchResponse(BaseModel):
  207. """Response for a print batch with progress stats."""
  208. id: int
  209. name: str
  210. archive_id: int | None = None
  211. library_file_id: int | None = None
  212. quantity: int
  213. status: str
  214. created_at: UTCDatetime
  215. created_by_id: int | None = None
  216. created_by_username: str | None = None
  217. # Derived counts
  218. pending_count: int = 0
  219. printing_count: int = 0
  220. completed_count: int = 0
  221. failed_count: int = 0
  222. cancelled_count: int = 0
  223. class Config:
  224. from_attributes = True