spoolman_tracking.py 60 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401
  1. """Spoolman per-filament usage tracking for active prints.
  2. Captures AMS tray state and G-code data at print start, then reports
  3. per-filament usage to the correct Spoolman spools at print completion.
  4. Supports accurate partial usage reporting for failed/cancelled prints.
  5. """
  6. import json
  7. import logging
  8. from sqlalchemy import delete, select
  9. from backend.app.core.config import settings as app_settings
  10. from backend.app.core.database import async_session
  11. from backend.app.services.spoolman import (
  12. SpoolmanClientError,
  13. SpoolmanNotFoundError,
  14. SpoolmanUnavailableError,
  15. get_spoolman_client,
  16. init_spoolman_client,
  17. )
  18. logger = logging.getLogger(__name__)
  19. # Zero UUID used by Bambu printers for empty/unset tray_uuid
  20. _ZERO_UUID = "00000000000000000000000000000000"
  21. _ZERO_TAG_UID = "0000000000000000"
  22. def _is_non_zero_identifier(value: str) -> bool:
  23. """Return True when identifier is non-empty and not all zeros."""
  24. if not value:
  25. return False
  26. return set(value) != {"0"}
  27. def _to_fixed_hex(value: int, width: int) -> str:
  28. """Mirror frontend toFixedHex(): uppercase, zero-padded, fixed width."""
  29. safe = max(0, int(value))
  30. return format(safe, "X").zfill(width)[-width:]
  31. def _hash_serial_to_hex32(serial: str) -> str:
  32. """Mirror frontend hashSerialToHex32() exactly (32-bit FNV-1a)."""
  33. input_str = (serial or "").strip().upper()
  34. hash_value = 0x811C9DC5
  35. for char in input_str:
  36. hash_value ^= ord(char)
  37. hash_value = (hash_value * 0x01000193) & 0xFFFFFFFF
  38. return format(hash_value, "X").zfill(8)
  39. def _global_tray_id_to_ams_slot(global_tray_id: int) -> tuple[int, int]:
  40. """Convert global tray id to (ams_id, tray_id) tuple for fallback tag generation."""
  41. # External spool slots use IDs 254/255 and map to ams_id=255 tray_id=0/1.
  42. if global_tray_id >= 254:
  43. return 255, max(0, global_tray_id - 254)
  44. # AMS-HT units are addressed by ams_id directly and have a single tray.
  45. if global_tray_id >= 128:
  46. return global_tray_id, 0
  47. # Standard AMS units: four trays each.
  48. return global_tray_id // 4, global_tray_id % 4
  49. def _get_fallback_spool_tag(printer_serial: str, global_tray_id: int) -> str:
  50. """Mirror frontend getFallbackSpoolTag(serial, amsId, trayId) exactly."""
  51. if not printer_serial:
  52. return ""
  53. ams_id, tray_id = _global_tray_id_to_ams_slot(global_tray_id)
  54. return get_fallback_spool_tag_for_slot(printer_serial, ams_id, tray_id)
  55. def get_fallback_spool_tag_for_slot(printer_serial: str, ams_id: int, tray_id: int) -> str:
  56. """Public helper matching frontend getFallbackSpoolTag(serial, amsId, trayId).
  57. Used by stale-tag cleanup (#1457) to detect Spoolman spools still holding
  58. this slot's deterministic fallback tag in extra.tag.
  59. """
  60. if not printer_serial:
  61. return ""
  62. return f"{_hash_serial_to_hex32(printer_serial)}{_to_fixed_hex(ams_id, 4)}{_to_fixed_hex(tray_id, 4)}"
  63. def _resolve_spool_tag(tray_info: dict, printer_serial: str = "", global_tray_id: int | None = None) -> str:
  64. """Get the best spool identifier from tray info (prefer tray_uuid over tag_uid).
  65. Returns empty string if no usable identifier is found.
  66. """
  67. tray_uuid = str(tray_info.get("tray_uuid", "") or "")
  68. tag_uid = str(tray_info.get("tag_uid", "") or "")
  69. if tray_uuid and tray_uuid != _ZERO_UUID and _is_non_zero_identifier(tray_uuid):
  70. return tray_uuid
  71. if tag_uid and tag_uid != _ZERO_TAG_UID and _is_non_zero_identifier(tag_uid):
  72. return tag_uid
  73. if global_tray_id is not None:
  74. return _get_fallback_spool_tag(printer_serial, global_tray_id)
  75. return ""
  76. async def _get_printer_serial(printer_id: int) -> str:
  77. """Get printer serial for deterministic fallback tag generation."""
  78. from backend.app.models.printer import Printer
  79. from backend.app.services.printer_manager import printer_manager
  80. printer_info = printer_manager.get_printer(printer_id)
  81. if printer_info and printer_info.serial_number:
  82. return printer_info.serial_number
  83. async with async_session() as db:
  84. result = await db.execute(select(Printer.serial_number).where(Printer.id == printer_id))
  85. serial_number = result.scalar_one_or_none()
  86. return serial_number or ""
  87. def _resolve_global_tray_id(slot_id: int, slot_to_tray: list | None, ams_trays: dict | None = None) -> int:
  88. """Map a 1-based slot_id to a global_tray_id using optional custom mapping.
  89. Custom mapping: slot_to_tray[slot_id - 1] is used when >= 0.
  90. A value of -1 in the custom mapping means the slicer routed this slot to
  91. the external spool. BambuStudio converts virtual tray IDs (254/255) to -1
  92. in the flat ams_mapping array before sending to the printer — see
  93. start_print() in bambu_mqtt.py which documents this convention. We mirror
  94. it here: when -1 is seen, look up the external spool's actual
  95. global_tray_id (254/255) in ams_trays rather than falling through to the
  96. position-based default (which would map slot_id=1 to the first AMS tray
  97. and credit an unrelated spool — see #1276, regression of #853).
  98. Position-based default: uses sorted ams_trays keys so external spools (ID 254/255)
  99. naturally follow standard AMS trays, matching the slicer's slot numbering.
  100. Final fallback: slot_id - 1 (legacy, works for pure AMS without external spools).
  101. """
  102. if slot_to_tray and slot_id <= len(slot_to_tray):
  103. mapped_tray = slot_to_tray[slot_id - 1]
  104. if mapped_tray >= 0:
  105. return mapped_tray
  106. if mapped_tray == -1 and ams_trays:
  107. # -1 means external spool. 254 = VIRTUAL_TRAY_DEPUTY_ID (main on
  108. # single-nozzle, left/deputy on H2D dual-nozzle); 255 =
  109. # VIRTUAL_TRAY_MAIN_ID. Prefer 254 when both exist since that's
  110. # what single-nozzle printers report via tray_now.
  111. for ext_id in (254, 255):
  112. if ext_id in ams_trays:
  113. return ext_id
  114. # Position-based default: sort available tray IDs so external spools (254/255)
  115. # come after standard AMS trays, matching the slicer's slot assignment order.
  116. if ams_trays:
  117. sorted_tray_ids = sorted(ams_trays.keys())
  118. if slot_id <= len(sorted_tray_ids):
  119. return sorted_tray_ids[slot_id - 1]
  120. return slot_id - 1
  121. def _resolve_slot_to_tray_fallback(printer_id: int, filament_usage: list[dict]) -> tuple[list[int] | None, str]:
  122. """Recover a slot-to-tray mapping at completion when print start captured none.
  123. ``store_print_data`` can only learn the mapping from two sources: the
  124. ``ams_mapping`` Bambuddy intercepts on the printer's local request topic, and
  125. a queue item's stored mapping. Neither exists for a print dispatched from
  126. Bambu Studio while the printer is cloud-bound — the command travels through
  127. Bambu's broker and never appears on the local topic we subscribe to. With
  128. ``slot_to_tray`` left NULL, ``_resolve_global_tray_id`` guesses by position:
  129. slicer slot 1 to the first loaded tray, slot 2 to the second, and so on. An
  130. AMS that isn't loaded in slicer order then charges every slot to the wrong
  131. spool, and the archive's filament is rewritten to match, so the print
  132. silently changes colour when it finishes (#2768).
  133. The printer knows the real answer. Its ``mapping`` field carries the actual
  134. slot-to-tray assignment for the running job, and for the models that never
  135. publish it (A1, P1S, P2S) the 3MF's per-slot colours can be matched against
  136. the loaded trays instead. The built-in inventory writer has consulted both
  137. for as long as it has resolved mappings at completion; this gives the
  138. Spoolman writer the same two fallbacks at the same moment.
  139. Deliberately at completion rather than inside ``store_print_data``: the
  140. printer keeps publishing ``mapping`` long after a job ends — it is still in
  141. the status payload while the printer sits idle — so reading it at print start
  142. risks stamping the *previous* job's mapping onto this one before the printer
  143. has pushed the update. At completion the field unambiguously describes the
  144. job that just ran.
  145. Args:
  146. printer_id: Printer whose live state is consulted.
  147. filament_usage: The 3MF's per-slot estimates, needed by the colour
  148. match. Only the ``slot_id``/``color`` keys are read.
  149. Returns:
  150. ``(mapping, source)``, or ``(None, "none")`` when neither fallback
  151. produced anything and the positional default stands.
  152. """
  153. from backend.app.services.printer_manager import printer_manager
  154. from backend.app.services.usage_tracker import _decode_mqtt_mapping, _match_slots_by_color
  155. state = printer_manager.get_status(printer_id)
  156. raw_data = getattr(state, "raw_data", None) if state else None
  157. if not raw_data:
  158. return None, "none"
  159. decoded = _decode_mqtt_mapping(raw_data.get("mapping"))
  160. if decoded:
  161. return decoded, "mqtt"
  162. matched = _match_slots_by_color(filament_usage, raw_data.get("ams"))
  163. if matched:
  164. return matched, "color_match"
  165. return None, "none"
  166. def build_ams_tray_lookup(raw_data: dict) -> dict[int, dict]:
  167. """Build lookup of global_tray_id -> tray info from printer state.
  168. Returns: {0: {"tray_uuid": "...", "tag_uid": "...", "tray_type": "..."}, ...}
  169. """
  170. lookup = {}
  171. ams_data = raw_data.get("ams", [])
  172. for ams_unit in ams_data:
  173. ams_id = int(ams_unit.get("id", 0))
  174. for tray in ams_unit.get("tray", []):
  175. tray_id = int(tray.get("id", 0))
  176. # AMS-HT units have IDs starting at 128 with a single tray
  177. global_tray_id = ams_id if ams_id >= 128 else ams_id * 4 + tray_id
  178. lookup[global_tray_id] = {
  179. "tray_uuid": tray.get("tray_uuid", ""),
  180. "tag_uid": tray.get("tag_uid", ""),
  181. "tray_type": tray.get("tray_type", ""),
  182. }
  183. # External spool(s) (vt_tray is a list, global_tray_id from each entry's "id")
  184. for vt in raw_data.get("vt_tray") or []:
  185. if vt.get("tray_type"):
  186. tray_id = int(vt.get("id", 254))
  187. lookup[tray_id] = {
  188. "tray_uuid": vt.get("tray_uuid", ""),
  189. "tag_uid": vt.get("tag_uid", ""),
  190. "tray_type": vt.get("tray_type", ""),
  191. }
  192. return lookup
  193. def _snapshot_tray_remain(raw_data: dict) -> dict[str, dict]:
  194. """Capture per-slot ``remain%`` + ``tray_uuid`` at print start so the
  195. completion path can compute a remain-delta when 3MF data doesn't cover
  196. the slot (or there's no 3MF at all — #1820).
  197. Returns ``{"<ams_id>-<tray_id>": {"remain": int, "tray_uuid": str}}``.
  198. Only slots whose ``remain`` is a valid 0..100 int are included; invalid
  199. values mean the AMS hasn't read the spool yet and a delta would be
  200. meaningless. Mirrors the gate in
  201. ``usage_tracker.on_print_start:309``.
  202. """
  203. snapshot: dict[str, dict] = {}
  204. ams_raw = raw_data.get("ams", [])
  205. ams_data = ams_raw.get("ams", []) if isinstance(ams_raw, dict) else ams_raw if isinstance(ams_raw, list) else []
  206. for ams_unit in ams_data:
  207. if not isinstance(ams_unit, dict):
  208. continue
  209. ams_id = int(ams_unit.get("id", 0))
  210. for tray in ams_unit.get("tray", []):
  211. if not isinstance(tray, dict):
  212. continue
  213. tray_id = int(tray.get("id", 0))
  214. remain = tray.get("remain", -1)
  215. if isinstance(remain, int) and 0 <= remain <= 100:
  216. snapshot[f"{ams_id}-{tray_id}"] = {
  217. "remain": remain,
  218. "tray_uuid": tray.get("tray_uuid", "") or "",
  219. }
  220. vt_tray_raw = raw_data.get("vt_tray") or []
  221. if isinstance(vt_tray_raw, dict):
  222. vt_tray_raw = [vt_tray_raw]
  223. for vt in vt_tray_raw:
  224. if not isinstance(vt, dict):
  225. continue
  226. vt_id = int(vt.get("id", 254))
  227. # 254 → (255, 0), 255 → (255, 1) — matches usage_tracker's encoding.
  228. vt_tray_id = vt_id - 254
  229. remain = vt.get("remain", -1)
  230. if isinstance(remain, int) and 0 <= remain <= 100:
  231. snapshot[f"255-{vt_tray_id}"] = {
  232. "remain": remain,
  233. "tray_uuid": vt.get("tray_uuid", "") or "",
  234. }
  235. return snapshot
  236. async def store_print_data(
  237. printer_id: int,
  238. archive_id: int,
  239. file_path: str,
  240. db,
  241. printer_manager,
  242. ams_mapping: list[int] | None = None,
  243. plate_id: int | None = None,
  244. ):
  245. """Store Spoolman tracking data at print start (persisted to database).
  246. Per-print tracking is the primary weight-update path for Spoolman, mirroring
  247. how the internal Filament Inventory works. The legacy AMS-remain%-based sync
  248. is no longer used as a weight writer (#1119), so this runs whenever Spoolman
  249. is enabled regardless of the deprecated `spoolman_disable_weight_sync` flag.
  250. ``plate_id``, when set, scopes the 3MF filament extract to a single plate so
  251. queue / direct-Print dispatch of plate N of a multi-plate file doesn't
  252. attribute every plate's filament to the printed spool (#1697). When unset,
  253. the queue item's plate_id (if any) is used; otherwise the whole-file sum is
  254. extracted, which is correct for direct prints that target the first/only
  255. plate of a single-plate file.
  256. """
  257. from backend.app.api.routes.settings import get_setting
  258. from backend.app.models.active_print_spoolman import ActivePrintSpoolman
  259. from backend.app.models.print_queue import PrintQueueItem
  260. from backend.app.utils.threemf_tools import (
  261. extract_filament_properties_from_3mf,
  262. extract_filament_usage_from_3mf,
  263. extract_layer_filament_usage_from_3mf,
  264. )
  265. # Check if Spoolman is enabled
  266. spoolman_enabled = await get_setting(db, "spoolman_enabled")
  267. if not spoolman_enabled or spoolman_enabled.lower() != "true":
  268. return
  269. # Get current AMS tray state up front — needed both for the 3MF path's
  270. # ams_trays field and for the remain%-delta snapshot (#1820 fallback for
  271. # no-3MF "Untitled" prints, mirroring usage_tracker.on_print_start).
  272. state = printer_manager.get_status(printer_id)
  273. ams_trays: dict[int, dict] = {}
  274. tray_remain_start: dict[str, dict] = {}
  275. if state and state.raw_data:
  276. ams_trays = build_ams_tray_lookup(state.raw_data)
  277. tray_remain_start = _snapshot_tray_remain(state.raw_data)
  278. # Try to read per-slot filament estimates from the 3MF. Two paths can
  279. # leave ``filament_usage`` empty: (1) fallback archive (no .gcode.3mf
  280. # was downloadable from the printer — "Untitled" prints, see #1820),
  281. # (2) 3MF present but slice_info missing per-filament estimates.
  282. # Both fall through to the remain%-delta path at completion.
  283. filament_usage: list | None = None
  284. layer_usage_json: dict | None = None
  285. filament_properties: dict | None = None
  286. full_path = (
  287. app_settings.base_dir / file_path
  288. ) # SEC-PATH-OK: file_path is archive.file_path / library_file.file_path — DB-stored, internally generated
  289. threemf_available = bool(file_path) and full_path.exists()
  290. queue_item = None
  291. if threemf_available:
  292. # Resolve the queue item once — used both for the plate-scoped 3MF parsing
  293. # fallback (#1697: multi-plate file dispatched for one plate must only count
  294. # that plate's filament) and for the ams_mapping fallback below.
  295. queue_result = await db.execute(
  296. select(PrintQueueItem)
  297. .where(PrintQueueItem.archive_id == archive_id)
  298. .where(PrintQueueItem.status == "printing")
  299. )
  300. queue_item = queue_result.scalar_one_or_none()
  301. # Caller-supplied plate_id wins (direct-Print path); fall back to the queue
  302. # item's plate_id (queue dispatch path).
  303. effective_plate_id = (
  304. plate_id if plate_id is not None else (queue_item.plate_id if queue_item is not None else None)
  305. )
  306. filament_usage = extract_filament_usage_from_3mf(full_path, effective_plate_id) or None
  307. layer_usage = extract_layer_filament_usage_from_3mf(full_path)
  308. if layer_usage:
  309. # Convert int keys to string for JSON serialization
  310. layer_usage_json = {str(k): v for k, v in layer_usage.items()}
  311. logger.debug("[SPOOLMAN] Parsed %s layers from G-code", len(layer_usage))
  312. filament_properties = extract_filament_properties_from_3mf(full_path)
  313. else:
  314. # No 3MF on disk — common for "Untitled" prints whose .gcode.3mf
  315. # was never on the printer's FTP. Logged at debug since the
  316. # fallback path below picks up the slack when remain% is available.
  317. logger.debug("[SPOOLMAN] 3MF file not available: %s", full_path)
  318. # If neither path has anything useful, there's nothing to track.
  319. if not filament_usage and not tray_remain_start:
  320. if threemf_available:
  321. logger.debug("[SPOOLMAN] No filament usage data in 3MF for archive %s", archive_id)
  322. return
  323. # Prefer the explicit mapping captured from the print command, then fall back
  324. # to any queue mapping stored for scheduled/reprint jobs.
  325. slot_to_tray = ams_mapping if ams_mapping is not None else None
  326. mapping_source = "print_cmd" if slot_to_tray else None
  327. if not slot_to_tray and queue_item and queue_item.ams_mapping:
  328. try:
  329. slot_to_tray = json.loads(queue_item.ams_mapping)
  330. mapping_source = "queue"
  331. except json.JSONDecodeError:
  332. pass # Ignore malformed AMS mapping; fall back to default slot assignment
  333. # Delete any existing row for this printer/archive (shouldn't exist, but just in case)
  334. await db.execute(
  335. delete(ActivePrintSpoolman)
  336. .where(ActivePrintSpoolman.printer_id == printer_id)
  337. .where(ActivePrintSpoolman.archive_id == archive_id)
  338. )
  339. # Insert new tracking data. ``filament_usage`` may be None for the
  340. # no-3MF case; report_usage falls back to ``tray_remain_start``.
  341. tracking = ActivePrintSpoolman(
  342. printer_id=printer_id,
  343. archive_id=archive_id,
  344. filament_usage=filament_usage,
  345. ams_trays=ams_trays,
  346. slot_to_tray=slot_to_tray,
  347. layer_usage=layer_usage_json,
  348. filament_properties=filament_properties,
  349. tray_remain_start=tray_remain_start or None,
  350. )
  351. db.add(tracking)
  352. await db.commit()
  353. logger.info(
  354. "[SPOOLMAN] Stored tracking data for print: printer=%s, archive=%s (3mf=%s, remain_snapshot=%d slot(s))",
  355. printer_id,
  356. archive_id,
  357. "yes" if filament_usage else "no",
  358. len(tray_remain_start),
  359. )
  360. logger.debug("[SPOOLMAN] Filament usage: %s", filament_usage)
  361. logger.debug("[SPOOLMAN] AMS trays: %s", list(ams_trays.keys()))
  362. # Logged at info even when there is no mapping: "source: none" here is the
  363. # signal that completion will have to fall back, which is the single most
  364. # useful line in the log when a print is charged to the wrong spool (#2768).
  365. logger.info(
  366. "[SPOOLMAN] Print start: archive %s slot_to_tray=%s (source: %s)",
  367. archive_id,
  368. slot_to_tray,
  369. mapping_source or "none",
  370. )
  371. if layer_usage_json:
  372. logger.debug("[SPOOLMAN] Layer usage data available for partial tracking")
  373. async def cleanup_tracking(
  374. printer_id: int,
  375. archive_id: int,
  376. db,
  377. last_layer_num: int | None = None,
  378. last_progress: int | None = None,
  379. ):
  380. """Report partial usage and clean up Spoolman tracking data for failed/aborted prints."""
  381. from backend.app.models.active_print_spoolman import ActivePrintSpoolman
  382. # Get tracking data first (needed for partial usage reporting)
  383. result = await db.execute(
  384. select(ActivePrintSpoolman)
  385. .where(ActivePrintSpoolman.printer_id == printer_id)
  386. .where(ActivePrintSpoolman.archive_id == archive_id)
  387. )
  388. tracking = result.scalar_one_or_none()
  389. if not tracking:
  390. logger.debug("[SPOOLMAN] No tracking data to clean up for printer=%s, archive=%s", printer_id, archive_id)
  391. return
  392. # Try to report partial usage before cleanup
  393. try:
  394. await _report_partial_usage(
  395. printer_id,
  396. tracking,
  397. last_layer_num=last_layer_num,
  398. last_progress=last_progress,
  399. )
  400. except Exception as e:
  401. logger.warning("[SPOOLMAN] Partial usage report failed: %s", e)
  402. # Delete tracking data
  403. await db.execute(
  404. delete(ActivePrintSpoolman)
  405. .where(ActivePrintSpoolman.printer_id == printer_id)
  406. .where(ActivePrintSpoolman.archive_id == archive_id)
  407. )
  408. await db.commit()
  409. logger.debug("[SPOOLMAN] Cleaned up tracking data for printer=%s, archive=%s", printer_id, archive_id)
  410. async def _get_spoolman_client_with_fallback():
  411. """Get Spoolman client, initializing from settings if needed.
  412. Returns (client, is_healthy) tuple. Client may be None.
  413. """
  414. client = await get_spoolman_client()
  415. if not client:
  416. async with async_session() as db:
  417. from backend.app.api.routes.settings import get_setting
  418. spoolman_url = await get_setting(db, "spoolman_url")
  419. if spoolman_url:
  420. try:
  421. client = await init_spoolman_client(spoolman_url)
  422. except ValueError as exc:
  423. logger.warning("Spoolman URL %r rejected by SSRF guard: %s", spoolman_url, exc)
  424. return None
  425. if not client:
  426. return None
  427. if not await client.health_check():
  428. logger.warning("Spoolman health check failed; skipping usage reporting")
  429. return None
  430. return client
  431. async def _resolve_spool_id_via_slot_assignment(printer_id: int, ams_id: int, tray_id: int) -> int | None:
  432. """Look up the Spoolman spool ID locally bound to (printer, ams, tray).
  433. Fallback path for #1459: when a tag-less spool was assigned via the
  434. Bambuddy UI, the user's deterministic fallback tag is intentionally NOT
  435. written to Spoolman's extra.tag (kept clean per #1457), so
  436. find_spool_by_tag misses. The local spoolman_slot_assignments table is
  437. the authoritative binding for those spools.
  438. """
  439. from backend.app.models.spoolman_slot_assignment import SpoolmanSlotAssignment
  440. async with async_session() as db:
  441. result = await db.execute(
  442. select(SpoolmanSlotAssignment.spoolman_spool_id).where(
  443. SpoolmanSlotAssignment.printer_id == printer_id,
  444. SpoolmanSlotAssignment.ams_id == ams_id,
  445. SpoolmanSlotAssignment.tray_id == tray_id,
  446. )
  447. )
  448. return result.scalar_one_or_none()
  449. async def _report_spool_usage_for_slots(
  450. client,
  451. filament_usage_items: list[tuple[int, float]],
  452. ams_trays: dict[int, dict],
  453. slot_to_tray: list | None,
  454. method_label: str,
  455. printer_serial: str = "",
  456. printer_id: int | None = None,
  457. slot_colors_out: dict[int, str] | None = None,
  458. slot_materials_out: dict[int, str] | None = None,
  459. ) -> int:
  460. """Report usage to Spoolman for a list of (slot_id, grams) pairs.
  461. Resolution order per slot: (1) Spoolman extra.tag match against the
  462. tray's RFID or deterministic fallback tag, (2) #1459 fallback —
  463. local spoolman_slot_assignments table keyed by (printer_id, ams_id,
  464. tray_id). Without (2), tag-less spools assigned via the Bambuddy UI
  465. never get their weight decremented because their extra.tag is empty
  466. on the Spoolman side.
  467. When ``slot_colors_out`` is provided it is populated with
  468. ``{slot_id: color_hex}`` for every resolved spool — used by
  469. :func:`report_usage` to stamp the archive's filament colour from the
  470. Spoolman spool rather than the slicer's 3MF value (#1494).
  471. Returns number of spools successfully updated.
  472. """
  473. spools_updated = 0
  474. for slot_id, grams_used in filament_usage_items:
  475. if grams_used <= 0:
  476. continue
  477. global_tray_id = _resolve_global_tray_id(slot_id, slot_to_tray, ams_trays)
  478. tray_info = ams_trays.get(global_tray_id)
  479. if not tray_info:
  480. logger.debug("[SPOOLMAN] Slot %s: no tray at global_tray_id %s", slot_id, global_tray_id)
  481. continue
  482. is_external = global_tray_id >= 254
  483. tray_type = tray_info.get("tray_type", "")
  484. logger.debug(
  485. "[SPOOLMAN] Slot %s resolved to global_tray_id %s (tray_type=%s, external=%s)",
  486. slot_id,
  487. global_tray_id,
  488. tray_type or "unknown",
  489. is_external,
  490. )
  491. spool_id_to_use: int | None = None
  492. resolution_path = ""
  493. # color_hex + material of the resolved spool's filament, for the #1494
  494. # archive colour rewrite and the #2563 type rewrite. The tag path
  495. # already has the full spool object; the slot-assignment path only
  496. # yields an id and is fetched below.
  497. spool_color_hex: str | None = None
  498. spool_material: str | None = None
  499. spool_tag = _resolve_spool_tag(tray_info, printer_serial, global_tray_id)
  500. if spool_tag:
  501. spool = await client.find_spool_by_tag(spool_tag)
  502. if spool:
  503. spool_id_to_use = spool["id"]
  504. resolution_path = "tag"
  505. spool_color_hex = (spool.get("filament") or {}).get("color_hex")
  506. spool_material = (spool.get("filament") or {}).get("material")
  507. if spool_id_to_use is None and printer_id is not None:
  508. ams_id, tray_id = _global_tray_id_to_ams_slot(global_tray_id)
  509. spool_id_to_use = await _resolve_spool_id_via_slot_assignment(printer_id, ams_id, tray_id)
  510. if spool_id_to_use is not None:
  511. resolution_path = "slot-assignment"
  512. if spool_id_to_use is None:
  513. logger.debug(
  514. "[SPOOLMAN] Slot %s: no spool resolved (tag=%s, no slot-assignment)",
  515. slot_id,
  516. spool_tag[:16] if spool_tag else "none",
  517. )
  518. continue
  519. # Record the spool's filament colour + material for the archive
  520. # rewrites (#1494, #2563). The slot-assignment path resolved only an
  521. # id, so fetch the spool once for whichever value is still missing.
  522. # Strictly best-effort: a fetch failure must never abort the weight
  523. # reporting for the remaining slots, so the catch is broad.
  524. if slot_colors_out is not None or slot_materials_out is not None:
  525. need_color = slot_colors_out is not None and spool_color_hex is None
  526. need_material = slot_materials_out is not None and spool_material is None
  527. if need_color or need_material:
  528. try:
  529. _fil = (await client.get_spool(spool_id_to_use)).get("filament") or {}
  530. if need_color:
  531. spool_color_hex = _fil.get("color_hex")
  532. if need_material:
  533. spool_material = _fil.get("material")
  534. except Exception as exc: # noqa: BLE001 — colour/material are non-critical
  535. logger.debug("[SPOOLMAN] Slot %s: could not fetch spool filament: %s", slot_id, exc)
  536. if slot_colors_out is not None and spool_color_hex:
  537. slot_colors_out[slot_id] = spool_color_hex
  538. if slot_materials_out is not None and spool_material:
  539. slot_materials_out[slot_id] = spool_material
  540. try:
  541. await client.use_spool(spool_id_to_use, grams_used)
  542. logger.info(
  543. "[SPOOLMAN] %s: slot %s: %sg -> spool %s (via %s)",
  544. method_label,
  545. slot_id,
  546. grams_used,
  547. spool_id_to_use,
  548. resolution_path,
  549. )
  550. spools_updated += 1
  551. except (SpoolmanNotFoundError, SpoolmanClientError, SpoolmanUnavailableError) as exc:
  552. logger.warning("[SPOOLMAN] Failed to record usage for spool %s: %s", spool_id_to_use, exc)
  553. return spools_updated
  554. async def _report_spool_usage_split_by_tray_changes(
  555. client,
  556. filament_usage: list[dict],
  557. tray_changes: list[tuple[int, int]],
  558. ams_trays: dict[int, dict],
  559. layer_usage: dict[int, dict[int, float]] | None,
  560. filament_properties: dict | None,
  561. total_layers: int,
  562. last_layer_num: int,
  563. method_label: str,
  564. printer_serial: str,
  565. printer_id: int,
  566. slot_colors_out: dict[int, str] | None = None,
  567. slot_materials_out: dict[int, str] | None = None,
  568. ) -> tuple[int, set[int]]:
  569. """Split each slot's grams across ``tray_changes`` and charge per-segment.
  570. Mirrors ``usage_tracker`` Path 1's tray-switch branch so Spoolman and
  571. the internal Spool inventory attribute mid-print AMS-backup switches
  572. identically (#1793 — reporter's origin spool was over-charged the
  573. whole print because this path didn't exist). ``compute_tray_split_grams``
  574. holds the shared segment-math; this function wraps the per-segment
  575. spool resolution + ``use_spool`` sink for the Spoolman side.
  576. Returns ``(spools_updated, handled_global_tray_ids)`` — the caller
  577. passes ``handled_global_tray_ids`` into the remain-delta fallback so
  578. a tray attributed here is not double-charged there.
  579. """
  580. from backend.app.utils.tray_split import compute_tray_split_grams
  581. spools_updated = 0
  582. handled_global_tray_ids: set[int] = set()
  583. for usage in filament_usage:
  584. slot_id = usage.get("slot_id", 0)
  585. total_weight = usage.get("used_g", 0)
  586. if total_weight <= 0 or slot_id <= 0:
  587. continue
  588. props = (filament_properties or {}).get(str(slot_id)) or (filament_properties or {}).get(slot_id) or {}
  589. segments = compute_tray_split_grams(
  590. tray_changes=tray_changes,
  591. total_weight=float(total_weight),
  592. slot_id=slot_id,
  593. layer_usage=layer_usage,
  594. density=float(props.get("density", 1.24)),
  595. diameter=float(props.get("diameter", 1.75)),
  596. total_layers=total_layers,
  597. last_layer_num=last_layer_num,
  598. )
  599. for seg_idx, tray_global, segment_grams in segments:
  600. if segment_grams <= 0:
  601. continue
  602. # Mark this tray as handled BEFORE the resolution attempt so
  603. # remain-delta doesn't double-charge it, even if we fail to
  604. # find a spool below. Matches usage_tracker behaviour: the
  605. # tray was physically fed from during this print, whether or
  606. # not Spoolman happens to have a matching row.
  607. handled_global_tray_ids.add(tray_global)
  608. tray_info = ams_trays.get(tray_global) or {}
  609. spool_id_to_use: int | None = None
  610. resolution_path = ""
  611. spool_color_hex: str | None = None
  612. spool_material: str | None = None
  613. spool_tag = _resolve_spool_tag(tray_info, printer_serial, tray_global) if tray_info else ""
  614. if spool_tag:
  615. spool = await client.find_spool_by_tag(spool_tag)
  616. if spool:
  617. spool_id_to_use = spool["id"]
  618. resolution_path = "tag"
  619. spool_color_hex = (spool.get("filament") or {}).get("color_hex")
  620. spool_material = (spool.get("filament") or {}).get("material")
  621. if spool_id_to_use is None:
  622. seg_ams_id, seg_tray_id = _global_tray_id_to_ams_slot(tray_global)
  623. spool_id_to_use = await _resolve_spool_id_via_slot_assignment(printer_id, seg_ams_id, seg_tray_id)
  624. if spool_id_to_use is not None:
  625. resolution_path = "slot-assignment"
  626. if spool_id_to_use is None:
  627. logger.info(
  628. "[SPOOLMAN] Split slot %s seg %s tray=%d: no spool resolved — %.2fg lost from split accounting",
  629. slot_id,
  630. seg_idx,
  631. tray_global,
  632. segment_grams,
  633. )
  634. continue
  635. # Colour (#1494) + material (#2563) rewrite — first segment for a
  636. # slot wins. The UI displays a single colour/type per slot, so
  637. # later segments on the same slot don't overwrite (a backup swap
  638. # can differ but the archive card stays consistent with the origin).
  639. need_color = slot_colors_out is not None and slot_id not in slot_colors_out and spool_color_hex is None
  640. need_material = (
  641. slot_materials_out is not None and slot_id not in slot_materials_out and spool_material is None
  642. )
  643. if need_color or need_material:
  644. try:
  645. _fil = (await client.get_spool(spool_id_to_use)).get("filament") or {}
  646. if need_color:
  647. spool_color_hex = _fil.get("color_hex")
  648. if need_material:
  649. spool_material = _fil.get("material")
  650. except Exception as exc: # noqa: BLE001 — colour/material are non-critical
  651. logger.debug("[SPOOLMAN] Split slot %s: could not fetch spool filament: %s", slot_id, exc)
  652. if slot_colors_out is not None and slot_id not in slot_colors_out and spool_color_hex:
  653. slot_colors_out[slot_id] = spool_color_hex
  654. if slot_materials_out is not None and slot_id not in slot_materials_out and spool_material:
  655. slot_materials_out[slot_id] = spool_material
  656. try:
  657. await client.use_spool(spool_id_to_use, round(segment_grams, 2))
  658. logger.info(
  659. "[SPOOLMAN] %s: slot %s seg %s tray=%d: %.2fg -> spool %s (via %s)",
  660. method_label,
  661. slot_id,
  662. seg_idx,
  663. tray_global,
  664. segment_grams,
  665. spool_id_to_use,
  666. resolution_path,
  667. )
  668. spools_updated += 1
  669. except (SpoolmanNotFoundError, SpoolmanClientError, SpoolmanUnavailableError) as exc:
  670. logger.warning(
  671. "[SPOOLMAN] Split slot %s seg %s: failed to record usage for spool %s: %s",
  672. slot_id,
  673. seg_idx,
  674. spool_id_to_use,
  675. exc,
  676. )
  677. return spools_updated, handled_global_tray_ids
  678. async def _report_partial_usage(
  679. printer_id: int,
  680. tracking,
  681. last_layer_num: int | None = None,
  682. last_progress: int | None = None,
  683. ):
  684. """Report partial filament usage based on actual G-code layer data.
  685. Uses per-layer cumulative extrusion from G-code parsing for accurate
  686. multi-material tracking. Falls back to linear interpolation if G-code
  687. data is unavailable.
  688. """
  689. from backend.app.services.printer_manager import printer_manager
  690. from backend.app.utils.threemf_tools import get_cumulative_usage_at_layer, mm_to_grams
  691. async with async_session() as db:
  692. from backend.app.api.routes.settings import get_setting
  693. # Check if partial usage reporting is enabled (default: true)
  694. report_partial = await get_setting(db, "spoolman_report_partial_usage")
  695. if report_partial and report_partial.lower() == "false":
  696. logger.debug("[SPOOLMAN] Partial usage reporting disabled by setting")
  697. return
  698. # Check if Spoolman is enabled
  699. spoolman_enabled = await get_setting(db, "spoolman_enabled")
  700. if not spoolman_enabled or spoolman_enabled.lower() != "true":
  701. return
  702. # Get current printer state for layer progress.
  703. # On failed/aborted prints the firmware may already reset to IDLE with layer=0,
  704. # so we fall back to completion-time hints captured from MQTT.
  705. state = printer_manager.get_status(printer_id)
  706. current_layer = state.layer_num if state else None
  707. total_layers = state.total_layers if state else None
  708. if (not current_layer or current_layer <= 0) and last_layer_num and last_layer_num > 0:
  709. current_layer = last_layer_num
  710. logger.debug("[SPOOLMAN] Using captured last_layer_num=%s for partial usage", current_layer)
  711. progress_ratio_from_event = None
  712. if last_progress is not None:
  713. try:
  714. progress_ratio_from_event = min(max(float(last_progress), 0.0), 100.0) / 100.0
  715. except (TypeError, ValueError):
  716. progress_ratio_from_event = None
  717. if (not current_layer or current_layer <= 0) and progress_ratio_from_event and total_layers and total_layers > 0:
  718. current_layer = max(1, int(round(total_layers * progress_ratio_from_event)))
  719. logger.debug(
  720. "[SPOOLMAN] Estimated layer from last_progress=%s%% and total_layers=%s -> %s",
  721. last_progress,
  722. total_layers,
  723. current_layer,
  724. )
  725. if not current_layer or current_layer <= 0:
  726. logger.debug(
  727. "[SPOOLMAN] No progress to report (layer 0/unknown, last_layer_num=%s, last_progress=%s)",
  728. last_layer_num,
  729. last_progress,
  730. )
  731. return
  732. logger.info("[SPOOLMAN] Reporting partial usage at layer %s/%s", current_layer, total_layers or "?")
  733. # Get tracking data
  734. layer_usage = tracking.layer_usage
  735. filament_properties = tracking.filament_properties or {}
  736. filament_usage = tracking.filament_usage or []
  737. ams_trays = {int(k): v for k, v in (tracking.ams_trays or {}).items()}
  738. slot_to_tray = tracking.slot_to_tray
  739. tray_remain_start = tracking.tray_remain_start or {}
  740. printer_serial = await _get_printer_serial(printer_id)
  741. client = await _get_spoolman_client_with_fallback()
  742. if not client:
  743. logger.warning("[SPOOLMAN] Not reachable for partial usage reporting")
  744. return
  745. # No-3MF aborted print (#1820 mirror of the completion path): nothing in
  746. # filament_usage or layer_usage to base partial estimates on, but the
  747. # remain%-delta snapshot we captured at start still describes consumption
  748. # up to the abort moment. Write it the same way report_usage's fallback
  749. # does, then return — there's no 3MF-derived partial to layer on top.
  750. # ``state`` was already fetched at the top of the function for current_layer.
  751. if not filament_usage and not layer_usage and tray_remain_start:
  752. current_lookup = _snapshot_tray_remain(state.raw_data) if state and state.raw_data else {}
  753. await _report_remain_delta_for_slots(
  754. client,
  755. printer_id=printer_id,
  756. tray_remain_start=tray_remain_start,
  757. current_lookup=current_lookup,
  758. handled_global_tray_ids=set(),
  759. archive_id=getattr(tracking, "archive_id", -1),
  760. )
  761. return
  762. # Same recovery the completion path does, for the same reason: a print
  763. # dispatched from Studio over the cloud left print start with no mapping to
  764. # store, and both paths below feed ``slot_to_tray`` to
  765. # ``_resolve_global_tray_id`` (#2768). An aborted print charges the wrong
  766. # spool just as readily as a finished one.
  767. if not slot_to_tray:
  768. slot_to_tray, _partial_mapping_source = _resolve_slot_to_tray_fallback(printer_id, filament_usage)
  769. logger.info(
  770. "[SPOOLMAN] Partial usage: slot_to_tray=%s (source: %s)",
  771. slot_to_tray,
  772. _partial_mapping_source,
  773. )
  774. # Try to use accurate G-code parsed data
  775. if layer_usage:
  776. layer_usage_int = {
  777. int(layer): {int(fid): mm for fid, mm in filaments.items()} for layer, filaments in layer_usage.items()
  778. }
  779. usage_mm = get_cumulative_usage_at_layer(layer_usage_int, current_layer)
  780. if usage_mm:
  781. logger.info("[SPOOLMAN] Using G-code parsed data for layer %s", current_layer)
  782. # Build (slot_id, grams) list using Spoolman densities with 3MF fallback
  783. usage_items = []
  784. for filament_id, mm_used in usage_mm.items():
  785. slot_id = filament_id + 1 # filament_id is 0-based, slot_id is 1-based
  786. # Get density from Spoolman (most accurate), fall back to 3MF, then PLA default
  787. global_tray_id = _resolve_global_tray_id(slot_id, slot_to_tray, ams_trays)
  788. tray_info = ams_trays.get(global_tray_id)
  789. density = None
  790. diameter = 1.75
  791. if tray_info:
  792. spool_tag = _resolve_spool_tag(tray_info, printer_serial, global_tray_id)
  793. if spool_tag:
  794. spool = await client.find_spool_by_tag(spool_tag)
  795. if spool:
  796. filament_data = spool.get("filament", {})
  797. density = filament_data.get("density")
  798. diameter = filament_data.get("diameter", 1.75)
  799. if not density:
  800. props = filament_properties.get(str(slot_id), filament_properties.get(slot_id, {}))
  801. density = props.get("density", 1.24)
  802. logger.debug("[SPOOLMAN] Using fallback density %s for slot %s", density, slot_id)
  803. grams_used = round(mm_to_grams(mm_used, diameter, density), 2)
  804. usage_items.append((slot_id, grams_used))
  805. spools_updated = await _report_spool_usage_for_slots(
  806. client,
  807. usage_items,
  808. ams_trays,
  809. slot_to_tray,
  810. "Partial (G-code)",
  811. printer_serial,
  812. printer_id=printer_id,
  813. )
  814. if spools_updated > 0:
  815. logger.info("[SPOOLMAN] Reported partial usage to %s spool(s) using G-code data", spools_updated)
  816. return
  817. # Fallback: linear interpolation (if no G-code data available)
  818. progress_ratio = None
  819. if total_layers and total_layers > 0:
  820. progress_ratio = min(current_layer / total_layers, 1.0)
  821. elif progress_ratio_from_event is not None:
  822. progress_ratio = progress_ratio_from_event
  823. if progress_ratio is None:
  824. logger.debug(
  825. "[SPOOLMAN] Cannot use linear fallback: total_layers=%s, last_progress=%s",
  826. total_layers,
  827. last_progress,
  828. )
  829. return
  830. logger.info("[SPOOLMAN] Falling back to linear interpolation (%s)", progress_ratio)
  831. usage_items = []
  832. for usage in filament_usage:
  833. slot_id = usage.get("slot_id", 0)
  834. total_used_g = usage.get("used_g", 0)
  835. if total_used_g > 0:
  836. partial_used_g = round(total_used_g * progress_ratio, 2)
  837. usage_items.append((slot_id, partial_used_g))
  838. spools_updated = await _report_spool_usage_for_slots(
  839. client,
  840. usage_items,
  841. ams_trays,
  842. slot_to_tray,
  843. "Partial (linear)",
  844. printer_serial,
  845. printer_id=printer_id,
  846. )
  847. if spools_updated > 0:
  848. logger.info("[SPOOLMAN] Reported partial usage to %s spool(s) using linear interpolation", spools_updated)
  849. async def report_usage(printer_id: int, archive_id: int):
  850. """Report filament usage to Spoolman after print completion.
  851. Two writers, mirroring the internal-inventory split in usage_tracker:
  852. 1. **3MF path (primary)** — per-filament slice estimates captured at
  853. print start drive a precise per-slot ``use_spool`` call.
  854. 2. **AMS remain%-delta (fallback)** — for slots the 3MF path didn't
  855. handle (including the no-3MF "Untitled" case from #1820): compute
  856. ``start_remain - current_remain``, multiply by the resolved
  857. Spoolman filament's reference weight, and write the delta. Mirrors
  858. ``usage_tracker.on_print_complete`` Path 2 (line 517).
  859. """
  860. async with async_session() as db:
  861. from backend.app.api.routes.settings import get_setting
  862. from backend.app.models.active_print_spoolman import ActivePrintSpoolman
  863. # Get tracking data stored at print start
  864. result = await db.execute(
  865. select(ActivePrintSpoolman)
  866. .where(ActivePrintSpoolman.printer_id == printer_id)
  867. .where(ActivePrintSpoolman.archive_id == archive_id)
  868. )
  869. tracking = result.scalar_one_or_none()
  870. if not tracking:
  871. logger.info("[SPOOLMAN] No tracking data for print (printer=%s, archive=%s)", printer_id, archive_id)
  872. return
  873. filament_usage = tracking.filament_usage or []
  874. ams_trays = {int(k): v for k, v in (tracking.ams_trays or {}).items()}
  875. slot_to_tray = tracking.slot_to_tray
  876. tray_remain_start = tracking.tray_remain_start or {}
  877. # ``layer_usage`` and ``filament_properties`` were added later than
  878. # the base tracking fields; use ``getattr`` so tests that stub
  879. # ``tracking`` as a lightweight SimpleNamespace stay valid, and
  880. # historic ORM rows loaded without these columns can't AttributeError
  881. # on read.
  882. layer_usage_raw = getattr(tracking, "layer_usage", None) or {}
  883. filament_properties = getattr(tracking, "filament_properties", None) or {}
  884. printer_serial = await _get_printer_serial(printer_id)
  885. # Delete tracking row (we're done with it)
  886. await db.delete(tracking)
  887. await db.commit()
  888. if not filament_usage and not tray_remain_start:
  889. logger.debug("[SPOOLMAN] No usage data or remain-snapshot for archive %s", archive_id)
  890. return
  891. # Check if Spoolman is enabled
  892. spoolman_enabled = await get_setting(db, "spoolman_enabled")
  893. if not spoolman_enabled or spoolman_enabled.lower() != "true":
  894. return
  895. client = await _get_spoolman_client_with_fallback()
  896. if not client:
  897. logger.warning("[SPOOLMAN] Not reachable for usage reporting")
  898. return
  899. # Consult the live printer state for the tray-change log written by
  900. # ``bambu_mqtt.py`` on every mid-print ``tray_now`` change (#957).
  901. # When there's more than one entry, the print traversed >1 AMS tray
  902. # and the split path attributes each segment to the tray that was
  903. # loaded at the time — matches the internal Spool inventory writer
  904. # in ``usage_tracker.py``. Without this, an AMS-backup runout switch
  905. # charges the whole slot to the origin spool and pushes it past
  906. # ``initial_weight`` (#1793).
  907. #
  908. # Split only for SINGLE-slot prints — same gate as
  909. # ``usage_tracker.py:1002``. Multi-slot (multi-colour) prints
  910. # naturally cycle trays for every colour change, so splitting each
  911. # slot's grams across ALL tray_change_log entries would attribute
  912. # slot 1's grams to the segments where slot 2's tray was loaded and
  913. # vice versa. Multi-slot prints fall through to the existing
  914. # single-tray path (which uses the stable ``slot_to_tray`` mapping).
  915. nonzero_slots = [u for u in filament_usage if u.get("used_g", 0) > 0]
  916. tray_changes: list[tuple[int, int]] = []
  917. _state = None
  918. if len(nonzero_slots) == 1:
  919. from backend.app.services.printer_manager import printer_manager as _pm
  920. _state = _pm.get_status(printer_id)
  921. if _state is not None:
  922. tray_changes = list(getattr(_state, "tray_change_log", []) or [])
  923. _total_layers = int(getattr(_state, "total_layers", 0) or 0) if _state else 0
  924. _current_layer = int(getattr(_state, "layer_num", 0) or 0) if _state else 0
  925. # For the linear-fallback denominator when total_layers is 0 (P1S
  926. # firmware resets it at print end). At completion the current layer
  927. # is the print's last valid layer.
  928. _layer_denom_hint = _total_layers or _current_layer
  929. # Recover the mapping when print start had nothing to store — the
  930. # cloud-dispatched Studio print of #2768. Only the 3MF path consumes
  931. # ``slot_to_tray``; the remain-delta path below resolves spools from the
  932. # AMS slot directly, so there is nothing to recover for it.
  933. mapping_source = "stored" if slot_to_tray else "none"
  934. if filament_usage and not slot_to_tray:
  935. slot_to_tray, mapping_source = _resolve_slot_to_tray_fallback(printer_id, filament_usage)
  936. logger.info(
  937. "[SPOOLMAN] Archive %s: slot_to_tray=%s (source: %s)",
  938. archive_id,
  939. slot_to_tray,
  940. mapping_source,
  941. )
  942. slot_colors: dict[int, str] = {}
  943. slot_materials: dict[int, str] = {}
  944. handled_global_tray_ids: set[int] = set()
  945. spools_updated = 0
  946. # --- Path 1: 3MF per-slot estimates -----------------------------
  947. if filament_usage:
  948. if len(tray_changes) > 1:
  949. # Tray-split path — attribute per-segment to the tray that
  950. # was loaded at that time.
  951. logger.info(
  952. "[SPOOLMAN] Reporting per-filament usage for archive %s with tray-split "
  953. "(tray_change_log=%s, denom_layers=%d)",
  954. archive_id,
  955. tray_changes,
  956. _layer_denom_hint,
  957. )
  958. # ``tracking.layer_usage`` was serialized to JSON so int keys
  959. # come back as strings. Restore them for the split math.
  960. layer_usage = None
  961. if layer_usage_raw:
  962. try:
  963. layer_usage = {
  964. int(layer): {int(fid): mm for fid, mm in filaments.items()}
  965. for layer, filaments in layer_usage_raw.items()
  966. }
  967. except (TypeError, ValueError, AttributeError):
  968. # AttributeError catches ``inner.items()`` when the
  969. # inner value isn't dict-shaped (corrupt JSON row).
  970. # Missing gcode falls through to the linear-ratio
  971. # branch inside ``compute_tray_split_grams`` — still
  972. # gives a correct split, just less precise.
  973. layer_usage = None
  974. split_updated, split_handled = await _report_spool_usage_split_by_tray_changes(
  975. client,
  976. filament_usage,
  977. tray_changes,
  978. ams_trays,
  979. layer_usage,
  980. filament_properties,
  981. _total_layers,
  982. _layer_denom_hint,
  983. f"Archive {archive_id}",
  984. printer_serial,
  985. printer_id=printer_id,
  986. slot_colors_out=slot_colors,
  987. slot_materials_out=slot_materials,
  988. )
  989. spools_updated += split_updated
  990. handled_global_tray_ids |= split_handled
  991. else:
  992. logger.info("[SPOOLMAN] Reporting per-filament usage for archive %s", archive_id)
  993. usage_items = [(u.get("slot_id", 0), u.get("used_g", 0)) for u in filament_usage]
  994. spools_updated = await _report_spool_usage_for_slots(
  995. client,
  996. usage_items,
  997. ams_trays,
  998. slot_to_tray,
  999. f"Archive {archive_id}",
  1000. printer_serial,
  1001. printer_id=printer_id,
  1002. slot_colors_out=slot_colors,
  1003. slot_materials_out=slot_materials,
  1004. )
  1005. # Track which physical slots the 3MF path already covered so
  1006. # Path 2 doesn't double-charge them.
  1007. for u in filament_usage:
  1008. slot_id = u.get("slot_id", 0)
  1009. handled_global_tray_ids.add(_resolve_global_tray_id(slot_id, slot_to_tray, ams_trays))
  1010. # --- Path 2: AMS remain%-delta for slots 3MF didn't cover -------
  1011. # Triggered for no-3MF "Untitled" prints (#1820) AND for partial
  1012. # 3MF coverage (slots whose filament_id wasn't in slice_info).
  1013. if tray_remain_start:
  1014. from backend.app.services.printer_manager import printer_manager
  1015. current = printer_manager.get_status(printer_id)
  1016. current_lookup = _snapshot_tray_remain(current.raw_data) if current and current.raw_data else {}
  1017. fallback_updates = await _report_remain_delta_for_slots(
  1018. client,
  1019. printer_id=printer_id,
  1020. tray_remain_start=tray_remain_start,
  1021. current_lookup=current_lookup,
  1022. handled_global_tray_ids=handled_global_tray_ids,
  1023. archive_id=archive_id,
  1024. slot_colors_out=slot_colors,
  1025. slot_materials_out=slot_materials,
  1026. )
  1027. spools_updated += fallback_updates
  1028. if spools_updated == 0:
  1029. logger.info("[SPOOLMAN] Archive %s: no spools updated", archive_id)
  1030. else:
  1031. logger.info("[SPOOLMAN] Archive %s: updated %s spool(s)", archive_id, spools_updated)
  1032. # Stamp the archive's filament colour from the matched Spoolman spools
  1033. # so it reflects the curated inventory colour, not the slicer's 3MF
  1034. # value (#1494) — mirrors the built-in inventory path in usage_tracker.
  1035. await _apply_spool_colors_to_archive(db, archive_id, filament_usage, slot_colors)
  1036. # Same for the material: a slot mapped to a differently-typed spool than
  1037. # it was sliced for otherwise records the sliced type (#2563).
  1038. await _apply_spool_types_to_archive(db, archive_id, filament_usage, slot_materials)
  1039. async def _report_remain_delta_for_slots(
  1040. client,
  1041. *,
  1042. printer_id: int,
  1043. tray_remain_start: dict[str, dict],
  1044. current_lookup: dict[str, dict],
  1045. handled_global_tray_ids: set[int],
  1046. archive_id: int,
  1047. slot_colors_out: dict[int, str] | None = None,
  1048. slot_materials_out: dict[int, str] | None = None,
  1049. ) -> int:
  1050. """AMS remain%-delta path: write ``(start - current) * filament.weight``
  1051. grams to Spoolman for slots the 3MF path didn't cover.
  1052. Mirrors ``usage_tracker.on_print_complete`` Path 2: per-slot, gated on a
  1053. valid current ``remain%``, skipped on spool swap (``tray_uuid`` changed),
  1054. using the resolved spool's filament reference weight rather than MQTT's
  1055. unreliable ``tray_weight`` (which is the failure mode #1119 documented).
  1056. """
  1057. spools_updated = 0
  1058. for slot_key, start in tray_remain_start.items():
  1059. try:
  1060. ams_id_str, tray_id_str = slot_key.split("-", 1)
  1061. ams_id, tray_id = int(ams_id_str), int(tray_id_str)
  1062. except (ValueError, AttributeError):
  1063. continue
  1064. # Skip slots already handled by the 3MF path. Encoding mirrors
  1065. # build_ams_tray_lookup: VT trays land at 254/255, AMS-HT keeps
  1066. # its native id (>=128), regular AMS slots are ams_id*4+tray_id.
  1067. if ams_id == 255:
  1068. global_tray_id = 254 + tray_id
  1069. elif ams_id >= 128:
  1070. global_tray_id = ams_id
  1071. else:
  1072. global_tray_id = ams_id * 4 + tray_id
  1073. if global_tray_id in handled_global_tray_ids:
  1074. continue
  1075. current = current_lookup.get(slot_key)
  1076. if not current:
  1077. logger.debug("[SPOOLMAN] AMS%d-T%d: no current remain%% at completion, skipping fallback", ams_id, tray_id)
  1078. continue
  1079. # Spool swap mid-print — tray_uuid changed. We don't know how much
  1080. # of the print went to which spool; skip rather than mis-attribute.
  1081. start_uuid = (start.get("tray_uuid") or "").lower()
  1082. cur_uuid = (current.get("tray_uuid") or "").lower()
  1083. if start_uuid and cur_uuid and start_uuid != cur_uuid:
  1084. logger.info(
  1085. "[SPOOLMAN] AMS%d-T%d: spool swapped mid-print (uuid changed), skipping remain-delta", ams_id, tray_id
  1086. )
  1087. continue
  1088. delta_pct = start["remain"] - current["remain"]
  1089. if delta_pct <= 0:
  1090. continue # No consumption captured at AMS granularity, or refilled
  1091. spool_id = await _resolve_spool_id_via_slot_assignment(printer_id, ams_id, tray_id)
  1092. if spool_id is None:
  1093. logger.debug("[SPOOLMAN] AMS%d-T%d: no Spoolman slot assignment, skipping fallback", ams_id, tray_id)
  1094. continue
  1095. # Look up the spool's filament reference weight. Use a fresh GET so
  1096. # we don't depend on a stale cached_spools list. Failure here is
  1097. # silent-skip rather than fatal — other slots can still be written.
  1098. try:
  1099. spool = await client.get_spool(spool_id)
  1100. except Exception as exc: # noqa: BLE001
  1101. logger.debug("[SPOOLMAN] AMS%d-T%d: get_spool(%s) failed: %s", ams_id, tray_id, spool_id, exc)
  1102. continue
  1103. filament = spool.get("filament") or {}
  1104. ref_weight = filament.get("weight")
  1105. if not ref_weight or ref_weight <= 0:
  1106. logger.debug(
  1107. "[SPOOLMAN] AMS%d-T%d: spool %s has no filament.weight, skipping remain-delta",
  1108. ams_id,
  1109. tray_id,
  1110. spool_id,
  1111. )
  1112. continue
  1113. grams_used = round((delta_pct / 100.0) * ref_weight, 2)
  1114. if grams_used <= 0:
  1115. continue
  1116. try:
  1117. await client.use_spool(spool_id, grams_used)
  1118. except Exception as exc: # noqa: BLE001
  1119. logger.warning(
  1120. "[SPOOLMAN] AMS%d-T%d: use_spool(%s, %.2fg) failed: %s", ams_id, tray_id, spool_id, grams_used, exc
  1121. )
  1122. continue
  1123. spools_updated += 1
  1124. # No 3MF slot_id for this path — use the AMS slot key so the maps can
  1125. # still be inspected by callers if needed. The archive rewrites
  1126. # (#1494 colour, #2563 type) key on 3MF slot_ids, so remain-delta-only
  1127. # prints intentionally don't participate (matches usage_tracker's
  1128. # slot_id=None).
  1129. if slot_colors_out is not None:
  1130. color = filament.get("color_hex")
  1131. if color:
  1132. slot_colors_out[-(global_tray_id + 1)] = color
  1133. if slot_materials_out is not None:
  1134. material = filament.get("material")
  1135. if material:
  1136. slot_materials_out[-(global_tray_id + 1)] = material
  1137. logger.info(
  1138. "[SPOOLMAN] Archive %s AMS%d-T%d: %.2fg via remain-delta (%d%% of %.0fg) -> spool %s",
  1139. archive_id,
  1140. ams_id,
  1141. tray_id,
  1142. grams_used,
  1143. delta_pct,
  1144. ref_weight,
  1145. spool_id,
  1146. )
  1147. return spools_updated
  1148. async def _apply_spool_colors_to_archive(
  1149. db,
  1150. archive_id: int,
  1151. filament_usage: list[dict],
  1152. slot_colors: dict[int, str],
  1153. ) -> None:
  1154. """Overwrite an archive's ``filament_color`` with the colours of the
  1155. Spoolman spools that fed the print (#1494).
  1156. All-or-nothing, exactly like the built-in inventory path: the colour is
  1157. only rewritten when every used slot resolved to a spool that carries a
  1158. colour, so a partial match never drops slots from the archive.
  1159. """
  1160. if not slot_colors:
  1161. return
  1162. from backend.app.models.archive import PrintArchive
  1163. from backend.app.services.usage_tracker import (
  1164. _archive_colors_from_spools,
  1165. _spool_color_to_hex,
  1166. )
  1167. results = [{"slot_id": sid, "color": _spool_color_to_hex(hex_)} for sid, hex_ in slot_colors.items()]
  1168. colors = _archive_colors_from_spools(filament_usage, results)
  1169. if not colors:
  1170. return
  1171. archive = (await db.execute(select(PrintArchive).where(PrintArchive.id == archive_id))).scalar_one_or_none()
  1172. if archive is None:
  1173. return
  1174. joined = ",".join(colors)
  1175. if joined != archive.filament_color:
  1176. logger.info(
  1177. "[SPOOLMAN] Archive %s filament_color %r -> %r (from Spoolman spools)",
  1178. archive_id,
  1179. archive.filament_color,
  1180. joined,
  1181. )
  1182. archive.filament_color = joined
  1183. await db.commit()
  1184. async def _apply_spool_types_to_archive(
  1185. db,
  1186. archive_id: int,
  1187. filament_usage: list[dict],
  1188. slot_materials: dict[int, str],
  1189. ) -> None:
  1190. """Overwrite an archive's ``filament_type`` with the materials of the
  1191. Spoolman spools that fed the print (#2563).
  1192. All-or-nothing, exactly like the colour path and the built-in inventory
  1193. path: the type is only rewritten when every used slot resolved to a spool
  1194. that carries a material, so a partial match never drops slots from the
  1195. archive or the material statistics.
  1196. """
  1197. if not slot_materials:
  1198. return
  1199. from backend.app.models.archive import PrintArchive
  1200. from backend.app.services.usage_tracker import _archive_types_from_spools
  1201. results = [{"slot_id": sid, "material": material} for sid, material in slot_materials.items()]
  1202. types = _archive_types_from_spools(filament_usage, results)
  1203. if not types:
  1204. return
  1205. archive = (await db.execute(select(PrintArchive).where(PrintArchive.id == archive_id))).scalar_one_or_none()
  1206. if archive is None:
  1207. return
  1208. joined = ",".join(types)
  1209. if joined != archive.filament_type:
  1210. logger.info(
  1211. "[SPOOLMAN] Archive %s filament_type %r -> %r (from Spoolman spools)",
  1212. archive_id,
  1213. archive.filament_type,
  1214. joined,
  1215. )
  1216. archive.filament_type = joined
  1217. await db.commit()