printer.py 10 KB

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