usage_tracker.py 88 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087
  1. """Automatic filament consumption tracking.
  2. Captures AMS tray remain% at print start, then computes consumption
  3. deltas at print complete to update spool weight_used and last_used.
  4. Primary tracking uses 3MF slicer estimates (precise per-filament data).
  5. AMS remain% delta is the fallback for trays not covered by 3MF data.
  6. """
  7. import asyncio
  8. import json
  9. import logging
  10. import re
  11. from dataclasses import dataclass, field
  12. from datetime import datetime, timezone
  13. from sqlalchemy import func, select, update
  14. from sqlalchemy.ext.asyncio import AsyncSession
  15. from backend.app.models.spool import Spool
  16. from backend.app.models.spool_assignment import SpoolAssignment
  17. from backend.app.models.spool_usage_history import SpoolUsageHistory
  18. logger = logging.getLogger(__name__)
  19. def _decode_mqtt_mapping(mapping_raw: list | None) -> list[int] | None:
  20. """Decode MQTT mapping field (snow-encoded) to bambuddy global tray IDs.
  21. The printer's MQTT mapping field is an array indexed by slicer filament slot
  22. (0-based). Each value uses snow encoding: ams_hw_id * 256 + local_slot.
  23. 65535 means unmapped.
  24. Returns a list of bambuddy global tray IDs (or -1 for unmapped), or None if
  25. no valid mappings found.
  26. """
  27. if not isinstance(mapping_raw, list) or not mapping_raw:
  28. return None
  29. result = []
  30. for value in mapping_raw:
  31. if not isinstance(value, int) or value >= 65535:
  32. result.append(-1)
  33. continue
  34. ams_hw_id = value >> 8
  35. slot = value & 0xFF
  36. if 0 <= ams_hw_id <= 3:
  37. # Regular AMS: sequential global ID
  38. result.append(ams_hw_id * 4 + (slot & 0x03))
  39. elif 128 <= ams_hw_id <= 135:
  40. # AMS-HT: global ID is the hardware ID (one slot per unit)
  41. result.append(ams_hw_id)
  42. elif ams_hw_id in (254, 255):
  43. # External spool
  44. result.append(254 if slot != 255 else 255)
  45. else:
  46. result.append(-1)
  47. # Only return if at least one valid mapping exists
  48. if all(v < 0 for v in result):
  49. return None
  50. return result
  51. def _spool_color_to_hex(rgba: str | None) -> str | None:
  52. """Normalise a ``Spool.rgba`` value (``RRGGBBAA`` hex, no ``#``) to the
  53. ``#RRGGBB`` form archives store in ``filament_color``.
  54. Alpha is dropped — the archive colour list and the Color Distribution
  55. graph treat filament colour as opaque. Returns ``None`` for a missing or
  56. too-short value so the caller can fall back to the 3MF colour.
  57. """
  58. if not rgba:
  59. return None
  60. h = rgba.strip().lstrip("#")
  61. if len(h) < 6:
  62. return None
  63. return "#" + h[:6].upper()
  64. def _archive_colors_from_spools(filament_usage: list[dict], results: list[dict]) -> list[str] | None:
  65. """Slot-ordered, de-duplicated hex colours for an archive's ``filament_color``,
  66. taken from the inventory spools that actually fed the print (#1494).
  67. The slicer's 3MF carries its own ``filament_colour`` per slot — a value
  68. picked independently of the colour the user curates on the matched
  69. inventory spool. So an archive printed from a ``#000000`` inventory spool
  70. would otherwise show the slicer's near-black ``#161616``. Once usage
  71. tracking has resolved the used slots to spools, the spool colours are the
  72. authoritative source and replace the 3MF values.
  73. Returns ``None`` — leave the 3MF colour untouched — unless *every* slot
  74. with non-zero usage was matched to a spool that carries a colour. A
  75. partial rewrite would silently drop the unmatched slots' colours from the
  76. archive (and the Color Distribution graph), so it is all-or-nothing.
  77. """
  78. used_slots = {u["slot_id"] for u in filament_usage if u.get("used_g", 0) > 0 and u.get("slot_id") is not None}
  79. if not used_slots:
  80. return None
  81. slot_color: dict[int, str] = {}
  82. for r in results:
  83. slot_id = r.get("slot_id")
  84. color = r.get("color")
  85. if slot_id is not None and color:
  86. slot_color.setdefault(slot_id, color)
  87. if not used_slots.issubset(slot_color):
  88. return None
  89. ordered: list[str] = []
  90. for slot_id in sorted(used_slots):
  91. color = slot_color[slot_id]
  92. if color not in ordered:
  93. ordered.append(color)
  94. return ordered
  95. def _archive_types_from_spools(filament_usage: list[dict], results: list[dict]) -> list[str] | None:
  96. """Slot-ordered, de-duplicated materials for an archive's ``filament_type``,
  97. taken from the inventory spools that actually fed the print (#2563).
  98. The slicer's 3MF records the filament type it was *sliced for*. When the
  99. user manually maps a slot to a differently-typed loaded spool in the Print
  100. dialog — a PLA slice routed to the only loaded PETG slot — that sliced type
  101. misclassifies the run in the archive card, the Print Log and the material
  102. statistics, even though the deduction correctly hit the PETG spool. Once
  103. usage tracking has resolved every used slot to an inventory spool, the
  104. spool's declared material is the authoritative record of what was consumed,
  105. the same reasoning that already adopts the spool colour (#1494).
  106. Returns ``None`` — leave the 3MF type untouched — unless *every* slot with
  107. non-zero usage was matched to a spool that carries a material. All-or-
  108. nothing, exactly like ``_archive_colors_from_spools``: a partial rewrite
  109. would silently drop the unmatched slots' types from the archive (and the
  110. material stats).
  111. """
  112. used_slots = {u["slot_id"] for u in filament_usage if u.get("used_g", 0) > 0 and u.get("slot_id") is not None}
  113. if not used_slots:
  114. return None
  115. slot_material: dict[int, str] = {}
  116. for r in results:
  117. slot_id = r.get("slot_id")
  118. material = (r.get("material") or "").strip()
  119. if slot_id is not None and material:
  120. slot_material.setdefault(slot_id, material)
  121. if not used_slots.issubset(slot_material):
  122. return None
  123. ordered: list[str] = []
  124. for slot_id in sorted(used_slots):
  125. material = slot_material[slot_id]
  126. if material not in ordered:
  127. ordered.append(material)
  128. return ordered
  129. def _match_slots_by_color(
  130. filament_usage: list[dict],
  131. ams_raw: dict | list | None,
  132. ) -> list[int] | None:
  133. """Match 3MF filament slots to AMS trays by color.
  134. Fallback mapping for printers that don't provide the MQTT mapping field
  135. or request topic subscription (e.g. A1, A1 Mini, P1S, P2S).
  136. Compares the 3MF slicer filament color (per slot) against each AMS tray's
  137. color to find a unique match. Only returns a mapping if every used slot
  138. matches exactly one tray (no ambiguity).
  139. Args:
  140. filament_usage: List of 3MF slot dicts with 'slot_id', 'color', 'type'
  141. ams_raw: raw_data["ams"] dict or list from printer state
  142. Returns:
  143. List of global tray IDs indexed by slicer slot (0-based), or None.
  144. """
  145. if not filament_usage or not ams_raw:
  146. return None
  147. ams_data = ams_raw.get("ams", []) if isinstance(ams_raw, dict) else ams_raw if isinstance(ams_raw, list) else []
  148. if not ams_data:
  149. return None
  150. # Build map of normalized color → list of global tray IDs
  151. color_to_trays: dict[str, list[int]] = {}
  152. for ams_unit in ams_data:
  153. ams_id = int(ams_unit.get("id", 0))
  154. for tray in ams_unit.get("tray", []):
  155. tray_id = int(tray.get("id", 0))
  156. tray_color = tray.get("tray_color", "")
  157. tray_type = tray.get("tray_type", "")
  158. if not tray_color or not tray_type:
  159. continue
  160. # Normalize AMS color: strip alpha (last 2 chars), lowercase
  161. norm = tray_color[:6].lower() if len(tray_color) >= 6 else tray_color.lower()
  162. if ams_id >= 128:
  163. global_id = ams_id # AMS-HT
  164. else:
  165. global_id = ams_id * 4 + tray_id
  166. color_to_trays.setdefault(norm, []).append(global_id)
  167. if not color_to_trays:
  168. return None
  169. # Find max slot_id to size the result array
  170. max_slot = max(u.get("slot_id", 0) for u in filament_usage)
  171. if max_slot <= 0:
  172. return None
  173. result = [-1] * max_slot
  174. used_trays: set[int] = set()
  175. for usage in filament_usage:
  176. slot_id = usage.get("slot_id", 0)
  177. if slot_id <= 0:
  178. continue
  179. slot_color = usage.get("color", "").lstrip("#").lower()
  180. if len(slot_color) < 6:
  181. return None # Can't match without a valid color
  182. slot_color = slot_color[:6] # Strip alpha if present
  183. candidates = color_to_trays.get(slot_color, [])
  184. # Filter out trays already claimed by another slot
  185. available = [t for t in candidates if t not in used_trays]
  186. if len(available) != 1:
  187. # Ambiguous (multiple trays with same color) or no match
  188. return None
  189. result[slot_id - 1] = available[0]
  190. used_trays.add(available[0])
  191. # Only return if at least one valid mapping exists
  192. if all(v < 0 for v in result):
  193. return None
  194. logger.info("[UsageTracker] Color-matched slot_to_tray: %s", result)
  195. return result
  196. @dataclass
  197. class PrintSession:
  198. printer_id: int
  199. print_name: str
  200. started_at: datetime
  201. tray_remain_start: dict[tuple[int, int], int] = field(default_factory=dict)
  202. # tray_now at print start (correct value, unlike at completion where it's 255)
  203. tray_now_at_start: int = -1
  204. # Snapshot of spool assignments at print start: {(ams_id, tray_id): spool_id}
  205. # Prevents usage loss when on_ams_change unlinks a spool mid-print
  206. spool_assignments: dict[tuple[int, int], int] = field(default_factory=dict)
  207. # AMS mapping from print command (captured at start, needed when auto-archive is off)
  208. ams_mapping: list[int] | None = None
  209. # Queue item's plate_id when this print is a multi-plate 3MF dispatched for a
  210. # single plate (#1697). None for non-queue prints — the file's first/only plate
  211. # is the default and the 3MF parser already returns the full file in that case.
  212. plate_id: int | None = None
  213. # Module-level storage, keyed by printer_id. Mirrored to the
  214. # ``active_print_sessions`` table so a restart mid-print doesn't lose the
  215. # context — see ``persist_session`` / ``restore_session``.
  216. _active_sessions: dict[int, PrintSession] = {}
  217. # Printers whose finished print is being closed out. on_print_complete takes the
  218. # session first and charges the usage several awaits later; the printer already
  219. # reports FINISH, so without this mark the AMS remain% weight sync would run in
  220. # between and the print would be charged twice (WU-3).
  221. _completing: set[int] = set()
  222. def mark_completing(printer_id: int) -> None:
  223. """The completion path's first step: hold the weight sync off until the
  224. usage is booked (discard_session) or the completion ends."""
  225. _completing.add(printer_id)
  226. def done_completing(printer_id: int) -> None:
  227. _completing.discard(printer_id)
  228. def is_completing(printer_id: int) -> bool:
  229. """The printer's finished print is being closed out (on_print_complete runs)."""
  230. return printer_id in _completing
  231. def printer_is_charging(printer_id: int) -> bool:
  232. """A print's usage is still to be charged: it runs, or it ended and its
  233. completion has not booked it yet. The AMS remain% sync leaves the
  234. printer's spools alone meanwhile (#880)."""
  235. return printer_id in _active_sessions or printer_id in _completing
  236. # Serialises the read-modify-write on the persisted tray-change log, per printer.
  237. _tray_change_locks: dict[int, asyncio.Lock] = {}
  238. def _tray_key_to_str(key: tuple[int, int]) -> str:
  239. return f"{key[0]}-{key[1]}"
  240. def _tray_key_from_str(key: str) -> tuple[int, int] | None:
  241. ams_str, _, tray_str = key.partition("-")
  242. try:
  243. return int(ams_str), int(tray_str)
  244. except ValueError:
  245. return None
  246. def _tray_map_to_json(mapping: dict[tuple[int, int], int]) -> dict[str, int]:
  247. return {_tray_key_to_str(k): v for k, v in mapping.items()}
  248. def _tray_map_from_json(mapping: dict | None) -> dict[tuple[int, int], int]:
  249. result: dict[tuple[int, int], int] = {}
  250. for raw_key, value in (mapping or {}).items():
  251. key = _tray_key_from_str(str(raw_key))
  252. if key is not None and isinstance(value, int):
  253. result[key] = value
  254. return result
  255. async def _charge_spool(db: AsyncSession, spool: Spool, grams: float) -> bool:
  256. """Add *grams* to the spool's usage in the database itself (WU-4).
  257. The spool was loaded several awaits earlier; a weight set by hand or
  258. weighed on the scale since then would be lost to a write of the value
  259. computed from that copy. The loaded copy is set to the stored result: from
  260. this statement until the commit, the transaction holds the row (the write
  261. lock on SQLite), so writing it back on flush changes nothing.
  262. False when the spool was deleted meanwhile: the caller skips it, since a
  263. usage row for it would break its foreign key on PostgreSQL and roll back
  264. the whole completion.
  265. """
  266. now = datetime.now(timezone.utc)
  267. stored = await db.scalar(
  268. update(Spool)
  269. .where(Spool.id == spool.id)
  270. .values(weight_used=func.coalesce(Spool.weight_used, 0) + grams, last_used=now)
  271. .returning(Spool.weight_used)
  272. .execution_options(synchronize_session=False)
  273. )
  274. if stored is None:
  275. return False
  276. spool.weight_used = stored
  277. spool.last_used = now
  278. return True
  279. async def persist_session(
  280. db: AsyncSession,
  281. session: PrintSession,
  282. tray_change_log: list | None = None,
  283. ) -> None:
  284. """Mirror ``session`` into ``active_print_sessions`` for restart recovery.
  285. Overwrites any existing row for the printer: a printer runs one print at a
  286. time, and a row left behind by a completion we never saw must not outlive
  287. the next print start.
  288. """
  289. from backend.app.models.active_print_session import ActivePrintSession
  290. row = await db.get(ActivePrintSession, session.printer_id)
  291. if row is None:
  292. row = ActivePrintSession(printer_id=session.printer_id)
  293. db.add(row)
  294. row.print_name = session.print_name or ""
  295. row.started_at = session.started_at.replace(tzinfo=None)
  296. row.tray_now_at_start = session.tray_now_at_start
  297. row.plate_id = session.plate_id
  298. row.ams_mapping = list(session.ams_mapping) if session.ams_mapping else None
  299. row.spool_assignments = _tray_map_to_json(session.spool_assignments) or None
  300. row.tray_remain_start = _tray_map_to_json(session.tray_remain_start) or None
  301. row.tray_change_log = [list(entry) for entry in (tray_change_log or [])] or None
  302. await db.commit()
  303. async def record_tray_change(db: AsyncSession, printer_id: int, tray_global: int, layer_num: int) -> None:
  304. """Append one tray change to the persisted log.
  305. No-op when no print-start row exists — a tray change outside a tracked
  306. print has nothing to attribute.
  307. """
  308. from backend.app.models.active_print_session import ActivePrintSession
  309. # Read-modify-write on a JSON column: two changes close together (a runout
  310. # parks the extruder and the backup tray loads moments later) would
  311. # otherwise race and drop a segment boundary.
  312. async with _tray_change_locks.setdefault(printer_id, asyncio.Lock()):
  313. row = await db.get(ActivePrintSession, printer_id)
  314. if row is None:
  315. return
  316. log = [list(entry) for entry in (row.tray_change_log or [])]
  317. entry = [tray_global, layer_num]
  318. if log and log[-1] == entry:
  319. # print-start seeds the log from PrinterState, which may already
  320. # hold a change this callback is also reporting.
  321. return
  322. log.append(entry)
  323. row.tray_change_log = log
  324. await db.commit()
  325. async def get_persisted_print_name(db: AsyncSession, printer_id: int) -> str | None:
  326. """Print name on the persisted row, for identity-checking a restored session."""
  327. from backend.app.models.active_print_session import ActivePrintSession
  328. row = await db.get(ActivePrintSession, printer_id)
  329. return row.print_name if row is not None else None
  330. async def restore_session(db: AsyncSession, printer_id: int, register_active: bool = True) -> list[list[int]] | None:
  331. """Rebuild the in-memory session for ``printer_id`` from the persisted row.
  332. Returns the persisted tray-change log so the caller can put it back on
  333. ``PrinterState``, or None when there is nothing to restore.
  334. ``register_active=False`` returns the log without publishing the session to
  335. ``_active_sessions`` — for Spoolman users, who need the tray-change log
  336. restored but whose remain%-sync must not be suppressed by it (see
  337. ``on_print_start``).
  338. """
  339. from backend.app.models.active_print_session import ActivePrintSession
  340. row = await db.get(ActivePrintSession, printer_id)
  341. if row is None:
  342. return None
  343. started_at = row.started_at
  344. if started_at.tzinfo is None:
  345. started_at = started_at.replace(tzinfo=timezone.utc)
  346. session = PrintSession(
  347. printer_id=printer_id,
  348. print_name=row.print_name or "",
  349. started_at=started_at,
  350. tray_remain_start=_tray_map_from_json(row.tray_remain_start),
  351. tray_now_at_start=row.tray_now_at_start,
  352. spool_assignments=_tray_map_from_json(row.spool_assignments),
  353. ams_mapping=list(row.ams_mapping) if row.ams_mapping else None,
  354. plate_id=row.plate_id,
  355. )
  356. if register_active:
  357. _active_sessions[printer_id] = session
  358. log = [list(entry) for entry in (row.tray_change_log or [])]
  359. logger.info(
  360. "[UsageTracker] Restored print session for printer %d: plate_id=%s, ams_mapping=%s, "
  361. "%d assignments, tray_change_log=%s",
  362. printer_id,
  363. row.plate_id,
  364. row.ams_mapping,
  365. len(row.spool_assignments or {}),
  366. log,
  367. )
  368. return log
  369. async def clear_persisted_session(db: AsyncSession, printer_id: int) -> None:
  370. """Drop the persisted print-start row once the print is closed out."""
  371. from backend.app.models.active_print_session import ActivePrintSession
  372. row = await db.get(ActivePrintSession, printer_id)
  373. if row is not None:
  374. await db.delete(row)
  375. await db.commit()
  376. def forget_printer(printer_id: int) -> None:
  377. """A deleted printer's print-start context in memory (the row goes with the printer)."""
  378. _active_sessions.pop(printer_id, None)
  379. _completing.discard(printer_id)
  380. _tray_change_locks.pop(printer_id, None)
  381. async def discard_session(db: AsyncSession, printer_id: int) -> None:
  382. """Forget a printer's print-start context, in memory and on disk.
  383. The completion path calls this for every print, including the ones whose
  384. usage Spoolman owns: the context is captured for both backends, but only
  385. the internal tracker's ``on_print_complete`` consumes (and pops) it.
  386. """
  387. _active_sessions.pop(printer_id, None)
  388. _completing.discard(printer_id)
  389. _tray_change_locks.pop(printer_id, None)
  390. await clear_persisted_session(db, printer_id)
  391. def _to_epoch_seconds(value: datetime | None) -> float | None:
  392. """Convert datetime to epoch seconds, assuming UTC for naive values."""
  393. if value is None:
  394. return None
  395. dt = value
  396. if dt.tzinfo is None:
  397. dt = dt.replace(tzinfo=timezone.utc)
  398. return dt.timestamp()
  399. async def _resolve_spool_id_for_tray(
  400. printer_id: int,
  401. ams_id: int,
  402. tray_id: int,
  403. db: AsyncSession,
  404. spool_assignments_snapshot: dict[tuple[int, int], int] | None = None,
  405. print_started_at: datetime | None = None,
  406. ) -> int | None:
  407. """Resolve spool ID for a tray with safe support for mid-print reassignment.
  408. Resolution order:
  409. 1. If snapshot exists and live assignment changed *during this print*, use live spool.
  410. 2. Otherwise use snapshot spool when available.
  411. 3. Fall back to live assignment.
  412. """
  413. key = (ams_id, tray_id)
  414. snapshot_spool_id = spool_assignments_snapshot.get(key) if spool_assignments_snapshot else None
  415. # Backward-compatible fast path: if we have a snapshot but no print-start
  416. # timestamp, preserve legacy behavior and avoid extra DB lookups.
  417. if snapshot_spool_id is not None and print_started_at is None:
  418. return snapshot_spool_id
  419. result = await db.execute(
  420. select(SpoolAssignment).where(
  421. SpoolAssignment.printer_id == printer_id,
  422. SpoolAssignment.ams_id == ams_id,
  423. SpoolAssignment.tray_id == tray_id,
  424. )
  425. )
  426. live_assignment = result.scalar_one_or_none()
  427. if snapshot_spool_id is not None:
  428. if live_assignment and live_assignment.spool_id != snapshot_spool_id:
  429. live_created_ts = _to_epoch_seconds(getattr(live_assignment, "created_at", None))
  430. started_ts = _to_epoch_seconds(print_started_at)
  431. if live_created_ts is not None and started_ts is not None and live_created_ts >= started_ts:
  432. logger.info(
  433. "[UsageTracker] Assignment changed during print for printer %d AMS%d-T%d: snapshot spool %d -> live spool %d",
  434. printer_id,
  435. ams_id,
  436. tray_id,
  437. snapshot_spool_id,
  438. live_assignment.spool_id,
  439. )
  440. return live_assignment.spool_id
  441. return snapshot_spool_id
  442. if live_assignment:
  443. return live_assignment.spool_id
  444. return None
  445. async def on_print_start(
  446. printer_id: int,
  447. data: dict,
  448. printer_manager,
  449. db: AsyncSession | None = None,
  450. spoolman_owns_usage: bool = False,
  451. ) -> None:
  452. """Capture AMS tray remain% and spool assignments at print start.
  453. The capture runs for both inventory backends — the persisted row carries
  454. the tray-change log, which is the only record of which spool fed which
  455. layers when AMS Filament Backup swaps trays, and Spoolman's own durable
  456. row (#1820) does not hold it.
  457. ``spoolman_owns_usage`` keeps the in-memory session out of
  458. ``_active_sessions`` when Spoolman is writing the usage. That dict doubles
  459. as ``on_ams_change``'s "a print is running, so skip the remain%-based
  460. weight sync because the internal tracker will deduct precisely" flag
  461. (#880); registering a session the internal tracker will never complete
  462. would suppress a sync those users still need.
  463. """
  464. # A new print: whatever the last completion left marked is over.
  465. _completing.discard(printer_id)
  466. state = printer_manager.get_status(printer_id)
  467. if not state or not state.raw_data:
  468. logger.debug("[UsageTracker] No state for printer %d, skipping", printer_id)
  469. return
  470. ams_raw = state.raw_data.get("ams", [])
  471. ams_data = ams_raw.get("ams", []) if isinstance(ams_raw, dict) else ams_raw if isinstance(ams_raw, list) else []
  472. tray_remain_start: dict[tuple[int, int], int] = {}
  473. skipped_invalid: list[str] = []
  474. for ams_unit in ams_data:
  475. ams_id = int(ams_unit.get("id", 0))
  476. for tray in ams_unit.get("tray", []):
  477. tray_id = int(tray.get("id", 0))
  478. remain = tray.get("remain", -1)
  479. if isinstance(remain, int) and 0 <= remain <= 100:
  480. tray_remain_start[(ams_id, tray_id)] = remain
  481. else:
  482. skipped_invalid.append(f"AMS{ams_id}-T{tray_id}(remain={remain})")
  483. # Also capture VT (external) tray remain% — these are separate from AMS units
  484. vt_tray_raw = state.raw_data.get("vt_tray") or []
  485. if isinstance(vt_tray_raw, dict):
  486. vt_tray_raw = [vt_tray_raw]
  487. for vt in vt_tray_raw:
  488. if not isinstance(vt, dict):
  489. continue
  490. vt_id = int(vt.get("id", 254))
  491. # VT tray id 254 → (ams_id=255, tray_id=0), id 255 → (ams_id=255, tray_id=1)
  492. vt_tray_id = vt_id - 254
  493. remain = vt.get("remain", -1)
  494. if isinstance(remain, int) and 0 <= remain <= 100:
  495. tray_remain_start[(255, vt_tray_id)] = remain
  496. else:
  497. skipped_invalid.append(f"VT{vt_id}(remain={remain})")
  498. if skipped_invalid:
  499. logger.info(
  500. "[UsageTracker] Skipped trays with invalid remain%% for printer %d: %s",
  501. printer_id,
  502. ", ".join(skipped_invalid),
  503. )
  504. if not ams_data and not vt_tray_raw:
  505. logger.debug("[UsageTracker] No AMS or VT tray data for printer %d, skipping", printer_id)
  506. return
  507. print_name = data.get("subtask_name", "") or data.get("filename", "unknown")
  508. # Capture tray_now at print start (reliable, unlike at completion where it's 255)
  509. tray_now_at_start = state.tray_now if state else -1
  510. # --- Diagnostic logging: dump mapping-related MQTT fields at print start ---
  511. # This helps us understand what each printer model reports for slot-to-tray mapping.
  512. mapping_field = state.raw_data.get("mapping")
  513. logger.info(
  514. "[UsageTracker] PRINT START printer %d: mapping=%s, tray_now=%d, last_loaded_tray=%s",
  515. printer_id,
  516. mapping_field,
  517. tray_now_at_start,
  518. getattr(state, "last_loaded_tray", "N/A"),
  519. )
  520. # Log all raw_data keys containing "map" or "ams" for discovery
  521. map_keys = {k: state.raw_data[k] for k in state.raw_data if "map" in k.lower()}
  522. if map_keys:
  523. logger.info("[UsageTracker] PRINT START printer %d: mapping-related keys: %s", printer_id, map_keys)
  524. # Log per-tray summary: tray_now, tray_tar, tray_type, tray_color for each slot
  525. for ams_unit in ams_data:
  526. ams_id = int(ams_unit.get("id", 0))
  527. tray_summary = []
  528. for tray in ams_unit.get("tray", []):
  529. tray_summary.append(
  530. f"T{tray.get('id', '?')}(type={tray.get('tray_type', '')}, "
  531. f"color={tray.get('tray_color', '')}, "
  532. f"now={ams_raw.get('tray_now', '?') if isinstance(ams_raw, dict) else '?'}, "
  533. f"tar={ams_raw.get('tray_tar', '?') if isinstance(ams_raw, dict) else '?'})"
  534. )
  535. logger.info("[UsageTracker] PRINT START printer %d AMS %d: %s", printer_id, ams_id, ", ".join(tray_summary))
  536. # Snapshot spool assignments so usage isn't lost if on_ams_change unlinks mid-print
  537. spool_assignments: dict[tuple[int, int], int] = {}
  538. if db:
  539. assign_result = await db.execute(select(SpoolAssignment).where(SpoolAssignment.printer_id == printer_id))
  540. for assignment in assign_result.scalars().all():
  541. spool_assignments[(assignment.ams_id, assignment.tray_id)] = assignment.spool_id
  542. if spool_assignments:
  543. logger.info(
  544. "[UsageTracker] Snapshotted %d spool assignments for printer %d: %s",
  545. len(spool_assignments),
  546. printer_id,
  547. {f"{k[0]}-{k[1]}": v for k, v in spool_assignments.items()},
  548. )
  549. # Capture the queue item's plate_id so 3MF parsing at completion is scoped to
  550. # the plate that actually ran, not the whole multi-plate file (#1697).
  551. plate_id: int | None = None
  552. if db:
  553. from backend.app.models.print_queue import PrintQueueItem
  554. queue_result = await db.execute(
  555. select(PrintQueueItem)
  556. .where(PrintQueueItem.printer_id == printer_id)
  557. .where(PrintQueueItem.status == "printing")
  558. )
  559. queue_item = queue_result.scalars().first()
  560. if queue_item is not None:
  561. plate_id = queue_item.plate_id
  562. # Always create session (even without valid remain data) so print_name
  563. # is available at completion for 3MF-based tracking
  564. session = PrintSession(
  565. printer_id=printer_id,
  566. print_name=print_name,
  567. started_at=datetime.now(timezone.utc),
  568. tray_remain_start=tray_remain_start,
  569. tray_now_at_start=tray_now_at_start,
  570. spool_assignments=spool_assignments,
  571. ams_mapping=data.get("ams_mapping"),
  572. plate_id=plate_id,
  573. )
  574. if spoolman_owns_usage:
  575. _active_sessions.pop(printer_id, None)
  576. else:
  577. _active_sessions[printer_id] = session
  578. # Mirror to the DB so a restart mid-print doesn't lose the context. The
  579. # tray-change log has already been cleared and seeded with the starting
  580. # tray by bambu_mqtt before this callback fires.
  581. if db:
  582. try:
  583. await persist_session(db, session, getattr(state, "tray_change_log", None))
  584. except Exception:
  585. logger.exception("[UsageTracker] Failed to persist print session for printer %d", printer_id)
  586. if tray_remain_start:
  587. logger.info(
  588. "[UsageTracker] Captured start remain%% for printer %d (%d trays): %s",
  589. printer_id,
  590. len(tray_remain_start),
  591. {f"{k[0]}-{k[1]}": v for k, v in tray_remain_start.items()},
  592. )
  593. else:
  594. logger.debug("[UsageTracker] No valid remain%% for printer %d, 3MF fallback available", printer_id)
  595. async def on_print_complete(
  596. printer_id: int,
  597. data: dict,
  598. printer_manager,
  599. db: AsyncSession,
  600. archive_id: int | None = None,
  601. ams_mapping: list[int] | None = None,
  602. ) -> list[dict]:
  603. """Compute consumption deltas and update spool weight_used/last_used.
  604. Uses two tracking strategies in priority order:
  605. 1. 3MF per-filament estimates (primary) — precise slicer data for all spools
  606. 2. AMS remain% delta (fallback) — only for trays not already handled by 3MF
  607. Returns a list of dicts describing what was logged (for WebSocket broadcast).
  608. """
  609. from sqlalchemy import select
  610. from backend.app.models.spool_usage_history import SpoolUsageHistory
  611. session = _active_sessions.pop(printer_id, None)
  612. if session is None:
  613. # Restart mid-print: the in-memory session is gone but the print-start
  614. # row survived. Without this the completion path loses the plate, the
  615. # dispatched mapping and the assignment snapshot, and attributes the
  616. # whole print to whichever tray happened to finish it.
  617. try:
  618. await restore_session(db, printer_id)
  619. except Exception:
  620. logger.exception("[UsageTracker] Failed to restore print session for printer %d", printer_id)
  621. session = _active_sessions.pop(printer_id, None)
  622. status = data.get("status", "completed")
  623. results = []
  624. handled_trays: set[tuple[int, int]] = set()
  625. # Fetch default filament cost from settings for fallback
  626. from backend.app.services.print_cost_estimate import default_cost_per_kg
  627. default_filament_cost = await default_cost_per_kg(db)
  628. # Fall back to ams_mapping captured at print start (needed when auto-archive is off
  629. # and the caller can't retrieve the mapping from _print_ams_mappings without archive_id)
  630. if not ams_mapping and session and session.ams_mapping:
  631. ams_mapping = session.ams_mapping
  632. logger.info(
  633. "[UsageTracker] on_print_complete: printer=%d, archive=%s, session=%s, ams_mapping=%s",
  634. printer_id,
  635. archive_id,
  636. "yes" if session else "no",
  637. ams_mapping,
  638. )
  639. # --- Diagnostic logging: dump mapping-related MQTT fields at print completion ---
  640. state = printer_manager.get_status(printer_id)
  641. if state and state.raw_data:
  642. logger.info(
  643. "[UsageTracker] PRINT COMPLETE printer %d: mapping=%s, tray_now=%s, last_loaded_tray=%s",
  644. printer_id,
  645. state.raw_data.get("mapping"),
  646. state.tray_now,
  647. getattr(state, "last_loaded_tray", "N/A"),
  648. )
  649. # --- Path 1 (PRIMARY): 3MF per-filament estimates ---
  650. print_name = (
  651. (session.print_name if session else None) or data.get("subtask_name", "") or data.get("filename", "unknown")
  652. )
  653. # When auto-archive is disabled (archive_id=None), try to find a 3MF by filename
  654. # from the library or previous archives so we can still track filament usage.
  655. threemf_path = None
  656. if not archive_id:
  657. from backend.app.core.config import settings as app_settings
  658. search_filename = data.get("filename") or data.get("subtask_name") or (session.print_name if session else "")
  659. if search_filename:
  660. threemf_path = await _find_3mf_by_filename(
  661. printer_id,
  662. search_filename,
  663. db,
  664. app_settings.base_dir,
  665. print_name=data.get("subtask_name") or (session.print_name if session else None),
  666. )
  667. if archive_id or threemf_path:
  668. threemf_results = await _track_from_3mf(
  669. printer_id,
  670. archive_id,
  671. status,
  672. print_name,
  673. handled_trays,
  674. printer_manager,
  675. db,
  676. ams_mapping=ams_mapping,
  677. tray_now_at_start=session.tray_now_at_start if session else -1,
  678. last_progress=data.get("last_progress", 0.0),
  679. last_layer_num=data.get("last_layer_num", 0),
  680. default_filament_cost=default_filament_cost,
  681. spool_assignments=session.spool_assignments if session else None,
  682. print_started_at=session.started_at if session else None,
  683. threemf_path=threemf_path,
  684. plate_id=session.plate_id if session else None,
  685. )
  686. results.extend(threemf_results)
  687. # --- Path 2 (FALLBACK): AMS remain% delta (only for trays not handled by 3MF) ---
  688. if session and session.tray_remain_start:
  689. state = printer_manager.get_status(printer_id)
  690. if state and state.raw_data:
  691. ams_raw = state.raw_data.get("ams", [])
  692. ams_data = (
  693. ams_raw.get("ams", []) if isinstance(ams_raw, dict) else ams_raw if isinstance(ams_raw, list) else []
  694. )
  695. # Build set of trays actually involved in this print (#1269).
  696. # Without this guard, swapping a spool in an UNUSED slot mid-print
  697. # makes that slot's remain% drop to 0, which the fallback below
  698. # would otherwise charge to the originally-assigned spool.
  699. def _global_to_ams_key(global_tray_id: int) -> tuple[int, int]:
  700. if global_tray_id >= 254:
  701. return (255, global_tray_id - 254)
  702. if global_tray_id >= 128:
  703. return (global_tray_id, 0)
  704. return (global_tray_id // 4, global_tray_id % 4)
  705. print_used_keys: set[tuple[int, int]] = set()
  706. if ams_mapping:
  707. for gid in ams_mapping:
  708. if isinstance(gid, int) and gid >= 0:
  709. print_used_keys.add(_global_to_ams_key(gid))
  710. for change in getattr(state, "tray_change_log", None) or []:
  711. if isinstance(change, (tuple, list)) and len(change) >= 1:
  712. gid = change[0]
  713. if isinstance(gid, int) and gid >= 0:
  714. print_used_keys.add(_global_to_ams_key(gid))
  715. # 255 is not a slot: it is what ``tray_now`` reads at rest, before
  716. # the printer has reported one and while nothing is loaded, and an
  717. # unparseable reading falls back to it too. Mapped as a tray id it
  718. # becomes (255, 1), and if it were the only evidence every real
  719. # slot would be excluded and the fallback would charge nothing at
  720. # all (#1820). The external spool reports 254 when in use.
  721. if session.tray_now_at_start is not None and 0 <= session.tray_now_at_start <= 254:
  722. print_used_keys.add(_global_to_ams_key(session.tray_now_at_start))
  723. # Collect all trays to check: AMS trays + VT (external) trays
  724. # Each entry: (ams_id_for_assignment, tray_id_for_assignment, current_remain, label)
  725. trays_to_check: list[tuple[int, int, int, str]] = []
  726. for ams_unit in ams_data:
  727. ams_id = int(ams_unit.get("id", 0))
  728. for tray in ams_unit.get("tray", []):
  729. tray_id = int(tray.get("id", 0))
  730. remain = tray.get("remain", -1)
  731. trays_to_check.append((ams_id, tray_id, remain, f"AMS{ams_id}-T{tray_id}"))
  732. # VT (external) trays — same remain% delta logic
  733. vt_tray_raw = state.raw_data.get("vt_tray") or []
  734. if isinstance(vt_tray_raw, dict):
  735. vt_tray_raw = [vt_tray_raw]
  736. for vt in vt_tray_raw:
  737. if not isinstance(vt, dict):
  738. continue
  739. vt_id = int(vt.get("id", 254))
  740. vt_tray_id = vt_id - 254 # 254→0, 255→1
  741. remain = vt.get("remain", -1)
  742. trays_to_check.append((255, vt_tray_id, remain, f"VT{vt_id}"))
  743. for assign_ams_id, assign_tray_id, current_remain, tray_label in trays_to_check:
  744. key = (assign_ams_id, assign_tray_id)
  745. if key in handled_trays:
  746. continue # Already tracked via 3MF
  747. if key not in session.tray_remain_start:
  748. # No usable remain% when the print began, so there is no delta
  749. # to charge. Said out loud for the same reason as the branches
  750. # below: a slot the print used, holding a spool the operator
  751. # assigned, otherwise vanished from the accounting without a
  752. # word. Common on non-RFID spools, which report remain = -1
  753. # until a remaining amount is set by hand.
  754. if not print_used_keys or key in print_used_keys:
  755. logger.info(
  756. "[UsageTracker] %s: no valid remain%% at print start, nothing to charge for printer %d",
  757. tray_label,
  758. printer_id,
  759. )
  760. continue
  761. # Skip trays the print never touched. Only enforce when we have
  762. # evidence of which trays the print used; if print_used_keys is
  763. # empty (no mapping, no change log, no tray_now_at_start) keep
  764. # the legacy behavior of scanning every tray.
  765. if print_used_keys and key not in print_used_keys:
  766. logger.info(
  767. "[UsageTracker] %s: not in print mapping/tray_change_log — skipping fallback for printer %d",
  768. tray_label,
  769. printer_id,
  770. )
  771. continue
  772. if not isinstance(current_remain, int) or current_remain < 0 or current_remain > 100:
  773. logger.info(
  774. "[UsageTracker] %s: invalid remain%% at completion (%s), skipping fallback for printer %d",
  775. tray_label,
  776. current_remain,
  777. printer_id,
  778. )
  779. continue
  780. start_remain = session.tray_remain_start[key]
  781. delta_pct = start_remain - current_remain
  782. if delta_pct <= 0:
  783. # Not necessarily "nothing was printed". A fresh spool sits
  784. # at 100% for the first tens of grams, and the AMS estimate
  785. # drifts upward on its own, so a real print can end with the
  786. # same or a higher reading than it started with. Said out
  787. # loud because the alternative -- charging nothing, silently
  788. # -- is indistinguishable from having nothing to charge, and
  789. # the operator has no other way to find the prints that went
  790. # uncounted (#1820).
  791. logger.info(
  792. "[UsageTracker] %s: remain%% did not fall over the print (%d%% -> %d%%), "
  793. "nothing charged for printer %d",
  794. tray_label,
  795. start_remain,
  796. current_remain,
  797. printer_id,
  798. )
  799. continue
  800. spool_id = await _resolve_spool_id_for_tray(
  801. printer_id=printer_id,
  802. ams_id=assign_ams_id,
  803. tray_id=assign_tray_id,
  804. db=db,
  805. spool_assignments_snapshot=session.spool_assignments,
  806. print_started_at=session.started_at,
  807. )
  808. if spool_id is None:
  809. logger.info(
  810. "[UsageTracker] %s: no spool assigned, skipping fallback for printer %d",
  811. tray_label,
  812. printer_id,
  813. )
  814. continue
  815. # Load spool
  816. spool_result = await db.execute(select(Spool).where(Spool.id == spool_id))
  817. spool = spool_result.scalar_one_or_none()
  818. if not spool:
  819. continue
  820. # Compute weight consumed
  821. weight_grams = (delta_pct / 100.0) * spool.label_weight
  822. # Update spool
  823. if not await _charge_spool(db, spool, weight_grams):
  824. continue
  825. # Calculate cost for this usage
  826. cost = None
  827. cost_per_kg = spool.cost_per_kg if spool.cost_per_kg is not None else default_filament_cost
  828. if cost_per_kg > 0:
  829. cost = round((weight_grams / 1000.0) * cost_per_kg, 2)
  830. # Insert usage history record
  831. history = SpoolUsageHistory(
  832. spool_id=spool.id,
  833. printer_id=printer_id,
  834. print_name=session.print_name,
  835. weight_used=round(weight_grams, 1),
  836. percent_used=delta_pct,
  837. status=status,
  838. cost=cost,
  839. archive_id=archive_id,
  840. )
  841. db.add(history)
  842. handled_trays.add(key)
  843. results.append(
  844. {
  845. "spool_id": spool.id,
  846. "weight_used": round(weight_grams, 1),
  847. "percent_used": delta_pct,
  848. "ams_id": assign_ams_id,
  849. "tray_id": assign_tray_id,
  850. "material": spool.material,
  851. "cost": cost,
  852. # AMS remain%-delta fallback has no 3MF slot — slot_id
  853. # stays None so it is excluded from the colour rewrite.
  854. "slot_id": None,
  855. "color": _spool_color_to_hex(spool.rgba),
  856. }
  857. )
  858. logger.info(
  859. "[UsageTracker] Spool %d consumed %.1fg (%d%%) on printer %d %s (AMS fallback, %s)",
  860. spool.id,
  861. weight_grams,
  862. delta_pct,
  863. printer_id,
  864. tray_label,
  865. status,
  866. )
  867. if results:
  868. await db.commit()
  869. # --- Update PrintArchive.cost from THIS print session only ---
  870. #
  871. # Cover any filament weight that wasn't tracked by an inventory spool with
  872. # the global default rate (#1344). Without this, a multi-color print where
  873. # only some AMS trays are mapped to inventory spools would record only the
  874. # mapped slots' share — e.g. $0.01 for a 110g print when 3 of 4 trays had
  875. # no spool record. The initial cost set by archive.py (total grams *
  876. # primary cost_per_kg) is fine on its own, but this block overwrites it,
  877. # so the overwrite must reconstruct the whole-print cost.
  878. if archive_id and results:
  879. from sqlalchemy import select
  880. from backend.app.models.archive import PrintArchive
  881. archive_result = await db.execute(select(PrintArchive).where(PrintArchive.id == archive_id))
  882. archive = archive_result.scalar_one_or_none()
  883. if archive:
  884. total_cost = sum(r.get("cost", 0) or 0 for r in results)
  885. tracked_grams = sum(r.get("weight_used", 0) or 0 for r in results)
  886. archive_grams = archive.filament_used_grams or 0
  887. untracked_grams = max(0.0, archive_grams - tracked_grams)
  888. if untracked_grams > 0 and default_filament_cost > 0:
  889. total_cost += (untracked_grams / 1000.0) * default_filament_cost
  890. if total_cost > 0:
  891. # Only overwrite archive.cost on the first run. Reprint actuals
  892. # live in PrintLogEntry; the archive card keeps the first run's
  893. # cost so a failed reprint doesn't visually clobber a successful
  894. # 100 g/$X print with a 10 g/$X/10 partial (#1378).
  895. from backend.app.services.print_log import other_runs_of
  896. if not await other_runs_of(db, archive):
  897. archive.cost = round(total_cost, 2)
  898. await db.commit()
  899. return results
  900. # A running print's ``filename`` is the path the printer is executing, and on a
  901. # sliced job that is always ``…/Metadata/plate_<N>.gcode``. Its stem names the
  902. # *plate*, not the model, and every Bambu print in existence has one — so it
  903. # identifies nothing and must never be used to match a 3MF. It reached the
  904. # matcher for real on H2-series and P2S prints, where the file goes to internal
  905. # eMMC, no 3MF can be fetched, and the archive keeps the gcode path as its
  906. # filename: `plate_1` then matched an unrelated `lid_plate_1.gcode.3mf` and that
  907. # print's filament figures were read off a different model entirely.
  908. _GENERIC_PLATE_STEM = re.compile(r"^plate_?\d+$", re.IGNORECASE)
  909. def _like_escape(value: str) -> str:
  910. """Escape LIKE metacharacters so a stem matches literally.
  911. ``_`` is a single-character wildcard, and model names are full of them.
  912. """
  913. return value.replace("\\", "\\\\").replace("%", "\\%").replace("_", "\\_")
  914. def _threemf_search_stem(*candidates: str | None) -> str | None:
  915. """First candidate that names a model, or None if none of them do.
  916. Candidates are tried in order and the generic plate name is skipped rather
  917. than accepted, so a print that only has one falls through to "no match"
  918. instead of matching everything.
  919. """
  920. for raw in candidates:
  921. if not raw:
  922. continue
  923. stem = raw.split("/")[-1].strip()
  924. for suffix in (".gcode.3mf", ".gcode", ".3mf"):
  925. if stem.lower().endswith(suffix):
  926. stem = stem[: -len(suffix)]
  927. break
  928. # Stripped only to judge the stem, never to change it: a real archive
  929. # here is named "…Face Down .gcode.3mf", and a stem trimmed to
  930. # "…Face Down" no longer matches the file it came from.
  931. probe = stem.strip()
  932. if probe and not _GENERIC_PLATE_STEM.match(probe):
  933. return stem
  934. return None
  935. def _stem_matches(column, stem: str):
  936. """Filter matching *stem* at a filename boundary rather than anywhere.
  937. ``ilike("%<stem>.%")`` also matched a *suffix* of a longer name, which is how
  938. `plate_1` reached `lid_plate_1.gcode.3mf`. A name is either the whole
  939. basename or the basename after a directory separator.
  940. """
  941. escaped = _like_escape(stem)
  942. return column.ilike(f"{escaped}.%", escape="\\") | column.ilike(f"%/{escaped}.%", escape="\\")
  943. def _expected_plate_for_print(plate_id: int | None, gcode_file: str | None) -> int | None:
  944. """The plate a running print is on, from whatever was recorded about it.
  945. ``plate_id`` is the reliable source, and the archives that need a donor 3MF
  946. have none: the no-3MF fallback row is created before any 3MF is read, so
  947. the column is never filled. The gcode path the printer echoed is the other
  948. source, exact on the firmwares that echo ``Metadata/plate_N.gcode``. Some
  949. P1S builds echo only the 3MF filename, and then the plate is simply not
  950. knowable at print start (#2957).
  951. """
  952. from backend.app.services.printer_manager import parse_plate_id
  953. if plate_id is not None:
  954. return plate_id
  955. return parse_plate_id(gcode_file)
  956. def _donor_3mf_conflicts(candidate, expected_plate: int | None) -> str | None:
  957. """Why *candidate* cannot be this print's 3MF, or None if nothing rules it out.
  958. A same-name 3MF is not the same print. Bambu Studio writes the printer-side
  959. filename from the project's ``Title`` metadata, so every plate of a project
  960. arrives under one name however the user renamed the file on disk, and a
  961. donor chosen on the name alone hands one plate's slicer estimates to another
  962. plate's print. A reporter's single-filament job was charged against three
  963. spools that way, and nothing about the deduction said it was a guess
  964. (#2957).
  965. The plate is the one thing that can settle this. It is the same comparison
  966. #1204 already makes against a freshly downloaded 3MF, so a single-plate
  967. export is known to carry its original index rather than a renumbered 1.
  968. Filament *count* deliberately is not checked, however tempting: the slicer's
  969. ``ams_mapping`` is indexed by the project's filament slot -- see
  970. ``slot_to_tray[slot_id - 1]`` below -- not by the plate's, so a real
  971. single-filament print reports ``[0, -1, -1, -1]`` and its length says
  972. nothing about how many filaments the plate uses.
  973. """
  974. from backend.app.services.archive import plate_indexes_in_3mf
  975. if expected_plate is None:
  976. return None
  977. plates = plate_indexes_in_3mf(candidate)
  978. if not plates or any(plate is None for plate in plates):
  979. # Nothing was read, or not all of it was, and neither is evidence about
  980. # the plate. Refusing here would drop the fallback for every 3MF variant
  981. # this parser does not understand; downstream reports that honestly as
  982. # "no filament usage data".
  983. return None
  984. if len(plates) == 1 and plates[0] != expected_plate:
  985. return f"it holds plate {plates[0]}, this print is plate {expected_plate}"
  986. if expected_plate not in plates:
  987. # An all-plates export is a good donor precisely when it carries the
  988. # plate that is running. Without this the plate is looked for
  989. # downstream, found missing, and the whole file's filaments are summed
  990. # onto one plate's print.
  991. return f"it has no plate {expected_plate}"
  992. return None
  993. async def _resolve_3mf_fallback(archive, db: AsyncSession, base_dir):
  994. """Try to find a 3MF file from library or a previous archive when the current archive has none.
  995. This handles fallback archives (FTP download failed) where the 3MF may already exist
  996. locally from a library upload or a previous successful print of the same file.
  997. A name match alone does not make a candidate this print's file, so every
  998. candidate is put through :func:`_donor_3mf_conflicts` before it is handed
  999. back (#2957).
  1000. """
  1001. from pathlib import Path
  1002. from backend.app.models.archive import PrintArchive
  1003. from backend.app.models.library import LibraryFile
  1004. # Derive search name from archive filename (e.g. "benchy.3mf" or "benchy.gcode.3mf"),
  1005. # falling back to the print name when the filename is only a plate path.
  1006. search_base = _threemf_search_stem(archive.filename, archive.print_name)
  1007. if not search_base:
  1008. return None
  1009. print_data = (getattr(archive, "extra_data", None) or {}).get("_print_data") or {}
  1010. expected_plate = _expected_plate_for_print(
  1011. getattr(archive, "plate_id", None),
  1012. archive.filename or print_data.get("filename"),
  1013. )
  1014. if expected_plate is None:
  1015. # Worth saying out loud. On the firmwares that echo only the 3MF
  1016. # filename there is nothing to check a donor against, so whatever is
  1017. # accepted below is accepted on its name alone -- which is how the
  1018. # reporter's spools were debited for another plate's filament. The
  1019. # deduction being silent was half the bug (#2957).
  1020. logger.warning(
  1021. "[UsageTracker] 3MF fallback: archive %s does not know its plate (%r), so a same-named "
  1022. "3MF can only be matched on its name",
  1023. archive.id,
  1024. archive.filename,
  1025. )
  1026. # 1. Try library files matching the name (match base name at file boundary)
  1027. try:
  1028. lib_result = await db.execute(
  1029. LibraryFile.active()
  1030. .where(_stem_matches(LibraryFile.file_path, search_base))
  1031. .where(LibraryFile.file_path.ilike("%.3mf"))
  1032. .order_by(LibraryFile.created_at.desc())
  1033. .limit(3)
  1034. )
  1035. for lib_file in lib_result.scalars().all():
  1036. lib_path = Path(lib_file.file_path)
  1037. candidate = lib_path if lib_path.is_absolute() else base_dir / lib_file.file_path
  1038. if candidate.exists() and candidate.suffix == ".3mf":
  1039. conflict = _donor_3mf_conflicts(candidate, expected_plate)
  1040. if conflict:
  1041. logger.warning(
  1042. "[UsageTracker] 3MF fallback: not using library file %s for archive %s — %s",
  1043. candidate,
  1044. archive.id,
  1045. conflict,
  1046. )
  1047. continue
  1048. logger.info(
  1049. "[UsageTracker] 3MF fallback: found library file %s for archive %s (expected plate=%s)",
  1050. candidate,
  1051. archive.id,
  1052. expected_plate,
  1053. )
  1054. return candidate
  1055. except Exception as e:
  1056. logger.debug("[UsageTracker] 3MF fallback: library lookup failed: %s", e)
  1057. # 2. Try previous archives with the same filename that have a valid file_path
  1058. try:
  1059. prev_result = await db.execute(
  1060. select(PrintArchive)
  1061. .where(PrintArchive.id != archive.id)
  1062. .where(PrintArchive.printer_id == archive.printer_id)
  1063. .where(PrintArchive.file_path != "")
  1064. .where(PrintArchive.file_path.isnot(None))
  1065. .where(_stem_matches(PrintArchive.filename, search_base))
  1066. .order_by(PrintArchive.created_at.desc())
  1067. .limit(3)
  1068. )
  1069. for prev_archive in prev_result.scalars().all():
  1070. candidate = base_dir / prev_archive.file_path
  1071. if candidate.exists() and candidate.suffix == ".3mf":
  1072. conflict = _donor_3mf_conflicts(candidate, expected_plate)
  1073. if conflict:
  1074. logger.warning(
  1075. "[UsageTracker] 3MF fallback: not using archive %s's file for archive %s — %s",
  1076. prev_archive.id,
  1077. archive.id,
  1078. conflict,
  1079. )
  1080. continue
  1081. logger.info(
  1082. "[UsageTracker] 3MF fallback: found previous archive %s file for archive %s (expected plate=%s)",
  1083. prev_archive.id,
  1084. archive.id,
  1085. expected_plate,
  1086. )
  1087. return candidate
  1088. except Exception as e:
  1089. logger.debug("[UsageTracker] 3MF fallback: previous archive lookup failed: %s", e)
  1090. return None
  1091. async def _find_3mf_by_filename(
  1092. printer_id: int,
  1093. filename: str,
  1094. db: AsyncSession,
  1095. base_dir,
  1096. print_name: str | None = None,
  1097. ):
  1098. """Find a 3MF file by filename from library or previous archives.
  1099. Used when auto-archive is disabled and there's no archive_id, but we still
  1100. need the 3MF slicer data for filament usage tracking.
  1101. ``print_name`` is the model name to fall back to when ``filename`` is the
  1102. printer's plate path, which names no model at all -- and when it is that
  1103. plate path, it is also what keeps a same-named file for a different plate
  1104. from being adopted (#2957); see :func:`_donor_3mf_conflicts`.
  1105. """
  1106. from pathlib import Path
  1107. from backend.app.models.archive import PrintArchive
  1108. from backend.app.models.library import LibraryFile
  1109. search_base = _threemf_search_stem(filename, print_name)
  1110. if not search_base:
  1111. return None
  1112. expected_plate = _expected_plate_for_print(None, filename)
  1113. # 1. Try library files matching the name
  1114. try:
  1115. lib_result = await db.execute(
  1116. LibraryFile.active()
  1117. .where(_stem_matches(LibraryFile.file_path, search_base))
  1118. .where(LibraryFile.file_path.ilike("%.3mf"))
  1119. .order_by(LibraryFile.created_at.desc())
  1120. .limit(3)
  1121. )
  1122. for lib_file in lib_result.scalars().all():
  1123. lib_path = Path(lib_file.file_path)
  1124. candidate = lib_path if lib_path.is_absolute() else base_dir / lib_file.file_path
  1125. if candidate.exists() and candidate.suffix == ".3mf":
  1126. conflict = _donor_3mf_conflicts(candidate, expected_plate)
  1127. if conflict:
  1128. logger.warning(
  1129. "[UsageTracker] 3MF (no-archive): not using library file %s for '%s' — %s",
  1130. candidate,
  1131. filename,
  1132. conflict,
  1133. )
  1134. continue
  1135. logger.info("[UsageTracker] 3MF (no-archive): found library file %s for '%s'", candidate, filename)
  1136. return candidate
  1137. except Exception as e:
  1138. logger.debug("[UsageTracker] 3MF (no-archive): library lookup failed: %s", e)
  1139. # 2. Try previous archives with a valid 3MF file_path
  1140. try:
  1141. prev_result = await db.execute(
  1142. select(PrintArchive)
  1143. .where(PrintArchive.printer_id == printer_id)
  1144. .where(PrintArchive.file_path != "")
  1145. .where(PrintArchive.file_path.isnot(None))
  1146. .where(_stem_matches(PrintArchive.filename, search_base))
  1147. .order_by(PrintArchive.created_at.desc())
  1148. .limit(3)
  1149. )
  1150. for prev_archive in prev_result.scalars().all():
  1151. candidate = base_dir / prev_archive.file_path
  1152. if candidate.exists() and candidate.suffix == ".3mf":
  1153. conflict = _donor_3mf_conflicts(candidate, expected_plate)
  1154. if conflict:
  1155. logger.warning(
  1156. "[UsageTracker] 3MF (no-archive): not using archive %s's file for '%s' — %s",
  1157. prev_archive.id,
  1158. filename,
  1159. conflict,
  1160. )
  1161. continue
  1162. logger.info(
  1163. "[UsageTracker] 3MF (no-archive): found previous archive %s file for '%s'",
  1164. prev_archive.id,
  1165. filename,
  1166. )
  1167. return candidate
  1168. except Exception as e:
  1169. logger.debug("[UsageTracker] 3MF (no-archive): previous archive lookup failed: %s", e)
  1170. return None
  1171. async def _track_from_3mf(
  1172. printer_id: int,
  1173. archive_id: int | None,
  1174. status: str,
  1175. print_name: str,
  1176. handled_trays: set[tuple[int, int]],
  1177. printer_manager,
  1178. db: AsyncSession,
  1179. ams_mapping: list[int] | None = None,
  1180. tray_now_at_start: int = -1,
  1181. last_progress: float = 0.0,
  1182. last_layer_num: int = 0,
  1183. default_filament_cost: float = 0.0,
  1184. spool_assignments: dict[tuple[int, int], int] | None = None,
  1185. print_started_at: datetime | None = None,
  1186. threemf_path=None,
  1187. plate_id: int | None = None,
  1188. ) -> list[dict]:
  1189. """Track usage from 3MF per-filament slicer data (primary path).
  1190. Uses slicer-estimated filament weight for all spools (BL and non-BL).
  1191. For partial prints (failed/aborted), tries per-layer gcode data first,
  1192. then falls back to linear scaling by progress.
  1193. When archive_id is None (auto-archive disabled), a pre-resolved threemf_path
  1194. can be provided to still track filament usage from slicer data.
  1195. When ``plate_id`` is set (queue prints of a single plate from a multi-plate
  1196. 3MF), only that plate's filaments contribute. Without it the 3MF parser sums
  1197. every plate, which is correct for direct/library Print flows that always
  1198. target the first or only plate (#1697).
  1199. Slot-to-tray mapping priority:
  1200. 1. Stored ams_mapping from print command (reprints/direct prints)
  1201. 2. MQTT mapping field from printer state (universal, all print sources)
  1202. 3. Queue item ams_mapping (for queue-initiated prints)
  1203. 4. tray_now from printer state (for single-filament non-queue prints)
  1204. 5. Position-based default using sorted available tray IDs (handles external spools)
  1205. 6. Default mapping: slot_id - 1 = global_tray_id (last resort)
  1206. """
  1207. from pathlib import Path
  1208. from backend.app.core.config import settings as app_settings
  1209. from backend.app.models.archive import PrintArchive
  1210. from backend.app.models.print_queue import PrintQueueItem
  1211. from backend.app.utils.threemf_tools import extract_filament_usage_from_3mf
  1212. file_path: Path | None = threemf_path
  1213. archive: PrintArchive | None = None
  1214. if file_path is None and archive_id:
  1215. result = await db.execute(select(PrintArchive).where(PrintArchive.id == archive_id))
  1216. archive = result.scalar_one_or_none()
  1217. if not archive:
  1218. logger.info("[UsageTracker] 3MF: archive %s not found, skipping", archive_id)
  1219. return []
  1220. # Try archive's own file_path first
  1221. if archive.file_path:
  1222. candidate = app_settings.base_dir / archive.file_path
  1223. if candidate.exists():
  1224. file_path = candidate
  1225. # Fallback: find 3MF from library or a previous archive with the same filename
  1226. if file_path is None:
  1227. file_path = await _resolve_3mf_fallback(archive, db, app_settings.base_dir)
  1228. if file_path is None:
  1229. logger.info("[UsageTracker] 3MF: no file available for archive %s, skipping", archive_id)
  1230. return []
  1231. # The queue item carries both the plate and the dispatched mapping; look it
  1232. # up at most once. A batch dispatches one archive as several queue items,
  1233. # on several printers at once: only this printer's newest is this run.
  1234. # Completion has already closed it, as cancelled for an aborted print.
  1235. _queue_item_lookup: list = []
  1236. async def _dispatch_queue_item():
  1237. if not _queue_item_lookup:
  1238. if not archive_id:
  1239. _queue_item_lookup.append(None)
  1240. else:
  1241. queue_result = await db.execute(
  1242. select(PrintQueueItem)
  1243. .where(PrintQueueItem.archive_id == archive_id)
  1244. .where(PrintQueueItem.printer_id == printer_id)
  1245. .where(PrintQueueItem.status.in_(["printing", "completed", "failed", "cancelled"]))
  1246. .order_by(PrintQueueItem.id.desc())
  1247. )
  1248. _queue_item_lookup.append(queue_result.scalars().first())
  1249. return _queue_item_lookup[0]
  1250. # The caller's plate_id comes from the in-memory session, which a restart
  1251. # mid-print destroys. Both the archive and the queue item recorded the
  1252. # plate at dispatch — without falling back to them the parser sums every
  1253. # plate of a multi-plate file and charges the lot to one spool. The
  1254. # newest queue item goes first: the archive keeps the plate of its first
  1255. # print, so a reprint of another plate is only on the queue item.
  1256. if plate_id is None:
  1257. plate_queue_item = await _dispatch_queue_item()
  1258. if plate_queue_item is not None and plate_queue_item.plate_id is not None:
  1259. plate_id = plate_queue_item.plate_id
  1260. logger.info(
  1261. "[UsageTracker] 3MF: plate_id=%s recovered from queue item %s",
  1262. plate_id,
  1263. plate_queue_item.id,
  1264. )
  1265. elif archive is not None and archive.plate_id is not None:
  1266. plate_id = archive.plate_id
  1267. logger.info("[UsageTracker] 3MF: plate_id=%s recovered from archive %s", plate_id, archive_id)
  1268. filament_usage = extract_filament_usage_from_3mf(file_path, plate_id)
  1269. if not filament_usage and plate_id is not None:
  1270. # The plate isn't in this file. That happens when the archive's own 3MF
  1271. # is gone and `_resolve_3mf_fallback` substituted a same-named file from
  1272. # the library that was sliced with different plates. Summing the whole
  1273. # file is wrong for a single-plate run, but it is closer than recording
  1274. # nothing at all — and unlike the silent whole-file sum this replaces,
  1275. # it says so.
  1276. filament_usage = extract_filament_usage_from_3mf(file_path, None)
  1277. if filament_usage:
  1278. logger.warning(
  1279. "[UsageTracker] 3MF: plate %s not present in %s — falling back to the whole-file total",
  1280. plate_id,
  1281. file_path,
  1282. )
  1283. plate_id = None
  1284. if not filament_usage:
  1285. logger.info("[UsageTracker] 3MF: no filament usage data in %s", file_path)
  1286. return []
  1287. logger.info("[UsageTracker] 3MF: archive %s, plate_id=%s, filament_usage=%s", archive_id, plate_id, filament_usage)
  1288. # --- Resolve slot-to-tray mapping ---
  1289. mapping_source = None
  1290. # 1. Use stored ams_mapping from the print command (reprints/direct prints)
  1291. slot_to_tray = ams_mapping
  1292. if slot_to_tray:
  1293. mapping_source = "print_cmd"
  1294. # 2. Try queue item ams_mapping (queue-initiated prints store the exact mapping)
  1295. #
  1296. # Ranked above the live MQTT field on purpose: `mapping` reports the tray
  1297. # the printer is feeding from *now*, and AMS filament backup rewrites it to
  1298. # the substitute tray when a spool runs dry. Read at completion it names
  1299. # the tray that finished the print, not the one the slicer assigned — the
  1300. # queue item's copy is the mapping the print was actually dispatched with.
  1301. if not slot_to_tray and archive_id:
  1302. queue_item = await _dispatch_queue_item()
  1303. if queue_item and queue_item.ams_mapping:
  1304. try:
  1305. slot_to_tray = json.loads(queue_item.ams_mapping)
  1306. mapping_source = "queue"
  1307. except (json.JSONDecodeError, TypeError):
  1308. pass
  1309. # 3. Try MQTT mapping field from printer state (universal, all print sources)
  1310. if not slot_to_tray:
  1311. state = printer_manager.get_status(printer_id)
  1312. raw_data = getattr(state, "raw_data", None) if state else None
  1313. if raw_data:
  1314. mqtt_mapping = raw_data.get("mapping")
  1315. decoded = _decode_mqtt_mapping(mqtt_mapping)
  1316. if decoded:
  1317. slot_to_tray = decoded
  1318. mapping_source = "mqtt"
  1319. # 4. Color-match 3MF filament slots to AMS trays (for printers without mapping field)
  1320. if not slot_to_tray:
  1321. state = printer_manager.get_status(printer_id)
  1322. raw_data = getattr(state, "raw_data", None) if state else None
  1323. if raw_data:
  1324. matched = _match_slots_by_color(filament_usage, raw_data.get("ams"))
  1325. if matched:
  1326. slot_to_tray = matched
  1327. mapping_source = "color_match"
  1328. logger.info(
  1329. "[UsageTracker] 3MF: slot_to_tray=%s (source: %s)",
  1330. slot_to_tray,
  1331. mapping_source or "none",
  1332. )
  1333. # A mapping that names no tray for any slot the print used carries no
  1334. # evidence at all -- typically a flat ams_mapping whose external spool is
  1335. # -1 and came without an ams_mapping2 to resolve it (#3166). Drop it so the
  1336. # tray_now evidence below decides, rather than leaving every slot unfed.
  1337. if slot_to_tray:
  1338. used_slots = [u.get("slot_id", 0) for u in filament_usage if u.get("used_g", 0) > 0]
  1339. if used_slots and all(
  1340. not (0 < s <= len(slot_to_tray) and isinstance(slot_to_tray[s - 1], int) and slot_to_tray[s - 1] >= 0)
  1341. for s in used_slots
  1342. ):
  1343. logger.info(
  1344. "[UsageTracker] 3MF: mapping %s names no tray for used slots %s — ignoring it",
  1345. slot_to_tray,
  1346. used_slots,
  1347. )
  1348. slot_to_tray = None
  1349. mapping_source = None
  1350. # 5. For single-filament non-queue prints, use tray_now from printer state
  1351. # Priority: tray_change_log (multi-tray split) > tray_now_at_start > current tray_now
  1352. # > last_loaded_tray > vt_tray check
  1353. #
  1354. # tray_change_log evidence wins over slot_to_tray when present: if the
  1355. # printer fed from multiple trays mid-print (AMS auto-fallback when one
  1356. # spool runs out, #957), the slicer's mapping captured at print start
  1357. # is stale and needs to be replaced with per-layer split attribution.
  1358. nonzero_slots = [u for u in filament_usage if u.get("used_g", 0) > 0]
  1359. tray_now_override: int | None = None
  1360. tray_changes: list[tuple[int, int]] = [] # [(global_tray_id, layer_num), ...]
  1361. state = printer_manager.get_status(printer_id) if len(nonzero_slots) == 1 else None
  1362. if state is not None:
  1363. tray_changes = getattr(state, "tray_change_log", []) or []
  1364. elif len(nonzero_slots) > 1:
  1365. # Multi-material print: every filament change moves tray_now, so the
  1366. # log can't be read as "this slot moved to that tray" and splitting
  1367. # would attribute worse than the mapping does. Say so rather than
  1368. # silently dropping the evidence — a runout mid-print on a
  1369. # multi-material job still lands entirely on the mapped tray.
  1370. _multi_state = printer_manager.get_status(printer_id)
  1371. if len(getattr(_multi_state, "tray_change_log", []) or []) > 1:
  1372. logger.warning(
  1373. "[UsageTracker] 3MF: %d tray changes observed but %d filament slots used — "
  1374. "splitting needs a single slot, attributing by mapping alone (printer %d, archive %s)",
  1375. len(_multi_state.tray_change_log),
  1376. len(nonzero_slots),
  1377. printer_id,
  1378. archive_id,
  1379. )
  1380. if len(tray_changes) > 1:
  1381. # Multi-tray usage detected — splitting takes over regardless of slot_to_tray.
  1382. logger.info("[UsageTracker] 3MF: tray change log: %s (will split weight)", tray_changes)
  1383. elif not slot_to_tray and len(nonzero_slots) == 1:
  1384. if 0 <= tray_now_at_start <= 254:
  1385. tray_now_override = tray_now_at_start
  1386. logger.info("[UsageTracker] 3MF: using tray_now_at_start=%d (single-filament fallback)", tray_now_at_start)
  1387. elif state and 0 <= state.tray_now <= 254:
  1388. tray_now_override = state.tray_now
  1389. logger.info("[UsageTracker] 3MF: using current tray_now=%d", state.tray_now)
  1390. elif state and 0 <= state.last_loaded_tray <= 253:
  1391. tray_now_override = state.last_loaded_tray
  1392. logger.info("[UsageTracker] 3MF: using last_loaded_tray=%d (post-retract fallback)", state.last_loaded_tray)
  1393. elif state and state.tray_now == 255:
  1394. # 255 = "no filament" on legacy printers, but valid 2nd external spool on H2-series
  1395. vt_tray = state.raw_data.get("vt_tray") or []
  1396. if any(int(vt.get("id", 0)) == 255 for vt in vt_tray if isinstance(vt, dict)):
  1397. tray_now_override = state.tray_now
  1398. logger.info("[UsageTracker] 3MF: using tray_now=255 (H2-series external spool)")
  1399. if tray_now_override is None:
  1400. logger.info(
  1401. "[UsageTracker] 3MF: no valid tray_now (at_start=%d, current=%s, last_loaded=%s)",
  1402. tray_now_at_start,
  1403. state.tray_now if state else "N/A",
  1404. state.last_loaded_tray if state else "N/A",
  1405. )
  1406. # Scale factor for partial prints (failed/aborted)
  1407. if status == "completed":
  1408. scale = 1.0
  1409. else:
  1410. state = printer_manager.get_status(printer_id)
  1411. progress = state.progress if state else 0
  1412. # Firmware resets progress to 0 on cancel — use last valid progress captured during print
  1413. if progress <= 0 and last_progress > 0:
  1414. progress = last_progress
  1415. logger.info("[UsageTracker] 3MF: using last_progress=%.1f (firmware reset current to 0)", last_progress)
  1416. scale = max(0.0, min(progress / 100.0, 1.0))
  1417. # Per-layer gcode accuracy for partial prints
  1418. layer_grams: dict[int, float] | None = None
  1419. if status != "completed":
  1420. state = printer_manager.get_status(printer_id)
  1421. current_layer = state.layer_num if state else 0
  1422. # Firmware resets layer_num to 0 on cancel — use last valid layer captured during print
  1423. if current_layer <= 0 and last_layer_num > 0:
  1424. current_layer = last_layer_num
  1425. logger.info("[UsageTracker] 3MF: using last_layer_num=%d (firmware reset current to 0)", last_layer_num)
  1426. if current_layer > 0:
  1427. try:
  1428. from backend.app.utils.threemf_tools import (
  1429. extract_filament_properties_from_3mf,
  1430. extract_layer_filament_usage_from_3mf,
  1431. get_cumulative_usage_at_layer,
  1432. mm_to_grams,
  1433. )
  1434. layer_usage = extract_layer_filament_usage_from_3mf(file_path, plate_id)
  1435. if layer_usage:
  1436. cumulative_mm = get_cumulative_usage_at_layer(layer_usage, current_layer)
  1437. filament_props = extract_filament_properties_from_3mf(file_path)
  1438. layer_grams = {}
  1439. for filament_id, mm_used in cumulative_mm.items():
  1440. slot_id = filament_id + 1 # 0-based to 1-based
  1441. props = filament_props.get(slot_id, {})
  1442. density = props.get("density", 1.24)
  1443. diameter = props.get("diameter", 1.75)
  1444. layer_grams[slot_id] = mm_to_grams(mm_used, diameter, density)
  1445. except Exception:
  1446. pass # Fall back to linear scaling
  1447. results = []
  1448. # Trays this print drew from that no longer have an assignment to charge.
  1449. # Collected rather than acted on inline so one notification covers the whole
  1450. # print instead of one per slot (#2812).
  1451. unassigned_global_trays: list[int] = []
  1452. # Deduplicated by slicer slot, not by tray (#3230). Several slots mapped to
  1453. # one tray, or a single-slot print that returns to an earlier tray, each
  1454. # consume filament of their own. A slot only repeats when the plate is
  1455. # unknown and every plate of the file is listed, and that must not charge
  1456. # the same slot once per plate. Only a tray the caller had already covered
  1457. # before this pass is skipped.
  1458. charged_slots: set[int] = set()
  1459. covered_before = frozenset(handled_trays)
  1460. for usage in filament_usage:
  1461. slot_id = usage.get("slot_id", 0)
  1462. used_g = usage.get("used_g", 0)
  1463. if used_g <= 0:
  1464. continue
  1465. if slot_id in charged_slots:
  1466. continue
  1467. charged_slots.add(slot_id)
  1468. # --- Mid-print tray switch: split weight across trays ---
  1469. # Split math is shared with the Spoolman writer via
  1470. # ``utils.tray_split.compute_tray_split_grams`` (#1793) — both
  1471. # inventory backends must attribute segments identically or a
  1472. # user running dual-mode sees divergent totals.
  1473. if len(tray_changes) > 1:
  1474. # Compute total weight for this slot (same logic as normal path)
  1475. if layer_grams and slot_id in layer_grams:
  1476. total_weight = layer_grams[slot_id]
  1477. else:
  1478. total_weight = used_g * scale
  1479. if total_weight <= 0:
  1480. continue
  1481. # Extract per-layer gcode for segment splitting
  1482. split_layer_usage = None
  1483. split_props: dict = {}
  1484. try:
  1485. from backend.app.utils.threemf_tools import (
  1486. extract_filament_properties_from_3mf,
  1487. extract_layer_filament_usage_from_3mf,
  1488. )
  1489. split_layer_usage = extract_layer_filament_usage_from_3mf(file_path, plate_id)
  1490. filament_props = extract_filament_properties_from_3mf(file_path)
  1491. split_props = filament_props.get(slot_id, {})
  1492. except Exception:
  1493. pass # Fall back to linear splitting
  1494. from backend.app.utils.tray_split import compute_tray_split_grams
  1495. segments = compute_tray_split_grams(
  1496. tray_changes=tray_changes,
  1497. total_weight=total_weight,
  1498. slot_id=slot_id,
  1499. layer_usage=split_layer_usage,
  1500. density=split_props.get("density", 1.24),
  1501. diameter=split_props.get("diameter", 1.75),
  1502. total_layers=(state.total_layers if state else 0) or 0,
  1503. last_layer_num=last_layer_num,
  1504. )
  1505. for seg_idx, tray_global, segment_grams in segments:
  1506. if segment_grams <= 0:
  1507. continue
  1508. # Convert global tray ID to (ams_id, tray_id)
  1509. if tray_global >= 254:
  1510. seg_ams_id = 255
  1511. seg_tray_id = tray_global - 254
  1512. elif tray_global >= 128:
  1513. seg_ams_id = tray_global
  1514. seg_tray_id = 0
  1515. else:
  1516. seg_ams_id = tray_global // 4
  1517. seg_tray_id = tray_global % 4
  1518. seg_key = (seg_ams_id, seg_tray_id)
  1519. if seg_key in covered_before:
  1520. continue
  1521. seg_start_layer = tray_changes[seg_idx][1]
  1522. is_last = seg_idx + 1 >= len(tray_changes)
  1523. logger.info(
  1524. "[UsageTracker] 3MF split: segment %d tray=%d (AMS%d-T%d) layers %d-%s -> %.1fg",
  1525. seg_idx,
  1526. tray_global,
  1527. seg_ams_id,
  1528. seg_tray_id,
  1529. seg_start_layer,
  1530. tray_changes[seg_idx + 1][1] if not is_last else "end",
  1531. segment_grams,
  1532. )
  1533. seg_spool_id = await _resolve_spool_id_for_tray(
  1534. printer_id=printer_id,
  1535. ams_id=seg_ams_id,
  1536. tray_id=seg_tray_id,
  1537. db=db,
  1538. spool_assignments_snapshot=spool_assignments,
  1539. print_started_at=print_started_at,
  1540. )
  1541. if seg_spool_id is None:
  1542. logger.info(
  1543. "[UsageTracker] 3MF split: no spool at printer %d AMS%d-T%d, skipping segment",
  1544. printer_id,
  1545. seg_ams_id,
  1546. seg_tray_id,
  1547. )
  1548. continue
  1549. spool_result = await db.execute(select(Spool).where(Spool.id == seg_spool_id))
  1550. spool = spool_result.scalar_one_or_none()
  1551. if not spool:
  1552. continue
  1553. if not await _charge_spool(db, spool, segment_grams):
  1554. continue
  1555. percent = round(segment_grams / (spool.label_weight or 1000) * 100)
  1556. cost = None
  1557. cost_per_kg = spool.cost_per_kg if spool.cost_per_kg is not None else default_filament_cost
  1558. if cost_per_kg > 0:
  1559. cost = round((segment_grams / 1000.0) * cost_per_kg, 2)
  1560. history = SpoolUsageHistory(
  1561. spool_id=spool.id,
  1562. printer_id=printer_id,
  1563. print_name=print_name,
  1564. weight_used=round(segment_grams, 1),
  1565. percent_used=percent,
  1566. status=status,
  1567. cost=cost,
  1568. archive_id=archive_id,
  1569. )
  1570. db.add(history)
  1571. handled_trays.add(seg_key)
  1572. results.append(
  1573. {
  1574. "spool_id": spool.id,
  1575. "weight_used": round(segment_grams, 1),
  1576. "percent_used": percent,
  1577. "ams_id": seg_ams_id,
  1578. "tray_id": seg_tray_id,
  1579. "material": spool.material,
  1580. "cost": cost,
  1581. "slot_id": slot_id,
  1582. "color": _spool_color_to_hex(spool.rgba),
  1583. }
  1584. )
  1585. logger.info(
  1586. "[UsageTracker] Spool %d consumed %.1fg (3MF split seg%d) on printer %d AMS%d-T%d (%s)",
  1587. spool.id,
  1588. segment_grams,
  1589. seg_idx,
  1590. printer_id,
  1591. seg_ams_id,
  1592. seg_tray_id,
  1593. status,
  1594. )
  1595. continue # Skip normal single-tray processing for this slot
  1596. # Map 3MF slot_id to physical (ams_id, tray_id) using resolved mapping
  1597. tray_is_guessed = False
  1598. if tray_now_override is not None:
  1599. # Single-filament non-queue print: use actual tray from printer state
  1600. global_tray_id = tray_now_override
  1601. else:
  1602. # Explicit mapping (print command, MQTT, queue, color match)
  1603. global_tray_id = None
  1604. if slot_to_tray and slot_id <= len(slot_to_tray):
  1605. mapped = slot_to_tray[slot_id - 1]
  1606. if isinstance(mapped, int) and mapped >= 0:
  1607. global_tray_id = mapped
  1608. elif mapped == -1:
  1609. # The mapping says this slot isn't fed from any tray. The
  1610. # position-based guess below would hand it the Nth loaded
  1611. # tray anyway -- an AMS spool that never moved (#3166), or
  1612. # the tray another slot really used, which that slot then
  1613. # finds already handled and drops (#2880).
  1614. logger.info(
  1615. "[UsageTracker] 3MF: slot_id=%d is unmapped (-1) — nothing charged (used_g=%.1f)",
  1616. slot_id,
  1617. used_g,
  1618. )
  1619. continue
  1620. # Position-based default: sort available tray IDs so external spools (254/255)
  1621. # naturally follow standard AMS trays, matching slicer slot numbering.
  1622. #
  1623. # Filter out AMS slots that have no spool loaded (empty `tray_type`) —
  1624. # BambuStudio/OrcaSlicer compact the slot list when assigning filaments
  1625. # and don't expose empty AMS slots to the user, so the slicer's 3MF
  1626. # slot N maps to the Nth *loaded* tray, not the Nth physical position.
  1627. # Without this filter a "3 AMS slots loaded + 1 empty + external"
  1628. # layout routes the slicer's 4th filament to the empty AMS slot
  1629. # instead of the external (#1607), and the external's spool usage
  1630. # never gets recorded. vt_tray entries are already filtered the
  1631. # same way inside `build_ams_tray_lookup` (line 174 checks
  1632. # `tray_type`), so this just mirrors that for the AMS side.
  1633. tray_is_guessed = global_tray_id is None
  1634. if global_tray_id is None:
  1635. _state = printer_manager.get_status(printer_id)
  1636. _raw = getattr(_state, "raw_data", None) if _state else None
  1637. if _raw:
  1638. from backend.app.services.spoolman_tracking import build_ams_tray_lookup
  1639. _lookup = build_ams_tray_lookup(_raw)
  1640. available_trays = sorted(gid for gid, info in _lookup.items() if info.get("tray_type"))
  1641. if slot_id <= len(available_trays):
  1642. global_tray_id = available_trays[slot_id - 1]
  1643. # Final fallback: slot_id - 1 (legacy, works for pure AMS without external spools)
  1644. if global_tray_id is None:
  1645. global_tray_id = slot_id - 1
  1646. if global_tray_id >= 254:
  1647. # External spool: ams_id=255 (sentinel), tray_id=slot index (0 or 1)
  1648. ams_id = 255
  1649. tray_id = global_tray_id - 254
  1650. elif global_tray_id >= 128:
  1651. ams_id = global_tray_id
  1652. tray_id = 0
  1653. else:
  1654. ams_id = global_tray_id // 4
  1655. tray_id = global_tray_id % 4
  1656. logger.info(
  1657. "[UsageTracker] 3MF: slot_id=%d -> global_tray=%d -> AMS%d-T%d (used_g=%.1f, tray_now_override=%s)",
  1658. slot_id,
  1659. global_tray_id,
  1660. ams_id,
  1661. tray_id,
  1662. used_g,
  1663. tray_now_override,
  1664. )
  1665. key = (ams_id, tray_id)
  1666. if key in covered_before:
  1667. continue
  1668. if tray_is_guessed and key in handled_trays:
  1669. # A guess by position can land a second slot on a tray another slot
  1670. # was charged for. That says nothing about which spool fed it, so
  1671. # it is not charged to that tray's spool as well.
  1672. continue
  1673. spool_id = await _resolve_spool_id_for_tray(
  1674. printer_id=printer_id,
  1675. ams_id=ams_id,
  1676. tray_id=tray_id,
  1677. db=db,
  1678. spool_assignments_snapshot=spool_assignments,
  1679. print_started_at=print_started_at,
  1680. )
  1681. if spool_id is None:
  1682. # WARNING, not INFO: everything upstream of this line succeeded --
  1683. # the 3MF was found, the grams were read, the tray resolved -- and
  1684. # the print will still report success while this filament is never
  1685. # deducted. At INFO it was invisible under the default log level and
  1686. # absent from the reasoning in support bundles (#2812).
  1687. logger.warning(
  1688. "[UsageTracker] 3MF: no spool assignment at printer %d AMS%d-T%d — %.1fg not deducted",
  1689. printer_id,
  1690. ams_id,
  1691. tray_id,
  1692. used_g,
  1693. )
  1694. unassigned_global_trays.append(global_tray_id)
  1695. continue
  1696. # Load spool
  1697. spool_result = await db.execute(select(Spool).where(Spool.id == spool_id))
  1698. spool = spool_result.scalar_one_or_none()
  1699. if not spool:
  1700. continue
  1701. # Use per-layer grams if available, otherwise linear scale
  1702. if layer_grams and slot_id in layer_grams:
  1703. weight_grams = layer_grams[slot_id]
  1704. else:
  1705. weight_grams = used_g * scale
  1706. if weight_grams <= 0:
  1707. continue
  1708. # Update spool
  1709. if not await _charge_spool(db, spool, weight_grams):
  1710. continue
  1711. percent = round(weight_grams / (spool.label_weight or 1000) * 100)
  1712. # Calculate cost for this usage
  1713. cost = None
  1714. cost_per_kg = spool.cost_per_kg if spool.cost_per_kg is not None else default_filament_cost
  1715. if cost_per_kg > 0:
  1716. cost = round((weight_grams / 1000.0) * cost_per_kg, 2)
  1717. # Insert usage history record
  1718. history = SpoolUsageHistory(
  1719. spool_id=spool.id,
  1720. printer_id=printer_id,
  1721. print_name=print_name,
  1722. weight_used=round(weight_grams, 1),
  1723. percent_used=percent,
  1724. status=status,
  1725. cost=cost,
  1726. archive_id=archive_id,
  1727. )
  1728. db.add(history)
  1729. handled_trays.add(key)
  1730. results.append(
  1731. {
  1732. "spool_id": spool.id,
  1733. "weight_used": round(weight_grams, 1),
  1734. "percent_used": percent,
  1735. "ams_id": ams_id,
  1736. "tray_id": tray_id,
  1737. "material": spool.material,
  1738. "cost": cost,
  1739. "slot_id": slot_id,
  1740. "color": _spool_color_to_hex(spool.rgba),
  1741. }
  1742. )
  1743. # Determine mapping source for debug logging
  1744. if tray_now_override is not None:
  1745. map_src = ", tray_now"
  1746. elif mapping_source:
  1747. map_src = f", {mapping_source}_map"
  1748. else:
  1749. map_src = ""
  1750. logger.info(
  1751. "[UsageTracker] Spool %d consumed %.1fg (3MF%s%s) on printer %d AMS%d-T%d (%s)",
  1752. spool.id,
  1753. weight_grams,
  1754. " per-layer" if (layer_grams and slot_id in layer_grams) else (f" scaled {scale:.0%}" if scale < 1 else ""),
  1755. map_src,
  1756. printer_id,
  1757. ams_id,
  1758. tray_id,
  1759. status,
  1760. )
  1761. # --- Adopt the matched inventory spools' colours for the archive (#1494) ---
  1762. # The archive's filament_color was set from the slicer's 3MF at creation
  1763. # time; now that every used slot has been resolved to an inventory spool,
  1764. # the curated spool colour is authoritative. Committed by the caller's
  1765. # `if results: await db.commit()`.
  1766. if archive is not None:
  1767. spool_colors = _archive_colors_from_spools(filament_usage, results)
  1768. if spool_colors:
  1769. joined = ",".join(spool_colors)
  1770. if joined != archive.filament_color:
  1771. logger.info(
  1772. "[UsageTracker] 3MF: archive %s filament_color %r -> %r (from inventory spools)",
  1773. archive_id,
  1774. archive.filament_color,
  1775. joined,
  1776. )
  1777. archive.filament_color = joined
  1778. # Adopt the matched spools' materials too (#2563) — a slot mapped to a
  1779. # differently-typed spool than it was sliced for otherwise records the
  1780. # sliced type in the archive, Print Log and material stats.
  1781. spool_types = _archive_types_from_spools(filament_usage, results)
  1782. if spool_types:
  1783. joined_types = ",".join(spool_types)
  1784. if joined_types != archive.filament_type:
  1785. logger.info(
  1786. "[UsageTracker] 3MF: archive %s filament_type %r -> %r (from inventory spools)",
  1787. archive_id,
  1788. archive.filament_type,
  1789. joined_types,
  1790. )
  1791. archive.filament_type = joined_types
  1792. if unassigned_global_trays:
  1793. from backend.app.services.spool_assignment_notifications import (
  1794. notify_missing_spool_assignments_on_print_complete,
  1795. )
  1796. await notify_missing_spool_assignments_on_print_complete(printer_id, unassigned_global_trays, db, logger)
  1797. return results