printer_diagnostic.py 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535
  1. """Connection diagnostic for Bambu printers.
  2. Runs the checks a maintainer performs by hand when triaging a
  3. "printer won't connect / won't print" report — port reachability, LAN
  4. developer mode, Docker network mode, subnet match, and MQTT credentials —
  5. so users can self-diagnose setup problems instead of opening an issue.
  6. See the 2026-05-21 issue-triage analysis: ~1/3 of closed issues were
  7. user-side setup errors clustered on exactly these causes.
  8. """
  9. import asyncio
  10. import ipaddress
  11. import logging
  12. import socket
  13. import ssl
  14. from backend.app.models.printer import Printer
  15. from backend.app.schemas.printer import DiagnosticCheck, PrinterDiagnosticResult
  16. from backend.app.services.bambu_ftp import find_remote_file_async
  17. from backend.app.services.bambu_mqtt import CONNECT_ERROR_AUTH_REJECTED
  18. from backend.app.services.camera import get_camera_port
  19. from backend.app.services.discovery import is_running_in_docker
  20. from backend.app.services.ftp_profiles import get_ftp_profile
  21. from backend.app.services.print_storage import (
  22. REASON_INTERNAL_STORAGE,
  23. StorageVerdict,
  24. ftp_probe_paths,
  25. last_print_storage_verdict,
  26. )
  27. from backend.app.services.printer_manager import printer_manager
  28. from backend.app.utils.printer_models import has_external_storage, has_remote_storage_toggle
  29. logger = logging.getLogger(__name__)
  30. # Bambu LAN-mode ports.
  31. PORT_MQTT = 8883 # MQTT over TLS — control + status. Connection-critical.
  32. PORT_FTPS = 990 # FTPS — file upload; required to send prints.
  33. PORT_RTSPS = 322 # RTSPS — camera stream; optional.
  34. PORT_CHAMBER_IMAGE = 6000 # Chamber image protocol — A1/P1 camera stream; optional.
  35. _PORT_PROBE_TIMEOUT = 3.0
  36. # Cap for the storage probe (#2856). One connection and a handful of directory
  37. # listings, so a healthy printer answers in well under a second. Kept short on
  38. # purpose: this check sits inside the support bundle's 15s-per-printer budget,
  39. # and in the interactive run it is spinner time the user is watching.
  40. _STORAGE_PROBE_TIMEOUT = 6.0
  41. # Default seconds the `printer_publishing` check will wait for the first
  42. # report-topic message before declaring fail. Bambu printers in idle publish
  43. # push_status every few seconds; 10s catches healthy bridges with margin while
  44. # staying short enough that the spinner-with-countdown UX stays acceptable.
  45. # The check exits the moment a message arrives, so the typical wall-clock is
  46. # 1–2s, not the full 10. Passed as ``wait_for_publish_seconds`` per call so
  47. # the support-package code path can skip the wait entirely (defaults to 0).
  48. PUBLISH_WAIT_DEFAULT = 10.0
  49. _PUBLISH_POLL_INTERVAL = 0.5
  50. async def _check_port(ip: str, port: int, timeout: float = _PORT_PROBE_TIMEOUT) -> bool:
  51. """Test TCP connectivity to ip:port. Returns True if reachable."""
  52. try:
  53. _reader, writer = await asyncio.wait_for(asyncio.open_connection(ip, port), timeout=timeout)
  54. writer.close()
  55. try:
  56. await writer.wait_closed()
  57. except Exception:
  58. pass
  59. return True
  60. except Exception:
  61. return False
  62. # Public alias. The connection watchdog probes the MQTT port before rebuilding a
  63. # client, so it can tell "the printer is switched off" (leave it alone, paho will
  64. # keep retrying) from "the printer is answering but our session is dead" (#2732).
  65. check_port = _check_port
  66. async def _check_ftps_tls(ip: str, model: str | None, timeout: float = _PORT_PROBE_TIMEOUT) -> str:
  67. """Probe port 990 the way the FTP client does, and say how far it got.
  68. Returns ``"ok"``, ``"closed"`` (nothing accepted the TCP connection) or
  69. ``"no_tls"`` (the port accepted the connection but the TLS handshake did
  70. not complete).
  71. A plain TCP probe cannot tell the last two apart, which is exactly how
  72. #2780 hid: the reporter's diagnostic reported port 990 as reachable and
  73. green while every real transfer died in the handshake with
  74. ``WRONG_VERSION_NUMBER``, so archives quietly arrived empty with nothing
  75. on screen to explain it.
  76. The context mirrors :class:`~backend.app.services.bambu_ftp.ImplicitFTP_TLS`
  77. -- including the model's TLS cap -- so a pass here means the FTP client
  78. would also get through. Handshake only; no login is attempted, so this
  79. stays valid for the pre-save Add-Printer flow where no access code exists
  80. yet.
  81. """
  82. context = ssl.create_default_context()
  83. context.check_hostname = False
  84. context.verify_mode = ssl.CERT_NONE
  85. context.minimum_version = ssl.TLSVersion.TLSv1_2
  86. if get_ftp_profile(model).cap_tls_v1_2:
  87. context.maximum_version = ssl.TLSVersion.TLSv1_2
  88. writer = None
  89. try:
  90. _reader, writer = await asyncio.wait_for(
  91. asyncio.open_connection(ip, PORT_FTPS, ssl=context),
  92. timeout=timeout,
  93. )
  94. return "ok"
  95. except ssl.SSLError:
  96. # The socket was accepted and then failed to negotiate TLS. Reaching
  97. # here at all proves something is listening, so this is never "port
  98. # blocked" -- it is the printer's file service in a state no retry
  99. # gets past.
  100. return "no_tls"
  101. except Exception:
  102. return "closed"
  103. finally:
  104. if writer is not None:
  105. writer.close()
  106. try:
  107. await writer.wait_closed()
  108. except Exception:
  109. pass
  110. async def _last_print_file_is_reachable(
  111. printer: Printer | None,
  112. verdict: StorageVerdict,
  113. *,
  114. ftps_ok: bool,
  115. ) -> bool:
  116. """Did the last print's file turn up on external storage after all? (#2856)
  117. ``verdict`` is read off the dispatch URL, which says where the printer
  118. *put* the file, not whether port 990 can serve it: an H2D with a card in
  119. the slot reports ``brtc://emmc/<name>`` and then hands the same file over
  120. from ``/cache`` without complaint. Warning that the file is out of reach
  121. while that user's archives are quietly complete would send them chasing a
  122. setting that is already right.
  123. So check before saying it -- one connection, one listing per candidate
  124. directory, no transfer, and only for the check that is about to warn.
  125. "Could not check" returns False and leaves the warning standing, which is
  126. the safe direction: it is a warn, not a fail.
  127. """
  128. if not ftps_ok or not verdict.probe_filename or printer is None:
  129. return False
  130. # Attribute reads inside the guard too: this also runs from the support
  131. # bundle, where the row may outlive its session, and a detached-instance
  132. # error there is "could not check", not a broken diagnostic.
  133. ip_address = None
  134. try:
  135. ip_address = getattr(printer, "ip_address", None)
  136. access_code = getattr(printer, "access_code", None)
  137. if not ip_address or not access_code:
  138. return False
  139. found = await find_remote_file_async(
  140. ip_address,
  141. access_code,
  142. ftp_probe_paths(verdict.probe_filename),
  143. timeout=_STORAGE_PROBE_TIMEOUT,
  144. socket_timeout=_STORAGE_PROBE_TIMEOUT,
  145. printer_model=getattr(printer, "model", None),
  146. )
  147. except Exception as e:
  148. logger.debug("Could not probe %s for %s: %s", ip_address, verdict.probe_filename, e)
  149. return False
  150. if found:
  151. logger.debug("Last print file is on external storage at %s despite %s", found, verdict.reason)
  152. return found is not None
  153. def _auth_reason_params(reason: str | None) -> dict:
  154. """Map a client's CONNACK-refusal slug onto the check's `params.reason`.
  155. The frontend renders `diagnostic.check.<id>.<status>_<reason>` when a reason
  156. is present and falls back to the plain per-status text otherwise, so an
  157. unknown or absent slug degrades to today's generic wording rather than a
  158. missing string. Only `auth_rejected` currently carries its own message:
  159. that is the one case where the printer positively told us the credentials
  160. were wrong, as opposed to us merely observing that we are not connected.
  161. """
  162. if reason == CONNECT_ERROR_AUTH_REJECTED:
  163. return {"reason": CONNECT_ERROR_AUTH_REJECTED}
  164. return {}
  165. def _camera_port_for_printer(printer: Printer | None) -> tuple[int, str]:
  166. """Return the model-specific camera diagnostic port and display protocol."""
  167. if not printer:
  168. return PORT_RTSPS, "RTSPS"
  169. model = getattr(printer, "model", None)
  170. if not model:
  171. return PORT_RTSPS, "RTSPS"
  172. camera_port = get_camera_port(model)
  173. if camera_port == PORT_CHAMBER_IMAGE:
  174. return camera_port, "Chamber Image"
  175. return camera_port, "RTSPS"
  176. def _detect_docker_network_mode() -> str:
  177. """Detect Docker network mode.
  178. In host mode the container shares the host network namespace, so Docker
  179. infrastructure interfaces (docker0, br-*, veth*) are visible. In bridge
  180. mode the container only sees its own eth0.
  181. """
  182. try:
  183. for _idx, name in socket.if_nameindex():
  184. if name.startswith(("docker", "br-", "veth", "virbr")):
  185. return "host"
  186. except Exception:
  187. pass
  188. return "bridge"
  189. def _get_host_ip() -> str | None:
  190. """Best-effort IPv4 address the Bambuddy host routes from."""
  191. try:
  192. s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
  193. try:
  194. # No packets are sent; this just picks the routing-table source IP.
  195. s.connect(("10.255.255.255", 1))
  196. return s.getsockname()[0]
  197. finally:
  198. s.close()
  199. except Exception:
  200. return None
  201. def _same_subnet(ip_a: str, ip_b: str) -> bool | None:
  202. """True/False if both are IPv4 literals in the same /24; None if undeterminable."""
  203. try:
  204. addr_a = ipaddress.ip_address(ip_a)
  205. addr_b = ipaddress.ip_address(ip_b)
  206. except ValueError:
  207. return None
  208. if addr_a.version != 4 or addr_b.version != 4:
  209. return None
  210. net_a = ipaddress.ip_network(f"{addr_a}/24", strict=False)
  211. net_b = ipaddress.ip_network(f"{addr_b}/24", strict=False)
  212. return net_a == net_b
  213. async def run_connection_diagnostic(
  214. ip_address: str,
  215. *,
  216. printer: Printer | None = None,
  217. serial_number: str | None = None,
  218. access_code: str | None = None,
  219. wait_for_publish_seconds: float = 0.0,
  220. ) -> PrinterDiagnosticResult:
  221. """Run connection checks for a printer.
  222. Works for an existing saved printer (pass ``printer``) and for the
  223. pre-save Add-Printer flow (pass ``serial_number`` + ``access_code``).
  224. Each check carries a stable ``id`` and a ``status`` of
  225. pass / fail / warn / skip; the frontend renders the human-readable
  226. title and fix text (localized) keyed on that id + status.
  227. """
  228. checks: list[DiagnosticCheck] = []
  229. # --- Port reachability (probed in parallel) ---
  230. camera_port, camera_protocol = _camera_port_for_printer(printer)
  231. mqtt_ok, ftps_state, camera_ok = await asyncio.gather(
  232. _check_port(ip_address, PORT_MQTT),
  233. _check_ftps_tls(ip_address, getattr(printer, "model", None) if printer else None),
  234. _check_port(ip_address, camera_port),
  235. )
  236. # MQTT is connection-critical; FTPS/camera only degrade printing/camera.
  237. checks.append(DiagnosticCheck(id="port_mqtt", status="pass" if mqtt_ok else "fail"))
  238. # "no_tls" gets its own message: the port is open, so the usual advice
  239. # (unblock port 990) is wrong and only a printer restart helps (#2780).
  240. checks.append(
  241. DiagnosticCheck(
  242. id="port_ftps",
  243. status="pass" if ftps_state == "ok" else "warn",
  244. params={} if ftps_state != "no_tls" else {"reason": "no_tls"},
  245. )
  246. )
  247. checks.append(
  248. DiagnosticCheck(
  249. id="port_rtsps",
  250. status="pass" if camera_ok else "warn",
  251. params={"port": camera_port, "protocol": camera_protocol},
  252. )
  253. )
  254. # --- Docker network mode ---
  255. network_mode: str | None = None
  256. if is_running_in_docker():
  257. network_mode = _detect_docker_network_mode()
  258. checks.append(
  259. DiagnosticCheck(
  260. id="network_mode",
  261. status="pass" if network_mode == "host" else "warn",
  262. params={"mode": network_mode},
  263. )
  264. )
  265. else:
  266. checks.append(DiagnosticCheck(id="network_mode", status="skip"))
  267. # --- Subnet match ---
  268. # Skipped in bridge mode: the container IP is the bridge IP, not the host's,
  269. # so the comparison is meaningless and the network_mode check already covers it.
  270. if network_mode == "bridge":
  271. checks.append(DiagnosticCheck(id="subnet", status="skip"))
  272. else:
  273. host_ip = _get_host_ip()
  274. same = _same_subnet(ip_address, host_ip) if host_ip else None
  275. if same is None:
  276. checks.append(DiagnosticCheck(id="subnet", status="skip"))
  277. else:
  278. checks.append(
  279. DiagnosticCheck(
  280. id="subnet",
  281. status="pass" if same else "warn",
  282. params={"printer_ip": ip_address, "host_ip": host_ip},
  283. )
  284. )
  285. # --- External storage (printer-side "Store sent files on external storage") ---
  286. # Install step 4. The setting has two variants depending on
  287. # firmware/slicer combo: on newer firmware the toggle lives on the
  288. # printer (P2S 01.02 / BambuStudio 2.6+), on older versions it's
  289. # purely a slicer-side preference.
  290. #
  291. # For the printer-side variant, `home_flag` bit 11 is pushed on every
  292. # status report and parsed into state.store_to_sdcard (bambu_mqtt.py
  293. # line 153). That's the signal here — instant, no FTP I/O.
  294. #
  295. # For the slicer-side variant, the printer never hears about it and
  296. # this check will pass even when the user is missing step 4. That gap
  297. # is covered separately by the "no_3mf_available" archive-fallback
  298. # banner. An FTP upload-and-verify probe was tried and rejected — the
  299. # /cache directory is always writable from Bambuddy regardless of
  300. # either toggle, so the probe always passes and detects nothing.
  301. #
  302. # Skip entirely on models with no external-storage slot at all (A1
  303. # and A1 Mini). They never set home_flag bit 11, so a naive read of
  304. # `store_to_sdcard` would fall through to a false `fail` for every
  305. # A1-series user (#1703).
  306. #
  307. # Some models (P1-series) DO have a slot but no reachable control to turn
  308. # the option on: the Bambu Studio toggle only appears when the printer
  309. # publishes `support_save_remote_print_file_to_storage`, which current
  310. # P1 firmware never does, and the P1S/P1P have no screen. For those,
  311. # `store_to_sdcard` is stuck False with no way to fix it — report `skip`
  312. # (with a reason the UI explains) instead of a permanently-red `fail`
  313. # (#2524).
  314. state = printer_manager.get_status(printer.id) if printer else None
  315. model = getattr(printer, "model", None) if printer else None
  316. model_has_slot = has_external_storage(model) if printer else True
  317. store_to_sdcard = getattr(state, "store_to_sdcard", None) if state else None
  318. if not model_has_slot or state is None or not state.connected:
  319. checks.append(DiagnosticCheck(id="external_storage", status="skip"))
  320. elif store_to_sdcard is False and not has_remote_storage_toggle(model):
  321. # Slot present but no way to enable it on this firmware — don't nag
  322. # with an unresolvable fail; explain why via the reason param.
  323. #
  324. # Ahead of the empty-slot check below on purpose (#2524 over #2780):
  325. # on a P1-series the toggle cannot be switched on at all, so telling
  326. # the operator to insert a card would promise a fix that inserting a
  327. # card does not deliver.
  328. checks.append(
  329. DiagnosticCheck(
  330. id="external_storage",
  331. status="skip",
  332. params={"reason": "unsupported_model"},
  333. )
  334. )
  335. elif getattr(state, "sdcard_reported", False) and not getattr(state, "sdcard", False):
  336. # The toggle can be on and still achieve nothing with an empty slot,
  337. # and that combination used to report a clean pass — #2780's H2C had
  338. # `store_to_sdcard` set and `sdcard` False for three solid weeks while
  339. # every one of its archives came out blank. Report the empty slot,
  340. # which is the part the operator can actually act on.
  341. checks.append(
  342. DiagnosticCheck(
  343. id="external_storage",
  344. status="fail",
  345. params={"reason": "no_media"},
  346. )
  347. )
  348. elif not (last_verdict := last_print_storage_verdict(state)).reachable and not await _last_print_file_is_reachable(
  349. printer, last_verdict, ftps_ok=ftps_state == "ok"
  350. ):
  351. # The toggle is on, a card is in, the printer said the last print's file
  352. # is on internal storage — and a probe confirmed it really is out of
  353. # reach. That is what H2-series and P2S firmware does, and no setting
  354. # here changes it (#2762 tracks reading that storage). A pass here would
  355. # be a lie; a fail would be unresolvable.
  356. #
  357. # The verdict's own reason, not a fixed one: a print started from the
  358. # printer's screen reaches this branch too, and it never involved a
  359. # slicer, so the advice attached to REASON_INTERNAL_STORAGE would name a
  360. # dialog its operator never opened (#1820).
  361. checks.append(
  362. DiagnosticCheck(
  363. id="external_storage",
  364. status="warn",
  365. params={"reason": last_verdict.reason or REASON_INTERNAL_STORAGE},
  366. )
  367. )
  368. elif not last_verdict.reachable:
  369. # Reached only when the probe above found the file: the printer named
  370. # internal storage and served it over FTPS anyway. What this check is
  371. # for is whether Bambuddy can read the print file, and it demonstrably
  372. # can — so pass, whatever the toggle happens to say (#2856).
  373. checks.append(DiagnosticCheck(id="external_storage", status="pass"))
  374. elif store_to_sdcard is True:
  375. checks.append(DiagnosticCheck(id="external_storage", status="pass"))
  376. elif store_to_sdcard is False:
  377. checks.append(DiagnosticCheck(id="external_storage", status="fail"))
  378. else:
  379. # State exists but the field was never populated — skip rather than
  380. # report a false fail.
  381. checks.append(DiagnosticCheck(id="external_storage", status="skip"))
  382. # --- MQTT credentials / connection ---
  383. if not mqtt_ok:
  384. # Can't reach the broker at all — the port check already reported it.
  385. checks.append(DiagnosticCheck(id="mqtt_auth", status="skip"))
  386. elif serial_number and access_code:
  387. # Pre-add flow: actively probe with the credentials the user entered.
  388. try:
  389. result = await printer_manager.test_connection(
  390. ip_address=ip_address,
  391. serial_number=serial_number,
  392. access_code=access_code,
  393. )
  394. checks.append(
  395. DiagnosticCheck(
  396. id="mqtt_auth",
  397. status="pass" if result.get("success") else "fail",
  398. params=_auth_reason_params(result.get("reason")),
  399. )
  400. )
  401. except Exception:
  402. logger.debug("test_connection failed during diagnostic", exc_info=True)
  403. checks.append(DiagnosticCheck(id="mqtt_auth", status="fail"))
  404. elif state is not None:
  405. # Existing printer: trust the live MQTT state rather than opening a
  406. # second connection (Bambu printers tolerate few concurrent sessions).
  407. # `connected == False` alone does not say *why* — the live client keeps
  408. # the last CONNACK refusal, so a rejected access code can be reported as
  409. # such instead of as a generic failure the user has to guess at (#2698).
  410. client = printer_manager.get_client(printer.id) if printer else None
  411. checks.append(
  412. DiagnosticCheck(
  413. id="mqtt_auth",
  414. status="pass" if state.connected else "fail",
  415. params={} if state.connected else _auth_reason_params(getattr(client, "last_connect_error", None)),
  416. )
  417. )
  418. else:
  419. checks.append(DiagnosticCheck(id="mqtt_auth", status="skip"))
  420. # --- LAN developer mode (only readable over a live MQTT connection) ---
  421. if state is not None and state.connected:
  422. if state.developer_mode is True:
  423. dev_status = "pass"
  424. elif state.developer_mode is False:
  425. dev_status = "fail"
  426. else:
  427. dev_status = "skip"
  428. checks.append(DiagnosticCheck(id="developer_mode", status=dev_status))
  429. else:
  430. checks.append(DiagnosticCheck(id="developer_mode", status="skip"))
  431. # --- Printer is actually publishing on its report topic ---
  432. # The mqtt_auth check above only proves TCP + TLS + auth + SUBSCRIBE
  433. # succeed. A printer with a wrong-cased serial — or one that simply isn't
  434. # publishing for some other reason — still passes mqtt_auth because the
  435. # broker accepts the subscription regardless. The user-visible symptom in
  436. # that case is "AMS / K-profiles / custom filaments missing on the slicer
  437. # side": the VP bridge has nothing cached to mirror because no reports
  438. # arrived. #1622 surfaced this: bridge keep-alive timeouts paired with
  439. # the `Connected and subscribed, but the printer has sent zero status
  440. # reports` warning. The check below turns that warning into a structured
  441. # diagnostic result the user can act on without grepping container logs.
  442. #
  443. # If ``_report_messages_since_connect`` is already > 0, we exit
  444. # immediately — the bridge has seen reports. If it's 0 and a wait is
  445. # requested, we poll every PUBLISH_POLL_INTERVAL up to
  446. # ``wait_for_publish_seconds`` so a fresh reconnect (counter reset to 0)
  447. # isn't reported as fail before the printer's first idle push lands.
  448. publishing_params: dict[str, int | float] | None = None
  449. publishing_status = "skip"
  450. if printer is not None and state is not None and state.connected:
  451. client = printer_manager.get_client(printer.id)
  452. if client is not None:
  453. wait_budget = max(wait_for_publish_seconds, 0.0)
  454. if wait_budget > 0:
  455. # Expose the budget so the UI can render a countdown next to
  456. # the spinner — the user knows how long this check might take.
  457. publishing_params = {"max_wait_seconds": wait_budget}
  458. loop = asyncio.get_running_loop()
  459. deadline = loop.time() + wait_budget
  460. while True:
  461. if client.report_messages_since_connect > 0:
  462. publishing_status = "pass"
  463. break
  464. if loop.time() >= deadline:
  465. publishing_status = "fail"
  466. break
  467. await asyncio.sleep(_PUBLISH_POLL_INTERVAL)
  468. checks.append(
  469. DiagnosticCheck(
  470. id="printer_publishing",
  471. status=publishing_status,
  472. params=publishing_params or {},
  473. )
  474. )
  475. statuses = {c.status for c in checks}
  476. if "fail" in statuses:
  477. overall = "problems"
  478. elif "warn" in statuses:
  479. overall = "warnings"
  480. else:
  481. overall = "ok"
  482. return PrinterDiagnosticResult(
  483. printer_id=printer.id if printer else None,
  484. ip_address=ip_address,
  485. overall=overall,
  486. checks=checks,
  487. )