printer_manager.py 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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._loop: asyncio.AbstractEventLoop | None = None
  17. def set_event_loop(self, loop: asyncio.AbstractEventLoop):
  18. """Set the event loop for async callbacks."""
  19. self._loop = loop
  20. def set_print_start_callback(self, callback: Callable[[int, dict], None]):
  21. """Set callback for print start events."""
  22. self._on_print_start = callback
  23. def set_print_complete_callback(self, callback: Callable[[int, dict], None]):
  24. """Set callback for print completion events."""
  25. self._on_print_complete = callback
  26. def set_status_change_callback(self, callback: Callable[[int, PrinterState], None]):
  27. """Set callback for status change events."""
  28. self._on_status_change = callback
  29. def _schedule_async(self, coro):
  30. """Schedule an async coroutine from a sync context."""
  31. if self._loop and self._loop.is_running():
  32. asyncio.run_coroutine_threadsafe(coro, self._loop)
  33. async def connect_printer(self, printer: Printer) -> bool:
  34. """Connect to a printer."""
  35. if printer.id in self._clients:
  36. self.disconnect_printer(printer.id)
  37. printer_id = printer.id
  38. def on_state_change(state: PrinterState):
  39. if self._on_status_change:
  40. self._schedule_async(
  41. self._on_status_change(printer_id, state)
  42. )
  43. def on_print_start(data: dict):
  44. if self._on_print_start:
  45. self._schedule_async(
  46. self._on_print_start(printer_id, data)
  47. )
  48. def on_print_complete(data: dict):
  49. if self._on_print_complete:
  50. self._schedule_async(
  51. self._on_print_complete(printer_id, data)
  52. )
  53. client = BambuMQTTClient(
  54. ip_address=printer.ip_address,
  55. serial_number=printer.serial_number,
  56. access_code=printer.access_code,
  57. on_state_change=on_state_change,
  58. on_print_start=on_print_start,
  59. on_print_complete=on_print_complete,
  60. )
  61. client.connect()
  62. self._clients[printer_id] = client
  63. # Wait a moment for connection
  64. await asyncio.sleep(1)
  65. return client.state.connected
  66. def disconnect_printer(self, printer_id: int):
  67. """Disconnect from a printer."""
  68. if printer_id in self._clients:
  69. self._clients[printer_id].disconnect()
  70. del self._clients[printer_id]
  71. def disconnect_all(self):
  72. """Disconnect from all printers."""
  73. for printer_id in list(self._clients.keys()):
  74. self.disconnect_printer(printer_id)
  75. def get_status(self, printer_id: int) -> PrinterState | None:
  76. """Get the current status of a printer."""
  77. if printer_id in self._clients:
  78. return self._clients[printer_id].state
  79. return None
  80. def get_all_statuses(self) -> dict[int, PrinterState]:
  81. """Get status of all connected printers."""
  82. return {
  83. printer_id: client.state
  84. for printer_id, client in self._clients.items()
  85. }
  86. def is_connected(self, printer_id: int) -> bool:
  87. """Check if a printer is connected."""
  88. if printer_id in self._clients:
  89. return self._clients[printer_id].state.connected
  90. return False
  91. def start_print(self, printer_id: int, filename: str) -> bool:
  92. """Start a print on a connected printer."""
  93. if printer_id in self._clients:
  94. return self._clients[printer_id].start_print(filename)
  95. return False
  96. def enable_logging(self, printer_id: int, enabled: bool = True) -> bool:
  97. """Enable or disable MQTT logging for a printer."""
  98. if printer_id in self._clients:
  99. self._clients[printer_id].enable_logging(enabled)
  100. return True
  101. return False
  102. def get_logs(self, printer_id: int) -> list[MQTTLogEntry]:
  103. """Get MQTT logs for a printer."""
  104. if printer_id in self._clients:
  105. return self._clients[printer_id].get_logs()
  106. return []
  107. def clear_logs(self, printer_id: int) -> bool:
  108. """Clear MQTT logs for a printer."""
  109. if printer_id in self._clients:
  110. self._clients[printer_id].clear_logs()
  111. return True
  112. return False
  113. def is_logging_enabled(self, printer_id: int) -> bool:
  114. """Check if logging is enabled for a printer."""
  115. if printer_id in self._clients:
  116. return self._clients[printer_id].logging_enabled
  117. return False
  118. async def test_connection(
  119. self,
  120. ip_address: str,
  121. serial_number: str,
  122. access_code: str,
  123. ) -> dict:
  124. """Test connection to a printer without persisting."""
  125. client = BambuMQTTClient(
  126. ip_address=ip_address,
  127. serial_number=serial_number,
  128. access_code=access_code,
  129. )
  130. try:
  131. client.connect()
  132. await asyncio.sleep(2)
  133. result = {
  134. "success": client.state.connected,
  135. "state": client.state.state if client.state.connected else None,
  136. "model": client.state.raw_data.get("device_model"),
  137. }
  138. finally:
  139. client.disconnect()
  140. return result
  141. def printer_state_to_dict(state: PrinterState, printer_id: int | None = None) -> dict:
  142. """Convert PrinterState to a JSON-serializable dict."""
  143. result = {
  144. "connected": state.connected,
  145. "state": state.state,
  146. "current_print": state.current_print,
  147. "subtask_name": state.subtask_name,
  148. "gcode_file": state.gcode_file,
  149. "progress": state.progress,
  150. "remaining_time": state.remaining_time,
  151. "layer_num": state.layer_num,
  152. "total_layers": state.total_layers,
  153. "temperatures": state.temperatures,
  154. "hms_errors": [
  155. {"code": e.code, "module": e.module, "severity": e.severity}
  156. for e in (state.hms_errors or [])
  157. ],
  158. }
  159. # Add cover URL if there's an active print and printer_id is provided
  160. if printer_id and state.state == "RUNNING" and state.gcode_file:
  161. result["cover_url"] = f"/api/v1/printers/{printer_id}/cover"
  162. else:
  163. result["cover_url"] = None
  164. return result
  165. # Global printer manager instance
  166. printer_manager = PrinterManager()
  167. async def init_printer_connections(db: AsyncSession):
  168. """Initialize connections to all active printers."""
  169. result = await db.execute(
  170. select(Printer).where(Printer.is_active == True)
  171. )
  172. printers = result.scalars().all()
  173. for printer in printers:
  174. await printer_manager.connect_printer(printer)