printer_manager.py 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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
  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. async def test_connection(
  97. self,
  98. ip_address: str,
  99. serial_number: str,
  100. access_code: str,
  101. ) -> dict:
  102. """Test connection to a printer without persisting."""
  103. client = BambuMQTTClient(
  104. ip_address=ip_address,
  105. serial_number=serial_number,
  106. access_code=access_code,
  107. )
  108. try:
  109. client.connect()
  110. await asyncio.sleep(2)
  111. result = {
  112. "success": client.state.connected,
  113. "state": client.state.state if client.state.connected else None,
  114. "model": client.state.raw_data.get("device_model"),
  115. }
  116. finally:
  117. client.disconnect()
  118. return result
  119. def printer_state_to_dict(state: PrinterState, printer_id: int | None = None) -> dict:
  120. """Convert PrinterState to a JSON-serializable dict."""
  121. result = {
  122. "connected": state.connected,
  123. "state": state.state,
  124. "current_print": state.current_print,
  125. "subtask_name": state.subtask_name,
  126. "gcode_file": state.gcode_file,
  127. "progress": state.progress,
  128. "remaining_time": state.remaining_time,
  129. "layer_num": state.layer_num,
  130. "total_layers": state.total_layers,
  131. "temperatures": state.temperatures,
  132. }
  133. # Add cover URL if there's an active print and printer_id is provided
  134. if printer_id and state.state == "RUNNING" and state.gcode_file:
  135. result["cover_url"] = f"/api/v1/printers/{printer_id}/cover"
  136. else:
  137. result["cover_url"] = None
  138. return result
  139. # Global printer manager instance
  140. printer_manager = PrinterManager()
  141. async def init_printer_connections(db: AsyncSession):
  142. """Initialize connections to all active printers."""
  143. result = await db.execute(
  144. select(Printer).where(Printer.is_active == True)
  145. )
  146. printers = result.scalars().all()
  147. for printer in printers:
  148. await printer_manager.connect_printer(printer)