printer.py 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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. max_temp: int = 0 # Max temperature rating °C (0 = not set)
  132. serial_number: str = "" # Nozzle serial number
  133. filament_color: str = "" # RGBA hex ("00000000" = no filament)
  134. filament_id: str = "" # Bambu filament ID
  135. filament_type: str = "" # Material type (e.g. "PLA", "PETG")
  136. class PrintOptionsResponse(BaseModel):
  137. """AI detection and print options from xcam data."""
  138. # Core AI detectors
  139. spaghetti_detector: bool = False
  140. print_halt: bool = False
  141. halt_print_sensitivity: str = "medium" # Spaghetti sensitivity
  142. first_layer_inspector: bool = False
  143. printing_monitor: bool = False
  144. buildplate_marker_detector: bool = False
  145. allow_skip_parts: bool = False
  146. # Additional AI detectors (decoded from cfg bitmask)
  147. nozzle_clumping_detector: bool = True
  148. nozzle_clumping_sensitivity: str = "medium"
  149. pileup_detector: bool = True
  150. pileup_sensitivity: str = "medium"
  151. airprint_detector: bool = True
  152. airprint_sensitivity: str = "medium"
  153. auto_recovery_step_loss: bool = True
  154. filament_tangle_detect: bool = False
  155. class PrinterStatus(BaseModel):
  156. id: int
  157. name: str
  158. connected: bool
  159. state: str | None = None
  160. current_print: str | None = None
  161. subtask_name: str | None = None
  162. gcode_file: str | None = None
  163. progress: float | None = None
  164. remaining_time: int | None = None
  165. layer_num: int | None = None
  166. total_layers: int | None = None
  167. temperatures: dict | None = None
  168. cover_url: str | None = None
  169. hms_errors: list[HMSErrorResponse] = []
  170. ams: list[AMSUnit] = []
  171. ams_exists: bool = False
  172. vt_tray: AMSTray | None = None # Virtual tray / external spool
  173. sdcard: bool = False # SD card inserted
  174. store_to_sdcard: bool = False # Store sent files on SD card
  175. timelapse: bool = False # Timelapse recording active
  176. ipcam: bool = False # Live view enabled
  177. wifi_signal: int | None = None # WiFi signal strength in dBm
  178. nozzles: list[NozzleInfoResponse] = [] # Nozzle hardware info (index 0=left/primary, 1=right)
  179. nozzle_rack: list[NozzleRackSlot] = [] # H2C 6-nozzle tool-changer rack
  180. print_options: PrintOptionsResponse | None = None # AI detection and print options
  181. # Calibration stage tracking
  182. stg_cur: int = -1 # Current stage number (-1 = not calibrating)
  183. stg_cur_name: str | None = None # Human-readable current stage name
  184. stg: list[int] = [] # List of stage numbers in calibration sequence
  185. # Air conditioning mode (0=cooling, 1=heating)
  186. airduct_mode: int = 0
  187. # Print speed level (1=silent, 2=standard, 3=sport, 4=ludicrous)
  188. speed_level: int = 2
  189. # Chamber light on/off
  190. chamber_light: bool = False
  191. # Active extruder for dual nozzle (0=right, 1=left)
  192. active_extruder: int = 0
  193. # AMS mapping for dual nozzle: which AMS is connected to which nozzle
  194. ams_mapping: list[int] = []
  195. # Per-AMS extruder map: {ams_id: extruder_id} where 0=right, 1=left
  196. ams_extruder_map: dict[str, int] = {}
  197. # Currently loaded tray (global ID): 254 = external spool, 255 = no filament
  198. tray_now: int = 255
  199. # AMS status for filament change tracking
  200. # Main status: 0=idle, 1=filament_change, 2=rfid_identifying, 3=assist, 4=calibration
  201. ams_status_main: int = 0
  202. # Sub status: specific step within filament change (when main=1)
  203. # Known values: 4=retraction, 6=load verification, 7=purge
  204. ams_status_sub: int = 0
  205. # mc_print_sub_stage - filament change step indicator used by OrcaSlicer/BambuStudio
  206. mc_print_sub_stage: int = 0
  207. # Timestamp of last AMS data update (for RFID refresh detection)
  208. last_ams_update: float = 0.0
  209. # Number of printable objects in current print (for skip objects feature)
  210. printable_objects_count: int = 0
  211. # Fan speeds (0-100 percentage, None if not available for this model)
  212. cooling_fan_speed: int | None = None # Part cooling fan
  213. big_fan1_speed: int | None = None # Auxiliary fan
  214. big_fan2_speed: int | None = None # Chamber/exhaust fan
  215. heatbreak_fan_speed: int | None = None # Hotend heatbreak fan
  216. # Firmware version (from info.module[name="ota"].sw_ver)
  217. firmware_version: str | None = None