spoolman_tracking.py 100 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246
  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. import math
  9. from dataclasses import dataclass
  10. from typing import cast
  11. from sqlalchemy import delete, select
  12. from sqlalchemy.engine import CursorResult
  13. from backend.app.api.routes._spoolman_helpers import spoolman_net_weight
  14. from backend.app.core.config import settings as app_settings
  15. from backend.app.core.database import async_session
  16. from backend.app.services import spool_swaps
  17. from backend.app.services.spoolman import (
  18. SpoolmanClientError,
  19. SpoolmanNotFoundError,
  20. SpoolmanUnavailableError,
  21. get_spoolman_client,
  22. init_spoolman_client,
  23. )
  24. logger = logging.getLogger(__name__)
  25. # Zero UUID used by Bambu printers for empty/unset tray_uuid
  26. _ZERO_UUID = "00000000000000000000000000000000"
  27. _ZERO_TAG_UID = "0000000000000000"
  28. # Highest global tray id that names a real slot. 255 does not: it is
  29. # ``PrinterState.tray_now``'s initial value, what an unparseable reading falls
  30. # back to, and what the field reads while nothing is loaded. The external spool
  31. # reports 254 when it is actually in use, and ``bambu_mqtt`` applies the same
  32. # cut-off when it seeds the tray-change log. Treating 255 as a slot would put
  33. # ``(255, 1)`` into the "slots this print used" evidence and exclude every real
  34. # one -- silently disabling the very fallback this guard protects (#1820).
  35. #
  36. # Applied to ``tray_now`` only. A 255 in the print's mapping or its tray-change
  37. # log was written there by a print and is evidence, however odd; a 255 in
  38. # ``tray_now`` is the field at rest, which is the absence of evidence.
  39. _MAX_REAL_TRAY_ID = 254
  40. def _is_real_tray_id(value) -> bool:
  41. """True when ``value`` names a physical slot rather than "nothing loaded"."""
  42. return isinstance(value, int) and not isinstance(value, bool) and 0 <= value <= _MAX_REAL_TRAY_ID
  43. def _is_non_zero_identifier(value: str) -> bool:
  44. """Return True when identifier is non-empty and not all zeros."""
  45. if not value:
  46. return False
  47. return set(value) != {"0"}
  48. def _to_fixed_hex(value: int, width: int) -> str:
  49. """Mirror frontend toFixedHex(): uppercase, zero-padded, fixed width."""
  50. safe = max(0, int(value))
  51. return format(safe, "X").zfill(width)[-width:]
  52. def _hash_serial_to_hex32(serial: str) -> str:
  53. """Mirror frontend hashSerialToHex32() exactly (32-bit FNV-1a)."""
  54. input_str = (serial or "").strip().upper()
  55. hash_value = 0x811C9DC5
  56. for char in input_str:
  57. hash_value ^= ord(char)
  58. hash_value = (hash_value * 0x01000193) & 0xFFFFFFFF
  59. return format(hash_value, "X").zfill(8)
  60. def _global_tray_id_to_ams_slot(global_tray_id: int) -> tuple[int, int]:
  61. """Convert global tray id to (ams_id, tray_id) tuple for fallback tag generation."""
  62. # External spool slots use IDs 254/255 and map to ams_id=255 tray_id=0/1.
  63. if global_tray_id >= 254:
  64. return 255, max(0, global_tray_id - 254)
  65. # AMS-HT units are addressed by ams_id directly and have a single tray.
  66. if global_tray_id >= 128:
  67. return global_tray_id, 0
  68. # Standard AMS units: four trays each.
  69. return global_tray_id // 4, global_tray_id % 4
  70. def _get_fallback_spool_tag(printer_serial: str, global_tray_id: int) -> str:
  71. """Mirror frontend getFallbackSpoolTag(serial, amsId, trayId) exactly."""
  72. if not printer_serial:
  73. return ""
  74. ams_id, tray_id = _global_tray_id_to_ams_slot(global_tray_id)
  75. return get_fallback_spool_tag_for_slot(printer_serial, ams_id, tray_id)
  76. def get_fallback_spool_tag_for_slot(printer_serial: str, ams_id: int, tray_id: int) -> str:
  77. """Public helper matching frontend getFallbackSpoolTag(serial, amsId, trayId).
  78. Used by stale-tag cleanup (#1457) to detect Spoolman spools still holding
  79. this slot's deterministic fallback tag in extra.tag.
  80. """
  81. if not printer_serial:
  82. return ""
  83. return f"{_hash_serial_to_hex32(printer_serial)}{_to_fixed_hex(ams_id, 4)}{_to_fixed_hex(tray_id, 4)}"
  84. def is_slot_fallback_tag(tag: str, printer_serials) -> bool:
  85. """Whether a tag is an AMS slot's generated fallback ID rather than a physical tag.
  86. Fallback IDs are 16 hex characters that start with the printer serial's hash
  87. (see get_fallback_spool_tag_for_slot). They identify a slot, not a spool, and
  88. have no place among Spoolman's native tags.
  89. """
  90. tag = (tag or "").strip('"').upper()
  91. if len(tag) != 16:
  92. return False
  93. return any(tag[:8] == _hash_serial_to_hex32(s) for s in printer_serials if s)
  94. def _resolve_spool_tag(tray_info: dict, printer_serial: str = "", global_tray_id: int | None = None) -> str:
  95. """Get the best spool identifier from tray info (prefer tray_uuid over tag_uid).
  96. Returns empty string if no usable identifier is found.
  97. """
  98. tray_uuid = str(tray_info.get("tray_uuid", "") or "")
  99. tag_uid = str(tray_info.get("tag_uid", "") or "")
  100. if tray_uuid and tray_uuid != _ZERO_UUID and _is_non_zero_identifier(tray_uuid):
  101. return tray_uuid
  102. if tag_uid and tag_uid != _ZERO_TAG_UID and _is_non_zero_identifier(tag_uid):
  103. return tag_uid
  104. if global_tray_id is not None:
  105. return _get_fallback_spool_tag(printer_serial, global_tray_id)
  106. return ""
  107. async def _get_printer_serial(printer_id: int) -> str:
  108. """Get printer serial for deterministic fallback tag generation."""
  109. from backend.app.models.printer import Printer
  110. from backend.app.services.printer_manager import printer_manager
  111. printer_info = printer_manager.get_printer(printer_id)
  112. if printer_info and printer_info.serial_number:
  113. return printer_info.serial_number
  114. async with async_session() as db:
  115. result = await db.execute(select(Printer.serial_number).where(Printer.id == printer_id))
  116. serial_number = result.scalar_one_or_none()
  117. return serial_number or ""
  118. def _resolve_global_tray_id(slot_id: int, slot_to_tray: list | None, ams_trays: dict | None = None) -> int:
  119. """Map a 1-based slot_id to a global_tray_id using optional custom mapping.
  120. Custom mapping: slot_to_tray[slot_id - 1] is used when >= 0. A negative
  121. value means the slot feeds nothing and answers -1, which no tray has: the
  122. flat ams_mapping writes an external spool as -1 on the wire, but the
  123. captured mapping puts its 254/255 back from ams_mapping2 (#3166), so a -1
  124. left in it is unmapped. Never the position-based default for such a slot,
  125. which would credit an unrelated spool (#1276, regression of #853).
  126. Position-based default: uses sorted ams_trays keys so external spools (ID 254/255)
  127. naturally follow standard AMS trays, matching the slicer's slot numbering.
  128. Final fallback: slot_id - 1 (legacy, works for pure AMS without external spools).
  129. """
  130. if slot_to_tray and slot_id <= len(slot_to_tray):
  131. mapped_tray = slot_to_tray[slot_id - 1]
  132. if mapped_tray >= 0:
  133. return mapped_tray
  134. # A negative value is a slot the print does not feed: the captured
  135. # mapping already names an external spool 254/255 (#3166), and the
  136. # built-in tracker charges nothing for it either. No tray has this id,
  137. # so the slot is skipped rather than guessed by position.
  138. return -1
  139. # Position-based default: sort available tray IDs so external spools (254/255)
  140. # come after standard AMS trays, matching the slicer's slot assignment order.
  141. if ams_trays:
  142. sorted_tray_ids = sorted(ams_trays.keys())
  143. if slot_id <= len(sorted_tray_ids):
  144. return sorted_tray_ids[slot_id - 1]
  145. return slot_id - 1
  146. def _single_slot_tray_from_state(
  147. state,
  148. filament_usage: list[dict],
  149. tray_now_at_start: int | None = None,
  150. ) -> tuple[int, int] | None:
  151. """The tray a single-slot print actually drew from, read off the printer.
  152. A1, A1 mini, P1S and P2S publish no ``mapping`` field and drop the MQTT
  153. connection when we subscribe to their request topic, so neither of the
  154. other two fallbacks can answer for them. What they do report is which tray
  155. the extruder is fed from, and for a print that uses exactly one filament
  156. slot that is the same question: the one slot came from the one tray.
  157. The ladder mirrors ``usage_tracker.on_print_complete`` step 5, which has
  158. consulted these same fields since it started resolving mappings at
  159. completion. Spoolman users were the only ones not getting them (#2953).
  160. Gated on exactly one slot with usage, like the internal writer: on a
  161. multi-colour print every filament change moves ``tray_now``, so a single
  162. tray reading says nothing about which slot it belongs to.
  163. More than one tray-change entry means the print switched trays mid-run
  164. (AMS backup on runout, #957). ``report_usage`` splits those per segment
  165. and must not be handed a single-tray mapping instead, so this declines.
  166. Returns ``(slot_id, global_tray_id)``, or None when the printer offered no
  167. usable reading and the positional default stands.
  168. """
  169. nonzero = [u for u in filament_usage or [] if u.get("used_g", 0) > 0]
  170. if len(nonzero) != 1:
  171. return None
  172. slot_id = nonzero[0].get("slot_id", 0)
  173. if slot_id <= 0:
  174. return None
  175. changes = list(getattr(state, "tray_change_log", None) or [])
  176. if len(changes) > 1:
  177. return None
  178. if len(changes) == 1:
  179. entry = changes[0]
  180. if isinstance(entry, (tuple, list)) and entry and _is_real_tray_id(entry[0]):
  181. # Strongest evidence there is: the printer announced this switch
  182. # while the job was running, so it describes this print and no
  183. # other. On the reporter's A1 it read (3, 0) -- tray 3 at layer 0
  184. # -- while the positional default was charging tray 0.
  185. return slot_id, entry[0]
  186. # No mid-print switch recorded. Fall back to the standing tray readings,
  187. # newest evidence first. ``tray_now`` is 255 both at rest and while
  188. # nothing is loaded, which is why _MAX_REAL_TRAY_ID excludes it; A1
  189. # firmware parks there the moment a print ends, leaving last_loaded_tray
  190. # as the only survivor.
  191. for candidate in (tray_now_at_start, getattr(state, "tray_now", None)):
  192. if _is_real_tray_id(candidate):
  193. return slot_id, candidate
  194. # On a printer with two external holders (H2D, X2D) tray_now 255 is the
  195. # right-hand one. Ahead of last_loaded_tray: a print from that holder never
  196. # updates it, so it still names the tray an earlier print used (#3405).
  197. from backend.app.services.usage_tracker import has_second_external_holder
  198. if getattr(state, "tray_now", None) == 255 and has_second_external_holder(getattr(state, "raw_data", None)):
  199. return slot_id, 255
  200. # 254 is no survivor of an unload: the same cap as the built-in inventory.
  201. last_loaded = getattr(state, "last_loaded_tray", None)
  202. if isinstance(last_loaded, int) and _is_real_tray_id(last_loaded) and last_loaded < 254:
  203. return slot_id, last_loaded
  204. return None
  205. def _mapping_names_no_used_tray(slot_to_tray: list | None, filament_usage: list[dict] | None) -> bool:
  206. """The built-in inventory's rule for a mapping that is no evidence (#3405)."""
  207. from backend.app.services.usage_tracker import mapping_names_no_used_tray
  208. return mapping_names_no_used_tray(slot_to_tray, filament_usage)
  209. def _resolve_slot_to_tray_fallback(
  210. printer_id: int,
  211. filament_usage: list[dict],
  212. tray_now_at_start: int | None = None,
  213. tray_state_only: bool = False,
  214. ) -> tuple[list[int] | None, str]:
  215. """Recover a slot-to-tray mapping at completion when print start captured none.
  216. ``store_print_data`` can only learn the mapping from two sources: the
  217. ``ams_mapping`` Bambuddy intercepts on the printer's local request topic, and
  218. a queue item's stored mapping. Neither exists for a print dispatched from
  219. Bambu Studio while the printer is cloud-bound — the command travels through
  220. Bambu's broker and never appears on the local topic we subscribe to. With
  221. ``slot_to_tray`` left NULL, ``_resolve_global_tray_id`` guesses by position:
  222. slicer slot 1 to the first loaded tray, slot 2 to the second, and so on. An
  223. AMS that isn't loaded in slicer order then charges every slot to the wrong
  224. spool, and the archive's filament is rewritten to match, so the print
  225. silently changes colour when it finishes (#2768).
  226. The printer knows the real answer. Its ``mapping`` field carries the actual
  227. slot-to-tray assignment for the running job, and for the models that never
  228. publish it (A1, P1S, P2S) the 3MF's per-slot colours can be matched against
  229. the loaded trays instead. Failing both, a print that used a single filament
  230. slot can be pinned to the tray the printer reported feeding from
  231. (``_single_slot_tray_from_state``).
  232. The built-in inventory writer has consulted all three for as long as it has
  233. resolved mappings at completion. The first version of this function offered
  234. only the first two, which left A1-class printers -- no ``mapping`` field, no
  235. request topic -- with nothing but the colour match, and that needs the
  236. slicer's filament colour to equal the tray's exactly. A generic black
  237. profile against a tray set to #111111 does not match, and the print is
  238. charged to whichever spool happens to sit in the first tray (#2953).
  239. Deliberately at completion rather than inside ``store_print_data``: the
  240. printer keeps publishing ``mapping`` long after a job ends — it is still in
  241. the status payload while the printer sits idle — so reading it at print start
  242. risks stamping the *previous* job's mapping onto this one before the printer
  243. has pushed the update. At completion the field unambiguously describes the
  244. job that just ran.
  245. Args:
  246. printer_id: Printer whose live state is consulted.
  247. filament_usage: The 3MF's per-slot estimates. The colour match reads
  248. ``slot_id``/``color``; the tray-state fallback reads
  249. ``slot_id``/``used_g``.
  250. tray_now_at_start: The tray the printer was feeding from when the print
  251. began, as captured by ``store_print_data``. Only consulted by the
  252. tray-state fallback.
  253. tray_state_only: Skip the ``mapping`` field and the colour match. For a
  254. stored mapping that named no tray: the built-in inventory goes
  255. straight to the printer's tray readings for one (#3405), and the
  256. colour match, which knows no external holder, would hand an
  257. external-spool print to a same-coloured AMS tray.
  258. Returns:
  259. ``(mapping, source)``, or ``(None, "none")`` when no fallback produced
  260. anything and the positional default stands.
  261. """
  262. from backend.app.services.printer_manager import printer_manager
  263. from backend.app.services.usage_tracker import _decode_mqtt_mapping, _match_slots_by_color
  264. state = printer_manager.get_status(printer_id)
  265. raw_data = getattr(state, "raw_data", None) if state else None
  266. # Both of the first two fallbacks read the status payload; the third reads
  267. # fields ``bambu_mqtt`` maintains on the state object itself, so an empty
  268. # payload must not short-circuit past it.
  269. if raw_data and not tray_state_only:
  270. decoded = _decode_mqtt_mapping(raw_data.get("mapping"))
  271. if decoded:
  272. return decoded, "mqtt"
  273. matched = _match_slots_by_color(filament_usage, raw_data.get("ams"))
  274. if matched:
  275. return matched, "color_match"
  276. single = _single_slot_tray_from_state(state, filament_usage, tray_now_at_start)
  277. if single is not None:
  278. slot_id, global_tray_id = single
  279. # Only the one slot is claimed. The -1 padding is the array's existing
  280. # "not an AMS tray" value, and the slots carrying it consumed nothing,
  281. # so no caller resolves them: ``_report_spool_usage_for_slots`` skips
  282. # zero-gram slots before resolving, ``_print_used_tray_keys`` skips
  283. # negatives, and report_usage's handled-set skips them too.
  284. mapping = [-1] * slot_id
  285. mapping[slot_id - 1] = global_tray_id
  286. return mapping, "tray_state"
  287. return None, "none"
  288. def build_ams_tray_lookup(raw_data: dict) -> dict[int, dict]:
  289. """Build lookup of global_tray_id -> tray info from printer state.
  290. Returns: {0: {"tray_uuid": "...", "tag_uid": "...", "tray_type": "..."}, ...}
  291. """
  292. lookup = {}
  293. ams_data = raw_data.get("ams", [])
  294. for ams_unit in ams_data:
  295. ams_id = int(ams_unit.get("id", 0))
  296. for tray in ams_unit.get("tray", []):
  297. tray_id = int(tray.get("id", 0))
  298. # AMS-HT units have IDs starting at 128 with a single tray
  299. global_tray_id = ams_id if ams_id >= 128 else ams_id * 4 + tray_id
  300. lookup[global_tray_id] = {
  301. "tray_uuid": tray.get("tray_uuid", ""),
  302. "tag_uid": tray.get("tag_uid", ""),
  303. "tray_type": tray.get("tray_type", ""),
  304. }
  305. # External spool(s) (vt_tray is a list, global_tray_id from each entry's "id")
  306. for vt in raw_data.get("vt_tray") or []:
  307. if vt.get("tray_type"):
  308. tray_id = int(vt.get("id", 254))
  309. lookup[tray_id] = {
  310. "tray_uuid": vt.get("tray_uuid", ""),
  311. "tag_uid": vt.get("tag_uid", ""),
  312. "tray_type": vt.get("tray_type", ""),
  313. }
  314. return lookup
  315. def _snapshot_tray_remain(raw_data: dict, skipped_out: list[str] | None = None) -> dict[str, dict]:
  316. """Capture per-slot ``remain%`` + ``tray_uuid`` at print start so the
  317. completion path can compute a remain-delta when 3MF data doesn't cover
  318. the slot (or there's no 3MF at all — #1820).
  319. Returns ``{"<ams_id>-<tray_id>": {"remain": int, "tray_uuid": str}}``.
  320. Only slots whose ``remain`` is a valid 0..100 int are included; invalid
  321. values mean the AMS hasn't read the spool yet and a delta would be
  322. meaningless. Mirrors the gate in
  323. ``usage_tracker.on_print_start:309``.
  324. A rejected slot is appended to *skipped_out* when one is supplied, so the
  325. caller can say which slots this print will not be able to charge. That is
  326. not hypothetical: an AMS reports a negative ``remain`` on a nearly empty
  327. spool, so the gate can drop the one slot that is about to do the printing
  328. (#1820).
  329. """
  330. snapshot: dict[str, dict] = {}
  331. ams_raw = raw_data.get("ams", [])
  332. ams_data = ams_raw.get("ams", []) if isinstance(ams_raw, dict) else ams_raw if isinstance(ams_raw, list) else []
  333. for ams_unit in ams_data:
  334. if not isinstance(ams_unit, dict):
  335. continue
  336. ams_id = int(ams_unit.get("id", 0))
  337. for tray in ams_unit.get("tray", []):
  338. if not isinstance(tray, dict):
  339. continue
  340. tray_id = int(tray.get("id", 0))
  341. remain = tray.get("remain", -1)
  342. if isinstance(remain, int) and 0 <= remain <= 100:
  343. snapshot[f"{ams_id}-{tray_id}"] = {
  344. "remain": remain,
  345. "tray_uuid": tray.get("tray_uuid", "") or "",
  346. }
  347. elif skipped_out is not None:
  348. skipped_out.append(f"AMS{ams_id}-T{tray_id}(remain={remain})")
  349. vt_tray_raw = raw_data.get("vt_tray") or []
  350. if isinstance(vt_tray_raw, dict):
  351. vt_tray_raw = [vt_tray_raw]
  352. for vt in vt_tray_raw:
  353. if not isinstance(vt, dict):
  354. continue
  355. vt_id = int(vt.get("id", 254))
  356. # 254 → (255, 0), 255 → (255, 1) — matches usage_tracker's encoding.
  357. vt_tray_id = vt_id - 254
  358. remain = vt.get("remain", -1)
  359. if isinstance(remain, int) and 0 <= remain <= 100:
  360. snapshot[f"255-{vt_tray_id}"] = {
  361. "remain": remain,
  362. "tray_uuid": vt.get("tray_uuid", "") or "",
  363. }
  364. elif skipped_out is not None:
  365. skipped_out.append(f"VT{vt_id}(remain={remain})")
  366. return snapshot
  367. def _extract_3mf_tracking(full_path, plate_id: int | None) -> tuple[list | None, dict | None, dict | None]:
  368. """What Spoolman's tracking reads from a print's 3MF: the per-filament
  369. grams, the per-layer usage (JSON keys) and the filament properties."""
  370. from backend.app.utils.threemf_tools import (
  371. extract_filament_properties_from_3mf,
  372. extract_filament_usage_from_3mf,
  373. extract_layer_filament_usage_from_3mf,
  374. )
  375. filament_usage = extract_filament_usage_from_3mf(full_path, plate_id) or None
  376. layer_usage_json: dict | None = None
  377. layer_usage = extract_layer_filament_usage_from_3mf(full_path, plate_id)
  378. if layer_usage:
  379. # Convert int keys to string for JSON serialization
  380. layer_usage_json = {str(k): v for k, v in layer_usage.items()}
  381. logger.debug("[SPOOLMAN] Parsed %s layers from G-code", len(layer_usage))
  382. return filament_usage, layer_usage_json, extract_filament_properties_from_3mf(full_path)
  383. async def attach_recovered_3mf(
  384. printer_id: int,
  385. archive_id: int,
  386. file_path: str,
  387. db,
  388. printer_manager,
  389. *,
  390. plate_id: int | None = None,
  391. may_start: bool = True,
  392. ) -> None:
  393. """Add a fallback archive's recovered 3MF to Spoolman's tracking of its print.
  394. Print start found no 3MF, so it stored only the AMS remain% snapshot and
  395. completion would charge the remain% change. Only the 3MF's own data is
  396. added: the snapshot was taken at print start, and replacing it from the
  397. mid-print state now would charge the wrong change for a slot the 3MF does
  398. not cover. Usage already tracked is left alone.
  399. When print start stored nothing (no slot had a remain%), the 3MF is
  400. tracked on its own if ``may_start``: from the recovery inside the print's
  401. close-out, which runs before the charge, or from any other while the print
  402. runs and is not being closed. During a close-out another recovery's row
  403. could follow the charge that already deleted the print's tracking, and be
  404. left behind.
  405. """
  406. from backend.app.api.routes.settings import get_setting
  407. from backend.app.models.active_print_spoolman import ActivePrintSpoolman
  408. spoolman_enabled = await get_setting(db, "spoolman_enabled")
  409. if not spoolman_enabled or spoolman_enabled.lower() != "true":
  410. return
  411. full_path = (
  412. app_settings.base_dir / file_path
  413. ) # SEC-PATH-OK: file_path is archive.file_path — DB-stored, internally generated
  414. if not file_path or not full_path.exists():
  415. return
  416. row = (
  417. await db.execute(
  418. select(ActivePrintSpoolman)
  419. .where(ActivePrintSpoolman.printer_id == printer_id)
  420. .where(ActivePrintSpoolman.archive_id == archive_id)
  421. )
  422. ).scalar_one_or_none()
  423. if row is not None and row.filament_usage:
  424. return
  425. if row is None and not may_start:
  426. return
  427. filament_usage, layer_usage_json, filament_properties = _extract_3mf_tracking(full_path, plate_id)
  428. if not filament_usage:
  429. return
  430. if row is None:
  431. state = printer_manager.get_status(printer_id)
  432. row = ActivePrintSpoolman(
  433. printer_id=printer_id,
  434. archive_id=archive_id,
  435. ams_trays=build_ams_tray_lookup(state.raw_data) if state and state.raw_data else {},
  436. )
  437. db.add(row)
  438. row.filament_usage = filament_usage
  439. row.layer_usage = layer_usage_json
  440. row.filament_properties = filament_properties
  441. await db.commit()
  442. logger.info(
  443. "[SPOOLMAN] Archive %s: tracking now has the recovered 3MF's usage (%d filament(s))",
  444. archive_id,
  445. len(filament_usage),
  446. )
  447. async def store_print_data(
  448. printer_id: int,
  449. archive_id: int,
  450. file_path: str,
  451. db,
  452. printer_manager,
  453. ams_mapping: list[int] | None = None,
  454. plate_id: int | None = None,
  455. ):
  456. """Store Spoolman tracking data at print start (persisted to database).
  457. Per-print tracking is the primary weight-update path for Spoolman, mirroring
  458. how the internal Filament Inventory works. The legacy AMS-remain%-based sync
  459. is no longer used as a weight writer (#1119), so this runs whenever Spoolman
  460. is enabled regardless of the deprecated `spoolman_disable_weight_sync` flag.
  461. ``plate_id``, when set, scopes the 3MF filament extract to a single plate so
  462. queue / direct-Print dispatch of plate N of a multi-plate file doesn't
  463. attribute every plate's filament to the printed spool (#1697). When unset,
  464. the queue item's plate_id (if any) is used; otherwise the whole-file sum is
  465. extracted, which is correct for direct prints that target the first/only
  466. plate of a single-plate file.
  467. """
  468. from backend.app.api.routes.settings import get_setting
  469. from backend.app.models.active_print_spoolman import ActivePrintSpoolman
  470. from backend.app.models.print_queue import PrintQueueItem
  471. # Check if Spoolman is enabled
  472. spoolman_enabled = await get_setting(db, "spoolman_enabled")
  473. if not spoolman_enabled or spoolman_enabled.lower() != "true":
  474. return
  475. # Get current AMS tray state up front — needed both for the 3MF path's
  476. # ams_trays field and for the remain%-delta snapshot (#1820 fallback for
  477. # no-3MF "Untitled" prints, mirroring usage_tracker.on_print_start).
  478. state = printer_manager.get_status(printer_id)
  479. ams_trays: dict[int, dict] = {}
  480. tray_remain_start: dict[str, dict] = {}
  481. if state and state.raw_data:
  482. ams_trays = build_ams_tray_lookup(state.raw_data)
  483. skipped_slots: list[str] = []
  484. tray_remain_start = _snapshot_tray_remain(state.raw_data, skipped_slots)
  485. if skipped_slots:
  486. # Matches what usage_tracker.on_print_start reports for the
  487. # internal inventory, so both backends name the slots that this
  488. # print will not be able to charge at AMS granularity.
  489. logger.info(
  490. "[SPOOLMAN] Printer %s: slots with no usable remain%% at print start: %s",
  491. printer_id,
  492. ", ".join(skipped_slots),
  493. )
  494. # Try to read per-slot filament estimates from the 3MF. Two paths can
  495. # leave ``filament_usage`` empty: (1) fallback archive (no .gcode.3mf
  496. # was downloadable from the printer — "Untitled" prints, see #1820),
  497. # (2) 3MF present but slice_info missing per-filament estimates.
  498. # Both fall through to the remain%-delta path at completion.
  499. filament_usage: list | None = None
  500. layer_usage_json: dict | None = None
  501. filament_properties: dict | None = None
  502. full_path = (
  503. app_settings.base_dir / file_path
  504. ) # SEC-PATH-OK: file_path is archive.file_path / library_file.file_path — DB-stored, internally generated
  505. threemf_available = bool(file_path) and full_path.exists()
  506. queue_item = None
  507. if threemf_available:
  508. # Resolve the queue item once — used both for the plate-scoped 3MF parsing
  509. # fallback (#1697: multi-plate file dispatched for one plate must only count
  510. # that plate's filament) and for the ams_mapping fallback below.
  511. # This printer's: a batch prints one archive as several queue items,
  512. # on several printers at once.
  513. queue_result = await db.execute(
  514. select(PrintQueueItem)
  515. .where(PrintQueueItem.archive_id == archive_id)
  516. .where(PrintQueueItem.printer_id == printer_id)
  517. .where(PrintQueueItem.status == "printing")
  518. .order_by(PrintQueueItem.id.desc())
  519. )
  520. queue_item = queue_result.scalars().first()
  521. # Caller-supplied plate_id wins (direct-Print path); fall back to the queue
  522. # item's plate_id (queue dispatch path).
  523. effective_plate_id = (
  524. plate_id if plate_id is not None else (queue_item.plate_id if queue_item is not None else None)
  525. )
  526. filament_usage, layer_usage_json, filament_properties = _extract_3mf_tracking(full_path, effective_plate_id)
  527. else:
  528. # No 3MF on disk — common for "Untitled" prints whose .gcode.3mf
  529. # was never on the printer's FTP. Logged at debug since the
  530. # fallback path below picks up the slack when remain% is available.
  531. logger.debug("[SPOOLMAN] 3MF file not available: %s", full_path)
  532. # If neither path has anything useful, there's nothing to track.
  533. if not filament_usage and not tray_remain_start:
  534. if threemf_available:
  535. logger.debug("[SPOOLMAN] No filament usage data in 3MF for archive %s", archive_id)
  536. return
  537. # Prefer the explicit mapping captured from the print command, then fall back
  538. # to any queue mapping stored for scheduled/reprint jobs.
  539. slot_to_tray = ams_mapping if ams_mapping is not None else None
  540. mapping_source = "print_cmd" if slot_to_tray else None
  541. if not slot_to_tray and queue_item and queue_item.ams_mapping:
  542. try:
  543. slot_to_tray = json.loads(queue_item.ams_mapping)
  544. mapping_source = "queue"
  545. except json.JSONDecodeError:
  546. pass # Ignore malformed AMS mapping; fall back to default slot assignment
  547. # Delete any existing row for this printer/archive (shouldn't exist, but just in case)
  548. await db.execute(
  549. delete(ActivePrintSpoolman)
  550. .where(ActivePrintSpoolman.printer_id == printer_id)
  551. .where(ActivePrintSpoolman.archive_id == archive_id)
  552. )
  553. # Insert new tracking data. ``filament_usage`` may be None for the
  554. # no-3MF case; report_usage falls back to ``tray_remain_start``.
  555. tracking = ActivePrintSpoolman(
  556. printer_id=printer_id,
  557. archive_id=archive_id,
  558. filament_usage=filament_usage,
  559. ams_trays=ams_trays,
  560. slot_to_tray=slot_to_tray,
  561. layer_usage=layer_usage_json,
  562. filament_properties=filament_properties,
  563. tray_remain_start=tray_remain_start or None,
  564. # Which slot the printer was drawing from when this print began. For a
  565. # print with no ams_mapping -- one started from the printer's own
  566. # screen, which is the case this whole fallback exists for -- it is the
  567. # only evidence of which slot the print used (#1820).
  568. tray_now_at_start=getattr(state, "tray_now", None) if state else None,
  569. )
  570. db.add(tracking)
  571. await db.commit()
  572. logger.info(
  573. "[SPOOLMAN] Stored tracking data for print: printer=%s, archive=%s (3mf=%s, remain_snapshot=%d slot(s))",
  574. printer_id,
  575. archive_id,
  576. "yes" if filament_usage else "no",
  577. len(tray_remain_start),
  578. )
  579. logger.debug("[SPOOLMAN] Filament usage: %s", filament_usage)
  580. logger.debug("[SPOOLMAN] AMS trays: %s", list(ams_trays.keys()))
  581. # Logged at info even when there is no mapping: "source: none" here is the
  582. # signal that completion will have to fall back, which is the single most
  583. # useful line in the log when a print is charged to the wrong spool (#2768).
  584. logger.info(
  585. "[SPOOLMAN] Print start: archive %s slot_to_tray=%s (source: %s)",
  586. archive_id,
  587. slot_to_tray,
  588. mapping_source or "none",
  589. )
  590. if layer_usage_json:
  591. logger.debug("[SPOOLMAN] Layer usage data available for partial tracking")
  592. async def cleanup_tracking(
  593. printer_id: int,
  594. archive_id: int,
  595. db,
  596. last_layer_num: int | None = None,
  597. last_progress: int | None = None,
  598. ) -> float | None:
  599. """Report partial usage and clean up Spoolman tracking data for failed/aborted prints.
  600. Returns what the charged spools' prices make the run cost, or None when
  601. none of them carries a price (or nothing was tracked)."""
  602. from backend.app.models.active_print_spoolman import ActivePrintSpoolman
  603. # Get tracking data first (needed for partial usage reporting)
  604. result = await db.execute(
  605. select(ActivePrintSpoolman)
  606. .where(ActivePrintSpoolman.printer_id == printer_id)
  607. .where(ActivePrintSpoolman.archive_id == archive_id)
  608. )
  609. tracking = result.scalar_one_or_none()
  610. if not tracking:
  611. logger.debug("[SPOOLMAN] No tracking data to clean up for printer=%s, archive=%s", printer_id, archive_id)
  612. return None
  613. # Try to report partial usage before cleanup, pricing each charge as it
  614. # is made: the run's own cost, as the built-in inventory logs it.
  615. print_cost = _PrintCost()
  616. try:
  617. await _report_partial_usage(
  618. printer_id,
  619. tracking,
  620. last_layer_num=last_layer_num,
  621. last_progress=last_progress,
  622. cost_out=print_cost,
  623. )
  624. except Exception as e:
  625. logger.warning("[SPOOLMAN] Partial usage report failed: %s", e)
  626. # Delete tracking data
  627. await db.execute(
  628. delete(ActivePrintSpoolman)
  629. .where(ActivePrintSpoolman.printer_id == printer_id)
  630. .where(ActivePrintSpoolman.archive_id == archive_id)
  631. )
  632. await db.commit()
  633. logger.debug("[SPOOLMAN] Cleaned up tracking data for printer=%s, archive=%s", printer_id, archive_id)
  634. # A charged spool without a price at the default rate, as the built-in
  635. # inventory prices its own (usage_tracker's cost_per_kg fallback).
  636. run_cost = print_cost.cost
  637. if print_cost.unpriced_grams > 0:
  638. from backend.app.services.print_cost_estimate import default_cost_per_kg
  639. run_cost += print_cost.unpriced_grams / 1000.0 * await default_cost_per_kg(db)
  640. return round(run_cost, 2) or None
  641. async def release_archived_slot_rows(printer_id: int) -> None:
  642. """Unassign the printer's slots whose Spoolman spool is archived or gone.
  643. Archiving a spool through Bambuddy keeps the slot of a printer whose print
  644. it may be feeding: usage is charged at completion by the slot's spool, and
  645. only the slot row knows it for a spool without a tag. Run once that print's
  646. usage is reported. Also catches spools archived or deleted in Spoolman.
  647. Best-effort: a failure leaves the rows for the next print to try again.
  648. """
  649. from backend.app.api.routes.settings import get_setting
  650. from backend.app.core.websocket import ws_manager
  651. from backend.app.models.spoolman_slot_assignment import SpoolmanSlotAssignment
  652. try:
  653. async with async_session() as db:
  654. enabled = await get_setting(db, "spoolman_enabled")
  655. if not enabled or enabled.lower() != "true":
  656. return
  657. rows = (
  658. (
  659. await db.execute(
  660. select(SpoolmanSlotAssignment).where(SpoolmanSlotAssignment.printer_id == printer_id)
  661. )
  662. )
  663. .scalars()
  664. .all()
  665. )
  666. if not rows:
  667. return
  668. client = await _get_spoolman_client_with_fallback()
  669. if not client:
  670. return
  671. live = {
  672. spool["id"]
  673. for spool in await client.get_all_spools(allow_archived=True)
  674. if spool.get("id") is not None and not spool.get("archived")
  675. }
  676. freed: list[tuple[int, int]] = []
  677. for row in rows:
  678. if row.spoolman_spool_id in live:
  679. continue
  680. # Keyed on the spool too: the slot may have been given another
  681. # spool since the rows were read.
  682. result = cast(
  683. CursorResult,
  684. await db.execute(
  685. delete(SpoolmanSlotAssignment).where(
  686. SpoolmanSlotAssignment.id == row.id,
  687. SpoolmanSlotAssignment.spoolman_spool_id == row.spoolman_spool_id,
  688. )
  689. ),
  690. )
  691. if result.rowcount:
  692. freed.append((row.ams_id, row.tray_id))
  693. if not freed:
  694. return
  695. await db.commit()
  696. for ams_id, tray_id in freed:
  697. await ws_manager.broadcast(
  698. {"type": "spool_assignment_changed", "printer_id": printer_id, "ams_id": ams_id, "tray_id": tray_id}
  699. )
  700. except Exception as e: # noqa: BLE001 — best-effort cleanup after completion
  701. logger.warning("[SPOOLMAN] Could not release archived spools' slots on printer %s: %s", printer_id, e)
  702. async def _get_spoolman_client_with_fallback():
  703. """Get Spoolman client, initializing from settings if needed.
  704. Returns (client, is_healthy) tuple. Client may be None.
  705. """
  706. client = await get_spoolman_client()
  707. if not client:
  708. async with async_session() as db:
  709. from backend.app.api.routes.settings import get_setting
  710. spoolman_url = await get_setting(db, "spoolman_url")
  711. if spoolman_url:
  712. try:
  713. client = await init_spoolman_client(spoolman_url)
  714. except ValueError as exc:
  715. logger.warning("Spoolman URL %r rejected by SSRF guard: %s", spoolman_url, exc)
  716. return None
  717. if not client:
  718. return None
  719. if not await client.health_check():
  720. logger.warning("Spoolman health check failed; skipping usage reporting")
  721. return None
  722. return client
  723. async def _resolve_spool_id_via_slot_assignment(printer_id: int, ams_id: int, tray_id: int) -> int | None:
  724. """Look up the Spoolman spool ID locally bound to (printer, ams, tray).
  725. Fallback path for #1459: when a tag-less spool was assigned via the
  726. Bambuddy UI, the user's deterministic fallback tag is intentionally NOT
  727. written to Spoolman's extra.tag (kept clean per #1457), so
  728. find_spool_by_tag misses. The local spoolman_slot_assignments table is
  729. the authoritative binding for those spools.
  730. """
  731. from backend.app.models.spoolman_slot_assignment import SpoolmanSlotAssignment
  732. async with async_session() as db:
  733. result = await db.execute(
  734. select(SpoolmanSlotAssignment.spoolman_spool_id).where(
  735. SpoolmanSlotAssignment.printer_id == printer_id,
  736. SpoolmanSlotAssignment.ams_id == ams_id,
  737. SpoolmanSlotAssignment.tray_id == tray_id,
  738. )
  739. )
  740. return result.scalar_one_or_none()
  741. def _as_positive_number(value) -> float | None:
  742. """``value`` as a float when it is a usable positive quantity, else None.
  743. Rejects bools (``True`` is an int in Python, and ``float(True)`` is 1.0 --
  744. a weight of 1 g would price a spool per-gram at its whole cost), and
  745. rejects NaN and infinity, which compare False against every bound and would
  746. otherwise reach the archive as a NaN cost that no later comparison can
  747. clear.
  748. """
  749. if isinstance(value, bool):
  750. return None
  751. try:
  752. number = float(value)
  753. except (TypeError, ValueError):
  754. return None
  755. if not math.isfinite(number) or number <= 0:
  756. return None
  757. return number
  758. def _spool_cost_per_gram(spool: dict | None) -> float | None:
  759. """What one gram off this Spoolman spool costs, or None if it can't be said.
  760. Spoolman prices a spool in two places. ``filament.price`` is the catalogue
  761. figure for a full spool of that filament, and ``price`` on the spool itself
  762. overrides it when a particular purchase cost something else -- a sale, a
  763. different vendor, import duty. The spool's own value wins, which is the
  764. order the Spoolman UI presents them in.
  765. Each price is divided by the net weight it was paid for, excluding the
  766. core: the spool's price by the spool's own ``initial_weight`` (falling
  767. back to the filament's), the catalogue price by ``filament.weight``. A
  768. 250 g spool of a 1000 g filament was otherwise charged at a quarter of
  769. its rate (#3194).
  770. A missing or non-positive price is not a free spool, it is an unpriced one,
  771. and returns None so the caller can fall back to the global default rate
  772. rather than silently recording that this print cost nothing. Mirrors the
  773. ``cost_per_kg > 0`` guard the built-in inventory writer applies to its own
  774. per-spool rate.
  775. """
  776. if not isinstance(spool, dict):
  777. return None
  778. filament = spool.get("filament")
  779. if not isinstance(filament, dict):
  780. filament = {}
  781. # A spool-level 0 is treated as "not overridden" rather than "this roll was
  782. # free": Spoolman leaves the field null when unset, but an import or an API
  783. # client that writes 0 instead is common enough that reading it as free
  784. # would price a whole print at the default rate while a perfectly good
  785. # catalogue price sat one level down.
  786. price = _as_positive_number(spool.get("price"))
  787. if price is not None:
  788. weight = _as_positive_number(spoolman_net_weight(spool))
  789. else:
  790. price = _as_positive_number(filament.get("price"))
  791. weight = _as_positive_number(filament.get("weight"))
  792. if price is None or weight is None:
  793. return None
  794. # Both operands can be finite and the quotient still overflow. A non-finite
  795. # rate would reach the archive as a NaN or inf cost, and every later
  796. # comparison against it is False, so nothing downstream would correct it.
  797. rate = price / weight
  798. return rate if math.isfinite(rate) else None
  799. @dataclass
  800. class _PrintCost:
  801. """What a print cost, accumulated as each slot is actually charged.
  802. Only grams that were both charged to a spool *and* priced from it are
  803. counted. Everything else -- a slot whose spool has no price, a tray with no
  804. Spoolman row at all, filament the 3MF never attributed -- is left for the
  805. caller to cover at the global default rate, in one subtraction against the
  806. archive's own total. That is the same shape as the built-in inventory
  807. writer's untracked-grams top-up (#1344), and it means a partially priced
  808. print reports a whole-print figure rather than only the priced share.
  809. """
  810. cost: float = 0.0
  811. priced_grams: float = 0.0
  812. unpriced_grams: float = 0.0
  813. priced: int = 0
  814. unpriced: int = 0
  815. def add(self, grams: float, spool: dict | None, label: str) -> None:
  816. """Price ``grams`` off ``spool``. Call only after the charge succeeded."""
  817. if grams <= 0:
  818. return
  819. rate = _spool_cost_per_gram(spool)
  820. if rate is None:
  821. self.unpriced += 1
  822. self.unpriced_grams += grams
  823. logger.debug("[SPOOLMAN] %s: spool has no usable price, will fall back to the default rate", label)
  824. return
  825. self.cost += grams * rate
  826. self.priced_grams += grams
  827. self.priced += 1
  828. def _swap_splitter(printer_id: int, state, layer_usage_raw, end_layer: int, completed: bool) -> spool_swaps.Splitter:
  829. """The print's spool swaps (services.spool_swaps) for the Spoolman charges.
  830. ``layer_usage_raw`` is the tracking row's JSON, keys as strings; parsed only
  831. when a tray was actually swapped.
  832. """
  833. if state is None:
  834. from backend.app.services.printer_manager import printer_manager
  835. state = printer_manager.get_status(printer_id)
  836. if not isinstance(end_layer, int) or end_layer <= 0:
  837. # The caller reads the printer's layers for single-slot prints only;
  838. # a multi-colour print's swap needs them as much.
  839. total = getattr(state, "total_layers", 0)
  840. current = getattr(state, "layer_num", 0)
  841. end_layer = max(total if isinstance(total, int) else 0, current if isinstance(current, int) else 0)
  842. def layer_usage():
  843. if not layer_usage_raw:
  844. return None
  845. return {int(layer): {int(fid): mm for fid, mm in f.items()} for layer, f in layer_usage_raw.items()}
  846. return spool_swaps.Splitter(
  847. getattr(state, "spool_change_log", None) if state is not None else None,
  848. spool_swaps.SPOOLMAN,
  849. layer_usage,
  850. end_layer,
  851. completed=completed,
  852. )
  853. async def _fetch_spool_for_price(client, spool_id: int) -> dict | None:
  854. """The spool row a swapped-in spool's share is priced from; None if it can't be read."""
  855. try:
  856. return await client.get_spool(spool_id)
  857. except Exception as exc: # noqa: BLE001 -- pricing is best-effort, the charge is not
  858. logger.debug("[SPOOLMAN] Could not fetch spool %s for its price: %s", spool_id, exc)
  859. return None
  860. async def _report_spool_usage_for_slots(
  861. client,
  862. filament_usage_items: list[tuple[int, float]],
  863. ams_trays: dict[int, dict],
  864. slot_to_tray: list | None,
  865. method_label: str,
  866. printer_serial: str = "",
  867. printer_id: int | None = None,
  868. slot_colors_out: dict[int, str] | None = None,
  869. slot_materials_out: dict[int, str] | None = None,
  870. cost_out: _PrintCost | None = None,
  871. swaps: spool_swaps.Splitter | None = None,
  872. ) -> int:
  873. """Report usage to Spoolman for a list of (slot_id, grams) pairs.
  874. Resolution order per slot: (1) Spoolman extra.tag match against the
  875. tray's RFID or deterministic fallback tag, (2) #1459 fallback —
  876. local spoolman_slot_assignments table keyed by (printer_id, ams_id,
  877. tray_id). Without (2), tag-less spools assigned via the Bambuddy UI
  878. never get their weight decremented because their extra.tag is empty
  879. on the Spoolman side.
  880. When ``slot_colors_out`` is provided it is populated with
  881. ``{slot_id: color_hex}`` for every resolved spool — used by
  882. :func:`report_usage` to stamp the archive's filament colour from the
  883. Spoolman spool rather than the slicer's 3MF value (#1494).
  884. Returns number of spools successfully updated.
  885. """
  886. spools_updated = 0
  887. for slot_id, grams_used in filament_usage_items:
  888. if grams_used <= 0:
  889. continue
  890. global_tray_id = _resolve_global_tray_id(slot_id, slot_to_tray, ams_trays)
  891. tray_info = ams_trays.get(global_tray_id)
  892. if not tray_info:
  893. logger.debug("[SPOOLMAN] Slot %s: no tray at global_tray_id %s", slot_id, global_tray_id)
  894. continue
  895. is_external = global_tray_id >= 254
  896. tray_type = tray_info.get("tray_type", "")
  897. logger.debug(
  898. "[SPOOLMAN] Slot %s resolved to global_tray_id %s (tray_type=%s, external=%s)",
  899. slot_id,
  900. global_tray_id,
  901. tray_type or "unknown",
  902. is_external,
  903. )
  904. spool_id_to_use: int | None = None
  905. resolution_path = ""
  906. # color_hex + material of the resolved spool's filament, for the #1494
  907. # archive colour rewrite and the #2563 type rewrite. The tag path
  908. # already has the full spool object; the slot-assignment path only
  909. # yields an id and is fetched below.
  910. spool_color_hex: str | None = None
  911. spool_material: str | None = None
  912. # Full spool row, kept so the price fields (#2591) can be read from the
  913. # same fetch the colour and material already pay for.
  914. spool_obj: dict | None = None
  915. spool_tag = _resolve_spool_tag(tray_info, printer_serial, global_tray_id)
  916. if spool_tag:
  917. spool = await client.find_spool_by_tag(spool_tag)
  918. if spool:
  919. spool_id_to_use = spool["id"]
  920. resolution_path = "tag"
  921. spool_obj = spool
  922. spool_color_hex = (spool.get("filament") or {}).get("color_hex")
  923. spool_material = (spool.get("filament") or {}).get("material")
  924. if spool_id_to_use is None and printer_id is not None:
  925. ams_id, tray_id = _global_tray_id_to_ams_slot(global_tray_id)
  926. spool_id_to_use = await _resolve_spool_id_via_slot_assignment(printer_id, ams_id, tray_id)
  927. if spool_id_to_use is not None:
  928. resolution_path = "slot-assignment"
  929. # A spool swapped by hand on this tray mid-print shares the slot's
  930. # grams with the one it replaced (#3325); the first is the archive's.
  931. shares = None
  932. if swaps is not None:
  933. shares = swaps.shares(*_global_tray_id_to_ams_slot(global_tray_id), slot_id, grams_used)
  934. if shares and shares[0][0] != spool_id_to_use:
  935. spool_id_to_use = shares[0][0]
  936. resolution_path = "swap log"
  937. spool_obj = spool_color_hex = spool_material = None
  938. if spool_id_to_use is None:
  939. logger.debug(
  940. "[SPOOLMAN] Slot %s: no spool resolved (tag=%s, no slot-assignment)",
  941. slot_id,
  942. spool_tag[:16] if spool_tag else "none",
  943. )
  944. continue
  945. # Record the spool's filament colour + material for the archive
  946. # rewrites (#1494, #2563). The slot-assignment path resolved only an
  947. # id, so fetch the spool once for whichever value is still missing.
  948. # Strictly best-effort: a fetch failure must never abort the weight
  949. # reporting for the remaining slots, so the catch is broad.
  950. if slot_colors_out is not None or slot_materials_out is not None or cost_out is not None:
  951. need_color = slot_colors_out is not None and spool_color_hex is None
  952. need_material = slot_materials_out is not None and spool_material is None
  953. need_price = cost_out is not None and spool_obj is None
  954. if need_color or need_material or need_price:
  955. try:
  956. spool_obj = await client.get_spool(spool_id_to_use)
  957. _fil = spool_obj.get("filament") or {}
  958. if need_color:
  959. spool_color_hex = _fil.get("color_hex")
  960. if need_material:
  961. spool_material = _fil.get("material")
  962. except Exception as exc: # noqa: BLE001 — colour/material/price are non-critical
  963. logger.debug("[SPOOLMAN] Slot %s: could not fetch spool filament: %s", slot_id, exc)
  964. if slot_colors_out is not None and spool_color_hex:
  965. slot_colors_out[slot_id] = spool_color_hex
  966. if slot_materials_out is not None and spool_material:
  967. slot_materials_out[slot_id] = spool_material
  968. for charge_spool_id, charge_grams in shares or [(spool_id_to_use, grams_used)]:
  969. charge_obj = spool_obj
  970. if shares:
  971. charge_grams = round(charge_grams, 2)
  972. if charge_grams <= 0:
  973. continue
  974. if charge_spool_id != spool_id_to_use:
  975. charge_obj = await _fetch_spool_for_price(client, charge_spool_id) if cost_out is not None else None
  976. try:
  977. await client.use_spool(charge_spool_id, charge_grams)
  978. logger.info(
  979. "[SPOOLMAN] %s: slot %s: %sg -> spool %s (via %s)",
  980. method_label,
  981. slot_id,
  982. charge_grams,
  983. charge_spool_id,
  984. resolution_path,
  985. )
  986. spools_updated += 1
  987. # Priced only after the charge landed, so a spool Spoolman refused
  988. # cannot contribute to what the print is said to have cost.
  989. if cost_out is not None:
  990. cost_out.add(charge_grams, charge_obj, f"Slot {slot_id}")
  991. except (SpoolmanNotFoundError, SpoolmanClientError, SpoolmanUnavailableError) as exc:
  992. logger.warning("[SPOOLMAN] Failed to record usage for spool %s: %s", charge_spool_id, exc)
  993. return spools_updated
  994. async def _report_spool_usage_split_by_tray_changes(
  995. client,
  996. filament_usage: list[dict],
  997. tray_changes: list[tuple[int, int]],
  998. ams_trays: dict[int, dict],
  999. layer_usage: dict[int, dict[int, float]] | None,
  1000. filament_properties: dict | None,
  1001. total_layers: int,
  1002. last_layer_num: int,
  1003. method_label: str,
  1004. printer_serial: str,
  1005. printer_id: int,
  1006. slot_colors_out: dict[int, str] | None = None,
  1007. slot_materials_out: dict[int, str] | None = None,
  1008. cost_out: _PrintCost | None = None,
  1009. swaps: spool_swaps.Splitter | None = None,
  1010. ) -> tuple[int, set[int]]:
  1011. """Split each slot's grams across ``tray_changes`` and charge per-segment.
  1012. Mirrors ``usage_tracker`` Path 1's tray-switch branch so Spoolman and
  1013. the internal Spool inventory attribute mid-print AMS-backup switches
  1014. identically (#1793 — reporter's origin spool was over-charged the
  1015. whole print because this path didn't exist). ``compute_tray_split_grams``
  1016. holds the shared segment-math; this function wraps the per-segment
  1017. spool resolution + ``use_spool`` sink for the Spoolman side.
  1018. Returns ``(spools_updated, handled_global_tray_ids)`` — the caller
  1019. passes ``handled_global_tray_ids`` into the remain-delta fallback so
  1020. a tray attributed here is not double-charged there.
  1021. """
  1022. from backend.app.utils.tray_split import compute_tray_split_grams
  1023. spools_updated = 0
  1024. handled_global_tray_ids: set[int] = set()
  1025. for usage in filament_usage:
  1026. slot_id = usage.get("slot_id", 0)
  1027. total_weight = usage.get("used_g", 0)
  1028. if total_weight <= 0 or slot_id <= 0:
  1029. continue
  1030. props = (filament_properties or {}).get(str(slot_id)) or (filament_properties or {}).get(slot_id) or {}
  1031. segments = compute_tray_split_grams(
  1032. tray_changes=tray_changes,
  1033. total_weight=float(total_weight),
  1034. slot_id=slot_id,
  1035. layer_usage=layer_usage,
  1036. density=float(props.get("density", 1.24)),
  1037. diameter=float(props.get("diameter", 1.75)),
  1038. total_layers=total_layers,
  1039. last_layer_num=last_layer_num,
  1040. )
  1041. for seg_idx, tray_global, segment_grams in segments:
  1042. if segment_grams <= 0:
  1043. continue
  1044. # Mark this tray as handled BEFORE the resolution attempt so
  1045. # remain-delta doesn't double-charge it, even if we fail to
  1046. # find a spool below. Matches usage_tracker behaviour: the
  1047. # tray was physically fed from during this print, whether or
  1048. # not Spoolman happens to have a matching row.
  1049. handled_global_tray_ids.add(tray_global)
  1050. tray_info = ams_trays.get(tray_global) or {}
  1051. spool_id_to_use: int | None = None
  1052. resolution_path = ""
  1053. spool_color_hex: str | None = None
  1054. spool_material: str | None = None
  1055. spool_obj: dict | None = None
  1056. spool_tag = _resolve_spool_tag(tray_info, printer_serial, tray_global) if tray_info else ""
  1057. if spool_tag:
  1058. spool = await client.find_spool_by_tag(spool_tag)
  1059. if spool:
  1060. spool_id_to_use = spool["id"]
  1061. resolution_path = "tag"
  1062. spool_obj = spool
  1063. spool_color_hex = (spool.get("filament") or {}).get("color_hex")
  1064. spool_material = (spool.get("filament") or {}).get("material")
  1065. if spool_id_to_use is None:
  1066. seg_ams_id, seg_tray_id = _global_tray_id_to_ams_slot(tray_global)
  1067. spool_id_to_use = await _resolve_spool_id_via_slot_assignment(printer_id, seg_ams_id, seg_tray_id)
  1068. if spool_id_to_use is not None:
  1069. resolution_path = "slot-assignment"
  1070. # A spool swapped by hand on this tray within the segment (#3325).
  1071. seg_shares = None
  1072. if swaps is not None:
  1073. is_last = seg_idx + 1 >= len(tray_changes)
  1074. seg_shares = swaps.shares(
  1075. *_global_tray_id_to_ams_slot(tray_global),
  1076. slot_id,
  1077. segment_grams,
  1078. tray_changes[seg_idx][1],
  1079. None if is_last else tray_changes[seg_idx + 1][1],
  1080. )
  1081. if seg_shares and seg_shares[0][0] != spool_id_to_use:
  1082. spool_id_to_use = seg_shares[0][0]
  1083. resolution_path = "swap log"
  1084. spool_obj = spool_color_hex = spool_material = None
  1085. if spool_id_to_use is None:
  1086. logger.info(
  1087. "[SPOOLMAN] Split slot %s seg %s tray=%d: no spool resolved — %.2fg lost from split accounting",
  1088. slot_id,
  1089. seg_idx,
  1090. tray_global,
  1091. segment_grams,
  1092. )
  1093. continue
  1094. # Colour (#1494) + material (#2563) rewrite — first segment for a
  1095. # slot wins. The UI displays a single colour/type per slot, so
  1096. # later segments on the same slot don't overwrite (a backup swap
  1097. # can differ but the archive card stays consistent with the origin).
  1098. need_color = slot_colors_out is not None and slot_id not in slot_colors_out and spool_color_hex is None
  1099. need_material = (
  1100. slot_materials_out is not None and slot_id not in slot_materials_out and spool_material is None
  1101. )
  1102. # Unlike the colour, every segment needs its own price: each was
  1103. # charged to its own spool, and a backup roll can have cost
  1104. # something different from the one it replaced.
  1105. need_price = cost_out is not None and spool_obj is None
  1106. if need_color or need_material or need_price:
  1107. try:
  1108. spool_obj = await client.get_spool(spool_id_to_use)
  1109. _fil = spool_obj.get("filament") or {}
  1110. if need_color:
  1111. spool_color_hex = _fil.get("color_hex")
  1112. if need_material:
  1113. spool_material = _fil.get("material")
  1114. except Exception as exc: # noqa: BLE001 — colour/material/price are non-critical
  1115. logger.debug("[SPOOLMAN] Split slot %s: could not fetch spool filament: %s", slot_id, exc)
  1116. if slot_colors_out is not None and slot_id not in slot_colors_out and spool_color_hex:
  1117. slot_colors_out[slot_id] = spool_color_hex
  1118. if slot_materials_out is not None and slot_id not in slot_materials_out and spool_material:
  1119. slot_materials_out[slot_id] = spool_material
  1120. for charge_spool_id, charge_grams in seg_shares or [(spool_id_to_use, segment_grams)]:
  1121. if charge_grams <= 0:
  1122. continue
  1123. charge_obj = spool_obj
  1124. if charge_spool_id != spool_id_to_use:
  1125. charge_obj = await _fetch_spool_for_price(client, charge_spool_id) if cost_out is not None else None
  1126. try:
  1127. await client.use_spool(charge_spool_id, round(charge_grams, 2))
  1128. logger.info(
  1129. "[SPOOLMAN] %s: slot %s seg %s tray=%d: %.2fg -> spool %s (via %s)",
  1130. method_label,
  1131. slot_id,
  1132. seg_idx,
  1133. tray_global,
  1134. charge_grams,
  1135. charge_spool_id,
  1136. resolution_path,
  1137. )
  1138. spools_updated += 1
  1139. if cost_out is not None:
  1140. cost_out.add(round(charge_grams, 2), charge_obj, f"Split slot {slot_id} seg {seg_idx}")
  1141. except (SpoolmanNotFoundError, SpoolmanClientError, SpoolmanUnavailableError) as exc:
  1142. logger.warning(
  1143. "[SPOOLMAN] Split slot %s seg %s: failed to record usage for spool %s: %s",
  1144. slot_id,
  1145. seg_idx,
  1146. charge_spool_id,
  1147. exc,
  1148. )
  1149. return spools_updated, handled_global_tray_ids
  1150. async def _report_partial_usage(
  1151. printer_id: int,
  1152. tracking,
  1153. last_layer_num: int | None = None,
  1154. last_progress: int | None = None,
  1155. cost_out: _PrintCost | None = None,
  1156. ):
  1157. """Report partial filament usage based on actual G-code layer data.
  1158. Uses per-layer cumulative extrusion from G-code parsing for accurate
  1159. multi-material tracking. Falls back to linear interpolation if G-code
  1160. data is unavailable.
  1161. """
  1162. from backend.app.services.printer_manager import printer_manager
  1163. from backend.app.utils.threemf_tools import (
  1164. get_cumulative_usage_at_layer,
  1165. mm_to_grams,
  1166. slicer_grams_at_layer,
  1167. )
  1168. async with async_session() as db:
  1169. from backend.app.api.routes.settings import get_setting
  1170. # Check if partial usage reporting is enabled (default: true)
  1171. report_partial = await get_setting(db, "spoolman_report_partial_usage")
  1172. if report_partial and report_partial.lower() == "false":
  1173. logger.debug("[SPOOLMAN] Partial usage reporting disabled by setting")
  1174. return
  1175. # Check if Spoolman is enabled
  1176. spoolman_enabled = await get_setting(db, "spoolman_enabled")
  1177. if not spoolman_enabled or spoolman_enabled.lower() != "true":
  1178. return
  1179. # Get current printer state for layer progress.
  1180. # On failed/aborted prints the firmware may already reset to IDLE with layer=0,
  1181. # so we fall back to completion-time hints captured from MQTT.
  1182. state = printer_manager.get_status(printer_id)
  1183. current_layer = state.layer_num if state else None
  1184. total_layers = state.total_layers if state else None
  1185. if (not current_layer or current_layer <= 0) and last_layer_num and last_layer_num > 0:
  1186. current_layer = last_layer_num
  1187. logger.debug("[SPOOLMAN] Using captured last_layer_num=%s for partial usage", current_layer)
  1188. progress_ratio_from_event = None
  1189. if last_progress is not None:
  1190. try:
  1191. progress_ratio_from_event = min(max(float(last_progress), 0.0), 100.0) / 100.0
  1192. except (TypeError, ValueError):
  1193. progress_ratio_from_event = None
  1194. if (not current_layer or current_layer <= 0) and progress_ratio_from_event and total_layers and total_layers > 0:
  1195. current_layer = max(1, int(round(total_layers * progress_ratio_from_event)))
  1196. logger.debug(
  1197. "[SPOOLMAN] Estimated layer from last_progress=%s%% and total_layers=%s -> %s",
  1198. last_progress,
  1199. total_layers,
  1200. current_layer,
  1201. )
  1202. if not current_layer or current_layer <= 0:
  1203. logger.debug(
  1204. "[SPOOLMAN] No progress to report (layer 0/unknown, last_layer_num=%s, last_progress=%s)",
  1205. last_layer_num,
  1206. last_progress,
  1207. )
  1208. return
  1209. logger.info("[SPOOLMAN] Reporting partial usage at layer %s/%s", current_layer, total_layers or "?")
  1210. # Get tracking data
  1211. layer_usage = tracking.layer_usage
  1212. filament_properties = tracking.filament_properties or {}
  1213. filament_usage = tracking.filament_usage or []
  1214. ams_trays = {int(k): v for k, v in (tracking.ams_trays or {}).items()}
  1215. slot_to_tray = tracking.slot_to_tray
  1216. tray_remain_start = tracking.tray_remain_start or {}
  1217. printer_serial = await _get_printer_serial(printer_id)
  1218. client = await _get_spoolman_client_with_fallback()
  1219. if not client:
  1220. logger.warning("[SPOOLMAN] Not reachable for partial usage reporting")
  1221. return
  1222. # No-3MF aborted print (#1820 mirror of the completion path): nothing in
  1223. # filament_usage or layer_usage to base partial estimates on, but the
  1224. # remain%-delta snapshot we captured at start still describes consumption
  1225. # up to the abort moment. Write it the same way report_usage's fallback
  1226. # does, then return — there's no 3MF-derived partial to layer on top.
  1227. # ``state`` was already fetched at the top of the function for current_layer.
  1228. if not filament_usage and not layer_usage and tray_remain_start:
  1229. current_lookup = _snapshot_tray_remain(state.raw_data) if state and state.raw_data else {}
  1230. await _report_remain_delta_for_slots(
  1231. client,
  1232. printer_id=printer_id,
  1233. tray_remain_start=tray_remain_start,
  1234. current_lookup=current_lookup,
  1235. handled_global_tray_ids=set(),
  1236. archive_id=getattr(tracking, "archive_id", -1),
  1237. print_used_keys=_print_used_tray_keys(slot_to_tray, getattr(tracking, "tray_now_at_start", None), state),
  1238. cost_out=cost_out,
  1239. )
  1240. return
  1241. # Same recovery the completion path does, for the same reason: a print
  1242. # dispatched from Studio over the cloud left print start with no mapping to
  1243. # store, and both paths below feed ``slot_to_tray`` to
  1244. # ``_resolve_global_tray_id`` (#2768). An aborted print charges the wrong
  1245. # spool just as readily as a finished one. A mapping that names no tray
  1246. # for any used slot is no mapping either, as in the built-in inventory
  1247. # (#3405).
  1248. unmapped = _mapping_names_no_used_tray(slot_to_tray, filament_usage)
  1249. if unmapped:
  1250. logger.info("[SPOOLMAN] Partial usage: mapping %s names no tray for the used slots, ignoring it", slot_to_tray)
  1251. slot_to_tray = None
  1252. if not slot_to_tray:
  1253. slot_to_tray, _partial_mapping_source = _resolve_slot_to_tray_fallback(
  1254. printer_id,
  1255. filament_usage,
  1256. getattr(tracking, "tray_now_at_start", None),
  1257. tray_state_only=unmapped,
  1258. )
  1259. logger.info(
  1260. "[SPOOLMAN] Partial usage: slot_to_tray=%s (source: %s)",
  1261. slot_to_tray,
  1262. _partial_mapping_source,
  1263. )
  1264. # A single-filament print that switched trays (AMS backup on a runout)
  1265. # charges each tray its share of the partial grams, by layer up to where
  1266. # the print stopped, as the built-in inventory and the completion path do
  1267. # (#1793). The G-code split is not used here: its last segment runs to the
  1268. # end of the whole print. A multi-material print keeps the mapping.
  1269. tray_changes = list(getattr(state, "tray_change_log", None) or []) if state else []
  1270. # Spools swapped by hand on a tray, shared out up to where the print stopped (#3325).
  1271. swaps = _swap_splitter(printer_id, state, layer_usage, current_layer, completed=False)
  1272. async def _charge(usage_items: list[tuple[int, float]], method_label: str) -> int:
  1273. used = [(slot_id, grams) for slot_id, grams in usage_items if grams > 0]
  1274. if len(used) == 1 and len(tray_changes) > 1:
  1275. # Never short of a logged switch: a stop layer estimated from
  1276. # progress could otherwise give a segment more than the total.
  1277. split_layers = max([current_layer] + [layer for _, layer in tray_changes])
  1278. updated, _ = await _report_spool_usage_split_by_tray_changes(
  1279. client,
  1280. [{"slot_id": used[0][0], "used_g": used[0][1]}],
  1281. tray_changes,
  1282. ams_trays,
  1283. None,
  1284. filament_properties,
  1285. split_layers,
  1286. split_layers,
  1287. f"{method_label} split",
  1288. printer_serial,
  1289. printer_id=printer_id,
  1290. cost_out=cost_out,
  1291. swaps=swaps,
  1292. )
  1293. return updated
  1294. return await _report_spool_usage_for_slots(
  1295. client,
  1296. usage_items,
  1297. ams_trays,
  1298. slot_to_tray,
  1299. method_label,
  1300. printer_serial,
  1301. printer_id=printer_id,
  1302. cost_out=cost_out,
  1303. swaps=swaps,
  1304. )
  1305. # Try to use accurate G-code parsed data
  1306. if layer_usage:
  1307. layer_usage_int = {
  1308. int(layer): {int(fid): mm for fid, mm in filaments.items()} for layer, filaments in layer_usage.items()
  1309. }
  1310. usage_mm = get_cumulative_usage_at_layer(layer_usage_int, current_layer)
  1311. if usage_mm:
  1312. logger.info("[SPOOLMAN] Using G-code parsed data for layer %s", current_layer)
  1313. # On the slicer's weights, as a finished print is (#3317); the
  1314. # rest from Spoolman densities with 3MF fallback.
  1315. scaled = slicer_grams_at_layer(layer_usage_int, current_layer, filament_usage)
  1316. # A finished print charges only the slots the slicer weighed, so a
  1317. # cancelled one charges no others either.
  1318. weighed = {u.get("slot_id") for u in filament_usage if (u.get("used_g") or 0) > 0}
  1319. usage_items = []
  1320. for filament_id, mm_used in usage_mm.items():
  1321. slot_id = filament_id + 1 # filament_id is 0-based, slot_id is 1-based
  1322. if filament_usage and slot_id not in weighed:
  1323. continue
  1324. if slot_id in scaled:
  1325. usage_items.append((slot_id, round(scaled[slot_id], 2)))
  1326. continue
  1327. # Get density from Spoolman (most accurate), fall back to 3MF, then PLA default
  1328. global_tray_id = _resolve_global_tray_id(slot_id, slot_to_tray, ams_trays)
  1329. tray_info = ams_trays.get(global_tray_id)
  1330. density = None
  1331. diameter = 1.75
  1332. if tray_info:
  1333. spool_tag = _resolve_spool_tag(tray_info, printer_serial, global_tray_id)
  1334. if spool_tag:
  1335. spool = await client.find_spool_by_tag(spool_tag)
  1336. if spool:
  1337. filament_data = spool.get("filament", {})
  1338. density = filament_data.get("density")
  1339. diameter = filament_data.get("diameter", 1.75)
  1340. if not density:
  1341. props = filament_properties.get(str(slot_id), filament_properties.get(slot_id, {}))
  1342. density = props.get("density", 1.24)
  1343. logger.debug("[SPOOLMAN] Using fallback density %s for slot %s", density, slot_id)
  1344. grams_used = round(mm_to_grams(mm_used, diameter, density), 2)
  1345. usage_items.append((slot_id, grams_used))
  1346. spools_updated = await _charge(usage_items, "Partial (G-code)")
  1347. if spools_updated > 0:
  1348. logger.info("[SPOOLMAN] Reported partial usage to %s spool(s) using G-code data", spools_updated)
  1349. return
  1350. # Fallback: linear interpolation (if no G-code data available)
  1351. progress_ratio = None
  1352. if total_layers and total_layers > 0:
  1353. progress_ratio = min(current_layer / total_layers, 1.0)
  1354. elif progress_ratio_from_event is not None:
  1355. progress_ratio = progress_ratio_from_event
  1356. if progress_ratio is None:
  1357. logger.debug(
  1358. "[SPOOLMAN] Cannot use linear fallback: total_layers=%s, last_progress=%s",
  1359. total_layers,
  1360. last_progress,
  1361. )
  1362. return
  1363. logger.info("[SPOOLMAN] Falling back to linear interpolation (%s)", progress_ratio)
  1364. usage_items = []
  1365. for usage in filament_usage:
  1366. slot_id = usage.get("slot_id", 0)
  1367. total_used_g = usage.get("used_g", 0)
  1368. if total_used_g > 0:
  1369. partial_used_g = round(total_used_g * progress_ratio, 2)
  1370. usage_items.append((slot_id, partial_used_g))
  1371. spools_updated = await _charge(usage_items, "Partial (linear)")
  1372. if spools_updated > 0:
  1373. logger.info("[SPOOLMAN] Reported partial usage to %s spool(s) using linear interpolation", spools_updated)
  1374. async def report_usage(printer_id: int, archive_id: int):
  1375. """Report filament usage to Spoolman after print completion.
  1376. Two writers, mirroring the internal-inventory split in usage_tracker:
  1377. 1. **3MF path (primary)** — per-filament slice estimates captured at
  1378. print start drive a precise per-slot ``use_spool`` call.
  1379. 2. **AMS remain%-delta (fallback)** — for slots the 3MF path didn't
  1380. handle (including the no-3MF "Untitled" case from #1820): compute
  1381. ``start_remain - current_remain``, multiply by the resolved
  1382. Spoolman filament's reference weight, and write the delta. Mirrors
  1383. ``usage_tracker.on_print_complete`` Path 2 (line 517).
  1384. """
  1385. async with async_session() as db:
  1386. from backend.app.api.routes.settings import get_setting
  1387. from backend.app.models.active_print_spoolman import ActivePrintSpoolman
  1388. # Get tracking data stored at print start
  1389. result = await db.execute(
  1390. select(ActivePrintSpoolman)
  1391. .where(ActivePrintSpoolman.printer_id == printer_id)
  1392. .where(ActivePrintSpoolman.archive_id == archive_id)
  1393. )
  1394. tracking = result.scalar_one_or_none()
  1395. if not tracking:
  1396. logger.info("[SPOOLMAN] No tracking data for print (printer=%s, archive=%s)", printer_id, archive_id)
  1397. return
  1398. filament_usage = tracking.filament_usage or []
  1399. ams_trays = {int(k): v for k, v in (tracking.ams_trays or {}).items()}
  1400. slot_to_tray = tracking.slot_to_tray
  1401. tray_remain_start = tracking.tray_remain_start or {}
  1402. # ``layer_usage`` and ``filament_properties`` were added later than
  1403. # the base tracking fields; use ``getattr`` so tests that stub
  1404. # ``tracking`` as a lightweight SimpleNamespace stay valid, and
  1405. # historic ORM rows loaded without these columns can't AttributeError
  1406. # on read.
  1407. layer_usage_raw = getattr(tracking, "layer_usage", None) or {}
  1408. filament_properties = getattr(tracking, "filament_properties", None) or {}
  1409. tray_now_at_start = getattr(tracking, "tray_now_at_start", None)
  1410. printer_serial = await _get_printer_serial(printer_id)
  1411. # Delete tracking row (we're done with it)
  1412. await db.delete(tracking)
  1413. await db.commit()
  1414. if not filament_usage and not tray_remain_start:
  1415. logger.debug("[SPOOLMAN] No usage data or remain-snapshot for archive %s", archive_id)
  1416. return
  1417. # Check if Spoolman is enabled
  1418. spoolman_enabled = await get_setting(db, "spoolman_enabled")
  1419. if not spoolman_enabled or spoolman_enabled.lower() != "true":
  1420. return
  1421. client = await _get_spoolman_client_with_fallback()
  1422. if not client:
  1423. logger.warning(
  1424. "[SPOOLMAN] Not reachable for usage reporting (printer=%s, archive=%s): "
  1425. "this print's usage is not sent, correct the spools in Spoolman by hand",
  1426. printer_id,
  1427. archive_id,
  1428. )
  1429. return
  1430. # Consult the live printer state for the tray-change log written by
  1431. # ``bambu_mqtt.py`` on every mid-print ``tray_now`` change (#957).
  1432. # When there's more than one entry, the print traversed >1 AMS tray
  1433. # and the split path attributes each segment to the tray that was
  1434. # loaded at the time — matches the internal Spool inventory writer
  1435. # in ``usage_tracker.py``. Without this, an AMS-backup runout switch
  1436. # charges the whole slot to the origin spool and pushes it past
  1437. # ``initial_weight`` (#1793).
  1438. #
  1439. # Split only for SINGLE-slot prints — same gate as
  1440. # ``usage_tracker.py:1002``. Multi-slot (multi-colour) prints
  1441. # naturally cycle trays for every colour change, so splitting each
  1442. # slot's grams across ALL tray_change_log entries would attribute
  1443. # slot 1's grams to the segments where slot 2's tray was loaded and
  1444. # vice versa. Multi-slot prints fall through to the existing
  1445. # single-tray path (which uses the stable ``slot_to_tray`` mapping).
  1446. nonzero_slots = [u for u in filament_usage if u.get("used_g", 0) > 0]
  1447. tray_changes: list[tuple[int, int]] = []
  1448. _state = None
  1449. if len(nonzero_slots) == 1:
  1450. from backend.app.services.printer_manager import printer_manager as _pm
  1451. _state = _pm.get_status(printer_id)
  1452. if _state is not None:
  1453. tray_changes = list(getattr(_state, "tray_change_log", []) or [])
  1454. _total_layers = int(getattr(_state, "total_layers", 0) or 0) if _state else 0
  1455. _current_layer = int(getattr(_state, "layer_num", 0) or 0) if _state else 0
  1456. # For the linear-fallback denominator when total_layers is 0 (P1S
  1457. # firmware resets it at print end). At completion the current layer
  1458. # is the print's last valid layer.
  1459. _layer_denom_hint = _total_layers or _current_layer
  1460. # Recover the mapping when print start had nothing to store — the
  1461. # cloud-dispatched Studio print of #2768. Only the 3MF path consumes
  1462. # ``slot_to_tray``; the remain-delta path below resolves spools from the
  1463. # AMS slot directly, so there is nothing to recover for it.
  1464. mapping_source = "stored" if slot_to_tray else "none"
  1465. # A flat ams_mapping whose only used slot is an external spool's -1,
  1466. # sent without ams_mapping2 (#3166), names no tray at all: the
  1467. # fallback below reads the tray the printer fed from, as the built-in
  1468. # inventory does. Kept, it charged that spool nothing (#3405).
  1469. unmapped = _mapping_names_no_used_tray(slot_to_tray, filament_usage)
  1470. if unmapped:
  1471. logger.info(
  1472. "[SPOOLMAN] Archive %s: mapping %s names no tray for the used slots, ignoring it",
  1473. archive_id,
  1474. slot_to_tray,
  1475. )
  1476. slot_to_tray = None
  1477. mapping_source = "none"
  1478. if filament_usage and not slot_to_tray:
  1479. slot_to_tray, mapping_source = _resolve_slot_to_tray_fallback(
  1480. printer_id, filament_usage, tray_now_at_start, tray_state_only=unmapped
  1481. )
  1482. logger.info(
  1483. "[SPOOLMAN] Archive %s: slot_to_tray=%s (source: %s)",
  1484. archive_id,
  1485. slot_to_tray,
  1486. mapping_source,
  1487. )
  1488. # Nothing named a tray for this print and no fallback could recover
  1489. # one, so every slot is about to be resolved by position -- slicer slot
  1490. # 1 to the first loaded tray, and so on. That guess is right for an AMS
  1491. # loaded in slicer order and wrong for any other, and the caller has no
  1492. # way to tell which it got. Say so at a level that survives the default
  1493. # log filter, so a support bundle carries the reason (#2953).
  1494. #
  1495. # Excludes the tray-split path. It never reads ``slot_to_tray`` at all:
  1496. # it charges each segment to the tray the printer announced switching
  1497. # to, which is the same evidence the tray-state fallback is built on and
  1498. # is not a guess. Calling it one would suppress the archive rewrite for
  1499. # exactly the prints -- an AMS-backup runout on a Studio job (#1793 in
  1500. # #2768's conditions) -- whose attribution is best supported.
  1501. mapping_is_guess = bool(filament_usage) and mapping_source == "none" and len(tray_changes) <= 1
  1502. if mapping_is_guess:
  1503. logger.warning(
  1504. "[SPOOLMAN] Archive %s: no slot-to-tray mapping from any source -- "
  1505. "charging by tray position, which is a guess. Verify the spool weights "
  1506. "if the AMS is not loaded in slicer order.",
  1507. archive_id,
  1508. )
  1509. slot_colors: dict[int, str] = {}
  1510. slot_materials: dict[int, str] = {}
  1511. # Priced as each charge lands, so the figure the archive ends up with
  1512. # describes the same grams Spoolman actually had deducted (#2591).
  1513. print_cost = _PrintCost()
  1514. swaps = _swap_splitter(printer_id, _state, layer_usage_raw, _layer_denom_hint, completed=True)
  1515. handled_global_tray_ids: set[int] = set()
  1516. spools_updated = 0
  1517. # --- Path 1: 3MF per-slot estimates -----------------------------
  1518. if filament_usage:
  1519. if len(tray_changes) > 1:
  1520. # Tray-split path — attribute per-segment to the tray that
  1521. # was loaded at that time.
  1522. logger.info(
  1523. "[SPOOLMAN] Reporting per-filament usage for archive %s with tray-split "
  1524. "(tray_change_log=%s, denom_layers=%d)",
  1525. archive_id,
  1526. tray_changes,
  1527. _layer_denom_hint,
  1528. )
  1529. # ``tracking.layer_usage`` was serialized to JSON so int keys
  1530. # come back as strings. Restore them for the split math.
  1531. layer_usage = None
  1532. if layer_usage_raw:
  1533. try:
  1534. layer_usage = {
  1535. int(layer): {int(fid): mm for fid, mm in filaments.items()}
  1536. for layer, filaments in layer_usage_raw.items()
  1537. }
  1538. except (TypeError, ValueError, AttributeError):
  1539. # AttributeError catches ``inner.items()`` when the
  1540. # inner value isn't dict-shaped (corrupt JSON row).
  1541. # Missing gcode falls through to the linear-ratio
  1542. # branch inside ``compute_tray_split_grams`` — still
  1543. # gives a correct split, just less precise.
  1544. layer_usage = None
  1545. split_updated, split_handled = await _report_spool_usage_split_by_tray_changes(
  1546. client,
  1547. filament_usage,
  1548. tray_changes,
  1549. ams_trays,
  1550. layer_usage,
  1551. filament_properties,
  1552. _total_layers,
  1553. _layer_denom_hint,
  1554. f"Archive {archive_id}",
  1555. printer_serial,
  1556. printer_id=printer_id,
  1557. slot_colors_out=slot_colors,
  1558. slot_materials_out=slot_materials,
  1559. cost_out=print_cost,
  1560. swaps=swaps,
  1561. )
  1562. spools_updated += split_updated
  1563. handled_global_tray_ids |= split_handled
  1564. else:
  1565. logger.info("[SPOOLMAN] Reporting per-filament usage for archive %s", archive_id)
  1566. usage_items = [(u.get("slot_id", 0), u.get("used_g", 0)) for u in filament_usage]
  1567. spools_updated = await _report_spool_usage_for_slots(
  1568. client,
  1569. usage_items,
  1570. ams_trays,
  1571. slot_to_tray,
  1572. f"Archive {archive_id}",
  1573. printer_serial,
  1574. printer_id=printer_id,
  1575. slot_colors_out=slot_colors,
  1576. slot_materials_out=slot_materials,
  1577. cost_out=print_cost,
  1578. swaps=swaps,
  1579. )
  1580. # Track which physical slots the 3MF path already covered so
  1581. # Path 2 doesn't double-charge them.
  1582. for u in filament_usage:
  1583. if u.get("used_g", 0) <= 0:
  1584. # ``_report_spool_usage_for_slots`` skipped this slot
  1585. # before resolving a tray for it, so nothing was
  1586. # charged and Path 2 is free to cover the slot from
  1587. # remain% -- claiming it here would suppress a real
  1588. # drop on the strength of a zero-gram estimate.
  1589. continue
  1590. slot_id = u.get("slot_id", 0)
  1591. handled_global_tray_ids.add(_resolve_global_tray_id(slot_id, slot_to_tray, ams_trays))
  1592. # --- Path 2: AMS remain%-delta for slots 3MF didn't cover -------
  1593. # Triggered for no-3MF "Untitled" prints (#1820) AND for partial
  1594. # 3MF coverage (slots whose filament_id wasn't in slice_info).
  1595. if tray_remain_start:
  1596. from backend.app.services.printer_manager import printer_manager
  1597. current = printer_manager.get_status(printer_id)
  1598. current_lookup = _snapshot_tray_remain(current.raw_data) if current and current.raw_data else {}
  1599. fallback_updates = await _report_remain_delta_for_slots(
  1600. client,
  1601. printer_id=printer_id,
  1602. tray_remain_start=tray_remain_start,
  1603. current_lookup=current_lookup,
  1604. handled_global_tray_ids=handled_global_tray_ids,
  1605. archive_id=archive_id,
  1606. print_used_keys=_print_used_tray_keys(slot_to_tray, tray_now_at_start, current),
  1607. slot_colors_out=slot_colors,
  1608. slot_materials_out=slot_materials,
  1609. cost_out=print_cost,
  1610. )
  1611. spools_updated += fallback_updates
  1612. if spools_updated == 0:
  1613. logger.info("[SPOOLMAN] Archive %s: no spools updated", archive_id)
  1614. else:
  1615. logger.info("[SPOOLMAN] Archive %s: updated %s spool(s)", archive_id, spools_updated)
  1616. # Stamp the archive's filament colour from the matched Spoolman spools
  1617. # so it reflects the curated inventory colour, not the slicer's 3MF
  1618. # value (#1494) — mirrors the built-in inventory path in usage_tracker.
  1619. #
  1620. # Skipped when the mapping was a positional guess. Charging the wrong
  1621. # spool costs grams the owner can put back; rewriting the archive's
  1622. # colour and material on top of it overwrites what the slicer actually
  1623. # recorded, and the print then reads as a different filament than the
  1624. # one that made it, with nothing left to compare against (#2953).
  1625. if mapping_is_guess:
  1626. if slot_colors or slot_materials:
  1627. logger.info(
  1628. "[SPOOLMAN] Archive %s: leaving filament colour/type as sliced — "
  1629. "the spools were matched by position, not by a known mapping",
  1630. archive_id,
  1631. )
  1632. else:
  1633. await _apply_spool_colors_to_archive(db, archive_id, filament_usage, slot_colors)
  1634. # Same for the material: a slot mapped to a differently-typed spool
  1635. # than it was sliced for otherwise records the sliced type (#2563).
  1636. await _apply_spool_types_to_archive(db, archive_id, filament_usage, slot_materials)
  1637. # Cost is applied whether or not the mapping was a guess, unlike the
  1638. # colour and material above. Those overwrite what the slicer recorded,
  1639. # which is why a guess must not touch them; the cost has no such
  1640. # original -- archive.py's figure is itself derived from a default rate
  1641. # -- and the grams have already been deducted from these spools, so the
  1642. # archive should say what that deduction was worth.
  1643. await _apply_spool_cost_to_archive(db, archive_id, print_cost)
  1644. def _print_used_tray_keys(
  1645. slot_to_tray: list | None,
  1646. tray_now_at_start: int | None,
  1647. state,
  1648. ) -> set[tuple[int, int]]:
  1649. """Which AMS slots this print actually drew from, as far as we can tell.
  1650. Mirrors the guard the internal tracker has carried since #1269. Without
  1651. it, swapping a spool in a slot the print never touched drops that slot's
  1652. ``remain%``, and the remain-delta path reads the drop as consumption and
  1653. charges it to whoever the slot is assigned to. That is a phantom write to
  1654. an uninvolved spool, and it is likeliest on exactly the prints this
  1655. fallback serves -- ones with no 3MF, where nothing else limits which slots
  1656. are considered.
  1657. Three sources, matching the internal tracker's:
  1658. - the print's ``ams_mapping``, stored here as ``slot_to_tray``;
  1659. - every tray the printer switched to mid-print;
  1660. - the tray it was drawing from at the start.
  1661. An empty result means no evidence, not "no slots" -- callers must then
  1662. consider every slot, as before, or a printer that reports none of the
  1663. three would silently stop being tracked at all.
  1664. Takes the two stored values rather than the tracking row: the caller
  1665. deletes that row before it gets this far, and everything read off it is
  1666. read into locals beforehand.
  1667. """
  1668. keys: set[tuple[int, int]] = set()
  1669. for global_tray_id in list(slot_to_tray or []):
  1670. if isinstance(global_tray_id, int) and global_tray_id >= 0:
  1671. keys.add(_global_tray_id_to_ams_slot(global_tray_id))
  1672. for change in getattr(state, "tray_change_log", None) or []:
  1673. if isinstance(change, (tuple, list)) and change:
  1674. global_tray_id = change[0]
  1675. if isinstance(global_tray_id, int) and global_tray_id >= 0:
  1676. keys.add(_global_tray_id_to_ams_slot(global_tray_id))
  1677. if isinstance(tray_now_at_start, int) and 0 <= tray_now_at_start <= _MAX_REAL_TRAY_ID:
  1678. keys.add(_global_tray_id_to_ams_slot(tray_now_at_start))
  1679. return keys
  1680. async def _report_remain_delta_for_slots(
  1681. client,
  1682. *,
  1683. printer_id: int,
  1684. tray_remain_start: dict[str, dict],
  1685. current_lookup: dict[str, dict],
  1686. handled_global_tray_ids: set[int],
  1687. archive_id: int,
  1688. print_used_keys: set[tuple[int, int]] | None = None,
  1689. slot_colors_out: dict[int, str] | None = None,
  1690. slot_materials_out: dict[int, str] | None = None,
  1691. cost_out: _PrintCost | None = None,
  1692. ) -> int:
  1693. """AMS remain%-delta path: write ``(start - current) * filament.weight``
  1694. grams to Spoolman for slots the 3MF path didn't cover.
  1695. Mirrors ``usage_tracker.on_print_complete`` Path 2: per-slot, gated on a
  1696. valid current ``remain%``, skipped on spool swap (``tray_uuid`` changed),
  1697. using the resolved spool's filament reference weight rather than MQTT's
  1698. unreliable ``tray_weight`` (which is the failure mode #1119 documented).
  1699. """
  1700. spools_updated = 0
  1701. not_in_print: list[str] = []
  1702. for slot_key, start in tray_remain_start.items():
  1703. try:
  1704. ams_id_str, tray_id_str = slot_key.split("-", 1)
  1705. ams_id, tray_id = int(ams_id_str), int(tray_id_str)
  1706. except (ValueError, AttributeError):
  1707. continue
  1708. # Skip slots already handled by the 3MF path. Encoding mirrors
  1709. # build_ams_tray_lookup: VT trays land at 254/255, AMS-HT keeps
  1710. # its native id (>=128), regular AMS slots are ams_id*4+tray_id.
  1711. if ams_id == 255:
  1712. global_tray_id = 254 + tray_id
  1713. elif ams_id >= 128:
  1714. global_tray_id = ams_id
  1715. else:
  1716. global_tray_id = ams_id * 4 + tray_id
  1717. if global_tray_id in handled_global_tray_ids:
  1718. continue
  1719. # Slots the print never touched (#1269's guard, see _print_used_tray_keys).
  1720. # Only enforced when there is evidence of which slots it did use.
  1721. # Collected rather than logged per slot: on a four-AMS farm a
  1722. # single-colour print leaves fifteen of these, and they are the
  1723. # expected case, unlike the "consumed but charged nothing" lines below.
  1724. if print_used_keys and (ams_id, tray_id) not in print_used_keys:
  1725. not_in_print.append(f"AMS{ams_id}-T{tray_id}")
  1726. continue
  1727. current = current_lookup.get(slot_key)
  1728. if not current:
  1729. # Reported at info, like the internal tracker's equivalent: on a
  1730. # near-empty spool the AMS reports a negative remain%, which the
  1731. # snapshot gate rejects, and the slot that was actually printing
  1732. # disappears from this path entirely (#1820).
  1733. logger.info(
  1734. "[SPOOLMAN] AMS%d-T%d: no valid remain%% at completion, nothing charged for this slot", ams_id, tray_id
  1735. )
  1736. continue
  1737. # Spool swap mid-print — tray_uuid changed. We don't know how much
  1738. # of the print went to which spool; skip rather than mis-attribute.
  1739. start_uuid = (start.get("tray_uuid") or "").lower()
  1740. cur_uuid = (current.get("tray_uuid") or "").lower()
  1741. if start_uuid and cur_uuid and start_uuid != cur_uuid:
  1742. logger.info(
  1743. "[SPOOLMAN] AMS%d-T%d: spool swapped mid-print (uuid changed), skipping remain-delta", ams_id, tray_id
  1744. )
  1745. continue
  1746. delta_pct = start["remain"] - current["remain"]
  1747. if delta_pct <= 0:
  1748. # A fresh spool reads 100% for the first tens of grams and the AMS
  1749. # estimate drifts upward on its own, so this covers a real print
  1750. # that simply left no trace at AMS granularity -- not only a refill.
  1751. # Said out loud so it can be told apart from having nothing to
  1752. # charge, which is what "no spools updated" alone looked like.
  1753. logger.info(
  1754. "[SPOOLMAN] AMS%d-T%d: remain%% did not fall over the print (%d%% -> %d%%), nothing charged",
  1755. ams_id,
  1756. tray_id,
  1757. start["remain"],
  1758. current["remain"],
  1759. )
  1760. continue # No consumption captured at AMS granularity, or refilled
  1761. spool_id = await _resolve_spool_id_via_slot_assignment(printer_id, ams_id, tray_id)
  1762. if spool_id is None:
  1763. logger.info(
  1764. "[SPOOLMAN] AMS%d-T%d: consumed %d%% but has no Spoolman slot assignment, nothing charged",
  1765. ams_id,
  1766. tray_id,
  1767. delta_pct,
  1768. )
  1769. continue
  1770. # Look up the spool's net weight (#3194). Use a fresh GET so
  1771. # we don't depend on a stale cached_spools list. Failure here is
  1772. # silent-skip rather than fatal — other slots can still be written.
  1773. try:
  1774. spool = await client.get_spool(spool_id)
  1775. except Exception as exc: # noqa: BLE001
  1776. logger.debug("[SPOOLMAN] AMS%d-T%d: get_spool(%s) failed: %s", ams_id, tray_id, spool_id, exc)
  1777. continue
  1778. filament = spool.get("filament") or {}
  1779. ref_weight = _as_positive_number(spoolman_net_weight(spool))
  1780. if ref_weight is None:
  1781. logger.debug(
  1782. "[SPOOLMAN] AMS%d-T%d: spool %s has no initial_weight or filament.weight, skipping remain-delta",
  1783. ams_id,
  1784. tray_id,
  1785. spool_id,
  1786. )
  1787. continue
  1788. grams_used = round((delta_pct / 100.0) * ref_weight, 2)
  1789. if grams_used <= 0:
  1790. continue
  1791. try:
  1792. await client.use_spool(spool_id, grams_used)
  1793. except Exception as exc: # noqa: BLE001
  1794. logger.warning(
  1795. "[SPOOLMAN] AMS%d-T%d: use_spool(%s, %.2fg) failed: %s", ams_id, tray_id, spool_id, grams_used, exc
  1796. )
  1797. continue
  1798. spools_updated += 1
  1799. # ``spool`` here is the full row fetched above for its net weight,
  1800. # so the price is already in hand (#2591).
  1801. if cost_out is not None:
  1802. cost_out.add(grams_used, spool, f"AMS{ams_id}-T{tray_id}")
  1803. # No 3MF slot_id for this path — use the AMS slot key so the maps can
  1804. # still be inspected by callers if needed. The archive rewrites
  1805. # (#1494 colour, #2563 type) key on 3MF slot_ids, so remain-delta-only
  1806. # prints intentionally don't participate (matches usage_tracker's
  1807. # slot_id=None).
  1808. if slot_colors_out is not None:
  1809. color = filament.get("color_hex")
  1810. if color:
  1811. slot_colors_out[-(global_tray_id + 1)] = color
  1812. if slot_materials_out is not None:
  1813. material = filament.get("material")
  1814. if material:
  1815. slot_materials_out[-(global_tray_id + 1)] = material
  1816. logger.info(
  1817. "[SPOOLMAN] Archive %s AMS%d-T%d: %.2fg via remain-delta (%d%% of %.0fg) -> spool %s",
  1818. archive_id,
  1819. ams_id,
  1820. tray_id,
  1821. grams_used,
  1822. delta_pct,
  1823. ref_weight,
  1824. spool_id,
  1825. )
  1826. if not_in_print:
  1827. logger.info(
  1828. "[SPOOLMAN] Archive %s: slots not part of this print, left alone: %s",
  1829. archive_id,
  1830. ", ".join(not_in_print),
  1831. )
  1832. return spools_updated
  1833. async def _apply_spool_cost_to_archive(db, archive_id: int, print_cost: _PrintCost) -> None:
  1834. """Set an archive's cost from what the Spoolman spools that fed it are worth (#2591).
  1835. Until now this was the one thing the Spoolman integration was asked for by
  1836. name and did not do. ``archive.py`` prices a print once, at archive time,
  1837. from the built-in Filament catalogue matched on the primary type, falling
  1838. back to the global default rate -- and in Spoolman mode nothing ever
  1839. revisited that figure, because the per-spool recompute in
  1840. ``usage_tracker.on_print_complete`` only runs over rows the built-in
  1841. inventory wrote and Spoolman mode writes none. An install with an empty
  1842. catalogue therefore priced every print at the default no matter what the
  1843. linked spool actually cost.
  1844. Multi-material was wrong twice over there: the primary type's rate applied
  1845. to the *whole* print's grams, so a slot of expensive PA came out at the
  1846. price of the PLA next to it. Summing per charged slot is what fixes that,
  1847. and it falls out of pricing each charge as it is made rather than pricing a
  1848. total afterwards.
  1849. Grams that could not be priced are covered at the global default rate in a
  1850. single subtraction against the archive's own total -- a slot whose spool has
  1851. no price, a tray with no Spoolman row, and filament the 3MF never attributed
  1852. are all the same case. Without it a print with one priced slot out of four
  1853. would report a quarter of its cost, which is #1344 in a different inventory
  1854. mode.
  1855. Only on the first run, matching the built-in writer (#1378): reprint actuals
  1856. live in ``PrintLogEntry``, and the archive card keeps the first run's figure
  1857. so a failed 10 g reprint doesn't visually clobber a successful 100 g print.
  1858. Does nothing when no slot could be priced, leaving whatever ``archive.py``
  1859. recorded. That keeps an install with prices in neither place exactly where
  1860. it was.
  1861. """
  1862. if print_cost.priced == 0:
  1863. if print_cost.unpriced:
  1864. logger.info(
  1865. "[SPOOLMAN] Archive %s: %d charged spool(s) carry no price -- "
  1866. "leaving the cost as recorded at archive time",
  1867. archive_id,
  1868. print_cost.unpriced,
  1869. )
  1870. return
  1871. from backend.app.api.routes.settings import get_setting
  1872. from backend.app.models.archive import PrintArchive
  1873. archive = (await db.execute(select(PrintArchive).where(PrintArchive.id == archive_id))).scalar_one_or_none()
  1874. if archive is None:
  1875. return
  1876. total = print_cost.cost
  1877. archive_grams = archive.filament_used_grams or 0
  1878. unpriced_grams = max(0.0, archive_grams - print_cost.priced_grams)
  1879. if unpriced_grams > 0:
  1880. # Malformed settings must not cost the whole usage report; the rate is
  1881. # the least important thing this pass produces.
  1882. try:
  1883. _setting = await get_setting(db, "default_filament_cost")
  1884. default_cost_per_kg = float(_setting) if _setting else 25.0
  1885. except (TypeError, ValueError):
  1886. default_cost_per_kg = 25.0
  1887. if default_cost_per_kg > 0:
  1888. total += (unpriced_grams / 1000.0) * default_cost_per_kg
  1889. if total <= 0:
  1890. return
  1891. from backend.app.services.print_log import other_runs_of
  1892. if await other_runs_of(db, archive):
  1893. return
  1894. new_cost = round(total, 2)
  1895. if new_cost != archive.cost:
  1896. logger.info(
  1897. "[SPOOLMAN] Archive %s cost %s -> %s (%d slot(s) priced from Spoolman over %.2fg, "
  1898. "%.2fg at the default rate)",
  1899. archive_id,
  1900. archive.cost,
  1901. new_cost,
  1902. print_cost.priced,
  1903. print_cost.priced_grams,
  1904. unpriced_grams,
  1905. )
  1906. archive.cost = new_cost
  1907. await db.commit()
  1908. async def _apply_spool_colors_to_archive(
  1909. db,
  1910. archive_id: int,
  1911. filament_usage: list[dict],
  1912. slot_colors: dict[int, str],
  1913. ) -> None:
  1914. """Overwrite an archive's ``filament_color`` with the colours of the
  1915. Spoolman spools that fed the print (#1494).
  1916. All-or-nothing, exactly like the built-in inventory path: the colour is
  1917. only rewritten when every used slot resolved to a spool that carries a
  1918. colour, so a partial match never drops slots from the archive.
  1919. """
  1920. if not slot_colors:
  1921. return
  1922. from backend.app.models.archive import PrintArchive
  1923. from backend.app.services.usage_tracker import (
  1924. _archive_colors_from_spools,
  1925. _spool_color_to_hex,
  1926. )
  1927. results = [{"slot_id": sid, "color": _spool_color_to_hex(hex_)} for sid, hex_ in slot_colors.items()]
  1928. colors = _archive_colors_from_spools(filament_usage, results)
  1929. if not colors:
  1930. return
  1931. archive = (await db.execute(select(PrintArchive).where(PrintArchive.id == archive_id))).scalar_one_or_none()
  1932. if archive is None:
  1933. return
  1934. joined = ",".join(colors)
  1935. if joined != archive.filament_color:
  1936. logger.info(
  1937. "[SPOOLMAN] Archive %s filament_color %r -> %r (from Spoolman spools)",
  1938. archive_id,
  1939. archive.filament_color,
  1940. joined,
  1941. )
  1942. archive.filament_color = joined
  1943. await db.commit()
  1944. async def _apply_spool_types_to_archive(
  1945. db,
  1946. archive_id: int,
  1947. filament_usage: list[dict],
  1948. slot_materials: dict[int, str],
  1949. ) -> None:
  1950. """Overwrite an archive's ``filament_type`` with the materials of the
  1951. Spoolman spools that fed the print (#2563).
  1952. All-or-nothing, exactly like the colour path and the built-in inventory
  1953. path: the type is only rewritten when every used slot resolved to a spool
  1954. that carries a material, so a partial match never drops slots from the
  1955. archive or the material statistics.
  1956. """
  1957. if not slot_materials:
  1958. return
  1959. from backend.app.models.archive import PrintArchive
  1960. from backend.app.services.usage_tracker import _archive_types_from_spools
  1961. results = [{"slot_id": sid, "material": material} for sid, material in slot_materials.items()]
  1962. types = _archive_types_from_spools(filament_usage, results)
  1963. if not types:
  1964. return
  1965. archive = (await db.execute(select(PrintArchive).where(PrintArchive.id == archive_id))).scalar_one_or_none()
  1966. if archive is None:
  1967. return
  1968. joined = ",".join(types)
  1969. if joined != archive.filament_type:
  1970. logger.info(
  1971. "[SPOOLMAN] Archive %s filament_type %r -> %r (from Spoolman spools)",
  1972. archive_id,
  1973. archive.filament_type,
  1974. joined,
  1975. )
  1976. archive.filament_type = joined
  1977. await db.commit()