print_storage.py 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. """Can FTPS see the file this print is running from? (#2780)
  2. Bambuddy reads a print's 3MF, cover and timelapse off the printer over implicit
  3. FTPS on port 990. On every Bambu model that port serves **external storage only**
  4. -- the SD card or USB stick. It is not a view of the printer's filesystem.
  5. H2-series and P2S firmware default to keeping the sliced file on internal eMMC
  6. instead, and BambuStudio uploads there over a separate service on port 6000
  7. (the "BambuTunnelLocal" protocol -- see #2762, which tracks implementing it).
  8. When that happens there is no file on FTPS to find, at any path, and no TLS
  9. option, retry or directory guess changes that. The dispatch says so plainly:
  10. the ``project_file`` command carries ``url``, which is ``ftp://<name>`` for
  11. external storage and ``brtc://emmc/<name>`` for internal.
  12. Before this module we ignored ``url`` and swept anyway: six filename variants
  13. across five directories with up to four retries for the 3MF, then sixteen more
  14. paths for the cover, then the timelapse scan -- roughly 110 FTPS connections per
  15. print, every one of them certain to 550. The user-visible result was an archive
  16. card with nothing on it and no stated reason, which read as a Bambuddy bug and
  17. was reported as one four times (#1170, #2524, #2762, #2780).
  18. The rule here is deliberately one-sided: **skip only on positive evidence**.
  19. Silence is not evidence -- a printer that never publishes ``sdcard`` and never
  20. had a ``project_file`` pass through the request topic (some brokers refuse the
  21. subscription) must keep the old behaviour exactly, or this becomes a regression
  22. for installs whose archives work fine today.
  23. """
  24. from __future__ import annotations
  25. from dataclasses import dataclass
  26. # The one URL scheme that means "on external storage, reachable over FTPS".
  27. # Anything else -- brtc://emmc today, whatever Bambu ships next -- is somewhere
  28. # port 990 does not serve. Matching the reachable value rather than the
  29. # unreachable one is what keeps a new scheme from silently reading as fine.
  30. _EXTERNAL_STORAGE_SCHEME = "ftp"
  31. # Reason slugs. These cross the API into the UI and into the connection
  32. # diagnostic, so they are part of the contract: the frontend maps each to its
  33. # own explanation and its own advice. Keep them stable.
  34. REASON_INTERNAL_STORAGE = "internal_storage"
  35. REASON_NO_EXTERNAL_STORAGE = "no_external_storage"
  36. @dataclass(frozen=True)
  37. class StorageVerdict:
  38. """Whether an FTPS sweep for this print's file is worth running.
  39. ``reachable`` False always carries a ``reason``; True never does.
  40. """
  41. reachable: bool
  42. reason: str | None = None
  43. _REACHABLE = StorageVerdict(reachable=True)
  44. def url_is_external_storage(project_url: str | None) -> bool | None:
  45. """Does *project_url* name a file on external storage?
  46. ``None`` when there is no URL to read, which is not the same answer as
  47. False and must not be collapsed into one by callers.
  48. """
  49. # Type-checked, not just truth-checked: this value arrives straight off the
  50. # wire, so it is whatever the sender put there. Anything that is not a
  51. # string is not an answer.
  52. if not isinstance(project_url, str) or not project_url:
  53. return None
  54. scheme, separator, _ = project_url.partition("://")
  55. if not separator:
  56. # No scheme at all. Real dispatches always carry one, so rather than
  57. # guess at a bare path, decline to answer and let the caller fall
  58. # through to its existing behaviour.
  59. return None
  60. return scheme.lower() == _EXTERNAL_STORAGE_SCHEME
  61. def external_storage_present(state: object | None) -> bool:
  62. """Does the printer have external storage for FTPS to serve at all?
  63. Narrower than :func:`print_file_reachable_over_ftp` and deliberately so.
  64. The printer records its timelapse to the card itself, so *where the sliced
  65. file went* says nothing about whether a video exists -- an H2C that kept
  66. the 3MF on eMMC still writes ``/timelapse`` to an inserted card. Only the
  67. empty-slot case rules a scan out, and only when the printer said the slot
  68. is empty rather than never mentioning it.
  69. """
  70. if state is None:
  71. return True
  72. return not (getattr(state, "sdcard_reported", False) and not getattr(state, "sdcard", False))
  73. def print_file_reachable_over_ftp(state: object | None) -> StorageVerdict:
  74. """Decide whether to run an FTPS sweep for the print *state* is running.
  75. *state* is a ``PrinterState`` (duck-typed so tests and callers can pass a
  76. stand-in). Reads ``current_project_url``, ``sdcard`` and ``sdcard_reported``.
  77. Deliberately the *per-print* URL, not the sticky one: a print Bambuddy saw
  78. no dispatch for must read as unknown and sweep, rather than inherit the
  79. previous job's destination. Roughly a fifth of the print starts in #2780's
  80. bundle had no dispatch on the request topic -- touchscreen reprints and
  81. restart recovery -- and inheriting a stale internal-storage answer there
  82. would skip a sweep that could have found the file.
  83. Returns :data:`_REACHABLE` unless something positively says otherwise.
  84. """
  85. return _verdict(getattr(state, "current_project_url", None), state)
  86. def last_print_storage_verdict(state: object | None) -> StorageVerdict:
  87. """Same question, asked of the last dispatch seen whenever that was.
  88. For reporting only -- the connection diagnostic is normally run after the
  89. print that prompted it, by which point the per-print URL has been cleared.
  90. Never gate an FTPS sweep on this: it may describe a different print.
  91. """
  92. return _verdict(getattr(state, "last_project_url", None), state)
  93. def _verdict(project_url: str | None, state: object | None) -> StorageVerdict:
  94. if state is None:
  95. return _REACHABLE
  96. # Strongest signal, and specific to the print in question: the dispatcher
  97. # named the destination.
  98. external = url_is_external_storage(project_url)
  99. if external is False:
  100. return StorageVerdict(reachable=False, reason=REASON_INTERNAL_STORAGE)
  101. if external is True:
  102. # It said external storage, so sweep even if the card flags disagree.
  103. # Trusting the specific claim over the general one is what keeps a
  104. # printer that misreports `sdcard` from losing archives that work
  105. # today -- a false skip is a regression, a needless sweep is only slow.
  106. return _REACHABLE
  107. # Model-independent fallback for printers whose broker refuses the request
  108. # topic, so we never see a `project_file` at all. An empty slot means FTPS
  109. # has nothing to serve from any path -- but only when the printer actually
  110. # said so. `sdcard` defaults to False, and acting on that default would
  111. # skip the sweep for every printer that simply doesn't publish the field.
  112. if getattr(state, "sdcard_reported", False) and not getattr(state, "sdcard", False):
  113. return StorageVerdict(reachable=False, reason=REASON_NO_EXTERNAL_STORAGE)
  114. return _REACHABLE