printer.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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. external_camera_snapshot_url: str | None = None # Optional single-frame override; #1177
  19. camera_rotation: int = 0 # 0, 90, 180, 270 degrees
  20. class PrinterCreate(PrinterBase):
  21. pass
  22. class PlateDetectionROI(BaseModel):
  23. """Region of interest for plate detection (percentages 0.0-1.0)."""
  24. x: float = Field(..., ge=0.0, le=1.0) # X start %
  25. y: float = Field(..., ge=0.0, le=1.0) # Y start %
  26. w: float = Field(..., ge=0.0, le=1.0) # Width %
  27. h: float = Field(..., ge=0.0, le=1.0) # Height %
  28. class PrinterUpdate(BaseModel):
  29. name: str | None = None
  30. ip_address: str | None = Field(
  31. default=None,
  32. max_length=253,
  33. 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])?)*)$",
  34. )
  35. access_code: str | None = None
  36. model: str | None = None
  37. location: str | None = None
  38. is_active: bool | None = None
  39. auto_archive: bool | None = None
  40. print_hours_offset: float | None = None
  41. external_camera_url: str | None = None
  42. external_camera_type: str | None = None
  43. external_camera_enabled: bool | None = None
  44. external_camera_snapshot_url: str | None = None # #1177
  45. camera_rotation: int | None = None # 0, 90, 180, 270 degrees
  46. plate_detection_enabled: bool | None = None
  47. plate_detection_roi: PlateDetectionROI | None = None
  48. class PrinterResponse(PrinterBase):
  49. id: int
  50. is_active: bool
  51. nozzle_count: int = 1 # 1 or 2, auto-detected from MQTT
  52. print_hours_offset: float = 0.0
  53. external_camera_url: str | None = None
  54. external_camera_type: str | None = None
  55. external_camera_enabled: bool = False
  56. external_camera_snapshot_url: str | None = None # #1177
  57. camera_rotation: int = 0 # 0, 90, 180, 270 degrees
  58. plate_detection_enabled: bool = False
  59. plate_detection_roi: PlateDetectionROI | None = None
  60. created_at: datetime
  61. updated_at: datetime
  62. class Config:
  63. from_attributes = True
  64. @classmethod
  65. def from_orm_with_roi(cls, printer) -> "PrinterResponse":
  66. """Create response from ORM model, converting ROI fields to nested object."""
  67. data = {
  68. "id": printer.id,
  69. "name": printer.name,
  70. "serial_number": printer.serial_number,
  71. "ip_address": printer.ip_address,
  72. "access_code": printer.access_code,
  73. "model": printer.model,
  74. "location": printer.location,
  75. "auto_archive": printer.auto_archive,
  76. "external_camera_url": printer.external_camera_url,
  77. "external_camera_type": printer.external_camera_type,
  78. "external_camera_enabled": printer.external_camera_enabled,
  79. "external_camera_snapshot_url": printer.external_camera_snapshot_url,
  80. "camera_rotation": printer.camera_rotation,
  81. "is_active": printer.is_active,
  82. "nozzle_count": printer.nozzle_count,
  83. "print_hours_offset": printer.print_hours_offset,
  84. "plate_detection_enabled": printer.plate_detection_enabled,
  85. "created_at": printer.created_at,
  86. "updated_at": printer.updated_at,
  87. }
  88. # Build ROI object if any ROI field is set
  89. if any(
  90. [
  91. printer.plate_detection_roi_x is not None,
  92. printer.plate_detection_roi_y is not None,
  93. printer.plate_detection_roi_w is not None,
  94. printer.plate_detection_roi_h is not None,
  95. ]
  96. ):
  97. data["plate_detection_roi"] = PlateDetectionROI(
  98. x=printer.plate_detection_roi_x or 0.15,
  99. y=printer.plate_detection_roi_y or 0.35,
  100. w=printer.plate_detection_roi_w or 0.70,
  101. h=printer.plate_detection_roi_h or 0.55,
  102. )
  103. return cls(**data)
  104. class HMSErrorResponse(BaseModel):
  105. code: str
  106. attr: int = 0 # Attribute value for constructing wiki URL
  107. module: int
  108. severity: int # 1=fatal, 2=serious, 3=common, 4=info
  109. class AMSTray(BaseModel):
  110. id: int
  111. tray_color: str | None = None
  112. tray_type: str | None = None
  113. tray_sub_brands: str | None = None # Full name like "PLA Basic", "PETG HF"
  114. tray_id_name: str | None = None # Bambu filament ID like "A00-Y2" (can decode to color)
  115. tray_info_idx: str | None = None # Filament preset ID like "GFA00"
  116. remain: int = 0
  117. k: float | None = None # Pressure advance value (from tray or K-profile lookup)
  118. cali_idx: int | None = None # Calibration index for K-profile lookup
  119. tag_uid: str | None = None # RFID tag UID (any tag)
  120. tray_uuid: str | None = None # Bambu Lab spool UUID (32-char hex)
  121. nozzle_temp_min: int | None = None # Min nozzle temperature
  122. nozzle_temp_max: int | None = None # Max nozzle temperature
  123. drying_temp: int | None = None # RFID-recommended drying temp
  124. drying_time: int | None = None # RFID-recommended drying time (hours)
  125. state: int | None = None # AMS tray state: 9=empty, 10=spool present not loaded, 11=loaded
  126. class AMSUnit(BaseModel):
  127. id: int
  128. humidity: int | None = None
  129. temp: float | None = None
  130. is_ams_ht: bool = False # True for AMS-HT (single spool), False for regular AMS (4 spools)
  131. tray: list[AMSTray] = []
  132. serial_number: str = "" # AMS unit serial number (sn from MQTT)
  133. sw_ver: str = "" # AMS firmware version (from get_version info.module)
  134. dry_time: int = 0 # Minutes remaining (0 = not drying, >0 = drying active)
  135. dry_status: int = 0 # 0=Off, 1=Checking, 2=Drying, 3=Cooling, 4=Stopping, 5=Error
  136. dry_sub_status: int = 0 # 0=Off, 1=Heating, 2=Dehumidify
  137. dry_sf_reason: list[int] = [] # Cannot-dry reasons from firmware (see CannotDryReason)
  138. module_type: str = "" # "ams", "n3f", "n3s"
  139. class NozzleInfoResponse(BaseModel):
  140. nozzle_type: str = "" # "stainless_steel" or "hardened_steel"
  141. nozzle_diameter: str = "" # e.g., "0.4"
  142. class NozzleRackSlot(BaseModel):
  143. """H2C nozzle rack slot (6-position tool-changer dock)."""
  144. id: int = 0
  145. nozzle_type: str = ""
  146. nozzle_diameter: str = ""
  147. wear: int | None = None
  148. stat: int | None = None # Nozzle status (e.g. mounted/docked)
  149. max_temp: int = 0 # Max temperature rating °C (0 = not set)
  150. serial_number: str = "" # Nozzle serial number
  151. filament_color: str = "" # RGBA hex ("00000000" = no filament)
  152. filament_id: str = "" # Bambu filament ID
  153. filament_type: str = "" # Material type (e.g. "PLA", "PETG")
  154. class AmsLabelBody(BaseModel):
  155. label: str = Field(..., min_length=1, max_length=100)
  156. ams_serial: str = Field(default="", max_length=50)
  157. class FilaSwitchResponse(BaseModel):
  158. """Filament Track Switch (FTS) state — accessory that mediates AMS-to-extruder routing.
  159. When installed, the AMS info field reports bits 8-11 = 0xE (uninitialized)
  160. because slots are dynamically routed via the FTS rather than tied to a
  161. specific extruder. Frontend uses `installed` to suppress the per-extruder
  162. slot filter in the print modal. See #1162.
  163. """
  164. installed: bool = False
  165. # in[track] = currently loaded slot for that track (-1 = empty)
  166. in_slots: list[int] = []
  167. # out[track] = extruder this track terminates at (0 = right, 1 = left)
  168. out_extruders: list[int] = []
  169. stat: int = 0
  170. info: int = 0
  171. class PrintOptionsResponse(BaseModel):
  172. """AI detection and print options from xcam data."""
  173. # Core AI detectors
  174. spaghetti_detector: bool = False
  175. print_halt: bool = False
  176. halt_print_sensitivity: str = "medium" # Spaghetti sensitivity
  177. first_layer_inspector: bool = False
  178. printing_monitor: bool = False
  179. buildplate_marker_detector: bool = False
  180. allow_skip_parts: bool = False
  181. # Additional AI detectors (decoded from cfg bitmask)
  182. nozzle_clumping_detector: bool = True
  183. nozzle_clumping_sensitivity: str = "medium"
  184. pileup_detector: bool = True
  185. pileup_sensitivity: str = "medium"
  186. airprint_detector: bool = True
  187. airprint_sensitivity: str = "medium"
  188. auto_recovery_step_loss: bool = True
  189. filament_tangle_detect: bool = False
  190. class PrinterStatus(BaseModel):
  191. id: int
  192. name: str
  193. connected: bool
  194. state: str | None = None
  195. current_print: str | None = None
  196. subtask_name: str | None = None
  197. gcode_file: str | None = None
  198. progress: float | None = None
  199. remaining_time: int | None = None
  200. layer_num: int | None = None
  201. total_layers: int | None = None
  202. temperatures: dict | None = None
  203. cover_url: str | None = None
  204. hms_errors: list[HMSErrorResponse] = []
  205. ams: list[AMSUnit] = []
  206. ams_exists: bool = False
  207. vt_tray: list[AMSTray] = [] # Virtual tray / external spool(s)
  208. sdcard: bool = False # SD card inserted
  209. store_to_sdcard: bool = False # Store sent files on SD card
  210. timelapse: bool = False # Timelapse recording active
  211. ipcam: bool = False # Live view enabled
  212. wifi_signal: int | None = None # WiFi signal strength in dBm
  213. wired_network: bool = False # Ethernet connection detected
  214. door_open: bool = False # Enclosure door open (X1/P1S/P2S/H2*)
  215. nozzles: list[NozzleInfoResponse] = [] # Nozzle hardware info (index 0=left/primary, 1=right)
  216. nozzle_rack: list[NozzleRackSlot] = [] # H2C 6-nozzle tool-changer rack
  217. print_options: PrintOptionsResponse | None = None # AI detection and print options
  218. # Calibration stage tracking
  219. stg_cur: int = -1 # Current stage number (-1 = not calibrating)
  220. stg_cur_name: str | None = None # Human-readable current stage name
  221. stg: list[int] = [] # List of stage numbers in calibration sequence
  222. # Air conditioning mode (0=cooling, 1=heating)
  223. airduct_mode: int = 0
  224. # Print speed level (1=silent, 2=standard, 3=sport, 4=ludicrous)
  225. speed_level: int = 2
  226. # Chamber light on/off
  227. chamber_light: bool = False
  228. # Active extruder for dual nozzle (0=right, 1=left)
  229. active_extruder: int = 0
  230. # AMS mapping for dual nozzle: which AMS is connected to which nozzle
  231. ams_mapping: list[int] = []
  232. # Per-AMS extruder map: {ams_id: extruder_id} where 0=right, 1=left
  233. ams_extruder_map: dict[str, int] = {}
  234. # Filament Track Switch (FTS) accessory — when installed, AMS reports
  235. # bits 8-11 = 0xE (uninitialized) and routing is dynamic via the FTS. See #1162.
  236. fila_switch: FilaSwitchResponse | None = None
  237. # Currently loaded tray (global ID): 254 = external spool, 255 = no filament
  238. tray_now: int = 255
  239. # AMS status for filament change tracking
  240. # Main status: 0=idle, 1=filament_change, 2=rfid_identifying, 3=assist, 4=calibration
  241. ams_status_main: int = 0
  242. # Sub status: specific step within filament change (when main=1)
  243. # Known values: 4=retraction, 6=load verification, 7=purge
  244. ams_status_sub: int = 0
  245. # mc_print_sub_stage - filament change step indicator used by OrcaSlicer/BambuStudio
  246. mc_print_sub_stage: int = 0
  247. # Timestamp of last AMS data update (for RFID refresh detection)
  248. last_ams_update: float = 0.0
  249. # Number of printable objects in current print (for skip objects feature)
  250. printable_objects_count: int = 0
  251. # Fan speeds (0-100 percentage, None if not available for this model)
  252. cooling_fan_speed: int | None = None # Part cooling fan
  253. big_fan1_speed: int | None = None # Auxiliary fan
  254. big_fan2_speed: int | None = None # Chamber/exhaust fan
  255. heatbreak_fan_speed: int | None = None # Hotend heatbreak fan
  256. # Firmware version (from info.module[name="ota"].sw_ver)
  257. firmware_version: str | None = None
  258. # Developer LAN mode: True = enabled, False = disabled (MQTT encryption), None = unknown
  259. developer_mode: bool | None = None
  260. # Queue: printer is awaiting the user to acknowledge the build plate is cleared
  261. # after a finished/failed print. Persisted across restarts (#961).
  262. awaiting_plate_clear: bool = False
  263. # AMS drying support
  264. supports_drying: bool = False
  265. # Linked archive for the active print (resolved via subtask_id). Frontend uses
  266. # this to fetch plate metadata and show the plate name when the source 3MF is
  267. # multi-plate (#881 follow-up).
  268. current_archive_id: int | None = None
  269. # 1-indexed plate number parsed from gcode_file (e.g. /Metadata/plate_2.gcode).
  270. # Set for every active print regardless of plate count; the frontend decides
  271. # whether to render it based on current_archive_id's is_multi_plate flag.
  272. current_plate_id: int | None = None