manager.py 61 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377
  1. """Virtual Printer Manager - coordinates SSDP, MQTT, and FTP services.
  2. Each virtual printer runs its own independent services (FTP, MQTT, SSDP, Bind)
  3. bound to its dedicated IP address, regardless of mode.
  4. """
  5. import asyncio
  6. import logging
  7. from collections.abc import Callable
  8. from datetime import datetime, timezone
  9. from pathlib import Path
  10. from typing import TYPE_CHECKING
  11. from backend.app.core.config import settings as app_settings
  12. from backend.app.models.virtual_printer import (
  13. VP_MODE_ARCHIVE,
  14. VP_MODE_PROXY,
  15. VP_MODE_QUEUE,
  16. normalize_vp_mode,
  17. )
  18. from backend.app.services.virtual_printer.bind_server import BindServer
  19. from backend.app.services.virtual_printer.certificate import CertificateService
  20. from backend.app.services.virtual_printer.ftp_server import VirtualPrinterFTPServer, compute_passive_port_slice
  21. from backend.app.services.virtual_printer.mqtt_bridge import MQTTBridge
  22. from backend.app.services.virtual_printer.mqtt_server import SimpleMQTTServer
  23. from backend.app.services.virtual_printer.ssdp_server import SSDPProxy, VirtualPrinterSSDPServer
  24. from backend.app.services.virtual_printer.tcp_proxy import SlicerProxyManager, TCPProxy
  25. if TYPE_CHECKING:
  26. from backend.app.services.printer_manager import PrinterManager
  27. logger = logging.getLogger(__name__)
  28. # Mapping of SSDP model codes to display names
  29. # These are the codes that slicers expect during discovery
  30. # Sources:
  31. # - https://gist.github.com/Alex-Schaefer/72a9e2491a42da2ef99fb87601955cc3
  32. # - https://github.com/psychoticbeef/BambuLabOrcaSlicerDiscovery
  33. VIRTUAL_PRINTER_MODELS = {
  34. # X1 Series
  35. "BL-P001": "X1C", # X1 Carbon
  36. "BL-P002": "X1", # X1
  37. "C13": "X1E", # X1E
  38. # X2 Series
  39. "N6": "X2D", # X2D
  40. # A2 Series (single-FDM + integrated cutter/plotter)
  41. "N9": "A2L", # A2L
  42. # P Series
  43. "C11": "P1P", # P1P
  44. "C12": "P1S", # P1S
  45. "N7": "P2S", # P2S
  46. # A1 Series
  47. "N2S": "A1", # A1
  48. "N1": "A1 Mini", # A1 Mini
  49. # H2 Series
  50. "O1D": "H2D", # H2D
  51. "O1E": "H2D Pro", # H2D Pro
  52. "O2D": "H2D Pro", # H2D Pro
  53. "O1C": "H2C", # H2C
  54. "O1C2": "H2C", # H2C (dual nozzle variant)
  55. "O1S": "H2S", # H2S
  56. }
  57. # Serial number prefixes for each model (based on Bambu Lab serial number format)
  58. # Format: MMM??RYMDDUUUUU (15 chars total)
  59. # MMM = Model prefix (3 chars)
  60. # ?? = Unknown/revision code (2 chars)
  61. # R = Revision letter (1 char)
  62. # Y = Year digit (1 char)
  63. # M = Month (1 char, hex: 1-9, A=Oct, B=Nov, C=Dec)
  64. # DD = Day (2 chars)
  65. # UUUUU = Unit number (5 chars)
  66. MODEL_SERIAL_PREFIXES = {
  67. # X1 Series
  68. "BL-P001": "00M00A", # X1C
  69. "BL-P002": "00M00A", # X1
  70. "C13": "03W00A", # X1E
  71. # X2 Series
  72. "N6": "20P90A", # X2D (first 4 chars "20P9" match real serials)
  73. # A2 Series
  74. "N9": "26A19A", # A2L (first 5 chars "26A19" match real serials)
  75. # P Series
  76. "C11": "01S00A", # P1P
  77. "C12": "01P00A", # P1S
  78. "N7": "22E00A", # P2S
  79. # A1 Series
  80. "N2S": "03900A", # A1
  81. "N1": "03000A", # A1 Mini
  82. # H2 Series
  83. "O1D": "09400A", # H2D
  84. "O1E": "09400A", # H2D Pro (same prefix family as H2D)
  85. "O2D": "09400A", # H2D Pro
  86. "O1C": "09400A", # H2C
  87. "O1C2": "09400A", # H2C (dual nozzle variant)
  88. "O1S": "09400A", # H2S
  89. }
  90. # Reverse mapping: display name → SSDP model code (for auto-inheriting from printer model)
  91. DISPLAY_NAME_TO_MODEL_CODE = {v: k for k, v in VIRTUAL_PRINTER_MODELS.items()}
  92. # Default model
  93. DEFAULT_VIRTUAL_PRINTER_MODEL = "BL-P001" # X1C
  94. # Bound on per-instance ``_slicer_print_options`` cache size. The slicer's
  95. # project_file MQTT command stashes one dict per filename; the
  96. # corresponding ``_add_to_print_queue`` pop only fires when the file
  97. # upload completes. Failed / cancelled / non-3MF uploads orphan their
  98. # stash. The bound triggers FIFO eviction in ``on_print_command`` once
  99. # the dict fills, so a long-running VP can't leak unbounded state.
  100. _SLICER_OPTIONS_CACHE_LIMIT = 128
  101. def _get_serial_for_model(model: str, serial_suffix: str) -> str:
  102. """Get serial number for the given model and suffix."""
  103. prefix = MODEL_SERIAL_PREFIXES.get(model, "00M09A")
  104. return f"{prefix}{serial_suffix}"
  105. class VirtualPrinterInstance:
  106. """Per-printer state and file handling logic.
  107. Each instance represents one virtual printer with its own config,
  108. upload directory, certificates, and file handling mode.
  109. """
  110. def __init__(
  111. self,
  112. *,
  113. vp_id: int,
  114. name: str,
  115. mode: str,
  116. model: str,
  117. access_code: str,
  118. serial_suffix: str,
  119. target_printer_ip: str = "",
  120. target_printer_serial: str = "",
  121. target_printer_id: int | None = None,
  122. auto_dispatch: bool = True,
  123. queue_force_color_match: bool = False,
  124. bind_ip: str = "",
  125. remote_interface_ip: str = "",
  126. tailscale_disabled: bool = True,
  127. base_dir: Path,
  128. session_factory: Callable | None = None,
  129. printer_manager: "PrinterManager | None" = None,
  130. ):
  131. self.id = vp_id
  132. self.name = name
  133. # Normalize on construction so the rest of the code only compares
  134. # canonical values, even when a legacy DB row hasn't been migrated
  135. # yet (e.g. fresh-from-disk during the boot window before the
  136. # one-shot migration in `core/database.py` has executed).
  137. self.mode = normalize_vp_mode(mode) or VP_MODE_ARCHIVE
  138. self.model = model
  139. self.access_code = access_code
  140. self.serial_suffix = serial_suffix
  141. self.target_printer_ip = target_printer_ip
  142. self.target_printer_serial = target_printer_serial
  143. self.target_printer_id = target_printer_id
  144. self.auto_dispatch = auto_dispatch
  145. self.queue_force_color_match = queue_force_color_match
  146. self.bind_ip = bind_ip
  147. self.remote_interface_ip = remote_interface_ip
  148. self.tailscale_disabled = tailscale_disabled
  149. self._session_factory = session_factory
  150. self._printer_manager = printer_manager
  151. # Directories
  152. self.upload_dir = base_dir / "uploads" / str(vp_id)
  153. self.cert_dir = base_dir / "certs" / str(vp_id)
  154. shared_ca_dir = base_dir / "certs"
  155. # Ensure directories exist
  156. self.upload_dir.mkdir(parents=True, exist_ok=True)
  157. (self.upload_dir / "cache").mkdir(exist_ok=True)
  158. self.cert_dir.mkdir(parents=True, exist_ok=True)
  159. # Certificate service (shared CA, per-instance printer cert)
  160. self._cert_service = CertificateService(
  161. cert_dir=self.cert_dir,
  162. serial=self.serial,
  163. shared_ca_dir=shared_ca_dir,
  164. )
  165. # Pending files for MQTT correlation
  166. self._pending_files: dict[str, Path] = {}
  167. # Slicer-side print options captured from the MQTT `project_file`
  168. # command, keyed by filename. Used by `_add_to_print_queue` so the
  169. # queue item inherits the user's slicer-chosen timelapse / bed_leveling
  170. # / flow_cali / vibration_cali / layer_inspect / use_ams toggles rather
  171. # than falling back to the global `default_*` settings (#1403). FTP
  172. # completes a few hundred ms before the slicer's MQTT `project_file`
  173. # arrives, so the queue-add path waits briefly on the event below
  174. # before reading the dict. Events are popped along with the options
  175. # so the dict stays bounded.
  176. self._slicer_print_options: dict[str, dict] = {}
  177. self._slicer_print_options_events: dict[str, asyncio.Event] = {}
  178. # Per-instance services
  179. self._proxy: SlicerProxyManager | None = None
  180. self._ftp: VirtualPrinterFTPServer | None = None
  181. self._mqtt: SimpleMQTTServer | None = None
  182. self._mqtt_bridge: MQTTBridge | None = None
  183. self._rtsp_proxy: TCPProxy | None = None
  184. self._bind: BindServer | None = None
  185. self._ssdp: VirtualPrinterSSDPServer | None = None
  186. self._ssdp_proxy: SSDPProxy | None = None
  187. self._tasks: list[asyncio.Task] = []
  188. # Pending timer that re-fires gcode_state=FINISH after a project_file
  189. # ack. See ``_schedule_finish_release`` for the #1658 rationale.
  190. self._finish_release_task: asyncio.Task | None = None
  191. @property
  192. def serial(self) -> str:
  193. """Full serial number for this virtual printer."""
  194. return _get_serial_for_model(self.model or DEFAULT_VIRTUAL_PRINTER_MODEL, self.serial_suffix)
  195. @property
  196. def cert_path(self) -> Path:
  197. return self._cert_service.cert_path
  198. @property
  199. def key_path(self) -> Path:
  200. return self._cert_service.key_path
  201. @property
  202. def is_proxy(self) -> bool:
  203. return self.mode == "proxy"
  204. @property
  205. def is_running(self) -> bool:
  206. return len(self._tasks) > 0 and all(not t.done() for t in self._tasks)
  207. def generate_certificates(self) -> tuple[Path, Path]:
  208. """Generate certificates for this instance."""
  209. self._cert_service.serial = self.serial if not self.is_proxy else (self.target_printer_serial or self.serial)
  210. additional_ips = [self.remote_interface_ip] if self.remote_interface_ip else None
  211. if self.bind_ip:
  212. additional_ips = additional_ips or []
  213. additional_ips.append(self.bind_ip)
  214. self._cert_service.delete_printer_certificate()
  215. return self._cert_service.generate_certificates(additional_ips=additional_ips)
  216. # -- File handling callbacks --
  217. async def on_file_received(self, file_path: Path, source_ip: str) -> None:
  218. """Handle file upload completion from FTP."""
  219. logger.info("[VP %s] Received file: %s from %s", self.name, file_path.name, source_ip)
  220. self._pending_files[file_path.name] = file_path
  221. # Accept both canonical (`archive`/`queue`) and legacy
  222. # (`immediate`/`print_queue`) wire values so a stale row that hasn't
  223. # been migrated yet still dispatches correctly. Migration in
  224. # `core/database.py` rewrites existing rows once at boot.
  225. mode = normalize_vp_mode(self.mode)
  226. if mode == VP_MODE_ARCHIVE:
  227. await self._archive_file(file_path, source_ip)
  228. elif mode == VP_MODE_QUEUE:
  229. await self._add_to_print_queue(file_path, source_ip)
  230. else:
  231. await self._queue_file(file_path, source_ip)
  232. # Signal job completion to the slicer. Send-flow slicers don't watch the
  233. # post-upload state and would be happy with anything; the Print flow
  234. # (intended for proxy-mode VPs, but users sometimes click it against
  235. # queue/immediate/review modes too — #1280) watches the gcode_state
  236. # cycle and only releases its in-flight-job lock when it sees FINISH.
  237. # Going PREPARE → IDLE wedges the slicer's UI at "Downloading...(0%)"
  238. # and blocks the next dispatch with "busy with another print job".
  239. # PREPARE → FINISH satisfies both flows. prepare_percent=100 also
  240. # unfreezes the slicer's "Downloading X%" progress bar which it ticks
  241. # against the same field during the upload window.
  242. if self._mqtt and file_path.suffix.lower() == ".3mf":
  243. self._mqtt.set_gcode_state("FINISH", filename=file_path.name, prepare_percent="100")
  244. # FINISH is the terminal state for the upload cycle per #1280
  245. # (commit 0d6171dc). The Print-flow slicer's in-flight-job lock
  246. # releases on FINISH; resetting to IDLE 2 s later would re-confuse
  247. # the slicer that just unwedged. Earlier audit suggesting the
  248. # IDLE reset was wrong — staying at FINISH is the designed
  249. # behaviour. The next upload's PREPARE→FINISH cycle starts fresh.
  250. async def on_print_command(self, filename: str, data: dict) -> None:
  251. """Handle print command from MQTT.
  252. Captures the slicer's project_file options (`timelapse`, `bed_leveling`,
  253. `flow_cali`, `vibration_cali`, `layer_inspect`, `use_ams`) so the
  254. VP-queue path can inherit them when adding the item to the queue,
  255. rather than falling back to the global default settings (#1403).
  256. Only queue mode consumes the capture; archive / review / proxy
  257. modes ignore the print command, so we skip the stash there to keep
  258. the dict from accumulating one entry per print over the VP's
  259. uptime.
  260. Also schedules the #1658 follow-up that re-fires gcode_state=FINISH a
  261. moment after the synthetic project_file ack — for every non-proxy
  262. mode — so the slicer's "Downloading" UI releases on the slicer's
  263. FTP-first-then-MQTT send order.
  264. """
  265. logger.info("[VP %s] Print command for: %s", self.name, filename)
  266. mode = normalize_vp_mode(self.mode)
  267. if mode != VP_MODE_PROXY and filename and self._mqtt is not None:
  268. self._schedule_finish_release(filename)
  269. if mode != VP_MODE_QUEUE:
  270. return
  271. # Drop the oldest stash if the cache is growing — happens when the
  272. # slicer sends project_file for a filename whose FTP upload was
  273. # rejected / cancelled / non-3MF, so _add_to_print_queue's pop
  274. # never fires. With no bound, a long-running VP accumulates one
  275. # dict per such mismatch.
  276. if len(self._slicer_print_options) >= _SLICER_OPTIONS_CACHE_LIMIT:
  277. try:
  278. stale_key = next(iter(self._slicer_print_options))
  279. self._slicer_print_options.pop(stale_key, None)
  280. self._slicer_print_options_events.pop(stale_key, None)
  281. logger.debug("[VP %s] Evicted stale slicer options for %s", self.name, stale_key)
  282. except StopIteration:
  283. pass
  284. self._slicer_print_options[filename] = dict(data)
  285. event = self._slicer_print_options_events.get(filename)
  286. if event:
  287. event.set()
  288. def _schedule_finish_release(self, filename: str, delay: float = 1.5) -> None:
  289. """Re-set gcode_state=FINISH on the VP after the project_file ack.
  290. #1280 set FINISH after the FTP upload completes — that was correct
  291. for the slicer flow at the time (MQTT project_file → FTP → done).
  292. Bambu Studio 2.7.x flipped the order to FTP → FTP → MQTT project_file,
  293. which means ``_send_print_response`` runs *after* the FINISH set in
  294. ``on_file_received`` and overwrites the state back to PREPARE. The
  295. slicer's 1 Hz status stream then carries PREPARE forever and the
  296. send modal sits at "Downloading" until the VP is restarted (#1658).
  297. Re-firing FINISH after a short delay closes the gap: the slicer sees
  298. the synthetic PREPARE in the project_file ack (and likely one PREPARE
  299. push on the 1 Hz cycle), then the next push carries FINISH and the
  300. modal releases. Proxy mode is exempt — there the real printer drives
  301. the state through the bridge and a synthetic FINISH would clobber a
  302. real PREPARE/RUNNING transition coming back from the printer.
  303. Cancels any in-flight timer before scheduling a new one so a slicer
  304. that fires project_file twice in quick succession only ends in one
  305. FINISH.
  306. """
  307. if self._mqtt is None:
  308. return
  309. if self._finish_release_task is not None and not self._finish_release_task.done():
  310. self._finish_release_task.cancel()
  311. self._finish_release_task = asyncio.create_task(
  312. self._delayed_finish_release(filename, delay),
  313. name=f"vp-{self.id}-finish-release",
  314. )
  315. async def _delayed_finish_release(self, filename: str, delay: float) -> None:
  316. """Sleep, then set gcode_state=FINISH. Used by ``_schedule_finish_release``."""
  317. try:
  318. await asyncio.sleep(delay)
  319. except asyncio.CancelledError:
  320. return
  321. if self._mqtt is None:
  322. return
  323. self._mqtt.set_gcode_state("FINISH", filename=filename, prepare_percent="100")
  324. logger.debug("[VP %s] Re-set gcode_state=FINISH after project_file ack (%s)", self.name, filename)
  325. async def _archive_file(self, file_path: Path, source_ip: str) -> None:
  326. """Archive file immediately."""
  327. if not self._session_factory:
  328. logger.error("Cannot archive: no database session factory configured")
  329. return
  330. if file_path.suffix.lower() != ".3mf":
  331. logger.debug("Skipping non-3MF file: %s", file_path.name)
  332. self._pending_files.pop(file_path.name, None)
  333. try:
  334. file_path.unlink()
  335. except OSError:
  336. pass
  337. return
  338. archived = False
  339. try:
  340. from backend.app.api.routes.settings import get_setting
  341. from backend.app.services.archive import ArchiveService
  342. async with self._session_factory() as db:
  343. name_source = await get_setting(db, "virtual_printer_archive_name_source")
  344. prefer_filename = name_source == "filename"
  345. service = ArchiveService(db)
  346. archive = await service.archive_print(
  347. printer_id=None,
  348. source_file=file_path,
  349. print_data={
  350. "status": "archived",
  351. "source": "virtual_printer",
  352. "source_ip": source_ip,
  353. },
  354. prefer_filename_for_name=prefer_filename,
  355. )
  356. if archive:
  357. logger.info("[VP %s] Archived: %s - %s", self.name, archive.id, archive.print_name)
  358. await self._broadcast_archive_created(archive)
  359. archived = True
  360. else:
  361. logger.error("Failed to archive file: %s", file_path.name)
  362. except Exception as e:
  363. logger.error("Error archiving file: %s", e)
  364. finally:
  365. # Always release the in-flight marker and delete the temp file —
  366. # previously the failure paths only logged and the next upload of
  367. # the same name was silently rejected with "already uploading",
  368. # the upload_dir filled up indefinitely, and the slicer received
  369. # a clean 226 even though no archive existed (#audit-R2-1).
  370. self._pending_files.pop(file_path.name, None)
  371. if archived:
  372. try:
  373. file_path.unlink()
  374. except OSError:
  375. pass
  376. else:
  377. # Drop the failed temp file so it doesn't accumulate.
  378. try:
  379. file_path.unlink(missing_ok=True)
  380. except OSError:
  381. pass
  382. async def _queue_file(self, file_path: Path, source_ip: str) -> None:
  383. """Queue file for user review."""
  384. if not self._session_factory:
  385. logger.error("Cannot queue: no database session factory configured")
  386. return
  387. if file_path.suffix.lower() != ".3mf":
  388. self._pending_files.pop(file_path.name, None)
  389. try:
  390. file_path.unlink()
  391. except OSError:
  392. pass
  393. return
  394. # Peek at the 3MF for the embedded title BEFORE we hand it off to the
  395. # DB. Storing it now means the /pending-uploads/ list doesn't have to
  396. # reopen every 3MF on every render to keep the review card and the
  397. # eventual archive name in sync (#1152 follow-up). Failure to parse is
  398. # not fatal — the response model falls back to the filename stem.
  399. metadata_print_name: str | None = None
  400. try:
  401. from backend.app.services.archive import ThreeMFParser
  402. parsed = ThreeMFParser(file_path).parse()
  403. raw_name = parsed.get("print_name")
  404. if isinstance(raw_name, str) and raw_name.strip():
  405. metadata_print_name = raw_name.strip()[:255]
  406. except Exception as e:
  407. logger.debug("[VP %s] Metadata title peek failed for %s: %s", self.name, file_path.name, e)
  408. try:
  409. from backend.app.models.pending_upload import PendingUpload
  410. async with self._session_factory() as db:
  411. pending = PendingUpload(
  412. filename=file_path.name,
  413. file_path=str(file_path),
  414. file_size=file_path.stat().st_size,
  415. source_ip=source_ip,
  416. status="pending",
  417. uploaded_at=datetime.now(timezone.utc),
  418. metadata_print_name=metadata_print_name,
  419. )
  420. db.add(pending)
  421. await db.commit()
  422. logger.info("[VP %s] Queued: %s - %s", self.name, pending.id, file_path.name)
  423. except Exception as e:
  424. logger.error("Error queueing file: %s", e)
  425. # Queue insert failed — drop the temp file so it doesn't
  426. # accumulate. The file is unreachable without the DB row.
  427. try:
  428. file_path.unlink(missing_ok=True)
  429. except OSError:
  430. pass
  431. finally:
  432. # Always release the in-flight marker so concurrent uploads
  433. # with the same filename aren't spuriously rejected after
  434. # a queue failure.
  435. self._pending_files.pop(file_path.name, None)
  436. async def _add_to_print_queue(self, file_path: Path, source_ip: str) -> None:
  437. """Archive file and add to print queue, assigned to target printer or model."""
  438. if not self._session_factory:
  439. logger.error("Cannot add to print queue: no database session factory configured")
  440. return
  441. if file_path.suffix.lower() != ".3mf":
  442. self._pending_files.pop(file_path.name, None)
  443. try:
  444. file_path.unlink()
  445. except OSError:
  446. pass
  447. return
  448. # Wait briefly for the slicer's MQTT `project_file` command so the
  449. # queue item can inherit the slicer-side print options the user
  450. # picked (timelapse, bed_leveling, etc). Slicers send the FTP upload
  451. # first and the MQTT command immediately after, so the typical lag
  452. # is a few hundred ms; 2 s is conservative without making every
  453. # VP-queue add visibly slow. Falls back to the global default_*
  454. # settings if MQTT doesn't arrive in time (legacy behaviour for
  455. # users on a slicer that doesn't send a print command). #1403.
  456. # The wait is skipped when there's no MQTT server attached — covers
  457. # unit tests that invoke `_add_to_print_queue` directly without
  458. # going through `on_print_command`, so they don't pay the 2 s tax.
  459. slicer_opts = self._slicer_print_options.pop(file_path.name, None)
  460. if slicer_opts is None and self._mqtt is not None:
  461. event = asyncio.Event()
  462. self._slicer_print_options_events[file_path.name] = event
  463. try:
  464. await asyncio.wait_for(event.wait(), timeout=2.0)
  465. slicer_opts = self._slicer_print_options.pop(file_path.name, None)
  466. except asyncio.TimeoutError:
  467. slicer_opts = None
  468. finally:
  469. self._slicer_print_options_events.pop(file_path.name, None)
  470. try:
  471. import json
  472. from backend.app.api.routes.settings import get_setting
  473. from backend.app.models.print_queue import PrintQueueItem
  474. from backend.app.services.archive import ArchiveService
  475. from backend.app.services.filament_requirements import extract_filament_requirements
  476. async with self._session_factory() as db:
  477. name_source = await get_setting(db, "virtual_printer_archive_name_source")
  478. prefer_filename = name_source == "filename"
  479. # Read workflow defaults from settings. Without this the
  480. # PrintQueueItem below would fall back to the column-level
  481. # defaults and ignore the user's workflow preferences (#1235).
  482. # Fallbacks match AppSettings defaults in schemas/settings.py.
  483. # The slicer-side options captured above (if any) take
  484. # precedence per-field over these defaults.
  485. def _bool_setting(value: str | None, default: bool) -> bool:
  486. return value.lower() == "true" if value is not None else default
  487. def _slicer_or(field_mqtt: str, settings_default: bool) -> bool:
  488. """Slicer's MQTT value if present, else the settings default.
  489. Slicer payloads carry both bool and int (0/1) shapes
  490. depending on firmware family — coerce via bool() so
  491. `0`/`False` and `1`/`True` both work.
  492. """
  493. if slicer_opts is not None and field_mqtt in slicer_opts:
  494. return bool(slicer_opts[field_mqtt])
  495. return settings_default
  496. # Note the MQTT field names differ from Bambuddy's column
  497. # names: MQTT uses `bed_leveling` (single L) while the
  498. # column / settings key use `bed_levelling` (double L).
  499. bed_levelling = _slicer_or(
  500. "bed_leveling", _bool_setting(await get_setting(db, "default_bed_levelling"), True)
  501. )
  502. flow_cali = _slicer_or("flow_cali", _bool_setting(await get_setting(db, "default_flow_cali"), False))
  503. vibration_cali = _slicer_or(
  504. "vibration_cali", _bool_setting(await get_setting(db, "default_vibration_cali"), True)
  505. )
  506. layer_inspect = _slicer_or(
  507. "layer_inspect", _bool_setting(await get_setting(db, "default_layer_inspect"), False)
  508. )
  509. timelapse = _slicer_or("timelapse", _bool_setting(await get_setting(db, "default_timelapse"), False))
  510. service = ArchiveService(db)
  511. archive = await service.archive_print(
  512. printer_id=None,
  513. source_file=file_path,
  514. print_data={
  515. "status": "archived",
  516. "source": "virtual_printer",
  517. "source_ip": source_ip,
  518. },
  519. prefer_filename_for_name=prefer_filename,
  520. )
  521. if archive:
  522. logger.info("[VP %s] Archived: %s - %s", self.name, archive.id, archive.print_name)
  523. # Assign to specific printer if configured, otherwise use model for "Any X" scheduling
  524. target_model = None
  525. if not self.target_printer_id and self.model:
  526. target_model = VIRTUAL_PRINTER_MODELS.get(self.model)
  527. # #1733: multi-plate "Send All" uploads ship every plate in
  528. # one 3MF — `slice_info.config` lists each `<plate>` with
  529. # its own index. Enqueue one PrintQueueItem per plate so
  530. # the scheduler runs each separately. Single-plate "Send"
  531. # comes through as `[N]` (one plate index) so the loop
  532. # below runs once and the existing behaviour is preserved.
  533. plate_ids = self._extract_plate_ids(file_path)
  534. # Pick a base position the same way the manual /print-queue/
  535. # POST does, then hand consecutive positions to each plate
  536. # so a Send All keeps plate-order execution inside the
  537. # queue (#1733). Previously hardcoded to 1, which created
  538. # duplicate position=1 rows on every VP upload and made
  539. # queue execution order non-deterministic for any non-
  540. # empty queue.
  541. from sqlalchemy import func, select as _sql_select
  542. queue_scope = _sql_select(func.max(PrintQueueItem.position)).where(
  543. PrintQueueItem.status == "pending"
  544. )
  545. if self.target_printer_id is not None:
  546. queue_scope = queue_scope.where(PrintQueueItem.printer_id == self.target_printer_id)
  547. else:
  548. queue_scope = queue_scope.where(PrintQueueItem.printer_id.is_(None))
  549. try:
  550. max_pos_raw = (await db.execute(queue_scope)).scalar()
  551. max_pos = int(max_pos_raw) if max_pos_raw is not None else 0
  552. except (TypeError, ValueError):
  553. max_pos = 0
  554. # Parse per-plate filament requirements (#1188). Each plate
  555. # has its own filament set in `slice_info.config`, so the
  556. # `required_filament_types` / `filament_overrides` columns
  557. # on each queue item reflect THAT plate, not the file's
  558. # first plate. Scoping was already plate-aware via #1697 —
  559. # the `extract_filament_requirements(path, plate_id)` filter
  560. # returns just the plate's filaments. required_filament_types
  561. # is populated unconditionally — it's cheap, lets the
  562. # scheduler reject obvious mis-matches even without
  563. # force_color_match. filament_overrides only carries
  564. # force_color_match=True when the per-VP setting is on, so
  565. # upgraders keep the old behaviour by default.
  566. queue_item_ids: list[int] = []
  567. for offset, plate_id in enumerate(plate_ids, start=1):
  568. required_filament_types_json: str | None = None
  569. filament_overrides_json: str | None = None
  570. requirements = extract_filament_requirements(file_path, plate_id)
  571. if requirements:
  572. types = sorted({r["type"] for r in requirements if r.get("type")})
  573. if types:
  574. required_filament_types_json = json.dumps(types)
  575. if self.queue_force_color_match:
  576. overrides = [
  577. {
  578. "slot_id": r["slot_id"],
  579. "type": r.get("type", ""),
  580. "color": r.get("color", ""),
  581. "force_color_match": True,
  582. }
  583. for r in requirements
  584. if r.get("type") and r.get("color")
  585. ]
  586. if overrides:
  587. filament_overrides_json = json.dumps(overrides)
  588. queue_item = PrintQueueItem(
  589. printer_id=self.target_printer_id,
  590. target_model=target_model,
  591. archive_id=archive.id,
  592. plate_id=plate_id,
  593. position=max_pos + offset,
  594. status="pending",
  595. manual_start=not self.auto_dispatch,
  596. required_filament_types=required_filament_types_json,
  597. filament_overrides=filament_overrides_json,
  598. bed_levelling=bed_levelling,
  599. flow_cali=flow_cali,
  600. vibration_cali=vibration_cali,
  601. layer_inspect=layer_inspect,
  602. timelapse=timelapse,
  603. )
  604. db.add(queue_item)
  605. await db.flush() # populate queue_item.id before logging
  606. queue_item_ids.append(queue_item.id)
  607. await db.commit()
  608. if len(queue_item_ids) == 1:
  609. logger.info("[VP %s] Added to queue: %s", self.name, queue_item_ids[0])
  610. else:
  611. logger.info(
  612. "[VP %s] Added %d queue items for multi-plate upload (plates %s): %s",
  613. self.name,
  614. len(queue_item_ids),
  615. plate_ids,
  616. queue_item_ids,
  617. )
  618. await self._broadcast_archive_created(archive)
  619. else:
  620. logger.error("Failed to archive file: %s", file_path.name)
  621. except Exception as e:
  622. logger.error("Error adding to print queue: %s", e)
  623. finally:
  624. # Always release the marker and clean the temp file. Without this
  625. # the same-name STOR guard would block the next upload and the
  626. # upload_dir would accumulate failed temp files forever
  627. # (#audit-R2-1).
  628. self._pending_files.pop(file_path.name, None)
  629. try:
  630. file_path.unlink(missing_ok=True)
  631. except OSError:
  632. pass
  633. async def _broadcast_archive_created(self, archive) -> None:
  634. """Notify connected clients that a new archive exists.
  635. Real-printer prints get this from main.py's MQTT print_start handler;
  636. VP-uploaded prints need their own broadcast or the Archives page stays
  637. stale until the user switches tabs (#1282).
  638. """
  639. try:
  640. from backend.app.core.websocket import ws_manager
  641. await ws_manager.send_archive_created(
  642. {
  643. "id": archive.id,
  644. "printer_id": archive.printer_id,
  645. "filename": archive.filename,
  646. "print_name": archive.print_name,
  647. "status": archive.status,
  648. }
  649. )
  650. except Exception as e:
  651. logger.debug("[VP %s] archive_created broadcast failed: %s", self.name, e)
  652. @staticmethod
  653. def _extract_plate_ids(file_path: Path) -> list[int]:
  654. """Extract every plate index from a 3MF's slice_info.config.
  655. A multi-plate "Send All" from BambuStudio / OrcaSlicer uploads a
  656. single 3MF containing every plate the user selected. Each plate
  657. has its own ``<plate>`` block with a ``<metadata key="index"
  658. value="N"/>`` child and its own ``Metadata/plate_N.gcode`` payload
  659. inside the same zip. Returning the full ordered list lets the VP
  660. queue path create one queue item per plate (`_add_to_print_queue`
  661. loops over the result), so "Send All" of a 3-plate file produces
  662. 3 queue items sharing the same archive — one per plate to print.
  663. Single-plate "Send" hits the same code path and returns ``[N]``
  664. for whichever plate the user selected; the loop runs once and the
  665. existing single-plate behaviour is preserved.
  666. Returns ``[1]`` when the 3MF is missing ``slice_info.config``,
  667. unparseable, or contains no plate-index metadata — the original
  668. single-plate fallback. Production logs at debug so a non-3MF
  669. upload doesn't spam, but the trail survives for support bundles.
  670. """
  671. try:
  672. import xml.etree.ElementTree as ET
  673. import zipfile
  674. with zipfile.ZipFile(file_path, "r") as zf:
  675. if "Metadata/slice_info.config" in zf.namelist():
  676. content = zf.read("Metadata/slice_info.config").decode()
  677. root = ET.fromstring(content) # noqa: S314 # nosec B314
  678. plate_ids: list[int] = []
  679. for plate in root.findall(".//plate"):
  680. for meta in plate.findall("metadata"):
  681. if meta.get("key") == "index" and meta.get("value"):
  682. try:
  683. plate_ids.append(int(meta.get("value")))
  684. except ValueError:
  685. continue
  686. break
  687. if plate_ids:
  688. return plate_ids
  689. except Exception as e:
  690. logger.debug("[VP] _extract_plate_ids failed for %s: %s", file_path.name, e)
  691. return [1]
  692. # -- Service lifecycle --
  693. def _resolve_cert_and_advertise(self) -> tuple[Path, Path, str]:
  694. """Return (cert_path, key_path, advertise_address) for TLS services.
  695. Always uses the self-signed cert chain (signed by `bbl_ca`). The user
  696. imports `bbl_ca.crt` once into the slicer; per-VP certs validate from
  697. there. Tailscale exposure is handled by the user picking the Tailscale
  698. IP in the bind_ip dropdown.
  699. """
  700. cert_path, key_path = self.generate_certificates()
  701. advertise = self.remote_interface_ip or self.bind_ip or ""
  702. return cert_path, key_path, advertise
  703. async def start_server(self) -> None:
  704. """Start server-mode services (FTP, MQTT, SSDP, Bind) on this VP's bind_ip."""
  705. logger.info("[VP %s] Starting server-mode services on %s", self.name, self.bind_ip)
  706. cert_path, key_path, advertise_addr = self._resolve_cert_and_advertise()
  707. bind_addr = self.bind_ip or "0.0.0.0" # nosec B104
  708. async def run_with_logging(coro, svc_name):
  709. try:
  710. await coro
  711. except Exception as e:
  712. logger.error("[VP %s] %s failed: %s", self.name, svc_name, e)
  713. self._tasks = []
  714. # FTP server. Each VP gets a non-overlapping passive-mode port slice
  715. # derived from its DB id so bridge-mode Docker users only have to
  716. # expose a narrow range (#1646). Default slice is 10 ports per VP;
  717. # see ftp_server.compute_passive_port_slice for the wrap-around
  718. # behaviour on installs with very high VP ids.
  719. passive_port_min, passive_port_max = compute_passive_port_slice(self.id)
  720. self._ftp = VirtualPrinterFTPServer(
  721. upload_dir=self.upload_dir,
  722. access_code=self.access_code,
  723. cert_path=cert_path,
  724. key_path=key_path,
  725. on_file_received=self.on_file_received,
  726. bind_address=bind_addr,
  727. vp_name=self.name,
  728. passive_port_min=passive_port_min,
  729. passive_port_max=passive_port_max,
  730. )
  731. self._tasks.append(
  732. asyncio.create_task(
  733. run_with_logging(self._ftp.start(), "FTP"),
  734. name=f"vp_{self.id}_ftp",
  735. )
  736. )
  737. # MQTT server
  738. self._mqtt = SimpleMQTTServer(
  739. serial=self.serial,
  740. access_code=self.access_code,
  741. cert_path=cert_path,
  742. key_path=key_path,
  743. on_print_command=self.on_print_command,
  744. model=self.model or DEFAULT_VIRTUAL_PRINTER_MODEL,
  745. bind_address=bind_addr,
  746. vp_name=self.name,
  747. )
  748. self._tasks.append(
  749. asyncio.create_task(
  750. run_with_logging(self._mqtt.start(), "MQTT"),
  751. name=f"vp_{self.id}_mqtt",
  752. )
  753. )
  754. # MQTT bridge — fans out the target printer's pushes to slicers connected
  755. # to this VP and forwards their commands back to the printer. Only meaningful
  756. # when a target printer is configured AND printer_manager was injected (it
  757. # always is at runtime; tests may omit it).
  758. if self.target_printer_id is not None and self._printer_manager is not None:
  759. self._mqtt_bridge = MQTTBridge(
  760. vp_id=self.id,
  761. vp_name=self.name,
  762. vp_serial=self.serial,
  763. target_printer_id=self.target_printer_id,
  764. mqtt_server=self._mqtt,
  765. printer_manager=self._printer_manager,
  766. )
  767. self._mqtt.set_bridge(self._mqtt_bridge)
  768. await self._mqtt_bridge.start()
  769. # RTSPS camera passthrough on port 322. BambuStudio's camera button
  770. # connects to the device IP it bound on (the VP), not the IP in
  771. # `ipcam.rtsp_url`. Without a listener on <bind_ip>:322 the slicer
  772. # gets connection refused → "LAN connection failed". Same raw TCP
  773. # pass-through used by SlicerProxyManager in proxy mode.
  774. target_client = self._printer_manager.get_client(self.target_printer_id)
  775. target_ip = getattr(target_client, "ip_address", None) if target_client else None
  776. if target_ip:
  777. self._rtsp_proxy = TCPProxy(
  778. name="RTSP",
  779. listen_port=322,
  780. target_host=target_ip,
  781. target_port=322,
  782. bind_address=bind_addr,
  783. )
  784. self._tasks.append(
  785. asyncio.create_task(
  786. run_with_logging(self._rtsp_proxy.start(), "RTSP"),
  787. name=f"vp_{self.id}_rtsp",
  788. )
  789. )
  790. # Bind server
  791. self._bind = BindServer(
  792. serial=self.serial,
  793. model=self.model or DEFAULT_VIRTUAL_PRINTER_MODEL,
  794. name=self.name,
  795. bind_address=bind_addr,
  796. cert_path=cert_path,
  797. key_path=key_path,
  798. )
  799. self._tasks.append(
  800. asyncio.create_task(
  801. run_with_logging(self._bind.start(), "Bind"),
  802. name=f"vp_{self.id}_bind",
  803. )
  804. )
  805. # SSDP server — advertise_addr is the remote_interface_ip (Tailscale
  806. # IP, when chosen from the bind_ip dropdown) or the bind_ip. SSDP
  807. # Location accepts IPs only; FQDNs go in through bind_ip selection
  808. # at the printer-IP level and resolve before reaching the SSDP
  809. # advertisement.
  810. self._ssdp = VirtualPrinterSSDPServer(
  811. name=self.name,
  812. serial=self.serial,
  813. model=self.model or DEFAULT_VIRTUAL_PRINTER_MODEL,
  814. advertise_ip=advertise_addr,
  815. bind_ip=bind_addr,
  816. )
  817. self._tasks.append(
  818. asyncio.create_task(
  819. run_with_logging(self._ssdp.start(), "SSDP"),
  820. name=f"vp_{self.id}_ssdp",
  821. )
  822. )
  823. # Wait briefly for every child service to actually finish binding its
  824. # socket so ``is_running`` doesn't lie. Without this barrier a caller
  825. # racing the start (e.g. the diagnostic route) would see is_running=True
  826. # while ports were still in the gap between task creation and the
  827. # ``asyncio.start_server`` returning. Bounded timeout — if a child
  828. # hangs we log it and move on; the existing task tracking still
  829. # catches the failure on the next iteration.
  830. ready_targets = [
  831. ("FTP", self._ftp.ready),
  832. ("MQTT", self._mqtt.ready),
  833. ("Bind", self._bind.ready),
  834. ("SSDP", self._ssdp.ready),
  835. ]
  836. try:
  837. await asyncio.wait_for(
  838. asyncio.gather(*(e.wait() for _, e in ready_targets)),
  839. timeout=5.0,
  840. )
  841. except TimeoutError:
  842. not_ready = [name for name, e in ready_targets if not e.is_set()]
  843. logger.warning(
  844. "[VP %s] Sub-service(s) didn't bind within 5s: %s — continuing anyway",
  845. self.name,
  846. ", ".join(not_ready) or "(none)",
  847. )
  848. logger.info("[VP %s] Server-mode services started on %s", self.name, bind_addr)
  849. async def stop_server(self) -> None:
  850. """Stop server-mode services."""
  851. if self._finish_release_task is not None and not self._finish_release_task.done():
  852. self._finish_release_task.cancel()
  853. self._finish_release_task = None
  854. if self._mqtt_bridge:
  855. try:
  856. await self._mqtt_bridge.stop()
  857. except Exception:
  858. logger.exception("[VP %s] MQTT bridge stop failed", self.name)
  859. if self._mqtt:
  860. self._mqtt.set_bridge(None)
  861. self._mqtt_bridge = None
  862. if self._rtsp_proxy:
  863. try:
  864. await self._rtsp_proxy.stop()
  865. except Exception:
  866. logger.exception("[VP %s] RTSP proxy stop failed", self.name)
  867. self._rtsp_proxy = None
  868. if self._ftp:
  869. await self._ftp.stop()
  870. self._ftp = None
  871. if self._mqtt:
  872. await self._mqtt.stop()
  873. self._mqtt = None
  874. if self._bind:
  875. await self._bind.stop()
  876. self._bind = None
  877. if self._ssdp:
  878. await self._ssdp.stop()
  879. self._ssdp = None
  880. await self._cancel_tasks()
  881. async def start_proxy(self) -> None:
  882. """Start proxy mode services for this instance."""
  883. logger.info("[VP %s] Starting proxy mode to %s", self.name, self.target_printer_ip)
  884. cert_path, key_path, _ = self._resolve_cert_and_advertise()
  885. self._proxy = SlicerProxyManager(
  886. target_host=self.target_printer_ip,
  887. cert_path=cert_path,
  888. key_path=key_path,
  889. on_activity=lambda n, m: logger.info("[VP %s] Proxy %s: %s", self.name, n, m),
  890. bind_address=self.bind_ip or "0.0.0.0", # nosec B104
  891. bind_identity={
  892. "serial": self.target_printer_serial or self.serial,
  893. "model": self.model or DEFAULT_VIRTUAL_PRINTER_MODEL,
  894. "name": self.name,
  895. "version": "01.00.00.00",
  896. },
  897. )
  898. async def run_with_logging(coro, svc_name):
  899. try:
  900. await coro
  901. except Exception as e:
  902. logger.error("[VP %s] %s failed: %s", self.name, svc_name, e)
  903. self._tasks = []
  904. # SSDP for proxy
  905. proxy_serial = self.target_printer_serial or self.serial
  906. if self.remote_interface_ip:
  907. from backend.app.services.network_utils import find_interface_for_ip
  908. local_iface = find_interface_for_ip(self.target_printer_ip)
  909. if local_iface:
  910. self._ssdp_proxy = SSDPProxy(
  911. local_interface_ip=local_iface["ip"],
  912. remote_interface_ip=self.remote_interface_ip,
  913. target_printer_ip=self.target_printer_ip,
  914. name=self.name,
  915. )
  916. self._tasks.append(
  917. asyncio.create_task(
  918. run_with_logging(self._ssdp_proxy.start(), "SSDP Proxy"),
  919. name=f"vp_{self.id}_ssdp_proxy",
  920. )
  921. )
  922. else:
  923. self._start_fallback_ssdp(proxy_serial, run_with_logging)
  924. else:
  925. self._start_fallback_ssdp(proxy_serial, run_with_logging)
  926. self._tasks.append(
  927. asyncio.create_task(
  928. run_with_logging(self._proxy.start(), "Proxy"),
  929. name=f"vp_{self.id}_proxy",
  930. )
  931. )
  932. def _start_fallback_ssdp(self, proxy_serial: str, run_with_logging) -> None:
  933. """Start single-interface SSDP server as fallback for proxy mode."""
  934. self._ssdp = VirtualPrinterSSDPServer(
  935. name=f"{self.name} (Proxy)",
  936. serial=proxy_serial,
  937. model=self.model or DEFAULT_VIRTUAL_PRINTER_MODEL,
  938. advertise_ip=self.bind_ip or "",
  939. bind_ip=self.bind_ip or "",
  940. )
  941. self._tasks.append(
  942. asyncio.create_task(
  943. run_with_logging(self._ssdp.start(), "SSDP"),
  944. name=f"vp_{self.id}_ssdp",
  945. )
  946. )
  947. async def stop_proxy(self) -> None:
  948. """Stop proxy mode services for this instance."""
  949. if self._proxy:
  950. await self._proxy.stop()
  951. self._proxy = None
  952. if self._ssdp:
  953. await self._ssdp.stop()
  954. self._ssdp = None
  955. if self._ssdp_proxy:
  956. await self._ssdp_proxy.stop()
  957. self._ssdp_proxy = None
  958. await self._cancel_tasks()
  959. async def _cancel_tasks(self) -> None:
  960. """Cancel all running tasks and wait for cleanup."""
  961. for task in self._tasks:
  962. task.cancel()
  963. if self._tasks:
  964. try:
  965. await asyncio.wait_for(asyncio.gather(*self._tasks, return_exceptions=True), timeout=1.0)
  966. except TimeoutError:
  967. pass
  968. self._tasks = []
  969. def get_status(self) -> dict:
  970. """Get status for this instance."""
  971. status: dict = {
  972. "running": self.is_running,
  973. "pending_files": len(self._pending_files),
  974. }
  975. if self.is_proxy and self._proxy:
  976. status["proxy"] = self._proxy.get_status()
  977. return status
  978. class VirtualPrinterManager:
  979. """Multi-instance virtual printer registry and orchestrator.
  980. Every VP runs its own independent services on a dedicated bind IP.
  981. """
  982. def __init__(self):
  983. self._session_factory: Callable | None = None
  984. self._printer_manager: PrinterManager | None = None
  985. self._instances: dict[int, VirtualPrinterInstance] = {}
  986. # Serialize sync_from_db so concurrent PUT /vp/{id} calls can't
  987. # race the start/stop sequence and leave duplicate sub-services
  988. # bound to the same port. The lock is fine-grained enough that
  989. # a single VP update completes in well under a second; if the
  990. # user holds the lock with a long-running start they intended
  991. # to anyway.
  992. self._sync_lock = asyncio.Lock()
  993. # Directories
  994. self._base_dir = app_settings.base_dir / "virtual_printer"
  995. # Ensure base directories exist
  996. self._ensure_base_directories()
  997. def _ensure_base_directories(self) -> None:
  998. """Create base directories at startup."""
  999. for dir_path in [self._base_dir, self._base_dir / "uploads", self._base_dir / "certs"]:
  1000. try:
  1001. dir_path.mkdir(parents=True, exist_ok=True)
  1002. except PermissionError:
  1003. logger.error(
  1004. f"Cannot create directory {dir_path}: Permission denied. "
  1005. f"For Docker: ensure the data volume is writable by the container user. "
  1006. f"For bare metal: run 'sudo chown -R $(whoami) {self._base_dir}'"
  1007. )
  1008. def set_session_factory(self, session_factory: Callable) -> None:
  1009. """Set the database session factory."""
  1010. self._session_factory = session_factory
  1011. def set_printer_manager(self, printer_manager: "PrinterManager") -> None:
  1012. """Inject the global printer_manager so non-proxy VPs can mirror their target's MQTT stream."""
  1013. self._printer_manager = printer_manager
  1014. def get_ca_certificate_info(self) -> dict:
  1015. """Return the shared virtual-printer CA certificate for slicer-trust import.
  1016. The CA is shared by every VP (one import covers all of them). It is
  1017. generated on demand here if no VP has triggered cert generation yet,
  1018. so the "copy/download certificate" UI works even before the first VP
  1019. is enabled.
  1020. """
  1021. certs_dir = self._base_dir / "certs"
  1022. cert_service = CertificateService(cert_dir=certs_dir, shared_ca_dir=certs_dir)
  1023. return cert_service.get_ca_certificate_info()
  1024. @property
  1025. def is_enabled(self) -> bool:
  1026. """Check if any virtual printer is running."""
  1027. return len(self._instances) > 0
  1028. async def sync_from_db(self) -> None:
  1029. """Load all VPs from DB, reconcile running state.
  1030. Serialised by ``self._sync_lock`` — concurrent PUT /vp/{id} routes
  1031. all call into this method; without the lock the start / stop
  1032. sequence races and can leave duplicate sub-services bound to the
  1033. same port or orphan still-running tasks.
  1034. """
  1035. if not self._session_factory:
  1036. logger.warning("Cannot sync virtual printers: no session factory")
  1037. return
  1038. async with self._sync_lock:
  1039. await self._sync_from_db_locked()
  1040. async def _sync_from_db_locked(self) -> None:
  1041. """Inner sync body — caller holds ``self._sync_lock``."""
  1042. from sqlalchemy import select
  1043. from backend.app.models.printer import Printer
  1044. from backend.app.models.virtual_printer import VirtualPrinter
  1045. async with self._session_factory() as db:
  1046. result = await db.execute(
  1047. select(VirtualPrinter).where(VirtualPrinter.enabled == True).order_by(VirtualPrinter.position) # noqa: E712
  1048. )
  1049. enabled_vps = result.scalars().all()
  1050. # Stop instances that are no longer enabled or changed mode
  1051. enabled_ids = {vp.id for vp in enabled_vps}
  1052. for vp_id in list(self._instances.keys()):
  1053. if vp_id not in enabled_ids:
  1054. await self.remove_instance(vp_id)
  1055. # Look up printer IPs for proxy VPs
  1056. proxy_vps = [vp for vp in enabled_vps if vp.mode == "proxy"]
  1057. proxy_ips: dict[int, tuple[str, str]] = {}
  1058. if proxy_vps:
  1059. async with self._session_factory() as db:
  1060. for pvp in proxy_vps:
  1061. if pvp.target_printer_id:
  1062. result = await db.execute(select(Printer).where(Printer.id == pvp.target_printer_id))
  1063. printer = result.scalar_one_or_none()
  1064. if printer:
  1065. proxy_ips[pvp.id] = (printer.ip_address, printer.serial_number)
  1066. # Detect config changes on running instances and restart if needed
  1067. for vp in enabled_vps:
  1068. instance = self._instances.get(vp.id)
  1069. if not instance:
  1070. continue
  1071. # Proxy mode: detect target printer IP / serial changes from the
  1072. # DB lookup above. Without this branch a DHCP renewal that gives
  1073. # the target printer a new IP would leave the running proxy
  1074. # forwarding to the stale IP until the user manually toggles the
  1075. # VP. The same shape covers a target-side serial change.
  1076. proxy_target_changed = False
  1077. if vp.mode == "proxy":
  1078. fresh = proxy_ips.get(vp.id)
  1079. if fresh is not None:
  1080. fresh_ip, fresh_serial = fresh
  1081. if (
  1082. getattr(instance, "target_printer_ip", None) != fresh_ip
  1083. or getattr(instance, "target_printer_serial", None) != fresh_serial
  1084. ):
  1085. proxy_target_changed = True
  1086. # Normalize the DB value before comparing — a legacy `immediate`
  1087. # row read before the migration window finishes would otherwise
  1088. # trip the "changed" branch and bounce every VP at boot.
  1089. db_mode = normalize_vp_mode(vp.mode)
  1090. changed = (
  1091. instance.mode != db_mode
  1092. or instance.model != (vp.model or DEFAULT_VIRTUAL_PRINTER_MODEL)
  1093. or instance.access_code != (vp.access_code or "")
  1094. or instance.bind_ip != (vp.bind_ip or "")
  1095. or instance.remote_interface_ip != (vp.remote_interface_ip or "")
  1096. or instance.target_printer_id != vp.target_printer_id
  1097. or instance.auto_dispatch != vp.auto_dispatch
  1098. # Queue-mode behaviour toggle — without it the running
  1099. # instance silently keeps the old value until process
  1100. # restart (#1552 follow-up family).
  1101. or instance.queue_force_color_match != vp.queue_force_color_match
  1102. or proxy_target_changed
  1103. )
  1104. if changed:
  1105. logger.info(
  1106. "VP %s config changed (mode: %s→%s), restarting",
  1107. instance.name,
  1108. instance.mode,
  1109. vp.mode,
  1110. )
  1111. await self.remove_instance(vp.id)
  1112. # Start instances for all enabled VPs (skip already running)
  1113. for vp in enabled_vps:
  1114. if vp.id in self._instances:
  1115. continue
  1116. if vp.mode == "proxy":
  1117. ip_info = proxy_ips.get(vp.id)
  1118. if not ip_info:
  1119. logger.warning("Proxy VP %s: target printer not found, skipping", vp.name)
  1120. continue
  1121. target_ip, target_serial = ip_info
  1122. instance = VirtualPrinterInstance(
  1123. vp_id=vp.id,
  1124. name=vp.name,
  1125. mode=vp.mode,
  1126. model=vp.model or DEFAULT_VIRTUAL_PRINTER_MODEL,
  1127. access_code=vp.access_code or "",
  1128. serial_suffix=vp.serial_suffix,
  1129. target_printer_ip=target_ip,
  1130. target_printer_serial=target_serial,
  1131. auto_dispatch=vp.auto_dispatch,
  1132. bind_ip=vp.bind_ip or "",
  1133. remote_interface_ip=vp.remote_interface_ip or "",
  1134. tailscale_disabled=vp.tailscale_disabled,
  1135. base_dir=self._base_dir,
  1136. session_factory=self._session_factory,
  1137. )
  1138. self._instances[vp.id] = instance
  1139. await instance.start_proxy()
  1140. logger.info("Started proxy VP: %s → %s (bind=%s)", instance.name, target_ip, instance.bind_ip)
  1141. else:
  1142. instance = VirtualPrinterInstance(
  1143. vp_id=vp.id,
  1144. name=vp.name,
  1145. mode=vp.mode,
  1146. model=vp.model or DEFAULT_VIRTUAL_PRINTER_MODEL,
  1147. access_code=vp.access_code or "",
  1148. serial_suffix=vp.serial_suffix,
  1149. target_printer_id=vp.target_printer_id,
  1150. auto_dispatch=vp.auto_dispatch,
  1151. queue_force_color_match=vp.queue_force_color_match,
  1152. bind_ip=vp.bind_ip or "",
  1153. remote_interface_ip=vp.remote_interface_ip or "",
  1154. tailscale_disabled=vp.tailscale_disabled,
  1155. base_dir=self._base_dir,
  1156. session_factory=self._session_factory,
  1157. printer_manager=self._printer_manager,
  1158. )
  1159. self._instances[vp.id] = instance
  1160. await instance.start_server()
  1161. logger.info("Started server-mode VP: %s on %s", instance.name, vp.bind_ip)
  1162. async def remove_instance(self, vp_id: int) -> None:
  1163. """Stop and remove a single VP instance."""
  1164. instance = self._instances.pop(vp_id, None)
  1165. if instance:
  1166. if instance.is_proxy:
  1167. await instance.stop_proxy()
  1168. else:
  1169. await instance.stop_server()
  1170. logger.info("Removed VP instance: %s", instance.name)
  1171. async def stop_all(self) -> None:
  1172. """Shutdown all virtual printer services."""
  1173. logger.info("Stopping all virtual printer services...")
  1174. for vp_id in list(self._instances.keys()):
  1175. await self.remove_instance(vp_id)
  1176. logger.info("All virtual printer services stopped")
  1177. def get_instance(self, vp_id: int) -> VirtualPrinterInstance | None:
  1178. """Get a running instance by ID."""
  1179. return self._instances.get(vp_id)
  1180. def get_all_status(self) -> list[dict]:
  1181. """Get status for all running instances."""
  1182. return [
  1183. {
  1184. "id": inst.id,
  1185. "name": inst.name,
  1186. "mode": inst.mode,
  1187. **inst.get_status(),
  1188. }
  1189. for inst in self._instances.values()
  1190. ]
  1191. # -- Legacy single-printer compat --
  1192. def get_status(self) -> dict:
  1193. """Get status for first virtual printer (backward compat)."""
  1194. if self._instances:
  1195. first = next(iter(self._instances.values()))
  1196. return {
  1197. "enabled": True,
  1198. "running": first.is_running,
  1199. "mode": first.mode,
  1200. "name": first.name,
  1201. "serial": first.serial,
  1202. "model": first.model or DEFAULT_VIRTUAL_PRINTER_MODEL,
  1203. "model_name": VIRTUAL_PRINTER_MODELS.get(
  1204. first.model or DEFAULT_VIRTUAL_PRINTER_MODEL,
  1205. first.model or DEFAULT_VIRTUAL_PRINTER_MODEL,
  1206. ),
  1207. "pending_files": first.get_status().get("pending_files", 0),
  1208. **({"target_printer_ip": first.target_printer_ip} if first.is_proxy else {}),
  1209. **({"proxy": first.get_status().get("proxy", {})} if first.is_proxy else {}),
  1210. }
  1211. return {
  1212. "enabled": False,
  1213. "running": False,
  1214. "mode": VP_MODE_ARCHIVE,
  1215. "name": "Bambuddy",
  1216. "serial": "",
  1217. "model": DEFAULT_VIRTUAL_PRINTER_MODEL,
  1218. "model_name": VIRTUAL_PRINTER_MODELS[DEFAULT_VIRTUAL_PRINTER_MODEL],
  1219. "pending_files": 0,
  1220. }
  1221. async def configure(
  1222. self,
  1223. enabled: bool,
  1224. access_code: str = "",
  1225. mode: str = VP_MODE_ARCHIVE,
  1226. model: str = "",
  1227. target_printer_ip: str = "",
  1228. target_printer_serial: str = "",
  1229. remote_interface_ip: str = "",
  1230. ) -> None:
  1231. """Legacy single-printer configure. Delegates to sync_from_db()."""
  1232. # This method is kept for backward compat with the settings endpoint.
  1233. # The actual work is done by sync_from_db() which reads from the DB.
  1234. await self.sync_from_db()
  1235. # Global instance
  1236. virtual_printer_manager = VirtualPrinterManager()