printer_manager.py 15 KB

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