manager.py 97 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033
  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 json
  7. import logging
  8. import time
  9. from collections.abc import Callable
  10. from datetime import datetime, timezone
  11. from pathlib import Path
  12. from typing import TYPE_CHECKING
  13. from backend.app.core.config import settings as app_settings
  14. from backend.app.models.virtual_printer import (
  15. VP_MODE_ARCHIVE,
  16. VP_MODE_PROXY,
  17. VP_MODE_QUEUE,
  18. normalize_vp_mode,
  19. )
  20. from backend.app.services.bambu_mqtt import resolve_external_spools_in_mapping
  21. from backend.app.services.virtual_printer.bind_server import BindServer
  22. from backend.app.services.virtual_printer.certificate import CertificateService
  23. from backend.app.services.virtual_printer.ftp_server import VirtualPrinterFTPServer, compute_passive_port_slice
  24. from backend.app.services.virtual_printer.mqtt_bridge import MQTTBridge
  25. from backend.app.services.virtual_printer.mqtt_server import SimpleMQTTServer
  26. from backend.app.services.virtual_printer.ssdp_server import SSDPProxy, VirtualPrinterSSDPServer
  27. from backend.app.services.virtual_printer.tcp_proxy import SlicerProxyManager, TCPProxy
  28. from backend.app.utils.printer_models import has_ams3h, is_nozzle_rack_model
  29. from backend.app.utils.tray_ids import TrayLayout
  30. if TYPE_CHECKING:
  31. from backend.app.services.printer_manager import PrinterManager
  32. logger = logging.getLogger(__name__)
  33. # Mapping of SSDP model codes to display names
  34. # These are the codes that slicers expect during discovery
  35. # Sources:
  36. # - https://gist.github.com/Alex-Schaefer/72a9e2491a42da2ef99fb87601955cc3
  37. # - https://github.com/psychoticbeef/BambuLabOrcaSlicerDiscovery
  38. VIRTUAL_PRINTER_MODELS = {
  39. # X1 Series
  40. "BL-P001": "X1C", # X1 Carbon
  41. "BL-P002": "X1", # X1
  42. "C13": "X1E", # X1E
  43. # X2 Series
  44. "N6": "X2D", # X2D
  45. # A2 Series (single-FDM + integrated cutter/plotter)
  46. "N9": "A2L", # A2L
  47. # P Series
  48. "C11": "P1P", # P1P
  49. "C12": "P1S", # P1S
  50. "N7": "P2S", # P2S
  51. # A1 Series
  52. "N2S": "A1", # A1
  53. "N1": "A1 Mini", # A1 Mini
  54. # H2 Series
  55. "O1D": "H2D", # H2D
  56. "O1E": "H2D Pro", # H2D Pro
  57. "O2D": "H2D Pro", # H2D Pro
  58. "O1C": "H2C", # H2C
  59. "O1C2": "H2C", # H2C (dual nozzle variant)
  60. "O1S": "H2S", # H2S
  61. }
  62. # Serial number prefixes for each model (based on Bambu Lab serial number format)
  63. # Format: MMM??RYMDDUUUUU (15 chars total)
  64. # MMM = Model prefix (3 chars)
  65. # ?? = Unknown/revision code (2 chars)
  66. # R = Revision letter (1 char)
  67. # Y = Year digit (1 char)
  68. # M = Month (1 char, hex: 1-9, A=Oct, B=Nov, C=Dec)
  69. # DD = Day (2 chars)
  70. # UUUUU = Unit number (5 chars)
  71. MODEL_SERIAL_PREFIXES = {
  72. # X1 Series
  73. "BL-P001": "00M00A", # X1C
  74. "BL-P002": "00M00A", # X1
  75. "C13": "03W00A", # X1E
  76. # X2 Series
  77. "N6": "20P90A", # X2D (first 4 chars "20P9" match real serials)
  78. # A2 Series
  79. "N9": "26A19A", # A2L (first 5 chars "26A19" match real serials)
  80. # P Series
  81. "C11": "01S00A", # P1P
  82. "C12": "01P00A", # P1S
  83. "N7": "22E00A", # P2S
  84. # A1 Series
  85. "N2S": "03900A", # A1
  86. "N1": "03000A", # A1 Mini
  87. # H2 Series
  88. "O1D": "09400A", # H2D
  89. "O1E": "09400A", # H2D Pro (same prefix family as H2D)
  90. "O2D": "09400A", # H2D Pro
  91. "O1C": "09400A", # H2C
  92. "O1C2": "09400A", # H2C (dual nozzle variant)
  93. "O1S": "09400A", # H2S
  94. }
  95. # Reverse mapping: display name → SSDP model code (for auto-inheriting from printer model)
  96. DISPLAY_NAME_TO_MODEL_CODE = {v: k for k, v in VIRTUAL_PRINTER_MODELS.items()}
  97. # Default model
  98. DEFAULT_VIRTUAL_PRINTER_MODEL = "BL-P001" # X1C
  99. # Bound on per-instance ``_slicer_print_options`` cache size. The slicer's
  100. # project_file MQTT command stashes one dict per filename; the
  101. # corresponding ``_add_to_print_queue`` pop only fires when the file
  102. # upload completes. Failed / cancelled / non-3MF uploads orphan their
  103. # stash. The bound triggers FIFO eviction in ``on_print_command`` once
  104. # the dict fills, so a long-running VP can't leak unbounded state.
  105. _SLICER_OPTIONS_CACHE_LIMIT = 128
  106. # How long ``_add_to_print_queue`` waits for the slicer's MQTT
  107. # ``project_file`` after the FTP upload completes (#1780 round 3).
  108. # Bambu Studio sends FTP first, then MQTT immediately after — but on
  109. # wireless / loaded setups the MQTT command can land 2+ s after FTP,
  110. # which used to time the wait out and silently drop ``nozzle_mapping``
  111. # + the other slicer-driven flags. The bumped window covers the
  112. # observed worst case in the field; the late-MQTT fallback in
  113. # ``on_print_command`` covers the rest.
  114. _SLICER_OPTIONS_WAIT_TIMEOUT = 5.0
  115. # How long ``on_print_command`` will retroactively stamp slicer fields
  116. # onto a recently-committed queue item when the MQTT print command
  117. # arrives after ``_SLICER_OPTIONS_WAIT_TIMEOUT`` expired. Covers
  118. # extra-late MQTT (slow wireless slicer, NIC drop+retry) and the
  119. # scheduler tick interval before dispatch picks the item up.
  120. _RECENT_QUEUE_ITEM_TTL = 30.0
  121. # BambuStudio's tri-state calibration options (bed_leveling / flow_cali /
  122. # nozzle_offset_cali) travel on the project_file command as an int — off=0,
  123. # on=1, auto=2 (getValueInt parity). Bed levelling and flow calibration add a
  124. # bool that is true only for "on"; nozzle offset calibration is the int alone.
  125. _TRISTATE_INT = {0: "off", 1: "on", 2: "auto"}
  126. def _tristate_from_slicer(data: dict, bool_field: str | None, int_field: str) -> str | None:
  127. """Reconstruct off/on/auto from a captured slicer project_file dict.
  128. Prefer the int companion (auto_bed_leveling / extrude_cali_flag / etc.) which
  129. carries all three states; fall back to the bool field (on/off only); return
  130. None when the slicer sent neither so the caller can use its own default.
  131. """
  132. if int_field in data:
  133. try:
  134. resolved = _TRISTATE_INT.get(int(data[int_field]))
  135. except (TypeError, ValueError):
  136. resolved = None
  137. if resolved is not None:
  138. return resolved
  139. if bool_field is not None and bool_field in data:
  140. return "on" if bool(data[bool_field]) else "off"
  141. return None
  142. def _extract_slicer_ams_mapping_json(
  143. data: dict, log_prefix: str, *, is_dual_nozzle: bool = False, layout: TrayLayout | None = None
  144. ) -> str | None:
  145. """Pull the slicer's own AMS-slot pick out of a captured project_file payload.
  146. BambuStudio/OrcaSlicer resolves the physical AMS tray for each filament
  147. live, right before sending — either automatically or via the slicer's
  148. manual per-filament AMS-slot assignment dialog — and embeds the result as
  149. ``ams_mapping`` (``list[int]``, position = slot_id-1, value = global tray
  150. ID) directly in the MQTT ``project_file`` command. Confirmed by wire
  151. capture: the field is present and already in the exact shape
  152. ``PrintQueueItem.ams_mapping`` expects.
  153. The VP-queue path previously never read this — every queued print had the
  154. scheduler re-derive a mapping from just the 3MF's static type/color at
  155. dispatch time (`PrintScheduler._compute_ams_mapping_for_printer`), discarding
  156. the slicer's already-correct, live-resolved pick. That re-derivation can
  157. land on the wrong physical spool whenever the file's type+color match
  158. isn't unique (e.g. two spools of the same color) or the file's own
  159. filament-slot color wasn't what the user actually intended for that
  160. particular print. Capturing it here — mirroring the existing
  161. ``nozzle_mapping`` passthrough for H2C rack-swap models (#1780) — lets the
  162. scheduler's "already resolved, don't touch it" branch in
  163. ``_ensure_ams_mapping`` use the slicer's own choice unchanged.
  164. That branch skipping ``_compute_ams_mapping_for_printer`` is also what
  165. makes this a trade rather than a pure win: ``prefer_lowest_filament``, its
  166. AMS-filament-backup gate (#1766), the inventory-remain overrides and the
  167. per-slot force-color overrides all live inside that function. Callers are
  168. responsible for the gating — this parser only says what the slicer sent.
  169. An external spool is ``-1`` in the flat list, the same as a filament with
  170. no tray, and is named only in ``ams_mapping2``. It is resolved from there
  171. to its global tray (254/255), as the usage capture does (#3166); dispatch
  172. turns that back into the external-spool entry. Kept as ``-1`` it dispatched
  173. as unmapped and the printer stopped with 0700-8012 (#3237).
  174. ``is_dual_nozzle`` is the target printer's, since that decides which
  175. global tray an external spool is. So is ``layout``: on a rack printer
  176. (H3C) each hotend has its own external input, 247-255.
  177. Returns ``None`` when the field is absent, unparsable, or the classic
  178. "all -1" unresolved-race sentinel (#2589) — never worth trusting over a
  179. fresh live computation.
  180. """
  181. raw = data.get("ams_mapping")
  182. if raw is None:
  183. return None
  184. if isinstance(raw, str):
  185. try:
  186. raw = json.loads(raw)
  187. except json.JSONDecodeError:
  188. logger.warning("%s Slicer ams_mapping is unparseable JSON, dropping: %r", log_prefix, raw)
  189. return None
  190. # bool is a subclass of int in Python — isinstance(True, int) is True —
  191. # so it must be excluded explicitly, or [True, False] would pass as a
  192. # valid mapping.
  193. if not isinstance(raw, list) or not raw or not all(isinstance(v, int) and not isinstance(v, bool) for v in raw):
  194. return None
  195. detail = data.get("ams_mapping2")
  196. if isinstance(detail, str):
  197. try:
  198. detail = json.loads(detail)
  199. except json.JSONDecodeError:
  200. detail = None
  201. raw = resolve_external_spools_in_mapping(raw, detail, is_dual_nozzle, layout)
  202. if all(v < 0 for v in raw):
  203. # #2589 sentinel — every slot unresolved. Let the scheduler compute a
  204. # fresh mapping from live AMS state instead of trusting this.
  205. return None
  206. return json.dumps(raw)
  207. def _get_serial_for_model(model: str, serial_suffix: str) -> str:
  208. """Get serial number for the given model and suffix."""
  209. prefix = MODEL_SERIAL_PREFIXES.get(model, "00M09A")
  210. return f"{prefix}{serial_suffix}"
  211. class VirtualPrinterInstance:
  212. """Per-printer state and file handling logic.
  213. Each instance represents one virtual printer with its own config,
  214. upload directory, certificates, and file handling mode.
  215. """
  216. def __init__(
  217. self,
  218. *,
  219. vp_id: int,
  220. name: str,
  221. mode: str,
  222. model: str,
  223. access_code: str,
  224. serial_suffix: str,
  225. target_printer_ip: str = "",
  226. target_printer_serial: str = "",
  227. target_printer_id: int | None = None,
  228. auto_dispatch: bool = True,
  229. queue_force_color_match: bool = False,
  230. save_ams_mapping: bool = False,
  231. gcode_injection: bool = False,
  232. bind_ip: str = "",
  233. remote_interface_ip: str = "",
  234. base_dir: Path,
  235. session_factory: Callable | None = None,
  236. printer_manager: "PrinterManager | None" = None,
  237. ):
  238. self.id = vp_id
  239. self.name = name
  240. # Normalize on construction so the rest of the code only compares
  241. # canonical values, even when a legacy DB row hasn't been migrated
  242. # yet (e.g. fresh-from-disk during the boot window before the
  243. # one-shot migration in `core/database.py` has executed).
  244. self.mode = normalize_vp_mode(mode) or VP_MODE_ARCHIVE
  245. self.model = model
  246. self.access_code = access_code
  247. self.serial_suffix = serial_suffix
  248. self.target_printer_ip = target_printer_ip
  249. self.target_printer_serial = target_printer_serial
  250. self.target_printer_id = target_printer_id
  251. self.auto_dispatch = auto_dispatch
  252. self.queue_force_color_match = queue_force_color_match
  253. self.save_ams_mapping = save_ams_mapping
  254. self.gcode_injection = gcode_injection
  255. self.bind_ip = bind_ip
  256. self.remote_interface_ip = remote_interface_ip
  257. self._session_factory = session_factory
  258. self._printer_manager = printer_manager
  259. # Directories
  260. self.upload_dir = base_dir / "uploads" / str(vp_id)
  261. self.cert_dir = base_dir / "certs" / str(vp_id)
  262. shared_ca_dir = base_dir / "certs"
  263. # Ensure directories exist
  264. self.upload_dir.mkdir(parents=True, exist_ok=True)
  265. (self.upload_dir / "cache").mkdir(exist_ok=True)
  266. self.cert_dir.mkdir(parents=True, exist_ok=True)
  267. # Certificate service (shared CA, per-instance printer cert)
  268. self._cert_service = CertificateService(
  269. cert_dir=self.cert_dir,
  270. serial=self.serial,
  271. shared_ca_dir=shared_ca_dir,
  272. )
  273. # Uploads received and not yet archived or queued; only counted, for
  274. # the status's pending_files.
  275. self._pending_files: dict[str, Path] = {}
  276. # Slicer-side print options captured from the MQTT `project_file`
  277. # command, keyed by filename. Used by `_add_to_print_queue` so the
  278. # queue item inherits the user's slicer-chosen timelapse / bed_leveling
  279. # / flow_cali / vibration_cali / layer_inspect / use_ams toggles rather
  280. # than falling back to the global `default_*` settings (#1403). FTP
  281. # completes a few hundred ms before the slicer's MQTT `project_file`
  282. # arrives, so the queue-add path waits briefly on the event below
  283. # before reading the dict. Events are popped along with the options
  284. # so the dict stays bounded.
  285. self._slicer_print_options: dict[str, dict] = {}
  286. self._slicer_print_options_events: dict[str, asyncio.Event] = {}
  287. # Queue items recently committed by `_add_to_print_queue`, keyed by
  288. # FTP filename. Used by `on_print_command` to retroactively stamp the
  289. # slicer's nozzle_mapping (and the other slicer-driven flags) onto a
  290. # queue item when the MQTT `project_file` arrives after the queue-add
  291. # wait timed out — the #1780 round-3 race. Value is
  292. # (queue_item_ids, monotonic_committed_at); entries older than
  293. # `_RECENT_QUEUE_ITEM_TTL` are evicted opportunistically on each
  294. # queue-add.
  295. self._recent_queue_items: dict[str, tuple[list[int], float]] = {}
  296. # Per entry above: the highest slot id each queued plate prints, so a
  297. # late slicer AMS mapping is stamped only onto plates it reaches.
  298. self._recent_queue_item_reach: dict[str, dict[int, int]] = {}
  299. # Per-instance services
  300. self._proxy: SlicerProxyManager | None = None
  301. self._ftp: VirtualPrinterFTPServer | None = None
  302. self._mqtt: SimpleMQTTServer | None = None
  303. self._mqtt_bridge: MQTTBridge | None = None
  304. self._rtsp_proxy: TCPProxy | None = None
  305. self._bind: BindServer | None = None
  306. self._ssdp: VirtualPrinterSSDPServer | None = None
  307. self._ssdp_proxy: SSDPProxy | None = None
  308. self._tasks: list[asyncio.Task] = []
  309. # Pending timer that re-fires gcode_state=FINISH after a project_file
  310. # ack. See ``_schedule_finish_release`` for the #1658 rationale.
  311. self._finish_release_task: asyncio.Task | None = None
  312. @property
  313. def serial(self) -> str:
  314. """Full serial number for this virtual printer."""
  315. return _get_serial_for_model(self.model or DEFAULT_VIRTUAL_PRINTER_MODEL, self.serial_suffix)
  316. @property
  317. def cert_path(self) -> Path:
  318. return self._cert_service.cert_path
  319. @property
  320. def key_path(self) -> Path:
  321. return self._cert_service.key_path
  322. @property
  323. def is_proxy(self) -> bool:
  324. return self.mode == "proxy"
  325. @property
  326. def is_running(self) -> bool:
  327. return len(self._tasks) > 0 and all(not t.done() for t in self._tasks)
  328. def generate_certificates(self) -> tuple[Path, Path]:
  329. """Generate certificates for this instance."""
  330. self._cert_service.serial = self.serial if not self.is_proxy else (self.target_printer_serial or self.serial)
  331. additional_ips = [self.remote_interface_ip] if self.remote_interface_ip else None
  332. if self.bind_ip:
  333. additional_ips = additional_ips or []
  334. additional_ips.append(self.bind_ip)
  335. self._cert_service.delete_printer_certificate()
  336. return self._cert_service.generate_certificates(additional_ips=additional_ips)
  337. # -- File handling callbacks --
  338. async def on_file_received(self, file_path: Path, source_ip: str) -> None:
  339. """Handle file upload completion from FTP."""
  340. logger.info("[VP %s] Received file: %s from %s", self.name, file_path.name, source_ip)
  341. self._pending_files[file_path.name] = file_path
  342. # Accept both canonical (`archive`/`queue`) and legacy
  343. # (`immediate`/`print_queue`) wire values so a stale row that hasn't
  344. # been migrated yet still dispatches correctly. Migration in
  345. # `core/database.py` rewrites existing rows once at boot.
  346. mode = normalize_vp_mode(self.mode)
  347. if mode == VP_MODE_ARCHIVE:
  348. await self._archive_file(file_path, source_ip)
  349. elif mode == VP_MODE_QUEUE:
  350. await self._add_to_print_queue(file_path, source_ip)
  351. else:
  352. await self._queue_file(file_path, source_ip)
  353. # Signal job completion to the slicer. Send-flow slicers don't watch the
  354. # post-upload state and would be happy with anything; the Print flow
  355. # (intended for proxy-mode VPs, but users sometimes click it against
  356. # queue/immediate/review modes too — #1280) watches the gcode_state
  357. # cycle and only releases its in-flight-job lock when it sees FINISH.
  358. # Going PREPARE → IDLE wedges the slicer's UI at "Downloading...(0%)"
  359. # and blocks the next dispatch with "busy with another print job".
  360. # PREPARE → FINISH satisfies both flows. prepare_percent=100 also
  361. # unfreezes the slicer's "Downloading X%" progress bar which it ticks
  362. # against the same field during the upload window.
  363. if self._mqtt and file_path.suffix.lower() == ".3mf":
  364. self._mqtt.set_gcode_state("FINISH", filename=file_path.name, prepare_percent="100")
  365. # FINISH is the terminal state for the upload cycle per #1280
  366. # (commit 0d6171dc). The Print-flow slicer's in-flight-job lock
  367. # releases on FINISH; resetting to IDLE 2 s later would re-confuse
  368. # the slicer that just unwedged. Earlier audit suggesting the
  369. # IDLE reset was wrong — staying at FINISH is the designed
  370. # behaviour. The next upload's PREPARE→FINISH cycle starts fresh.
  371. def _keeps_slicer_nozzle_mapping(self) -> bool:
  372. """Whether the slicer's ``nozzle_mapping`` is stored on the queue item.
  373. On a rack model (H2C) it holds physical rack positions, which name the
  374. rack of the printer the slicer was looking at: kept only when this VP
  375. targets that one printer. A VP for any printer of the model leaves it
  376. empty, and the dispatch resolves the positions against the rack of the
  377. printer it picks. Elsewhere it holds extruder indices, which are the
  378. same on every printer of the model.
  379. """
  380. return self.target_printer_id is not None or not is_nozzle_rack_model(self.model)
  381. def _keeps_slicer_use_ams(self) -> bool:
  382. """Whether the slicer's ``use_ams`` is stored on the queue item.
  383. The slicer was looking at this VP, which advertises no AMS. Only a VP
  384. bound to one printer stands for a machine whose AMS the user chose to
  385. use or not; a VP for any printer of the model leaves it on and the
  386. dispatch reconciles it against the printer it picks (#2595).
  387. """
  388. return self.target_printer_id is not None
  389. def _target_is_dual_nozzle(self) -> bool:
  390. """Whether the target printer has two nozzles, which decides the global
  391. tray of an external spool in the slicer's mapping (#3237).
  392. Same signal dispatch uses: the live client's detection, else its model.
  393. Without a client, the VP's own model is what the slicer sliced for.
  394. """
  395. from backend.app.utils.printer_models import is_dual_nozzle_model
  396. client = (
  397. self._printer_manager.get_client(self.target_printer_id)
  398. if self._printer_manager is not None and self.target_printer_id is not None
  399. else None
  400. )
  401. if client is not None:
  402. return bool(getattr(client, "_is_dual_nozzle", False)) or is_dual_nozzle_model(
  403. getattr(client, "model", None)
  404. )
  405. return is_dual_nozzle_model(self.model)
  406. def _target_tray_layout(self) -> TrayLayout:
  407. """How the target printer numbers its trays, which places an H3C's
  408. per-hotend external inputs in the slicer's mapping.
  409. The target's live AMS data, else its model; without a target printer
  410. ("any available" queue mode), the VP's own model.
  411. """
  412. if self._printer_manager is not None and self.target_printer_id is not None:
  413. from backend.app.services.printer_manager import printer_tray_layout
  414. return printer_tray_layout(self.target_printer_id, self._printer_manager)
  415. return TrayLayout.ams3h() if has_ams3h(self.model) else TrayLayout()
  416. async def on_print_command(self, filename: str, data: dict) -> None:
  417. """Handle print command from MQTT.
  418. Captures the slicer's project_file options (`timelapse`, `bed_leveling`,
  419. `flow_cali`, `vibration_cali`, `layer_inspect`, `use_ams`, plus the
  420. H2C rack-pick `nozzle_mapping`) so the VP-queue path can inherit them
  421. when adding the item to the queue, rather than falling back to the
  422. global default settings (#1403, #1780).
  423. Only queue mode consumes the capture; archive / review / proxy
  424. modes ignore the print command, so we skip the stash there to keep
  425. the dict from accumulating one entry per print over the VP's
  426. uptime.
  427. Also schedules the #1658 follow-up that re-fires gcode_state=FINISH a
  428. moment after the synthetic project_file ack — for every non-proxy
  429. mode — so the slicer's "Downloading" UI releases on the slicer's
  430. FTP-first-then-MQTT send order.
  431. ``filename`` is the slicer's ``subtask_name`` (bare model name, no
  432. extension) — used verbatim for `_schedule_finish_release` because
  433. push_status echoes it back to the slicer as gcode_file / subtask_name.
  434. The queue-side stash key is derived from ``data["file"]`` (the FTP
  435. filename with extension) so `_add_to_print_queue`'s
  436. ``file_path.name`` lookup matches; falls back to ``filename`` when
  437. ``data["file"]`` is absent (legacy slicers / non-3MF uploads).
  438. Stash/lookup mismatch was the #1780 root cause — every captured field
  439. silently fell back to settings defaults on every Bambu Studio "Send".
  440. """
  441. logger.info("[VP %s] Print command for: %s", self.name, filename)
  442. mode = normalize_vp_mode(self.mode)
  443. if mode != VP_MODE_PROXY and filename and self._mqtt is not None:
  444. self._schedule_finish_release(filename)
  445. if mode != VP_MODE_QUEUE:
  446. return
  447. # Stash key must match `_add_to_print_queue`'s lookup, which uses
  448. # `file_path.name` (FTP filename WITH extension). The slicer's
  449. # `subtask_name` (== this method's `filename` arg) is the bare model
  450. # name, no extension — using it as the stash key was the #1780 root
  451. # cause.
  452. stash_key = data.get("file") or filename
  453. # Drop the oldest stash if the cache is growing — happens when the
  454. # slicer sends project_file for a filename whose FTP upload was
  455. # rejected / cancelled / non-3MF, so _add_to_print_queue's pop
  456. # never fires. With no bound, a long-running VP accumulates one
  457. # dict per such mismatch.
  458. if len(self._slicer_print_options) >= _SLICER_OPTIONS_CACHE_LIMIT:
  459. try:
  460. stale_key = next(iter(self._slicer_print_options))
  461. self._slicer_print_options.pop(stale_key, None)
  462. self._slicer_print_options_events.pop(stale_key, None)
  463. logger.debug("[VP %s] Evicted stale slicer options for %s", self.name, stale_key)
  464. except StopIteration:
  465. pass
  466. self._slicer_print_options[stash_key] = dict(data)
  467. event = self._slicer_print_options_events.get(stash_key)
  468. if event:
  469. event.set()
  470. return
  471. # No consumer waiting: `_add_to_print_queue` either already gave up
  472. # (wait_for timed out) or hasn't started yet (FTP still uploading).
  473. # If a queue item was committed within the last
  474. # `_RECENT_QUEUE_ITEM_TTL`, the wait timed out and the row holds
  475. # settings defaults instead of the slicer's pick — retroactively
  476. # stamp the slicer-driven fields so the dispatcher honours the
  477. # user's choice. Covers the #1780 round-3 race where Bambu Studio's
  478. # MQTT lands just past the bumped wait ceiling.
  479. await self._restamp_recent_queue_item(stash_key, data)
  480. async def _restamp_recent_queue_item(self, stash_key: str, data: dict) -> None:
  481. """Patch slicer-driven fields onto a queue item the MQTT command missed.
  482. ``_add_to_print_queue`` waits up to ``_SLICER_OPTIONS_WAIT_TIMEOUT``
  483. for the slicer's MQTT ``project_file`` before committing the queue
  484. item. If the MQTT command arrives after that window — observed in
  485. the field at ~2.1 s on H2C / wireless setups (#1780 round 3) — the
  486. row was already written with settings defaults. This method runs
  487. on the late MQTT path: it looks up the most recent queue items
  488. committed for this filename and patches in the slicer's
  489. ``nozzle_mapping`` + ``ams_mapping`` + workflow flags, but only
  490. while the items are still ``pending`` (scheduler hasn't dispatched
  491. them yet). The two mappings pass the same gates as in
  492. ``_add_to_print_queue``.
  493. """
  494. if not self._session_factory:
  495. return
  496. entry = self._recent_queue_items.get(stash_key)
  497. plate_reach = self._recent_queue_item_reach.pop(stash_key, {})
  498. if entry is None:
  499. return
  500. queue_item_ids, committed_at = entry
  501. if time.monotonic() - committed_at > _RECENT_QUEUE_ITEM_TTL:
  502. self._recent_queue_items.pop(stash_key, None)
  503. return
  504. import json
  505. # Mirror the field set `_add_to_print_queue` reads off slicer_opts.
  506. # MQTT uses `bed_leveling` (single L); the column is `bed_levelling`.
  507. # `nozzles_info` is intentionally not stamped — column kept for
  508. # legacy rows but never written; see PrintQueueItem.nozzles_info.
  509. patch: dict = {}
  510. # Tri-state options (off/on/auto) — reconstruct from the int companion.
  511. for bool_field, int_field, column in (
  512. ("bed_leveling", "auto_bed_leveling", "bed_levelling"),
  513. ("flow_cali", "extrude_cali_flag", "flow_cali"),
  514. (None, "nozzle_offset_cali", "nozzle_offset_cali"),
  515. ):
  516. resolved = _tristate_from_slicer(data, bool_field, int_field)
  517. if resolved is not None:
  518. patch[column] = resolved
  519. # On/off options.
  520. for mqtt_field, column in (
  521. ("vibration_cali", "vibration_cali"),
  522. ("layer_inspect", "layer_inspect"),
  523. ("timelapse", "timelapse"),
  524. ("use_ams", "use_ams"),
  525. ):
  526. if mqtt_field == "use_ams" and not self._keeps_slicer_use_ams():
  527. continue
  528. if mqtt_field in data:
  529. patch[column] = bool(data[mqtt_field])
  530. # Same gate as in `_add_to_print_queue`.
  531. raw = data.get("nozzle_mapping") if self._keeps_slicer_nozzle_mapping() else None
  532. if raw is not None:
  533. if isinstance(raw, str):
  534. try:
  535. raw = json.loads(raw)
  536. except json.JSONDecodeError:
  537. logger.warning(
  538. "[VP %s] Late MQTT nozzle_mapping is unparseable JSON, dropping: %r",
  539. self.name,
  540. raw,
  541. )
  542. raw = None
  543. if raw is not None:
  544. patch["nozzle_mapping"] = json.dumps(raw)
  545. # Same two gates as the immediate path in `_add_to_print_queue`: a
  546. # model-based VP has no live AMS layout for the slicer to have resolved
  547. # tray IDs against, and taking the slicer's pick at all is the per-VP
  548. # `save_ams_mapping` opt-in (it makes the scheduler skip
  549. # `_compute_ams_mapping_for_printer`, and with it prefer-lowest and the
  550. # #1766 backup gate).
  551. ams_mapping_json = (
  552. _extract_slicer_ams_mapping_json(
  553. data,
  554. f"[VP {self.name}] Late MQTT",
  555. is_dual_nozzle=self._target_is_dual_nozzle(),
  556. layout=self._target_tray_layout(),
  557. )
  558. if self.target_printer_id is not None and self.save_ams_mapping
  559. else None
  560. )
  561. # `Force color match` still wins for this dispatch — see the same
  562. # decision in `_add_to_print_queue`. The archive patch below is
  563. # deliberately not gated on it: persisting the pick for later reprints
  564. # is exactly what the toggle promises.
  565. if ams_mapping_json is not None and not self.queue_force_color_match:
  566. patch["ams_mapping"] = ams_mapping_json
  567. # `ams_mapping_json` alone is enough to keep going even when `patch` is
  568. # empty: with `Force color match` on it never reaches the queue item,
  569. # but it still has to be written onto the archive below.
  570. if not patch and ams_mapping_json is None:
  571. self._recent_queue_items.pop(stash_key, None)
  572. return
  573. from sqlalchemy import select, update
  574. from backend.app.models.archive import PrintArchive
  575. from backend.app.models.print_queue import PrintQueueItem
  576. try:
  577. async with self._session_factory() as db:
  578. # Only stamp items still pending and unclaimed; once the
  579. # scheduler has picked the row up we can't safely race the
  580. # dispatcher. A claim leaves the status "pending" while the
  581. # upload runs with the row's current mapping, so the claim is
  582. # checked too, and again in the writes below.
  583. unclaimed = (PrintQueueItem.status == "pending", PrintQueueItem.dispatching_at.is_(None))
  584. result = await db.execute(
  585. select(PrintQueueItem.id, PrintQueueItem.archive_id, PrintQueueItem.dispatching_at).where(
  586. PrintQueueItem.id.in_(queue_item_ids),
  587. PrintQueueItem.status == "pending",
  588. )
  589. )
  590. rows = result.all()
  591. eligible_ids = [row[0] for row in rows if row[2] is None]
  592. # A claimed job's archive still takes the slicer's pick below:
  593. # that copy serves later reprints, not this run.
  594. if not eligible_ids and (ams_mapping_json is None or not rows):
  595. self._recent_queue_items.pop(stash_key, None)
  596. return
  597. # As in `_add_to_print_queue`: a mapping shorter than a plate's
  598. # highest slot id cannot address that plate's own slots, so that
  599. # plate is left to the scheduler's computed mapping.
  600. short_ids: list[int] = []
  601. if "ams_mapping" in patch:
  602. reach = len(json.loads(patch["ams_mapping"]))
  603. short_ids = [i for i in eligible_ids if plate_reach.get(i, 0) > reach]
  604. if short_ids:
  605. logger.warning(
  606. "[VP %s] Late slicer ams_mapping has %d entries, too few for queue item(s) %s; "
  607. "leaving those to the scheduler's mapping.",
  608. self.name,
  609. reach,
  610. short_ids,
  611. )
  612. reached_ids = [i for i in eligible_ids if i not in short_ids]
  613. if patch and reached_ids:
  614. await db.execute(
  615. update(PrintQueueItem).where(PrintQueueItem.id.in_(reached_ids), *unclaimed).values(**patch)
  616. )
  617. short_patch = {k: v for k, v in patch.items() if k != "ams_mapping"}
  618. if short_ids and short_patch:
  619. await db.execute(
  620. update(PrintQueueItem).where(PrintQueueItem.id.in_(short_ids), *unclaimed).values(**short_patch)
  621. )
  622. # The archive was already created (with no slicer_ams_mapping)
  623. # before this late MQTT arrived — see
  624. # `_extract_slicer_ams_mapping_json`'s docstring. Patch it here
  625. # too so a reprint later still picks up the slicer's pick, and
  626. # the "AMS mapping from slicer" badge reflects reality instead
  627. # of staying stuck on the archive's initial (empty) snapshot.
  628. # Already gated on `save_ams_mapping` above, and deliberately
  629. # NOT on `queue_force_color_match`: that toggle decides how
  630. # *this* print is matched, not whether the pick is worth
  631. # keeping for a later reprint.
  632. if ams_mapping_json is not None:
  633. archive_ids = {row[1] for row in rows if row[1] is not None}
  634. if archive_ids:
  635. archive_result = await db.execute(select(PrintArchive).where(PrintArchive.id.in_(archive_ids)))
  636. for archive in archive_result.scalars().all():
  637. extra = dict(archive.extra_data or {})
  638. extra["slicer_ams_mapping"] = {
  639. "mapping": json.loads(ams_mapping_json),
  640. "printer_id": self.target_printer_id,
  641. }
  642. archive.extra_data = extra
  643. await db.commit()
  644. logger.info(
  645. "[VP %s] Late slicer MQTT for %s — retroactively stamped %s onto queue item(s) %s%s",
  646. self.name,
  647. stash_key,
  648. sorted(patch.keys()),
  649. eligible_ids,
  650. " and saved the slicer's AMS pick onto the archive" if ams_mapping_json is not None else "",
  651. )
  652. except Exception as e:
  653. logger.error(
  654. "[VP %s] Failed to retroactively stamp queue item(s) %s for %s: %s",
  655. self.name,
  656. queue_item_ids,
  657. stash_key,
  658. e,
  659. )
  660. finally:
  661. self._recent_queue_items.pop(stash_key, None)
  662. def _schedule_finish_release(self, filename: str, delay: float = 1.5) -> None:
  663. """Re-set gcode_state=FINISH on the VP after the project_file ack.
  664. #1280 set FINISH after the FTP upload completes — that was correct
  665. for the slicer flow at the time (MQTT project_file → FTP → done).
  666. Bambu Studio 2.7.x flipped the order to FTP → FTP → MQTT project_file,
  667. which means ``_send_print_response`` runs *after* the FINISH set in
  668. ``on_file_received`` and overwrites the state back to PREPARE. The
  669. slicer's 1 Hz status stream then carries PREPARE forever and the
  670. send modal sits at "Downloading" until the VP is restarted (#1658).
  671. Re-firing FINISH after a short delay closes the gap: the slicer sees
  672. the synthetic PREPARE in the project_file ack (and likely one PREPARE
  673. push on the 1 Hz cycle), then the next push carries FINISH and the
  674. modal releases. Proxy mode is exempt — there the real printer drives
  675. the state through the bridge and a synthetic FINISH would clobber a
  676. real PREPARE/RUNNING transition coming back from the printer.
  677. Cancels any in-flight timer before scheduling a new one so a slicer
  678. that fires project_file twice in quick succession only ends in one
  679. FINISH.
  680. """
  681. if self._mqtt is None:
  682. return
  683. if self._finish_release_task is not None and not self._finish_release_task.done():
  684. self._finish_release_task.cancel()
  685. self._finish_release_task = asyncio.create_task(
  686. self._delayed_finish_release(filename, delay),
  687. name=f"vp-{self.id}-finish-release",
  688. )
  689. async def _delayed_finish_release(self, filename: str, delay: float) -> None:
  690. """Sleep, then set gcode_state=FINISH. Used by ``_schedule_finish_release``."""
  691. try:
  692. await asyncio.sleep(delay)
  693. except asyncio.CancelledError:
  694. return
  695. if self._mqtt is None:
  696. return
  697. self._mqtt.set_gcode_state("FINISH", filename=filename, prepare_percent="100")
  698. logger.debug("[VP %s] Re-set gcode_state=FINISH after project_file ack (%s)", self.name, filename)
  699. async def _archive_file(self, file_path: Path, source_ip: str) -> None:
  700. """Archive file immediately."""
  701. if not self._session_factory:
  702. logger.error("Cannot archive: no database session factory configured")
  703. self._pending_files.pop(file_path.name, None)
  704. return
  705. if file_path.suffix.lower() != ".3mf":
  706. logger.debug("Skipping non-3MF file: %s", file_path.name)
  707. self._pending_files.pop(file_path.name, None)
  708. try:
  709. file_path.unlink()
  710. except OSError:
  711. pass
  712. return
  713. archived = False
  714. try:
  715. from backend.app.api.routes.settings import get_setting
  716. from backend.app.services.archive import ArchiveService
  717. async with self._session_factory() as db:
  718. name_source = await get_setting(db, "virtual_printer_archive_name_source")
  719. prefer_filename = name_source == "filename"
  720. service = ArchiveService(db)
  721. archive = await service.archive_print(
  722. printer_id=None,
  723. source_file=file_path,
  724. print_data={
  725. "status": "archived",
  726. "source": "virtual_printer",
  727. "source_ip": source_ip,
  728. },
  729. prefer_filename_for_name=prefer_filename,
  730. )
  731. if archive:
  732. logger.info("[VP %s] Archived: %s - %s", self.name, archive.id, archive.print_name)
  733. await self._broadcast_archive_created(archive)
  734. archived = True
  735. else:
  736. logger.error("Failed to archive file: %s", file_path.name)
  737. except Exception as e:
  738. logger.error("Error archiving file: %s", e)
  739. finally:
  740. # Always drop it from the pending count and delete the temp file —
  741. # previously the failure paths only logged, the upload_dir filled
  742. # up indefinitely, and the slicer received a clean 226 even though
  743. # no archive existed (#audit-R2-1).
  744. self._pending_files.pop(file_path.name, None)
  745. if archived:
  746. try:
  747. file_path.unlink()
  748. except OSError:
  749. pass
  750. else:
  751. # Drop the failed temp file so it doesn't accumulate.
  752. try:
  753. file_path.unlink(missing_ok=True)
  754. except OSError:
  755. pass
  756. async def _queue_file(self, file_path: Path, source_ip: str) -> None:
  757. """Queue file for user review."""
  758. if not self._session_factory:
  759. logger.error("Cannot queue: no database session factory configured")
  760. self._pending_files.pop(file_path.name, None)
  761. return
  762. if file_path.suffix.lower() != ".3mf":
  763. self._pending_files.pop(file_path.name, None)
  764. try:
  765. file_path.unlink()
  766. except OSError:
  767. pass
  768. return
  769. # Peek at the 3MF for the embedded title BEFORE we hand it off to the
  770. # DB. Storing it now means the /pending-uploads/ list doesn't have to
  771. # reopen every 3MF on every render to keep the review card and the
  772. # eventual archive name in sync (#1152 follow-up). Failure to parse is
  773. # not fatal — the response model falls back to the filename stem.
  774. metadata_print_name: str | None = None
  775. try:
  776. from backend.app.services.archive import ThreeMFParser
  777. parsed = ThreeMFParser(file_path).parse()
  778. raw_name = parsed.get("print_name")
  779. if isinstance(raw_name, str) and raw_name.strip():
  780. metadata_print_name = raw_name.strip()[:255]
  781. except Exception as e:
  782. logger.debug("[VP %s] Metadata title peek failed for %s: %s", self.name, file_path.name, e)
  783. try:
  784. from backend.app.models.pending_upload import PendingUpload
  785. async with self._session_factory() as db:
  786. pending = PendingUpload(
  787. filename=file_path.name,
  788. file_path=str(file_path),
  789. file_size=file_path.stat().st_size,
  790. source_ip=source_ip,
  791. status="pending",
  792. uploaded_at=datetime.now(timezone.utc),
  793. metadata_print_name=metadata_print_name,
  794. )
  795. db.add(pending)
  796. await db.commit()
  797. logger.info("[VP %s] Queued: %s - %s", self.name, pending.id, file_path.name)
  798. except Exception as e:
  799. logger.error("Error queueing file: %s", e)
  800. # Queue insert failed — drop the temp file so it doesn't
  801. # accumulate. The file is unreachable without the DB row.
  802. try:
  803. file_path.unlink(missing_ok=True)
  804. except OSError:
  805. pass
  806. finally:
  807. # Always drop it from the pending count, also after a queue
  808. # failure.
  809. self._pending_files.pop(file_path.name, None)
  810. async def _add_to_print_queue(self, file_path: Path, source_ip: str) -> None:
  811. """Archive file and add to print queue, assigned to target printer or model."""
  812. if not self._session_factory:
  813. logger.error("Cannot add to print queue: no database session factory configured")
  814. self._pending_files.pop(file_path.name, None)
  815. return
  816. if file_path.suffix.lower() != ".3mf":
  817. self._pending_files.pop(file_path.name, None)
  818. try:
  819. file_path.unlink()
  820. except OSError:
  821. pass
  822. return
  823. # Wait briefly for the slicer's MQTT `project_file` command so the
  824. # queue item can inherit the slicer-side print options the user
  825. # picked (timelapse, bed_leveling, etc). Slicers send the FTP upload
  826. # first and the MQTT command immediately after, so the typical lag
  827. # is a few hundred ms. The window is generous enough to absorb
  828. # wireless / loaded-Pi jitter without making every VP-queue add
  829. # visibly slow — observed worst case in #1780 round 3 was 2.085 s,
  830. # the previous 2.0 s ceiling. Falls back to the global default_*
  831. # settings if MQTT doesn't arrive in time (legacy behaviour for
  832. # users on a slicer that doesn't send a print command). #1403.
  833. # The wait is skipped when there's no MQTT server attached — covers
  834. # unit tests that invoke `_add_to_print_queue` directly without
  835. # going through `on_print_command`, so they don't pay the wait tax.
  836. slicer_opts = self._slicer_print_options.pop(file_path.name, None)
  837. if slicer_opts is None and self._mqtt is not None:
  838. event = asyncio.Event()
  839. self._slicer_print_options_events[file_path.name] = event
  840. try:
  841. await asyncio.wait_for(event.wait(), timeout=_SLICER_OPTIONS_WAIT_TIMEOUT)
  842. slicer_opts = self._slicer_print_options.pop(file_path.name, None)
  843. except TimeoutError:
  844. slicer_opts = None
  845. finally:
  846. self._slicer_print_options_events.pop(file_path.name, None)
  847. # If the cache still misses, queued workflow flags / nozzle pick will
  848. # silently fall back to settings defaults. Surface the missed key so a
  849. # future stash/lookup mismatch (the #1780 root cause) is obvious in
  850. # the log instead of needing a wire capture to diagnose.
  851. if slicer_opts is None:
  852. logger.debug(
  853. "[VP %s] No slicer options cached for %r (cache keys: %s); "
  854. "workflow flags + nozzle pick will fall back to settings defaults.",
  855. self.name,
  856. file_path.name,
  857. sorted(self._slicer_print_options.keys()),
  858. )
  859. try:
  860. import json
  861. from backend.app.api.routes.settings import get_setting
  862. from backend.app.models.print_queue import PrintQueueItem
  863. from backend.app.services.archive import ArchiveService
  864. from backend.app.services.filament_requirements import extract_filament_requirements
  865. from backend.app.services.print_confirmation import confirm_outcome_for_new_queue_item
  866. from backend.app.services.print_defaults import default_print_options
  867. async with self._session_factory() as db:
  868. name_source = await get_setting(db, "virtual_printer_archive_name_source")
  869. prefer_filename = name_source == "filename"
  870. # Workflow defaults from settings. Without this the
  871. # PrintQueueItem below would fall back to the column-level
  872. # defaults and ignore the user's workflow preferences (#1235).
  873. # The slicer-side options captured above (if any) take
  874. # precedence per-field over these defaults.
  875. defaults = await default_print_options(db)
  876. def _slicer_or(field_mqtt: str, settings_default: bool) -> bool:
  877. """Slicer's MQTT value if present, else the settings default.
  878. Slicer payloads carry both bool and int (0/1) shapes
  879. depending on firmware family — coerce via bool() so
  880. `0`/`False` and `1`/`True` both work.
  881. """
  882. if slicer_opts is not None and field_mqtt in slicer_opts:
  883. return bool(slicer_opts[field_mqtt])
  884. return settings_default
  885. def _slicer_tristate(bool_field: str | None, int_field: str, settings_default: str) -> str:
  886. """Slicer's tri-state (off/on/auto) if present, else the default."""
  887. if slicer_opts is not None:
  888. resolved = _tristate_from_slicer(slicer_opts, bool_field, int_field)
  889. if resolved is not None:
  890. return resolved
  891. return settings_default
  892. # Note the MQTT field names differ from Bambuddy's column
  893. # names: MQTT uses `bed_leveling` (single L) while the
  894. # column / settings key use `bed_levelling` (double L).
  895. bed_levelling = _slicer_tristate(
  896. "bed_leveling",
  897. "auto_bed_leveling",
  898. defaults["bed_levelling"],
  899. )
  900. flow_cali = _slicer_tristate(
  901. "flow_cali",
  902. "extrude_cali_flag",
  903. defaults["flow_cali"],
  904. )
  905. nozzle_offset_cali = _slicer_tristate(
  906. None,
  907. "nozzle_offset_cali",
  908. defaults["nozzle_offset_cali"],
  909. )
  910. vibration_cali = _slicer_or("vibration_cali", defaults["vibration_cali"])
  911. layer_inspect = _slicer_or("layer_inspect", defaults["layer_inspect"])
  912. timelapse = _slicer_or("timelapse", defaults["timelapse"])
  913. # No setting for it: the column default (on) when the slicer is
  914. # silent, or when the VP does not stand for one printer.
  915. use_ams = _slicer_or("use_ams", True) if self._keeps_slicer_use_ams() else True
  916. # "Ask for Outcome" is a default print option like the ones
  917. # above. A plate sent here from Bambu Studio is also one of the
  918. # prints `confirm_outcome_external_prints` names — but it
  919. # arrives with a queue item, so on_print_start resumes the
  920. # archive created below instead of treating it as external, and
  921. # without this the setting could never reach it (#1898).
  922. confirm_outcome = await confirm_outcome_for_new_queue_item(db, started_outside_bambuddy=True)
  923. # H2C dual-nozzle-rack slicer-pick preservation (#1780).
  924. # BambuStudio's project_file MQTT command for rack-swap models
  925. # (O1C2 today) carries `nozzle_mapping` — a per-filament array
  926. # of physical nozzle position IDs (`list[int]`). Forward it
  927. # verbatim onto the queue item so the dispatcher can replay it
  928. # in its own project_file command. Without this the H2C
  929. # firmware falls back to "last matching nozzle" auto-pick and
  930. # ignores the user's Bambu Studio choice. Every other model
  931. # has it absent from slicer_opts, so the capture is a
  932. # transparent no-op there. (`nozzles_info` was also captured
  933. # in the original fix but BambuStudio never actually sends it
  934. # — verified via wire capture on H2C — so only `nozzle_mapping`
  935. # is forwarded now.)
  936. #
  937. # On a rack model only for a VP that targets one printer: the
  938. # positions name that printer's rack. An "Any <model>" VP leaves
  939. # them to the dispatch, which resolves them against the rack of
  940. # the printer it picks. See `_keeps_slicer_nozzle_mapping`.
  941. nozzle_mapping_json: str | None = None
  942. if slicer_opts is not None and self._keeps_slicer_nozzle_mapping():
  943. raw = slicer_opts.get("nozzle_mapping")
  944. if raw is not None:
  945. # BambuStudio's NetworkAgent embeds this as parsed
  946. # JSON in the project_file body (matching the
  947. # ams_mapping shape Bambuddy already consumes as
  948. # list[int]). Accept a JSON-encoded string defensively
  949. # in case any path arrives stringified.
  950. if isinstance(raw, str):
  951. try:
  952. raw = json.loads(raw)
  953. except json.JSONDecodeError:
  954. logger.warning(
  955. "[VP %s] Slicer nozzle_mapping is unparseable JSON, dropping: %r",
  956. self.name,
  957. raw,
  958. )
  959. raw = None
  960. if raw is not None:
  961. nozzle_mapping_json = json.dumps(raw)
  962. # Slicer's own live-resolved AMS-slot pick (see docstring on
  963. # `_extract_slicer_ams_mapping_json`). Stamped onto every plate
  964. # below, same treatment as nozzle_mapping_json above — when
  965. # present it makes `_ensure_ams_mapping` skip its own
  966. # type/color re-derivation entirely and dispatch use exactly
  967. # the tray the slicer/user picked.
  968. #
  969. # Two gates, both required:
  970. #
  971. # 1. This VP must target one fixed printer. A model-based
  972. # ("Any <model>") VP has no MQTT bridge to a real printer,
  973. # so the slicer has no live AMS layout to resolve tray IDs
  974. # against — whatever it sends here is meaningless (or,
  975. # worse, coincidentally valid for the wrong printer once
  976. # the scheduler later picks one).
  977. # 2. The per-VP `save_ams_mapping` opt-in must be on. Taking
  978. # the slicer's pick means `_ensure_ams_mapping` returns
  979. # early and `_compute_ams_mapping_for_printer` never runs —
  980. # and that function is where `prefer_lowest_filament`, its
  981. # AMS-filament-backup gate (#1766) and the inventory-remain
  982. # overrides live. Honouring the slicer unconditionally would
  983. # silently retire all of that for every existing queue-mode
  984. # VP on upgrade, so it's opt-in like every other queue-mode
  985. # behaviour toggle (#2700 review).
  986. #
  987. # Either gate failing leaves it unset, and the scheduler's
  988. # normal type/color re-derivation runs against whichever
  989. # printer actually gets the job.
  990. ams_mapping_json: str | None = None
  991. if slicer_opts is not None and self.target_printer_id is not None and self.save_ams_mapping:
  992. ams_mapping_json = _extract_slicer_ams_mapping_json(
  993. slicer_opts,
  994. f"[VP {self.name}]",
  995. is_dual_nozzle=self._target_is_dual_nozzle(),
  996. layout=self._target_tray_layout(),
  997. )
  998. # `Force color match` is the user asking Bambuddy to do the
  999. # matching strictly, against the printer's live trays. Its only
  1000. # effect on a fixed-printer item is via the per-slot
  1001. # `filament_overrides` written below, which are consumed inside
  1002. # `_compute_ams_mapping_for_printer` — the exact function a
  1003. # stored mapping skips. So when both toggles are on, the
  1004. # explicit strictness wins for *this* dispatch and the slicer's
  1005. # pick is still persisted onto the archive for later reprints,
  1006. # which is what `Save AMS mapping` actually promises (#2700
  1007. # review).
  1008. queue_ams_mapping_json = ams_mapping_json
  1009. if queue_ams_mapping_json is not None and self.queue_force_color_match:
  1010. logger.info(
  1011. "[VP %s] Saved the slicer's AMS pick to the archive but not onto the queue item(s): "
  1012. "'Force color match' is on, so the scheduler matches against live trays for this print.",
  1013. self.name,
  1014. )
  1015. queue_ams_mapping_json = None
  1016. # Parsed once for the per-plate length check in the loop below.
  1017. queue_ams_mapping = json.loads(queue_ams_mapping_json) if queue_ams_mapping_json else None
  1018. service = ArchiveService(db)
  1019. archive = await service.archive_print(
  1020. printer_id=None,
  1021. source_file=file_path,
  1022. print_data={
  1023. "status": "archived",
  1024. "source": "virtual_printer",
  1025. "source_ip": source_ip,
  1026. },
  1027. prefer_filename_for_name=prefer_filename,
  1028. # Slicer's own live AMS-slot pick -- promoted to
  1029. # `extra_data.slicer_ams_mapping` by archive_print() so a
  1030. # later reprint can reuse it. Already gated on the per-VP
  1031. # `save_ams_mapping` opt-in above. Tagged with the printer
  1032. # it was resolved against so a later reprint on a
  1033. # *different* printer knows not to reuse it (#2700 review).
  1034. slicer_ams_mapping=(json.loads(ams_mapping_json) if ams_mapping_json else None),
  1035. slicer_ams_mapping_printer_id=self.target_printer_id,
  1036. )
  1037. if archive:
  1038. logger.info("[VP %s] Archived: %s - %s", self.name, archive.id, archive.print_name)
  1039. # Assign to specific printer if configured, otherwise use model for "Any X" scheduling
  1040. target_model = None
  1041. if not self.target_printer_id and self.model:
  1042. target_model = VIRTUAL_PRINTER_MODELS.get(self.model)
  1043. # #1733: multi-plate "Send All" uploads ship every plate in
  1044. # one 3MF — `slice_info.config` lists each `<plate>` with
  1045. # its own index. Enqueue one PrintQueueItem per plate so
  1046. # the scheduler runs each separately. Single-plate "Send"
  1047. # comes through as `[N]` (one plate index) so the loop
  1048. # below runs once and the existing behaviour is preserved.
  1049. plate_ids = self._extract_plate_ids(file_path)
  1050. # Pick a base position the same way the manual /print-queue/
  1051. # POST does, then hand consecutive positions to each plate
  1052. # so a Send All keeps plate-order execution inside the
  1053. # queue (#1733). Previously hardcoded to 1, which created
  1054. # duplicate position=1 rows on every VP upload and made
  1055. # queue execution order non-deterministic for any non-
  1056. # empty queue.
  1057. from sqlalchemy import func, select as _sql_select
  1058. # One sequence across all pending items, not one per
  1059. # printer (#3200): the plates go to the end of the queue.
  1060. queue_scope = _sql_select(func.max(PrintQueueItem.position)).where(
  1061. PrintQueueItem.status == "pending"
  1062. )
  1063. try:
  1064. max_pos_raw = (await db.execute(queue_scope)).scalar()
  1065. max_pos = int(max_pos_raw) if max_pos_raw is not None else 0
  1066. except (TypeError, ValueError):
  1067. max_pos = 0
  1068. # Parse per-plate filament requirements (#1188). Each plate
  1069. # has its own filament set in `slice_info.config`, so the
  1070. # `required_filament_types` / `filament_overrides` columns
  1071. # on each queue item reflect THAT plate, not the file's
  1072. # first plate. Scoping was already plate-aware via #1697 —
  1073. # the `extract_filament_requirements(path, plate_id)` filter
  1074. # returns just the plate's filaments. required_filament_types
  1075. # is populated unconditionally — it's cheap, lets the
  1076. # scheduler reject obvious mis-matches even without
  1077. # force_color_match. filament_overrides only carries
  1078. # force_color_match=True when the per-VP setting is on, so
  1079. # upgraders keep the old behaviour by default.
  1080. queue_item_ids: list[int] = []
  1081. queue_item_reach: dict[int, int] = {}
  1082. for offset, plate_id in enumerate(plate_ids, start=1):
  1083. required_filament_types_json: str | None = None
  1084. filament_overrides_json: str | None = None
  1085. requirements = extract_filament_requirements(file_path, plate_id)
  1086. if requirements:
  1087. types = sorted({r["type"] for r in requirements if r.get("type")})
  1088. if types:
  1089. required_filament_types_json = json.dumps(types)
  1090. if self.queue_force_color_match:
  1091. # Carry tray_info_idx so force_color_match can
  1092. # tell Bambu PLA variants apart (#2650). Bambu
  1093. # reports Basic/Matte/Silk all as tray_type
  1094. # "PLA"; the variant lives only in tray_info_idx
  1095. # (GFA00/GFA01/GFA06/...). A blank idx (custom or
  1096. # third-party spool) means "no variant
  1097. # constraint" and the scheduler falls back to
  1098. # type+colour.
  1099. overrides = [
  1100. {
  1101. "slot_id": r["slot_id"],
  1102. "type": r.get("type", ""),
  1103. "color": r.get("color", ""),
  1104. "tray_info_idx": r.get("tray_info_idx", ""),
  1105. "force_color_match": True,
  1106. }
  1107. for r in requirements
  1108. if r.get("type") and r.get("color")
  1109. ]
  1110. if overrides:
  1111. filament_overrides_json = json.dumps(overrides)
  1112. # The slicer's mapping is indexed by the 3MF's own
  1113. # file-global slot ids (position = slot_id - 1), so one
  1114. # array covers every plate of a multi-plate Send All —
  1115. # each plate just reads the entries for the slots it
  1116. # actually prints. What must be checked is that it
  1117. # reaches that far: a mapping shorter than this plate's
  1118. # highest slot id can't address the plate's own slots,
  1119. # and `_ensure_ams_mapping` would keep it anyway
  1120. # because it only rejects an all-unresolved mapping. Fall
  1121. # back to a computed mapping for that plate instead
  1122. # (#2700 review).
  1123. plate_ams_mapping_json = queue_ams_mapping_json
  1124. plate_max_slot = max((r.get("slot_id") or 0) for r in requirements) if requirements else 0
  1125. if queue_ams_mapping is not None and requirements:
  1126. max_slot_id = max((r.get("slot_id") or 0) for r in requirements)
  1127. if max_slot_id > len(queue_ams_mapping):
  1128. logger.warning(
  1129. "[VP %s] Slicer ams_mapping has %d entries but plate %s needs slot %d; "
  1130. "dropping it for this plate so the scheduler computes one from live AMS state.",
  1131. self.name,
  1132. len(queue_ams_mapping),
  1133. plate_id,
  1134. max_slot_id,
  1135. )
  1136. plate_ams_mapping_json = None
  1137. queue_item = PrintQueueItem(
  1138. printer_id=self.target_printer_id,
  1139. target_model=target_model,
  1140. archive_id=archive.id,
  1141. plate_id=plate_id,
  1142. position=max_pos + offset,
  1143. status="pending",
  1144. manual_start=not self.auto_dispatch,
  1145. required_filament_types=required_filament_types_json,
  1146. filament_overrides=filament_overrides_json,
  1147. bed_levelling=bed_levelling,
  1148. flow_cali=flow_cali,
  1149. nozzle_offset_cali=nozzle_offset_cali,
  1150. vibration_cali=vibration_cali,
  1151. layer_inspect=layer_inspect,
  1152. timelapse=timelapse,
  1153. use_ams=use_ams,
  1154. confirm_outcome=confirm_outcome,
  1155. # Per-VP opt-in for auto-print G-code injection (#1516).
  1156. # Default off; when on, the scheduler still no-ops unless
  1157. # gcode_snippets are configured for the target model, so it's
  1158. # effectively "inject when enabled AND snippets exist".
  1159. gcode_injection=self.gcode_injection,
  1160. # H2C rack-swap slicer pick (#1780). Captured above;
  1161. # stamped on every plate so a multi-plate Send All keeps
  1162. # the same nozzle pick across plates rather than only the
  1163. # first one (mirrors the #1697 / #1188 per-plate loop fix).
  1164. nozzle_mapping=nozzle_mapping_json,
  1165. # Slicer's own live AMS-slot pick, when present —
  1166. # see `_extract_slicer_ams_mapping_json`.
  1167. ams_mapping=plate_ams_mapping_json,
  1168. )
  1169. db.add(queue_item)
  1170. await db.flush() # populate queue_item.id before logging
  1171. queue_item_ids.append(queue_item.id)
  1172. queue_item_reach[queue_item.id] = plate_max_slot
  1173. await db.commit()
  1174. # Track the freshly-committed queue items so
  1175. # `on_print_command` can retroactively stamp slicer-side
  1176. # fields if the MQTT `project_file` lands AFTER the
  1177. # `_SLICER_OPTIONS_WAIT_TIMEOUT` window expired — the
  1178. # #1780 round-3 race. Eviction of stale entries here
  1179. # keeps the dict bounded; the queue path is the only
  1180. # writer, so doing it on commit is enough.
  1181. now = time.monotonic()
  1182. cutoff = now - _RECENT_QUEUE_ITEM_TTL
  1183. self._recent_queue_items = {k: v for k, v in self._recent_queue_items.items() if v[1] > cutoff}
  1184. self._recent_queue_items[file_path.name] = (list(queue_item_ids), now)
  1185. self._recent_queue_item_reach = {
  1186. k: v for k, v in self._recent_queue_item_reach.items() if k in self._recent_queue_items
  1187. }
  1188. self._recent_queue_item_reach[file_path.name] = queue_item_reach
  1189. # Last-chance check: MQTT for this filename could have
  1190. # arrived during ANY await between the initial pop and
  1191. # now — wait_for itself, archive_print, db.flush,
  1192. # db.commit. In all those cases `on_print_command`
  1193. # stashed its data but neither the event-signal path nor
  1194. # the retroactive `_recent_queue_items` path was in
  1195. # place to consume it. Pop any late stash and apply
  1196. # inline so the late MQTT never leaks past the queue-add.
  1197. late_opts = self._slicer_print_options.pop(file_path.name, None)
  1198. if late_opts is not None:
  1199. logger.info(
  1200. "[VP %s] Late slicer MQTT detected for %s during queue-add — "
  1201. "applying inline (race vs commit/archive/flush yield)",
  1202. self.name,
  1203. file_path.name,
  1204. )
  1205. await self._restamp_recent_queue_item(file_path.name, late_opts)
  1206. if len(queue_item_ids) == 1:
  1207. logger.info("[VP %s] Added to queue: %s", self.name, queue_item_ids[0])
  1208. else:
  1209. logger.info(
  1210. "[VP %s] Added %d queue items for multi-plate upload (plates %s): %s",
  1211. self.name,
  1212. len(queue_item_ids),
  1213. plate_ids,
  1214. queue_item_ids,
  1215. )
  1216. await self._broadcast_archive_created(archive)
  1217. from backend.app.services.queue_announcements import announce_queue_jobs_added
  1218. await announce_queue_jobs_added(db, queue_item_ids)
  1219. else:
  1220. logger.error("Failed to archive file: %s", file_path.name)
  1221. except Exception as e:
  1222. logger.error("Error adding to print queue: %s", e)
  1223. finally:
  1224. # Always release the marker and clean the temp file. Without this
  1225. # the same-name STOR guard would block the next upload and the
  1226. # upload_dir would accumulate failed temp files forever
  1227. # (#audit-R2-1).
  1228. self._pending_files.pop(file_path.name, None)
  1229. try:
  1230. file_path.unlink(missing_ok=True)
  1231. except OSError:
  1232. pass
  1233. async def _broadcast_archive_created(self, archive) -> None:
  1234. """Notify connected clients that a new archive exists.
  1235. Real-printer prints get this from main.py's MQTT print_start handler;
  1236. VP-uploaded prints need their own broadcast or the Archives page stays
  1237. stale until the user switches tabs (#1282).
  1238. """
  1239. try:
  1240. from backend.app.core.websocket import ws_manager
  1241. await ws_manager.send_archive_created(
  1242. {
  1243. "id": archive.id,
  1244. "printer_id": archive.printer_id,
  1245. "filename": archive.filename,
  1246. "print_name": archive.print_name,
  1247. "status": archive.status,
  1248. }
  1249. )
  1250. except Exception as e:
  1251. logger.debug("[VP %s] archive_created broadcast failed: %s", self.name, e)
  1252. @staticmethod
  1253. def _extract_plate_ids(file_path: Path) -> list[int]:
  1254. """Extract every plate index from a 3MF's slice_info.config.
  1255. A multi-plate "Send All" from BambuStudio / OrcaSlicer uploads a
  1256. single 3MF containing every plate the user selected. Each plate
  1257. has its own ``<plate>`` block with a ``<metadata key="index"
  1258. value="N"/>`` child and its own ``Metadata/plate_N.gcode`` payload
  1259. inside the same zip. Returning the full ordered list lets the VP
  1260. queue path create one queue item per plate (`_add_to_print_queue`
  1261. loops over the result), so "Send All" of a 3-plate file produces
  1262. 3 queue items sharing the same archive — one per plate to print.
  1263. Single-plate "Send" hits the same code path and returns ``[N]``
  1264. for whichever plate the user selected; the loop runs once and the
  1265. existing single-plate behaviour is preserved.
  1266. Returns ``[1]`` when the 3MF is missing ``slice_info.config``,
  1267. unparseable, or contains no plate-index metadata — the original
  1268. single-plate fallback. Production logs at debug so a non-3MF
  1269. upload doesn't spam, but the trail survives for support bundles.
  1270. """
  1271. try:
  1272. import xml.etree.ElementTree as ET
  1273. import zipfile
  1274. with zipfile.ZipFile(file_path, "r") as zf:
  1275. if "Metadata/slice_info.config" in zf.namelist():
  1276. content = zf.read("Metadata/slice_info.config").decode()
  1277. root = ET.fromstring(content) # noqa: S314 # nosec B314
  1278. plate_ids: list[int] = []
  1279. for plate in root.findall(".//plate"):
  1280. for meta in plate.findall("metadata"):
  1281. if meta.get("key") == "index" and meta.get("value"):
  1282. try:
  1283. plate_ids.append(int(meta.get("value")))
  1284. except ValueError:
  1285. continue
  1286. break
  1287. if plate_ids:
  1288. return plate_ids
  1289. except Exception as e:
  1290. logger.debug("[VP] _extract_plate_ids failed for %s: %s", file_path.name, e)
  1291. return [1]
  1292. # -- Service lifecycle --
  1293. def _resolve_cert_and_advertise(self) -> tuple[Path, Path, str]:
  1294. """Return (cert_path, key_path, advertise_address) for TLS services.
  1295. Always uses the self-signed cert chain (signed by `bbl_ca`). The user
  1296. imports `bbl_ca.crt` once into the slicer; per-VP certs validate from
  1297. there. Tailscale exposure is handled by the user picking the Tailscale
  1298. IP in the bind_ip dropdown.
  1299. """
  1300. cert_path, key_path = self.generate_certificates()
  1301. advertise = self.remote_interface_ip or self.bind_ip or ""
  1302. return cert_path, key_path, advertise
  1303. async def start_server(self) -> None:
  1304. """Start server-mode services (FTP, MQTT, SSDP, Bind) on this VP's bind_ip."""
  1305. logger.info("[VP %s] Starting server-mode services on %s", self.name, self.bind_ip)
  1306. cert_path, key_path, advertise_addr = self._resolve_cert_and_advertise()
  1307. bind_addr = self.bind_ip or "0.0.0.0" # nosec B104
  1308. async def run_with_logging(coro, svc_name):
  1309. try:
  1310. await coro
  1311. except Exception as e:
  1312. logger.error("[VP %s] %s failed: %s", self.name, svc_name, e)
  1313. self._tasks = []
  1314. # FTP server. Each VP gets a non-overlapping passive-mode port slice
  1315. # derived from its DB id so bridge-mode Docker users only have to
  1316. # expose a narrow range (#1646). Default slice is 10 ports per VP;
  1317. # see ftp_server.compute_passive_port_slice for the wrap-around
  1318. # behaviour on installs with very high VP ids.
  1319. passive_port_min, passive_port_max = compute_passive_port_slice(self.id)
  1320. self._ftp = VirtualPrinterFTPServer(
  1321. upload_dir=self.upload_dir,
  1322. access_code=self.access_code,
  1323. cert_path=cert_path,
  1324. key_path=key_path,
  1325. on_file_received=self.on_file_received,
  1326. bind_address=bind_addr,
  1327. vp_name=self.name,
  1328. passive_port_min=passive_port_min,
  1329. passive_port_max=passive_port_max,
  1330. )
  1331. self._tasks.append(
  1332. asyncio.create_task(
  1333. run_with_logging(self._ftp.start(), "FTP"),
  1334. name=f"vp_{self.id}_ftp",
  1335. )
  1336. )
  1337. # MQTT server
  1338. self._mqtt = SimpleMQTTServer(
  1339. serial=self.serial,
  1340. access_code=self.access_code,
  1341. cert_path=cert_path,
  1342. key_path=key_path,
  1343. on_print_command=self.on_print_command,
  1344. model=self.model or DEFAULT_VIRTUAL_PRINTER_MODEL,
  1345. bind_address=bind_addr,
  1346. vp_name=self.name,
  1347. )
  1348. self._tasks.append(
  1349. asyncio.create_task(
  1350. run_with_logging(self._mqtt.start(), "MQTT"),
  1351. name=f"vp_{self.id}_mqtt",
  1352. )
  1353. )
  1354. # MQTT bridge — fans out the target printer's pushes to slicers connected
  1355. # to this VP and forwards their commands back to the printer. Only meaningful
  1356. # when a target printer is configured AND printer_manager was injected (it
  1357. # always is at runtime; tests may omit it).
  1358. if self.target_printer_id is not None and self._printer_manager is not None:
  1359. self._mqtt_bridge = MQTTBridge(
  1360. vp_id=self.id,
  1361. vp_name=self.name,
  1362. vp_serial=self.serial,
  1363. target_printer_id=self.target_printer_id,
  1364. mqtt_server=self._mqtt,
  1365. printer_manager=self._printer_manager,
  1366. )
  1367. self._mqtt.set_bridge(self._mqtt_bridge)
  1368. await self._mqtt_bridge.start()
  1369. # Camera passthrough. BambuStudio / OrcaSlicer connect the "camera"
  1370. # button to the device IP they bound on (the VP), not the IP in the
  1371. # printer's `ipcam.rtsp_url`. Without a listener the slicer gets
  1372. # connection refused → "LAN connection failed" (RTSP models) or
  1373. # OrcaSlicer error `[2:-10061]` (chamber-image models, #1868).
  1374. #
  1375. # The port depends on the TARGET printer's model:
  1376. # RTSPS (X1/X2/H2/P2S) → 322
  1377. # chamber-image (A1/P1P/P1S) → 6000
  1378. #
  1379. # `get_camera_port()` is the same source of truth used by
  1380. # `routes/camera.py`, so slicer and Bambuddy UI agree.
  1381. target_client = self._printer_manager.get_client(self.target_printer_id)
  1382. target_ip = getattr(target_client, "ip_address", None) if target_client else None
  1383. target_model = getattr(target_client, "model", None) if target_client else None
  1384. if target_ip:
  1385. from backend.app.services.camera import get_camera_port
  1386. camera_port = get_camera_port(target_model)
  1387. self._rtsp_proxy = TCPProxy(
  1388. name=f"Camera-{camera_port}",
  1389. listen_port=camera_port,
  1390. target_host=target_ip,
  1391. target_port=camera_port,
  1392. bind_address=bind_addr,
  1393. )
  1394. self._tasks.append(
  1395. asyncio.create_task(
  1396. run_with_logging(self._rtsp_proxy.start(), f"Camera-{camera_port}"),
  1397. name=f"vp_{self.id}_camera",
  1398. )
  1399. )
  1400. # Bind server
  1401. self._bind = BindServer(
  1402. serial=self.serial,
  1403. model=self.model or DEFAULT_VIRTUAL_PRINTER_MODEL,
  1404. name=self.name,
  1405. bind_address=bind_addr,
  1406. cert_path=cert_path,
  1407. key_path=key_path,
  1408. )
  1409. self._tasks.append(
  1410. asyncio.create_task(
  1411. run_with_logging(self._bind.start(), "Bind"),
  1412. name=f"vp_{self.id}_bind",
  1413. )
  1414. )
  1415. # SSDP server — advertise_addr is the remote_interface_ip (Tailscale
  1416. # IP, when chosen from the bind_ip dropdown) or the bind_ip. SSDP
  1417. # Location accepts IPs only; FQDNs go in through bind_ip selection
  1418. # at the printer-IP level and resolve before reaching the SSDP
  1419. # advertisement.
  1420. self._ssdp = VirtualPrinterSSDPServer(
  1421. name=self.name,
  1422. serial=self.serial,
  1423. model=self.model or DEFAULT_VIRTUAL_PRINTER_MODEL,
  1424. advertise_ip=advertise_addr,
  1425. bind_ip=bind_addr,
  1426. )
  1427. self._tasks.append(
  1428. asyncio.create_task(
  1429. run_with_logging(self._ssdp.start(), "SSDP"),
  1430. name=f"vp_{self.id}_ssdp",
  1431. )
  1432. )
  1433. # Wait briefly for every child service to actually finish binding its
  1434. # socket so ``is_running`` doesn't lie. Without this barrier a caller
  1435. # racing the start (e.g. the diagnostic route) would see is_running=True
  1436. # while ports were still in the gap between task creation and the
  1437. # ``asyncio.start_server`` returning. Bounded timeout — if a child
  1438. # hangs we log it and move on; the existing task tracking still
  1439. # catches the failure on the next iteration.
  1440. ready_targets = [
  1441. ("FTP", self._ftp.ready),
  1442. ("MQTT", self._mqtt.ready),
  1443. ("Bind", self._bind.ready),
  1444. ("SSDP", self._ssdp.ready),
  1445. ]
  1446. try:
  1447. await asyncio.wait_for(
  1448. asyncio.gather(*(e.wait() for _, e in ready_targets)),
  1449. timeout=5.0,
  1450. )
  1451. except TimeoutError:
  1452. not_ready = [name for name, e in ready_targets if not e.is_set()]
  1453. logger.warning(
  1454. "[VP %s] Sub-service(s) didn't bind within 5s: %s — continuing anyway",
  1455. self.name,
  1456. ", ".join(not_ready) or "(none)",
  1457. )
  1458. logger.info("[VP %s] Server-mode services started on %s", self.name, bind_addr)
  1459. async def stop_server(self) -> None:
  1460. """Stop server-mode services."""
  1461. if self._finish_release_task is not None and not self._finish_release_task.done():
  1462. self._finish_release_task.cancel()
  1463. self._finish_release_task = None
  1464. if self._mqtt_bridge:
  1465. try:
  1466. await self._mqtt_bridge.stop()
  1467. except Exception:
  1468. logger.exception("[VP %s] MQTT bridge stop failed", self.name)
  1469. if self._mqtt:
  1470. self._mqtt.set_bridge(None)
  1471. self._mqtt_bridge = None
  1472. if self._rtsp_proxy:
  1473. try:
  1474. await self._rtsp_proxy.stop()
  1475. except Exception:
  1476. logger.exception("[VP %s] Camera proxy stop failed", self.name)
  1477. self._rtsp_proxy = None
  1478. if self._ftp:
  1479. await self._ftp.stop()
  1480. self._ftp = None
  1481. if self._mqtt:
  1482. await self._mqtt.stop()
  1483. self._mqtt = None
  1484. if self._bind:
  1485. await self._bind.stop()
  1486. self._bind = None
  1487. if self._ssdp:
  1488. await self._ssdp.stop()
  1489. self._ssdp = None
  1490. await self._cancel_tasks()
  1491. async def start_proxy(self) -> None:
  1492. """Start proxy mode services for this instance."""
  1493. logger.info("[VP %s] Starting proxy mode to %s", self.name, self.target_printer_ip)
  1494. cert_path, key_path, _ = self._resolve_cert_and_advertise()
  1495. self._proxy = SlicerProxyManager(
  1496. target_host=self.target_printer_ip,
  1497. cert_path=cert_path,
  1498. key_path=key_path,
  1499. on_activity=lambda n, m: logger.info("[VP %s] Proxy %s: %s", self.name, n, m),
  1500. bind_address=self.bind_ip or "0.0.0.0", # nosec B104
  1501. bind_identity={
  1502. "serial": self.target_printer_serial or self.serial,
  1503. "model": self.model or DEFAULT_VIRTUAL_PRINTER_MODEL,
  1504. "name": self.name,
  1505. "version": "01.00.00.00",
  1506. },
  1507. )
  1508. async def run_with_logging(coro, svc_name):
  1509. try:
  1510. await coro
  1511. except Exception as e:
  1512. logger.error("[VP %s] %s failed: %s", self.name, svc_name, e)
  1513. self._tasks = []
  1514. # SSDP for proxy
  1515. proxy_serial = self.target_printer_serial or self.serial
  1516. if self.remote_interface_ip:
  1517. from backend.app.services.network_utils import find_interface_for_ip
  1518. local_iface = find_interface_for_ip(self.target_printer_ip)
  1519. if local_iface:
  1520. self._ssdp_proxy = SSDPProxy(
  1521. local_interface_ip=local_iface["ip"],
  1522. remote_interface_ip=self.remote_interface_ip,
  1523. target_printer_ip=self.target_printer_ip,
  1524. name=self.name,
  1525. )
  1526. self._tasks.append(
  1527. asyncio.create_task(
  1528. run_with_logging(self._ssdp_proxy.start(), "SSDP Proxy"),
  1529. name=f"vp_{self.id}_ssdp_proxy",
  1530. )
  1531. )
  1532. else:
  1533. self._start_fallback_ssdp(proxy_serial, run_with_logging)
  1534. else:
  1535. self._start_fallback_ssdp(proxy_serial, run_with_logging)
  1536. self._tasks.append(
  1537. asyncio.create_task(
  1538. run_with_logging(self._proxy.start(), "Proxy"),
  1539. name=f"vp_{self.id}_proxy",
  1540. )
  1541. )
  1542. def _start_fallback_ssdp(self, proxy_serial: str, run_with_logging) -> None:
  1543. """Start single-interface SSDP server as fallback for proxy mode."""
  1544. self._ssdp = VirtualPrinterSSDPServer(
  1545. name=f"{self.name} (Proxy)",
  1546. serial=proxy_serial,
  1547. model=self.model or DEFAULT_VIRTUAL_PRINTER_MODEL,
  1548. advertise_ip=self.bind_ip or "",
  1549. bind_ip=self.bind_ip or "",
  1550. )
  1551. self._tasks.append(
  1552. asyncio.create_task(
  1553. run_with_logging(self._ssdp.start(), "SSDP"),
  1554. name=f"vp_{self.id}_ssdp",
  1555. )
  1556. )
  1557. async def stop_proxy(self) -> None:
  1558. """Stop proxy mode services for this instance."""
  1559. if self._proxy:
  1560. await self._proxy.stop()
  1561. self._proxy = None
  1562. if self._ssdp:
  1563. await self._ssdp.stop()
  1564. self._ssdp = None
  1565. if self._ssdp_proxy:
  1566. await self._ssdp_proxy.stop()
  1567. self._ssdp_proxy = None
  1568. await self._cancel_tasks()
  1569. async def _cancel_tasks(self) -> None:
  1570. """Cancel all running tasks and wait for cleanup."""
  1571. for task in self._tasks:
  1572. task.cancel()
  1573. if self._tasks:
  1574. try:
  1575. await asyncio.wait_for(asyncio.gather(*self._tasks, return_exceptions=True), timeout=1.0)
  1576. except TimeoutError:
  1577. pass
  1578. self._tasks = []
  1579. def get_status(self) -> dict:
  1580. """Get status for this instance."""
  1581. status: dict = {
  1582. "running": self.is_running,
  1583. "pending_files": len(self._pending_files),
  1584. }
  1585. if self.is_proxy and self._proxy:
  1586. status["proxy"] = self._proxy.get_status()
  1587. return status
  1588. class VirtualPrinterManager:
  1589. """Multi-instance virtual printer registry and orchestrator.
  1590. Every VP runs its own independent services on a dedicated bind IP.
  1591. """
  1592. def __init__(self):
  1593. self._session_factory: Callable | None = None
  1594. self._printer_manager: PrinterManager | None = None
  1595. self._instances: dict[int, VirtualPrinterInstance] = {}
  1596. # Serialize sync_from_db so concurrent PUT /vp/{id} calls can't
  1597. # race the start/stop sequence and leave duplicate sub-services
  1598. # bound to the same port. The lock is fine-grained enough that
  1599. # a single VP update completes in well under a second; if the
  1600. # user holds the lock with a long-running start they intended
  1601. # to anyway.
  1602. self._sync_lock = asyncio.Lock()
  1603. # Directories
  1604. self._base_dir = app_settings.base_dir / "virtual_printer"
  1605. # Ensure base directories exist
  1606. self._ensure_base_directories()
  1607. def _ensure_base_directories(self) -> None:
  1608. """Create base directories at startup."""
  1609. for dir_path in [self._base_dir, self._base_dir / "uploads", self._base_dir / "certs"]:
  1610. try:
  1611. dir_path.mkdir(parents=True, exist_ok=True)
  1612. except PermissionError:
  1613. logger.error(
  1614. f"Cannot create directory {dir_path}: Permission denied. "
  1615. f"For Docker: ensure the data volume is writable by the container user. "
  1616. f"For bare metal: run 'sudo chown -R $(whoami) {self._base_dir}'"
  1617. )
  1618. def set_session_factory(self, session_factory: Callable) -> None:
  1619. """Set the database session factory."""
  1620. self._session_factory = session_factory
  1621. def set_printer_manager(self, printer_manager: "PrinterManager") -> None:
  1622. """Inject the global printer_manager so non-proxy VPs can mirror their target's MQTT stream."""
  1623. self._printer_manager = printer_manager
  1624. def get_ca_certificate_info(self) -> dict:
  1625. """Return the shared virtual-printer CA certificate for slicer-trust import.
  1626. The CA is shared by every VP (one import covers all of them). It is
  1627. generated on demand here if no VP has triggered cert generation yet,
  1628. so the "copy/download certificate" UI works even before the first VP
  1629. is enabled.
  1630. """
  1631. certs_dir = self._base_dir / "certs"
  1632. cert_service = CertificateService(cert_dir=certs_dir, shared_ca_dir=certs_dir)
  1633. return cert_service.get_ca_certificate_info()
  1634. @property
  1635. def is_enabled(self) -> bool:
  1636. """Check if any virtual printer is running."""
  1637. return len(self._instances) > 0
  1638. async def sync_from_db(self) -> None:
  1639. """Load all VPs from DB, reconcile running state.
  1640. Serialised by ``self._sync_lock`` — concurrent PUT /vp/{id} routes
  1641. all call into this method; without the lock the start / stop
  1642. sequence races and can leave duplicate sub-services bound to the
  1643. same port or orphan still-running tasks.
  1644. """
  1645. if not self._session_factory:
  1646. logger.warning("Cannot sync virtual printers: no session factory")
  1647. return
  1648. async with self._sync_lock:
  1649. await self._sync_from_db_locked()
  1650. async def _sync_from_db_locked(self) -> None:
  1651. """Inner sync body — caller holds ``self._sync_lock``."""
  1652. from sqlalchemy import select
  1653. from backend.app.models.printer import Printer
  1654. from backend.app.models.virtual_printer import VirtualPrinter
  1655. async with self._session_factory() as db:
  1656. result = await db.execute(
  1657. select(VirtualPrinter).where(VirtualPrinter.enabled == True).order_by(VirtualPrinter.position) # noqa: E712
  1658. )
  1659. enabled_vps = result.scalars().all()
  1660. # Stop instances that are no longer enabled or changed mode
  1661. enabled_ids = {vp.id for vp in enabled_vps}
  1662. for vp_id in list(self._instances.keys()):
  1663. if vp_id not in enabled_ids:
  1664. await self.remove_instance(vp_id)
  1665. # Look up printer IPs for proxy VPs
  1666. proxy_vps = [vp for vp in enabled_vps if vp.mode == "proxy"]
  1667. proxy_ips: dict[int, tuple[str, str]] = {}
  1668. if proxy_vps:
  1669. async with self._session_factory() as db:
  1670. for pvp in proxy_vps:
  1671. if pvp.target_printer_id:
  1672. result = await db.execute(select(Printer).where(Printer.id == pvp.target_printer_id))
  1673. printer = result.scalar_one_or_none()
  1674. if printer:
  1675. proxy_ips[pvp.id] = (printer.ip_address, printer.serial_number)
  1676. # Detect config changes on running instances and restart if needed
  1677. for vp in enabled_vps:
  1678. instance = self._instances.get(vp.id)
  1679. if not instance:
  1680. continue
  1681. # Proxy mode: detect target printer IP / serial changes from the
  1682. # DB lookup above. Without this branch a DHCP renewal that gives
  1683. # the target printer a new IP would leave the running proxy
  1684. # forwarding to the stale IP until the user manually toggles the
  1685. # VP. The same shape covers a target-side serial change.
  1686. proxy_target_changed = False
  1687. if vp.mode == "proxy":
  1688. fresh = proxy_ips.get(vp.id)
  1689. if fresh is not None:
  1690. fresh_ip, fresh_serial = fresh
  1691. if (
  1692. getattr(instance, "target_printer_ip", None) != fresh_ip
  1693. or getattr(instance, "target_printer_serial", None) != fresh_serial
  1694. ):
  1695. proxy_target_changed = True
  1696. # Normalize the DB value before comparing — a legacy `immediate`
  1697. # row read before the migration window finishes would otherwise
  1698. # trip the "changed" branch and bounce every VP at boot.
  1699. db_mode = normalize_vp_mode(vp.mode)
  1700. changed = (
  1701. instance.mode != db_mode
  1702. # The name is advertised to the slicer (SSDP, bind) by the
  1703. # running services, so a rename only reaches it on restart.
  1704. or instance.name != vp.name
  1705. or instance.model != (vp.model or DEFAULT_VIRTUAL_PRINTER_MODEL)
  1706. or instance.access_code != (vp.access_code or "")
  1707. or instance.bind_ip != (vp.bind_ip or "")
  1708. or instance.remote_interface_ip != (vp.remote_interface_ip or "")
  1709. or instance.target_printer_id != vp.target_printer_id
  1710. or instance.auto_dispatch != vp.auto_dispatch
  1711. # Queue-mode behaviour toggle — without it the running
  1712. # instance silently keeps the old value until process
  1713. # restart (#1552 follow-up family).
  1714. or instance.queue_force_color_match != vp.queue_force_color_match
  1715. or instance.save_ams_mapping != vp.save_ams_mapping
  1716. or instance.gcode_injection != vp.gcode_injection
  1717. or proxy_target_changed
  1718. )
  1719. if changed:
  1720. logger.info(
  1721. "VP %s config changed (mode: %s→%s), restarting",
  1722. instance.name,
  1723. instance.mode,
  1724. vp.mode,
  1725. )
  1726. await self.remove_instance(vp.id)
  1727. # Start instances for all enabled VPs (skip already running)
  1728. for vp in enabled_vps:
  1729. if vp.id in self._instances:
  1730. continue
  1731. if vp.mode == "proxy":
  1732. ip_info = proxy_ips.get(vp.id)
  1733. if not ip_info:
  1734. logger.warning("Proxy VP %s: target printer not found, skipping", vp.name)
  1735. continue
  1736. target_ip, target_serial = ip_info
  1737. instance = VirtualPrinterInstance(
  1738. vp_id=vp.id,
  1739. name=vp.name,
  1740. mode=vp.mode,
  1741. model=vp.model or DEFAULT_VIRTUAL_PRINTER_MODEL,
  1742. access_code=vp.access_code or "",
  1743. serial_suffix=vp.serial_suffix,
  1744. target_printer_ip=target_ip,
  1745. target_printer_serial=target_serial,
  1746. # Carried although the proxy does not use them: the change
  1747. # check above compares them with the row, and a missing
  1748. # value restarted every proxy VP on every sync.
  1749. target_printer_id=vp.target_printer_id,
  1750. auto_dispatch=vp.auto_dispatch,
  1751. queue_force_color_match=vp.queue_force_color_match,
  1752. save_ams_mapping=vp.save_ams_mapping,
  1753. gcode_injection=vp.gcode_injection,
  1754. bind_ip=vp.bind_ip or "",
  1755. remote_interface_ip=vp.remote_interface_ip or "",
  1756. base_dir=self._base_dir,
  1757. session_factory=self._session_factory,
  1758. )
  1759. self._instances[vp.id] = instance
  1760. await instance.start_proxy()
  1761. logger.info("Started proxy VP: %s → %s (bind=%s)", instance.name, target_ip, instance.bind_ip)
  1762. else:
  1763. instance = VirtualPrinterInstance(
  1764. vp_id=vp.id,
  1765. name=vp.name,
  1766. mode=vp.mode,
  1767. model=vp.model or DEFAULT_VIRTUAL_PRINTER_MODEL,
  1768. access_code=vp.access_code or "",
  1769. serial_suffix=vp.serial_suffix,
  1770. target_printer_id=vp.target_printer_id,
  1771. auto_dispatch=vp.auto_dispatch,
  1772. queue_force_color_match=vp.queue_force_color_match,
  1773. save_ams_mapping=vp.save_ams_mapping,
  1774. gcode_injection=vp.gcode_injection,
  1775. bind_ip=vp.bind_ip or "",
  1776. remote_interface_ip=vp.remote_interface_ip or "",
  1777. base_dir=self._base_dir,
  1778. session_factory=self._session_factory,
  1779. printer_manager=self._printer_manager,
  1780. )
  1781. self._instances[vp.id] = instance
  1782. await instance.start_server()
  1783. logger.info("Started server-mode VP: %s on %s", instance.name, vp.bind_ip)
  1784. async def remove_instance(self, vp_id: int) -> None:
  1785. """Stop and remove a single VP instance."""
  1786. instance = self._instances.pop(vp_id, None)
  1787. if instance:
  1788. if instance.is_proxy:
  1789. await instance.stop_proxy()
  1790. else:
  1791. await instance.stop_server()
  1792. logger.info("Removed VP instance: %s", instance.name)
  1793. async def stop_all(self) -> None:
  1794. """Shutdown all virtual printer services."""
  1795. logger.info("Stopping all virtual printer services...")
  1796. for vp_id in list(self._instances.keys()):
  1797. await self.remove_instance(vp_id)
  1798. logger.info("All virtual printer services stopped")
  1799. def get_instance(self, vp_id: int) -> VirtualPrinterInstance | None:
  1800. """Get a running instance by ID."""
  1801. return self._instances.get(vp_id)
  1802. # -- Legacy single-printer compat --
  1803. def get_status(self) -> dict:
  1804. """Get status for first virtual printer (backward compat)."""
  1805. if self._instances:
  1806. first = next(iter(self._instances.values()))
  1807. return {
  1808. "enabled": True,
  1809. "running": first.is_running,
  1810. "mode": first.mode,
  1811. "name": first.name,
  1812. "serial": first.serial,
  1813. "model": first.model or DEFAULT_VIRTUAL_PRINTER_MODEL,
  1814. "model_name": VIRTUAL_PRINTER_MODELS.get(
  1815. first.model or DEFAULT_VIRTUAL_PRINTER_MODEL,
  1816. first.model or DEFAULT_VIRTUAL_PRINTER_MODEL,
  1817. ),
  1818. "pending_files": first.get_status().get("pending_files", 0),
  1819. **({"target_printer_ip": first.target_printer_ip} if first.is_proxy else {}),
  1820. **({"proxy": first.get_status().get("proxy", {})} if first.is_proxy else {}),
  1821. }
  1822. return {
  1823. "enabled": False,
  1824. "running": False,
  1825. "mode": VP_MODE_ARCHIVE,
  1826. "name": "Bambuddy",
  1827. "serial": "",
  1828. "model": DEFAULT_VIRTUAL_PRINTER_MODEL,
  1829. "model_name": VIRTUAL_PRINTER_MODELS[DEFAULT_VIRTUAL_PRINTER_MODEL],
  1830. "pending_files": 0,
  1831. }
  1832. async def configure(
  1833. self,
  1834. enabled: bool,
  1835. access_code: str = "",
  1836. mode: str = VP_MODE_ARCHIVE,
  1837. model: str = "",
  1838. target_printer_ip: str = "",
  1839. target_printer_serial: str = "",
  1840. remote_interface_ip: str = "",
  1841. ) -> None:
  1842. """Legacy single-printer configure. Delegates to sync_from_db()."""
  1843. # This method is kept for backward compat with the settings endpoint.
  1844. # The actual work is done by sync_from_db() which reads from the DB.
  1845. await self.sync_from_db()
  1846. # Global instance
  1847. virtual_printer_manager = VirtualPrinterManager()