printer.py 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. from datetime import datetime
  2. from pydantic import BaseModel, Field
  3. class PrinterBase(BaseModel):
  4. name: str = Field(..., min_length=1, max_length=100)
  5. serial_number: str = Field(..., min_length=1, max_length=50)
  6. ip_address: str = Field(
  7. ...,
  8. max_length=253,
  9. pattern=r"^(\d{1,3}(\.\d{1,3}){3}|[a-zA-Z0-9]([a-zA-Z0-9\-]{0,61}[a-zA-Z0-9])?(\.[a-zA-Z0-9]([a-zA-Z0-9\-]{0,61}[a-zA-Z0-9])?)*)$",
  10. )
  11. access_code: str = Field(..., min_length=1, max_length=20)
  12. model: str | None = None
  13. location: str | None = None # Group/location name
  14. auto_archive: bool = True
  15. external_camera_url: str | None = None
  16. external_camera_type: str | None = None # "mjpeg", "rtsp", "snapshot", "usb"
  17. external_camera_enabled: bool = False
  18. class PrinterCreate(PrinterBase):
  19. pass
  20. class PlateDetectionROI(BaseModel):
  21. """Region of interest for plate detection (percentages 0.0-1.0)."""
  22. x: float = Field(..., ge=0.0, le=1.0) # X start %
  23. y: float = Field(..., ge=0.0, le=1.0) # Y start %
  24. w: float = Field(..., ge=0.0, le=1.0) # Width %
  25. h: float = Field(..., ge=0.0, le=1.0) # Height %
  26. class PrinterUpdate(BaseModel):
  27. name: str | None = None
  28. ip_address: str | None = Field(
  29. default=None,
  30. max_length=253,
  31. pattern=r"^(\d{1,3}(\.\d{1,3}){3}|[a-zA-Z0-9]([a-zA-Z0-9\-]{0,61}[a-zA-Z0-9])?(\.[a-zA-Z0-9]([a-zA-Z0-9\-]{0,61}[a-zA-Z0-9])?)*)$",
  32. )
  33. access_code: str | None = None
  34. model: str | None = None
  35. location: str | None = None
  36. is_active: bool | None = None
  37. auto_archive: bool | None = None
  38. print_hours_offset: float | None = None
  39. external_camera_url: str | None = None
  40. external_camera_type: str | None = None
  41. external_camera_enabled: bool | None = None
  42. plate_detection_enabled: bool | None = None
  43. plate_detection_roi: PlateDetectionROI | None = None
  44. class PrinterResponse(PrinterBase):
  45. id: int
  46. is_active: bool
  47. nozzle_count: int = 1 # 1 or 2, auto-detected from MQTT
  48. print_hours_offset: float = 0.0
  49. external_camera_url: str | None = None
  50. external_camera_type: str | None = None
  51. external_camera_enabled: bool = False
  52. plate_detection_enabled: bool = False
  53. plate_detection_roi: PlateDetectionROI | None = None
  54. created_at: datetime
  55. updated_at: datetime
  56. class Config:
  57. from_attributes = True
  58. @classmethod
  59. def from_orm_with_roi(cls, printer) -> "PrinterResponse":
  60. """Create response from ORM model, converting ROI fields to nested object."""
  61. data = {
  62. "id": printer.id,
  63. "name": printer.name,
  64. "serial_number": printer.serial_number,
  65. "ip_address": printer.ip_address,
  66. "access_code": printer.access_code,
  67. "model": printer.model,
  68. "location": printer.location,
  69. "auto_archive": printer.auto_archive,
  70. "external_camera_url": printer.external_camera_url,
  71. "external_camera_type": printer.external_camera_type,
  72. "external_camera_enabled": printer.external_camera_enabled,
  73. "is_active": printer.is_active,
  74. "nozzle_count": printer.nozzle_count,
  75. "print_hours_offset": printer.print_hours_offset,
  76. "plate_detection_enabled": printer.plate_detection_enabled,
  77. "created_at": printer.created_at,
  78. "updated_at": printer.updated_at,
  79. }
  80. # Build ROI object if any ROI field is set
  81. if any(
  82. [
  83. printer.plate_detection_roi_x is not None,
  84. printer.plate_detection_roi_y is not None,
  85. printer.plate_detection_roi_w is not None,
  86. printer.plate_detection_roi_h is not None,
  87. ]
  88. ):
  89. data["plate_detection_roi"] = PlateDetectionROI(
  90. x=printer.plate_detection_roi_x or 0.15,
  91. y=printer.plate_detection_roi_y or 0.35,
  92. w=printer.plate_detection_roi_w or 0.70,
  93. h=printer.plate_detection_roi_h or 0.55,
  94. )
  95. return cls(**data)
  96. class HMSErrorResponse(BaseModel):
  97. code: str
  98. attr: int = 0 # Attribute value for constructing wiki URL
  99. module: int
  100. severity: int # 1=fatal, 2=serious, 3=common, 4=info
  101. class AMSTray(BaseModel):
  102. id: int
  103. tray_color: str | None = None
  104. tray_type: str | None = None
  105. tray_sub_brands: str | None = None # Full name like "PLA Basic", "PETG HF"
  106. tray_id_name: str | None = None # Bambu filament ID like "A00-Y2" (can decode to color)
  107. tray_info_idx: str | None = None # Filament preset ID like "GFA00"
  108. remain: int = 0
  109. k: float | None = None # Pressure advance value (from tray or K-profile lookup)
  110. cali_idx: int | None = None # Calibration index for K-profile lookup
  111. tag_uid: str | None = None # RFID tag UID (any tag)
  112. tray_uuid: str | None = None # Bambu Lab spool UUID (32-char hex)
  113. nozzle_temp_min: int | None = None # Min nozzle temperature
  114. nozzle_temp_max: int | None = None # Max nozzle temperature
  115. class AMSUnit(BaseModel):
  116. id: int
  117. humidity: int | None = None
  118. temp: float | None = None
  119. is_ams_ht: bool = False # True for AMS-HT (single spool), False for regular AMS (4 spools)
  120. tray: list[AMSTray] = []
  121. class NozzleInfoResponse(BaseModel):
  122. nozzle_type: str = "" # "stainless_steel" or "hardened_steel"
  123. nozzle_diameter: str = "" # e.g., "0.4"
  124. class NozzleRackSlot(BaseModel):
  125. """H2C nozzle rack slot (6-position tool-changer dock)."""
  126. id: int = 0
  127. nozzle_type: str = ""
  128. nozzle_diameter: str = ""
  129. wear: int | None = None
  130. stat: int | None = None # Nozzle status (e.g. mounted/docked)
  131. class PrintOptionsResponse(BaseModel):
  132. """AI detection and print options from xcam data."""
  133. # Core AI detectors
  134. spaghetti_detector: bool = False
  135. print_halt: bool = False
  136. halt_print_sensitivity: str = "medium" # Spaghetti sensitivity
  137. first_layer_inspector: bool = False
  138. printing_monitor: bool = False
  139. buildplate_marker_detector: bool = False
  140. allow_skip_parts: bool = False
  141. # Additional AI detectors (decoded from cfg bitmask)
  142. nozzle_clumping_detector: bool = True
  143. nozzle_clumping_sensitivity: str = "medium"
  144. pileup_detector: bool = True
  145. pileup_sensitivity: str = "medium"
  146. airprint_detector: bool = True
  147. airprint_sensitivity: str = "medium"
  148. auto_recovery_step_loss: bool = True
  149. filament_tangle_detect: bool = False
  150. class PrinterStatus(BaseModel):
  151. id: int
  152. name: str
  153. connected: bool
  154. state: str | None = None
  155. current_print: str | None = None
  156. subtask_name: str | None = None
  157. gcode_file: str | None = None
  158. progress: float | None = None
  159. remaining_time: int | None = None
  160. layer_num: int | None = None
  161. total_layers: int | None = None
  162. temperatures: dict | None = None
  163. cover_url: str | None = None
  164. hms_errors: list[HMSErrorResponse] = []
  165. ams: list[AMSUnit] = []
  166. ams_exists: bool = False
  167. vt_tray: AMSTray | None = None # Virtual tray / external spool
  168. sdcard: bool = False # SD card inserted
  169. store_to_sdcard: bool = False # Store sent files on SD card
  170. timelapse: bool = False # Timelapse recording active
  171. ipcam: bool = False # Live view enabled
  172. wifi_signal: int | None = None # WiFi signal strength in dBm
  173. nozzles: list[NozzleInfoResponse] = [] # Nozzle hardware info (index 0=left/primary, 1=right)
  174. nozzle_rack: list[NozzleRackSlot] = [] # H2C 6-nozzle tool-changer rack
  175. print_options: PrintOptionsResponse | None = None # AI detection and print options
  176. # Calibration stage tracking
  177. stg_cur: int = -1 # Current stage number (-1 = not calibrating)
  178. stg_cur_name: str | None = None # Human-readable current stage name
  179. stg: list[int] = [] # List of stage numbers in calibration sequence
  180. # Air conditioning mode (0=cooling, 1=heating)
  181. airduct_mode: int = 0
  182. # Print speed level (1=silent, 2=standard, 3=sport, 4=ludicrous)
  183. speed_level: int = 2
  184. # Chamber light on/off
  185. chamber_light: bool = False
  186. # Active extruder for dual nozzle (0=right, 1=left)
  187. active_extruder: int = 0
  188. # AMS mapping for dual nozzle: which AMS is connected to which nozzle
  189. ams_mapping: list[int] = []
  190. # Per-AMS extruder map: {ams_id: extruder_id} where 0=right, 1=left
  191. ams_extruder_map: dict[str, int] = {}
  192. # Currently loaded tray (global ID): 254 = external spool, 255 = no filament
  193. tray_now: int = 255
  194. # AMS status for filament change tracking
  195. # Main status: 0=idle, 1=filament_change, 2=rfid_identifying, 3=assist, 4=calibration
  196. ams_status_main: int = 0
  197. # Sub status: specific step within filament change (when main=1)
  198. # Known values: 4=retraction, 6=load verification, 7=purge
  199. ams_status_sub: int = 0
  200. # mc_print_sub_stage - filament change step indicator used by OrcaSlicer/BambuStudio
  201. mc_print_sub_stage: int = 0
  202. # Timestamp of last AMS data update (for RFID refresh detection)
  203. last_ams_update: float = 0.0
  204. # Number of printable objects in current print (for skip objects feature)
  205. printable_objects_count: int = 0
  206. # Fan speeds (0-100 percentage, None if not available for this model)
  207. cooling_fan_speed: int | None = None # Part cooling fan
  208. big_fan1_speed: int | None = None # Auxiliary fan
  209. big_fan2_speed: int | None = None # Chamber/exhaust fan
  210. heatbreak_fan_speed: int | None = None # Hotend heatbreak fan
  211. # Firmware version (from info.module[name="ota"].sw_ver)
  212. firmware_version: str | None = None