printer_manager.py 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655
  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, get_stage_name
  7. # Models that have a real chamber temperature sensor
  8. # Based on Home Assistant Bambu Lab integration
  9. # P1P/P1S and A1/A1Mini do NOT have chamber temp sensors
  10. # Includes both display names and internal codes from MQTT/SSDP
  11. CHAMBER_TEMP_SUPPORTED_MODELS = frozenset(
  12. [
  13. # Display names
  14. "X1",
  15. "X1C",
  16. "X1E", # X1 series
  17. "P2S", # P2 series
  18. "H2C",
  19. "H2D",
  20. "H2DPRO",
  21. "H2S", # H2 series
  22. # Internal codes (from MQTT/SSDP)
  23. "BL-P001", # X1/X1C
  24. "C13", # X1E
  25. "O1D", # H2D
  26. "O1C", # H2C
  27. "O1S", # H2S
  28. "O1E", # H2D Pro
  29. "O2D", # H2D Pro (alternate code)
  30. "N7", # P2S
  31. ]
  32. )
  33. # Models that may incorrectly report stg_cur=0 when idle (firmware bug)
  34. # Based on Home Assistant Bambu Lab integration observations
  35. # See: https://github.com/greghesp/ha-bambulab/blob/main/custom_components/bambu_lab/pybambu/models.py
  36. A1_MODELS = frozenset(
  37. [
  38. # Display names
  39. "A1",
  40. "A1 MINI",
  41. "A1-MINI",
  42. "A1MINI",
  43. # Internal codes (from MQTT/SSDP)
  44. "N1", # A1 Mini
  45. "N2S", # A1
  46. ]
  47. )
  48. def supports_chamber_temp(model: str | None) -> bool:
  49. """Check if a printer model has a real chamber temperature sensor.
  50. P1P, P1S, A1, and A1Mini do NOT have chamber temp sensors.
  51. The 'chamber_temper' value they report is meaningless.
  52. """
  53. if not model:
  54. return False
  55. # Normalize model name (uppercase, strip whitespace)
  56. model_upper = model.strip().upper()
  57. return model_upper in CHAMBER_TEMP_SUPPORTED_MODELS
  58. def has_stg_cur_idle_bug(model: str | None) -> bool:
  59. """Check if a printer model may incorrectly report stg_cur=0 when idle.
  60. Some A1/A1 Mini firmware versions report stg_cur=0 (which maps to "Printing")
  61. even when the printer is idle. This is a known firmware bug that was observed
  62. in the Home Assistant Bambu Lab integration.
  63. """
  64. if not model:
  65. return False
  66. model_upper = model.strip().upper()
  67. return model_upper in A1_MODELS
  68. class PrinterInfo:
  69. """Basic printer info for callbacks."""
  70. def __init__(self, name: str, serial_number: str):
  71. self.name = name
  72. self.serial_number = serial_number
  73. class PrinterManager:
  74. """Manager for multiple printer connections."""
  75. def __init__(self):
  76. self._clients: dict[int, BambuMQTTClient] = {}
  77. self._models: dict[int, str | None] = {} # Cache printer models for feature detection
  78. self._printer_info: dict[int, PrinterInfo] = {} # Cache printer name/serial for callbacks
  79. self._on_print_start: Callable[[int, dict], None] | None = None
  80. self._on_print_complete: Callable[[int, dict], None] | None = None
  81. self._on_status_change: Callable[[int, PrinterState], None] | None = None
  82. self._on_ams_change: Callable[[int, list], None] | None = None
  83. self._on_layer_change: Callable[[int, int], None] | None = None
  84. self._loop: asyncio.AbstractEventLoop | None = None
  85. def get_printer(self, printer_id: int) -> PrinterInfo | None:
  86. """Get printer info by ID."""
  87. return self._printer_info.get(printer_id)
  88. def set_event_loop(self, loop: asyncio.AbstractEventLoop):
  89. """Set the event loop for async callbacks."""
  90. self._loop = loop
  91. def set_print_start_callback(self, callback: Callable[[int, dict], None]):
  92. """Set callback for print start events."""
  93. self._on_print_start = callback
  94. def set_print_complete_callback(self, callback: Callable[[int, dict], None]):
  95. """Set callback for print completion events."""
  96. self._on_print_complete = callback
  97. def set_status_change_callback(self, callback: Callable[[int, PrinterState], None]):
  98. """Set callback for status change events."""
  99. self._on_status_change = callback
  100. def set_ams_change_callback(self, callback: Callable[[int, list], None]):
  101. """Set callback for AMS data change events."""
  102. self._on_ams_change = callback
  103. def set_layer_change_callback(self, callback: Callable[[int, int], None]):
  104. """Set callback for layer change events. Receives (printer_id, layer_num)."""
  105. self._on_layer_change = callback
  106. def _schedule_async(self, coro):
  107. """Schedule an async coroutine from a sync context.
  108. Captures exceptions from the coroutine and logs them to prevent
  109. silent failures in callbacks.
  110. """
  111. if self._loop and self._loop.is_running():
  112. future = asyncio.run_coroutine_threadsafe(coro, self._loop)
  113. def handle_exception(f):
  114. try:
  115. # This will re-raise any exception from the coroutine
  116. f.result()
  117. except Exception as e:
  118. import logging
  119. logging.getLogger(__name__).error(f"Exception in scheduled callback: {e}", exc_info=True)
  120. future.add_done_callback(handle_exception)
  121. async def connect_printer(self, printer: Printer) -> bool:
  122. """Connect to a printer."""
  123. if printer.id in self._clients:
  124. self.disconnect_printer(printer.id)
  125. printer_id = printer.id
  126. def on_state_change(state: PrinterState):
  127. if self._on_status_change:
  128. self._schedule_async(self._on_status_change(printer_id, state))
  129. def on_print_start(data: dict):
  130. if self._on_print_start:
  131. self._schedule_async(self._on_print_start(printer_id, data))
  132. def on_print_complete(data: dict):
  133. if self._on_print_complete:
  134. self._schedule_async(self._on_print_complete(printer_id, data))
  135. def on_ams_change(ams_data: list):
  136. if self._on_ams_change:
  137. self._schedule_async(self._on_ams_change(printer_id, ams_data))
  138. def on_layer_change(layer_num: int):
  139. if self._on_layer_change:
  140. self._schedule_async(self._on_layer_change(printer_id, layer_num))
  141. client = BambuMQTTClient(
  142. ip_address=printer.ip_address,
  143. serial_number=printer.serial_number,
  144. access_code=printer.access_code,
  145. on_state_change=on_state_change,
  146. on_print_start=on_print_start,
  147. on_print_complete=on_print_complete,
  148. on_ams_change=on_ams_change,
  149. on_layer_change=on_layer_change,
  150. )
  151. client.connect()
  152. self._clients[printer_id] = client
  153. self._models[printer_id] = printer.model # Cache model for feature detection
  154. self._printer_info[printer_id] = PrinterInfo(printer.name, printer.serial_number)
  155. # Wait a moment for connection
  156. await asyncio.sleep(1)
  157. return client.state.connected
  158. def disconnect_printer(self, printer_id: int):
  159. """Disconnect from a printer."""
  160. if printer_id in self._clients:
  161. self._clients[printer_id].disconnect()
  162. del self._clients[printer_id]
  163. self._models.pop(printer_id, None) # Clean up model cache
  164. self._printer_info.pop(printer_id, None) # Clean up printer info cache
  165. def disconnect_all(self):
  166. """Disconnect from all printers."""
  167. for printer_id in list(self._clients.keys()):
  168. self.disconnect_printer(printer_id)
  169. def get_status(self, printer_id: int) -> PrinterState | None:
  170. """Get the current status of a printer (checks for stale connections)."""
  171. if printer_id in self._clients:
  172. client = self._clients[printer_id]
  173. # Check staleness and update connected state if needed
  174. client.check_staleness()
  175. return client.state
  176. return None
  177. def get_model(self, printer_id: int) -> str | None:
  178. """Get the cached model for a printer."""
  179. return self._models.get(printer_id)
  180. def get_all_statuses(self) -> dict[int, PrinterState]:
  181. """Get status of all connected printers (checks for stale connections)."""
  182. result = {}
  183. for printer_id, client in self._clients.items():
  184. # Check staleness and update connected state if needed
  185. client.check_staleness()
  186. result[printer_id] = client.state
  187. return result
  188. def is_connected(self, printer_id: int) -> bool:
  189. """Check if a printer is connected (checks for stale connections)."""
  190. if printer_id in self._clients:
  191. client = self._clients[printer_id]
  192. # Check staleness and update connected state if needed
  193. return client.check_staleness()
  194. return False
  195. def get_client(self, printer_id: int) -> BambuMQTTClient | None:
  196. """Get the MQTT client for a printer."""
  197. return self._clients.get(printer_id)
  198. def mark_printer_offline(self, printer_id: int):
  199. """Mark a printer as offline and trigger status callback.
  200. This is used when we know the printer power was cut (e.g., smart plug turned off)
  201. to immediately update the UI without waiting for MQTT timeout.
  202. """
  203. import logging
  204. logger = logging.getLogger(__name__)
  205. if printer_id in self._clients:
  206. client = self._clients[printer_id]
  207. if client.state.connected:
  208. logger.info(f"Marking printer {printer_id} as offline (smart plug power off)")
  209. client.state.connected = False
  210. client.state.state = "unknown"
  211. # Trigger the status change callback to broadcast via WebSocket
  212. if self._on_status_change:
  213. self._schedule_async(self._on_status_change(printer_id, client.state))
  214. def start_print(
  215. self,
  216. printer_id: int,
  217. filename: str,
  218. plate_id: int = 1,
  219. ams_mapping: list[int] | None = None,
  220. bed_levelling: bool = True,
  221. flow_cali: bool = False,
  222. vibration_cali: bool = True,
  223. layer_inspect: bool = False,
  224. timelapse: bool = False,
  225. use_ams: bool = True,
  226. ) -> bool:
  227. """Start a print on a connected printer."""
  228. if printer_id in self._clients:
  229. return self._clients[printer_id].start_print(
  230. filename,
  231. plate_id,
  232. ams_mapping=ams_mapping,
  233. timelapse=timelapse,
  234. bed_levelling=bed_levelling,
  235. flow_cali=flow_cali,
  236. vibration_cali=vibration_cali,
  237. layer_inspect=layer_inspect,
  238. use_ams=use_ams,
  239. )
  240. return False
  241. def stop_print(self, printer_id: int) -> bool:
  242. """Stop the current print on a connected printer."""
  243. if printer_id in self._clients:
  244. return self._clients[printer_id].stop_print()
  245. return False
  246. async def wait_for_cooldown(
  247. self,
  248. printer_id: int,
  249. target_temp: float = 50.0,
  250. timeout: int = 600,
  251. check_interval: int = 10,
  252. ) -> bool:
  253. """Wait for the nozzle to cool down to a safe temperature.
  254. Args:
  255. printer_id: The printer to monitor
  256. target_temp: Target temperature to wait for (default 50°C)
  257. timeout: Maximum seconds to wait (default 600s = 10 min)
  258. check_interval: Seconds between temperature checks (default 10s)
  259. Returns:
  260. True if cooled down, False if timeout or not connected
  261. """
  262. import logging
  263. logger = logging.getLogger(__name__)
  264. elapsed = 0
  265. while elapsed < timeout:
  266. state = self.get_status(printer_id)
  267. if not state or not state.connected:
  268. logger.warning(f"Printer {printer_id} disconnected during cooldown wait")
  269. return False
  270. # Check nozzle temperature (and nozzle_2 for dual extruders)
  271. nozzle_temp = state.temperatures.get("nozzle", 0)
  272. nozzle_2_temp = state.temperatures.get("nozzle_2", 0)
  273. max_temp = max(nozzle_temp, nozzle_2_temp)
  274. if max_temp <= target_temp:
  275. logger.info(f"Printer {printer_id} cooled down to {max_temp}°C")
  276. return True
  277. logger.debug(f"Printer {printer_id} nozzle at {max_temp}°C, waiting for {target_temp}°C...")
  278. await asyncio.sleep(check_interval)
  279. elapsed += check_interval
  280. logger.warning(f"Printer {printer_id} cooldown timeout after {timeout}s")
  281. return False
  282. def enable_logging(self, printer_id: int, enabled: bool = True) -> bool:
  283. """Enable or disable MQTT logging for a printer."""
  284. if printer_id in self._clients:
  285. self._clients[printer_id].enable_logging(enabled)
  286. return True
  287. return False
  288. def get_logs(self, printer_id: int) -> list[MQTTLogEntry]:
  289. """Get MQTT logs for a printer."""
  290. if printer_id in self._clients:
  291. return self._clients[printer_id].get_logs()
  292. return []
  293. def clear_logs(self, printer_id: int) -> bool:
  294. """Clear MQTT logs for a printer."""
  295. if printer_id in self._clients:
  296. self._clients[printer_id].clear_logs()
  297. return True
  298. return False
  299. def is_logging_enabled(self, printer_id: int) -> bool:
  300. """Check if logging is enabled for a printer."""
  301. if printer_id in self._clients:
  302. return self._clients[printer_id].logging_enabled
  303. return False
  304. def request_status_update(self, printer_id: int) -> bool:
  305. """Request a full status update from the printer.
  306. This sends a 'pushall' command to get the latest data including nozzle info.
  307. """
  308. if printer_id in self._clients:
  309. return self._clients[printer_id].request_status_update()
  310. return False
  311. async def test_connection(
  312. self,
  313. ip_address: str,
  314. serial_number: str,
  315. access_code: str,
  316. ) -> dict:
  317. """Test connection to a printer without persisting."""
  318. client = BambuMQTTClient(
  319. ip_address=ip_address,
  320. serial_number=serial_number,
  321. access_code=access_code,
  322. )
  323. try:
  324. client.connect()
  325. await asyncio.sleep(2)
  326. result = {
  327. "success": client.state.connected,
  328. "state": client.state.state if client.state.connected else None,
  329. "model": client.state.raw_data.get("device_model"),
  330. }
  331. finally:
  332. client.disconnect()
  333. return result
  334. def get_derived_status_name(state: PrinterState, model: str | None = None) -> str | None:
  335. """
  336. Compute a human-readable status name based on printer state.
  337. Uses stg_cur when available, otherwise derives status from temperature data
  338. when the printer is heating before a print starts.
  339. Args:
  340. state: The printer state to analyze
  341. model: Optional printer model for model-specific workarounds
  342. """
  343. # A1/A1 Mini firmware bug: some versions report stg_cur=0 when idle
  344. # Only correct this specific case (IDLE + stg_cur=0) for affected models
  345. if state.state == "IDLE" and state.stg_cur == 0 and has_stg_cur_idle_bug(model):
  346. return None
  347. # If we have a valid calibration stage, use it
  348. # X1 models use -1 for idle, A1/P1 models use 255 for idle
  349. # Valid stage numbers are 0-254
  350. if 0 <= state.stg_cur < 255:
  351. return get_stage_name(state.stg_cur)
  352. # If not in RUNNING state, no derived status needed
  353. if state.state != "RUNNING":
  354. return None
  355. # Check if we're in an early phase where temperatures are heating
  356. temps = state.temperatures or {}
  357. progress = state.progress or 0
  358. # Only derive heating status when progress is very low (< 2%)
  359. # This indicates we're in the preparation phase, not actually printing
  360. if progress >= 2:
  361. return None
  362. # Check bed temperature - if target is set and current is significantly below
  363. bed_temp = temps.get("bed", 0)
  364. bed_target = temps.get("bed_target", 0)
  365. # Check nozzle temperature
  366. nozzle_temp = temps.get("nozzle", 0)
  367. nozzle_target = temps.get("nozzle_target", 0)
  368. # Temperature thresholds: consider "heating" if more than 10°C below target
  369. TEMP_THRESHOLD = 10
  370. # Determine what's heating (prioritize bed since it takes longer)
  371. if bed_target > 30 and (bed_target - bed_temp) > TEMP_THRESHOLD:
  372. return "Heating heatbed"
  373. elif nozzle_target > 30 and (nozzle_target - nozzle_temp) > TEMP_THRESHOLD:
  374. return "Heating nozzle"
  375. # If targets are set but we're close to them, we might be in final prep
  376. if bed_target > 30 or nozzle_target > 30:
  377. if progress == 0 and state.layer_num == 0:
  378. return "Preparing"
  379. return None
  380. def printer_state_to_dict(state: PrinterState, printer_id: int | None = None, model: str | None = None) -> dict:
  381. """Convert PrinterState to a JSON-serializable dict.
  382. Args:
  383. state: The printer state to convert
  384. printer_id: Optional printer ID for generating cover URLs
  385. model: Optional printer model for filtering unsupported features
  386. """
  387. # Parse AMS data from raw_data
  388. ams_units = []
  389. vt_tray = None
  390. raw_data = state.raw_data or {}
  391. # Build K-profile lookup map: cali_idx -> k_value
  392. kprofile_map: dict[int, float] = {}
  393. for kp in state.kprofiles or []:
  394. if kp.slot_id is not None and kp.k_value:
  395. try:
  396. kprofile_map[kp.slot_id] = float(kp.k_value)
  397. except (ValueError, TypeError):
  398. pass
  399. if "ams" in raw_data and isinstance(raw_data["ams"], list):
  400. for ams_data in raw_data["ams"]:
  401. trays = []
  402. for tray in ams_data.get("tray", []):
  403. tag_uid = tray.get("tag_uid")
  404. if tag_uid in ("", "0000000000000000"):
  405. tag_uid = None
  406. tray_uuid = tray.get("tray_uuid")
  407. if tray_uuid in ("", "00000000000000000000000000000000"):
  408. tray_uuid = None
  409. # Get K value: first try tray's k field, then lookup from K-profiles
  410. k_value = tray.get("k")
  411. cali_idx = tray.get("cali_idx")
  412. if k_value is None and cali_idx is not None and cali_idx in kprofile_map:
  413. k_value = kprofile_map[cali_idx]
  414. trays.append(
  415. {
  416. "id": tray.get("id", 0),
  417. "tray_color": tray.get("tray_color"),
  418. "tray_type": tray.get("tray_type"),
  419. "tray_sub_brands": tray.get("tray_sub_brands"),
  420. "tray_id_name": tray.get("tray_id_name"),
  421. "tray_info_idx": tray.get("tray_info_idx"),
  422. "remain": tray.get("remain", 0),
  423. "k": k_value,
  424. "cali_idx": cali_idx,
  425. "tag_uid": tag_uid,
  426. "tray_uuid": tray_uuid,
  427. "nozzle_temp_min": tray.get("nozzle_temp_min"),
  428. "nozzle_temp_max": tray.get("nozzle_temp_max"),
  429. }
  430. )
  431. # Prefer humidity_raw (actual percentage) over humidity (index 1-5)
  432. humidity_raw = ams_data.get("humidity_raw")
  433. humidity_idx = ams_data.get("humidity")
  434. humidity_value = None
  435. if humidity_raw is not None:
  436. try:
  437. humidity_value = int(humidity_raw)
  438. except (ValueError, TypeError):
  439. pass
  440. # Fall back to index if no raw value (index is 1-5, not percentage)
  441. if humidity_value is None and humidity_idx is not None:
  442. try:
  443. humidity_value = int(humidity_idx)
  444. except (ValueError, TypeError):
  445. pass
  446. # AMS-HT has 1 tray, regular AMS has 4 trays
  447. is_ams_ht = len(trays) == 1
  448. ams_units.append(
  449. {
  450. "id": ams_data.get("id", 0),
  451. "humidity": humidity_value,
  452. "temp": ams_data.get("temp"),
  453. "is_ams_ht": is_ams_ht,
  454. "tray": trays,
  455. }
  456. )
  457. # Parse virtual tray (external spool)
  458. if "vt_tray" in raw_data:
  459. vt_data = raw_data["vt_tray"]
  460. vt_tag_uid = vt_data.get("tag_uid")
  461. if vt_tag_uid in ("", "0000000000000000"):
  462. vt_tag_uid = None
  463. vt_tray_uuid = vt_data.get("tray_uuid")
  464. if vt_tray_uuid in ("", "00000000000000000000000000000000"):
  465. vt_tray_uuid = None
  466. # Get K value for vt_tray
  467. vt_k_value = vt_data.get("k")
  468. vt_cali_idx = vt_data.get("cali_idx")
  469. if vt_k_value is None and vt_cali_idx is not None and vt_cali_idx in kprofile_map:
  470. vt_k_value = kprofile_map[vt_cali_idx]
  471. vt_tray = {
  472. "id": 254,
  473. "tray_color": vt_data.get("tray_color"),
  474. "tray_type": vt_data.get("tray_type"),
  475. "tray_sub_brands": vt_data.get("tray_sub_brands"),
  476. "tray_id_name": vt_data.get("tray_id_name"),
  477. "tray_info_idx": vt_data.get("tray_info_idx"),
  478. "remain": vt_data.get("remain", 0),
  479. "k": vt_k_value,
  480. "cali_idx": vt_cali_idx,
  481. "tag_uid": vt_tag_uid,
  482. "tray_uuid": vt_tray_uuid,
  483. "nozzle_temp_min": vt_data.get("nozzle_temp_min"),
  484. "nozzle_temp_max": vt_data.get("nozzle_temp_max"),
  485. }
  486. # Get ams_extruder_map from raw_data (populated by MQTT handler from AMS info field)
  487. ams_extruder_map = raw_data.get("ams_extruder_map", {})
  488. # Filter out chamber temp for models that don't have a real sensor
  489. # P1P, P1S, A1, A1Mini report meaningless chamber_temper values
  490. temperatures = state.temperatures
  491. if not supports_chamber_temp(model):
  492. temperatures = {
  493. k: v for k, v in temperatures.items() if k not in ("chamber", "chamber_target", "chamber_heating")
  494. }
  495. result = {
  496. "connected": state.connected,
  497. "state": state.state,
  498. "current_print": state.current_print,
  499. "subtask_name": state.subtask_name,
  500. "gcode_file": state.gcode_file,
  501. "progress": state.progress,
  502. "remaining_time": state.remaining_time,
  503. "layer_num": state.layer_num,
  504. "total_layers": state.total_layers,
  505. "temperatures": temperatures,
  506. "hms_errors": [
  507. {"code": e.code, "attr": e.attr, "module": e.module, "severity": e.severity}
  508. for e in (state.hms_errors or [])
  509. ],
  510. # AMS data for filament colors
  511. "ams": ams_units if ams_units else None,
  512. "vt_tray": vt_tray,
  513. # AMS status for filament change tracking
  514. "ams_status_main": state.ams_status_main,
  515. "ams_status_sub": state.ams_status_sub,
  516. "tray_now": state.tray_now,
  517. # Per-AMS extruder map: {ams_id: extruder_id} where 0=right, 1=left
  518. "ams_extruder_map": ams_extruder_map,
  519. # WiFi signal strength
  520. "wifi_signal": state.wifi_signal,
  521. # Calibration stage tracking
  522. "stg_cur": state.stg_cur,
  523. "stg_cur_name": get_derived_status_name(state, model),
  524. # Printable objects count for skip objects feature
  525. "printable_objects_count": len(state.printable_objects),
  526. # Fan speeds (0-100 percentage, None if not available)
  527. "cooling_fan_speed": state.cooling_fan_speed,
  528. "big_fan1_speed": state.big_fan1_speed,
  529. "big_fan2_speed": state.big_fan2_speed,
  530. "heatbreak_fan_speed": state.heatbreak_fan_speed,
  531. # Chamber light state
  532. "chamber_light": state.chamber_light,
  533. # Active extruder for dual-nozzle printers (0=right, 1=left)
  534. "active_extruder": state.active_extruder,
  535. }
  536. # Add cover URL if there's an active print and printer_id is provided
  537. # Include PAUSE/PAUSED states so skip objects modal can show cover
  538. if printer_id and state.state in ("RUNNING", "PAUSE", "PAUSED") and state.gcode_file:
  539. result["cover_url"] = f"/api/v1/printers/{printer_id}/cover"
  540. else:
  541. result["cover_url"] = None
  542. return result
  543. # Global printer manager instance
  544. printer_manager = PrinterManager()
  545. async def init_printer_connections(db: AsyncSession):
  546. """Initialize connections to all active printers."""
  547. result = await db.execute(select(Printer).where(Printer.is_active.is_(True)))
  548. printers = result.scalars().all()
  549. for printer in printers:
  550. await printer_manager.connect_printer(printer)