printer_manager.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. import asyncio
  2. from collections.abc import Callable
  3. from sqlalchemy import select
  4. from sqlalchemy.ext.asyncio import AsyncSession
  5. from backend.app.models.printer import Printer
  6. from backend.app.services.bambu_mqtt import BambuMQTTClient, MQTTLogEntry, PrinterState
  7. class PrinterManager:
  8. """Manager for multiple printer connections."""
  9. def __init__(self):
  10. self._clients: dict[int, BambuMQTTClient] = {}
  11. self._on_print_start: Callable[[int, dict], None] | None = None
  12. self._on_print_complete: Callable[[int, dict], None] | None = None
  13. self._on_status_change: Callable[[int, PrinterState], None] | None = None
  14. self._on_ams_change: Callable[[int, list], None] | None = None
  15. self._loop: asyncio.AbstractEventLoop | None = None
  16. def set_event_loop(self, loop: asyncio.AbstractEventLoop):
  17. """Set the event loop for async callbacks."""
  18. self._loop = loop
  19. def set_print_start_callback(self, callback: Callable[[int, dict], None]):
  20. """Set callback for print start events."""
  21. self._on_print_start = callback
  22. def set_print_complete_callback(self, callback: Callable[[int, dict], None]):
  23. """Set callback for print completion events."""
  24. self._on_print_complete = callback
  25. def set_status_change_callback(self, callback: Callable[[int, PrinterState], None]):
  26. """Set callback for status change events."""
  27. self._on_status_change = callback
  28. def set_ams_change_callback(self, callback: Callable[[int, list], None]):
  29. """Set callback for AMS data change events."""
  30. self._on_ams_change = callback
  31. def _schedule_async(self, coro):
  32. """Schedule an async coroutine from a sync context.
  33. Captures exceptions from the coroutine and logs them to prevent
  34. silent failures in callbacks.
  35. """
  36. if self._loop and self._loop.is_running():
  37. future = asyncio.run_coroutine_threadsafe(coro, self._loop)
  38. def handle_exception(f):
  39. try:
  40. # This will re-raise any exception from the coroutine
  41. f.result()
  42. except Exception as e:
  43. import logging
  44. logging.getLogger(__name__).error(f"Exception in scheduled callback: {e}", exc_info=True)
  45. future.add_done_callback(handle_exception)
  46. async def connect_printer(self, printer: Printer) -> bool:
  47. """Connect to a printer."""
  48. if printer.id in self._clients:
  49. self.disconnect_printer(printer.id)
  50. printer_id = printer.id
  51. def on_state_change(state: PrinterState):
  52. if self._on_status_change:
  53. self._schedule_async(self._on_status_change(printer_id, state))
  54. def on_print_start(data: dict):
  55. if self._on_print_start:
  56. self._schedule_async(self._on_print_start(printer_id, data))
  57. def on_print_complete(data: dict):
  58. if self._on_print_complete:
  59. self._schedule_async(self._on_print_complete(printer_id, data))
  60. def on_ams_change(ams_data: list):
  61. if self._on_ams_change:
  62. self._schedule_async(self._on_ams_change(printer_id, ams_data))
  63. client = BambuMQTTClient(
  64. ip_address=printer.ip_address,
  65. serial_number=printer.serial_number,
  66. access_code=printer.access_code,
  67. on_state_change=on_state_change,
  68. on_print_start=on_print_start,
  69. on_print_complete=on_print_complete,
  70. on_ams_change=on_ams_change,
  71. )
  72. client.connect()
  73. self._clients[printer_id] = client
  74. # Wait a moment for connection
  75. await asyncio.sleep(1)
  76. return client.state.connected
  77. def disconnect_printer(self, printer_id: int):
  78. """Disconnect from a printer."""
  79. if printer_id in self._clients:
  80. self._clients[printer_id].disconnect()
  81. del self._clients[printer_id]
  82. def disconnect_all(self):
  83. """Disconnect from all printers."""
  84. for printer_id in list(self._clients.keys()):
  85. self.disconnect_printer(printer_id)
  86. def get_status(self, printer_id: int) -> PrinterState | None:
  87. """Get the current status of a printer (checks for stale connections)."""
  88. if printer_id in self._clients:
  89. client = self._clients[printer_id]
  90. # Check staleness and update connected state if needed
  91. client.check_staleness()
  92. return client.state
  93. return None
  94. def get_all_statuses(self) -> dict[int, PrinterState]:
  95. """Get status of all connected printers (checks for stale connections)."""
  96. result = {}
  97. for printer_id, client in self._clients.items():
  98. # Check staleness and update connected state if needed
  99. client.check_staleness()
  100. result[printer_id] = client.state
  101. return result
  102. def is_connected(self, printer_id: int) -> bool:
  103. """Check if a printer is connected (checks for stale connections)."""
  104. if printer_id in self._clients:
  105. client = self._clients[printer_id]
  106. # Check staleness and update connected state if needed
  107. return client.check_staleness()
  108. return False
  109. def get_client(self, printer_id: int) -> BambuMQTTClient | None:
  110. """Get the MQTT client for a printer."""
  111. return self._clients.get(printer_id)
  112. def mark_printer_offline(self, printer_id: int):
  113. """Mark a printer as offline and trigger status callback.
  114. This is used when we know the printer power was cut (e.g., smart plug turned off)
  115. to immediately update the UI without waiting for MQTT timeout.
  116. """
  117. import logging
  118. logger = logging.getLogger(__name__)
  119. if printer_id in self._clients:
  120. client = self._clients[printer_id]
  121. if client.state.connected:
  122. logger.info(f"Marking printer {printer_id} as offline (smart plug power off)")
  123. client.state.connected = False
  124. client.state.state = "unknown"
  125. # Trigger the status change callback to broadcast via WebSocket
  126. if self._on_status_change:
  127. self._schedule_async(self._on_status_change(printer_id, client.state))
  128. def start_print(self, printer_id: int, filename: str, plate_id: int = 1) -> bool:
  129. """Start a print on a connected printer."""
  130. if printer_id in self._clients:
  131. return self._clients[printer_id].start_print(filename, plate_id)
  132. return False
  133. def stop_print(self, printer_id: int) -> bool:
  134. """Stop the current print on a connected printer."""
  135. if printer_id in self._clients:
  136. return self._clients[printer_id].stop_print()
  137. return False
  138. async def wait_for_cooldown(
  139. self,
  140. printer_id: int,
  141. target_temp: float = 50.0,
  142. timeout: int = 600,
  143. check_interval: int = 10,
  144. ) -> bool:
  145. """Wait for the nozzle to cool down to a safe temperature.
  146. Args:
  147. printer_id: The printer to monitor
  148. target_temp: Target temperature to wait for (default 50°C)
  149. timeout: Maximum seconds to wait (default 600s = 10 min)
  150. check_interval: Seconds between temperature checks (default 10s)
  151. Returns:
  152. True if cooled down, False if timeout or not connected
  153. """
  154. import logging
  155. logger = logging.getLogger(__name__)
  156. elapsed = 0
  157. while elapsed < timeout:
  158. state = self.get_status(printer_id)
  159. if not state or not state.connected:
  160. logger.warning(f"Printer {printer_id} disconnected during cooldown wait")
  161. return False
  162. # Check nozzle temperature (and nozzle_2 for dual extruders)
  163. nozzle_temp = state.temperatures.get("nozzle", 0)
  164. nozzle_2_temp = state.temperatures.get("nozzle_2", 0)
  165. max_temp = max(nozzle_temp, nozzle_2_temp)
  166. if max_temp <= target_temp:
  167. logger.info(f"Printer {printer_id} cooled down to {max_temp}°C")
  168. return True
  169. logger.debug(f"Printer {printer_id} nozzle at {max_temp}°C, waiting for {target_temp}°C...")
  170. await asyncio.sleep(check_interval)
  171. elapsed += check_interval
  172. logger.warning(f"Printer {printer_id} cooldown timeout after {timeout}s")
  173. return False
  174. def enable_logging(self, printer_id: int, enabled: bool = True) -> bool:
  175. """Enable or disable MQTT logging for a printer."""
  176. if printer_id in self._clients:
  177. self._clients[printer_id].enable_logging(enabled)
  178. return True
  179. return False
  180. def get_logs(self, printer_id: int) -> list[MQTTLogEntry]:
  181. """Get MQTT logs for a printer."""
  182. if printer_id in self._clients:
  183. return self._clients[printer_id].get_logs()
  184. return []
  185. def clear_logs(self, printer_id: int) -> bool:
  186. """Clear MQTT logs for a printer."""
  187. if printer_id in self._clients:
  188. self._clients[printer_id].clear_logs()
  189. return True
  190. return False
  191. def is_logging_enabled(self, printer_id: int) -> bool:
  192. """Check if logging is enabled for a printer."""
  193. if printer_id in self._clients:
  194. return self._clients[printer_id].logging_enabled
  195. return False
  196. def request_status_update(self, printer_id: int) -> bool:
  197. """Request a full status update from the printer.
  198. This sends a 'pushall' command to get the latest data including nozzle info.
  199. """
  200. if printer_id in self._clients:
  201. return self._clients[printer_id].request_status_update()
  202. return False
  203. async def test_connection(
  204. self,
  205. ip_address: str,
  206. serial_number: str,
  207. access_code: str,
  208. ) -> dict:
  209. """Test connection to a printer without persisting."""
  210. client = BambuMQTTClient(
  211. ip_address=ip_address,
  212. serial_number=serial_number,
  213. access_code=access_code,
  214. )
  215. try:
  216. client.connect()
  217. await asyncio.sleep(2)
  218. result = {
  219. "success": client.state.connected,
  220. "state": client.state.state if client.state.connected else None,
  221. "model": client.state.raw_data.get("device_model"),
  222. }
  223. finally:
  224. client.disconnect()
  225. return result
  226. def printer_state_to_dict(state: PrinterState, printer_id: int | None = None) -> dict:
  227. """Convert PrinterState to a JSON-serializable dict."""
  228. # Parse AMS data from raw_data
  229. ams_units = []
  230. vt_tray = None
  231. raw_data = state.raw_data or {}
  232. if "ams" in raw_data and isinstance(raw_data["ams"], list):
  233. for ams_data in raw_data["ams"]:
  234. trays = []
  235. for tray in ams_data.get("tray", []):
  236. tag_uid = tray.get("tag_uid")
  237. if tag_uid in ("", "0000000000000000"):
  238. tag_uid = None
  239. tray_uuid = tray.get("tray_uuid")
  240. if tray_uuid in ("", "00000000000000000000000000000000"):
  241. tray_uuid = None
  242. trays.append(
  243. {
  244. "id": tray.get("id", 0),
  245. "tray_color": tray.get("tray_color"),
  246. "tray_type": tray.get("tray_type"),
  247. "tray_sub_brands": tray.get("tray_sub_brands"),
  248. "remain": tray.get("remain", 0),
  249. "k": tray.get("k"),
  250. "tag_uid": tag_uid,
  251. "tray_uuid": tray_uuid,
  252. }
  253. )
  254. # Prefer humidity_raw (actual percentage) over humidity (index 1-5)
  255. humidity_raw = ams_data.get("humidity_raw")
  256. humidity_idx = ams_data.get("humidity")
  257. humidity_value = None
  258. if humidity_raw is not None:
  259. try:
  260. humidity_value = int(humidity_raw)
  261. except (ValueError, TypeError):
  262. pass
  263. # Fall back to index if no raw value (index is 1-5, not percentage)
  264. if humidity_value is None and humidity_idx is not None:
  265. try:
  266. humidity_value = int(humidity_idx)
  267. except (ValueError, TypeError):
  268. pass
  269. # AMS-HT has 1 tray, regular AMS has 4 trays
  270. is_ams_ht = len(trays) == 1
  271. ams_units.append(
  272. {
  273. "id": ams_data.get("id", 0),
  274. "humidity": humidity_value,
  275. "temp": ams_data.get("temp"),
  276. "is_ams_ht": is_ams_ht,
  277. "tray": trays,
  278. }
  279. )
  280. # Parse virtual tray (external spool)
  281. if "vt_tray" in raw_data:
  282. vt_data = raw_data["vt_tray"]
  283. vt_tag_uid = vt_data.get("tag_uid")
  284. if vt_tag_uid in ("", "0000000000000000"):
  285. vt_tag_uid = None
  286. vt_tray = {
  287. "id": 254,
  288. "tray_color": vt_data.get("tray_color"),
  289. "tray_type": vt_data.get("tray_type"),
  290. "tray_sub_brands": vt_data.get("tray_sub_brands"),
  291. "remain": vt_data.get("remain", 0),
  292. "tag_uid": vt_tag_uid,
  293. }
  294. # Get ams_extruder_map from raw_data (populated by MQTT handler from AMS info field)
  295. ams_extruder_map = raw_data.get("ams_extruder_map", {})
  296. result = {
  297. "connected": state.connected,
  298. "state": state.state,
  299. "current_print": state.current_print,
  300. "subtask_name": state.subtask_name,
  301. "gcode_file": state.gcode_file,
  302. "progress": state.progress,
  303. "remaining_time": state.remaining_time,
  304. "layer_num": state.layer_num,
  305. "total_layers": state.total_layers,
  306. "temperatures": state.temperatures,
  307. "hms_errors": [
  308. {"code": e.code, "attr": e.attr, "module": e.module, "severity": e.severity}
  309. for e in (state.hms_errors or [])
  310. ],
  311. # AMS data for filament colors
  312. "ams": ams_units if ams_units else None,
  313. "vt_tray": vt_tray,
  314. # AMS status for filament change tracking
  315. "ams_status_main": state.ams_status_main,
  316. "ams_status_sub": state.ams_status_sub,
  317. "tray_now": state.tray_now,
  318. # Per-AMS extruder map: {ams_id: extruder_id} where 0=right, 1=left
  319. "ams_extruder_map": ams_extruder_map,
  320. # WiFi signal strength
  321. "wifi_signal": state.wifi_signal,
  322. }
  323. # Add cover URL if there's an active print and printer_id is provided
  324. if printer_id and state.state == "RUNNING" and state.gcode_file:
  325. result["cover_url"] = f"/api/v1/printers/{printer_id}/cover"
  326. else:
  327. result["cover_url"] = None
  328. return result
  329. # Global printer manager instance
  330. printer_manager = PrinterManager()
  331. async def init_printer_connections(db: AsyncSession):
  332. """Initialize connections to all active printers."""
  333. result = await db.execute(select(Printer).where(Printer.is_active.is_(True)))
  334. printers = result.scalars().all()
  335. for printer in printers:
  336. await printer_manager.connect_printer(printer)