filament_deficit.py 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843
  1. """Filament-deficit check used by every queue dispatch path.
  2. The PrintModal warns when an assigned spool can't satisfy a print's per-slot
  3. filament weight (``Pre-print checks now also warn when the spool has
  4. insufficient material`` — #720). That check only runs when the user clicks
  5. "Print" inside PrintModal; ``QueuePage`` Play button, ``start_queue_item``
  6. route, and the VP intake + scheduler auto-dispatch path all skip it (#1496).
  7. This module is the single source of truth for the check. Both the route
  8. handler (``POST /print-queue/{id}/start``) and the dispatch scheduler call
  9. ``compute_deficit_for_queue_item`` against live spool state.
  10. Design notes:
  11. * The 3MF parser is the same one used by PrintModal: per-slot ``used_grams``
  12. comes from ``extract_filament_requirements`` (#1188's filament-overrides
  13. pipeline) or — when the item points at an unsliced library file — falls
  14. through to the file's archive copy. Anything that yields no requirements
  15. is treated as "no deficit" so a malformed or stripped 3MF never blocks.
  16. * Both internal-inventory and Spoolman modes are covered. Internal mode
  17. resolves via ``SpoolAssignment`` joined to ``Spool`` (``label_weight``
  18. minus ``weight_used``). Spoolman mode resolves via
  19. ``SpoolmanSlotAssignment`` then ``SpoolmanClient.get_spool`` for the live
  20. remaining weight; if Spoolman is unreachable we return no deficit rather
  21. than wedge the queue on a flaky network call.
  22. * The ``disable_filament_warnings`` user setting is respected at the
  23. service boundary — callers do not have to know about it.
  24. """
  25. from __future__ import annotations
  26. import json
  27. import logging
  28. from collections import defaultdict
  29. from dataclasses import dataclass
  30. from pathlib import Path
  31. from sqlalchemy import select
  32. from sqlalchemy.ext.asyncio import AsyncSession
  33. from sqlalchemy.orm import selectinload
  34. from backend.app.api.routes._spoolman_helpers import _map_spoolman_spool, spoolman_net_weight
  35. from backend.app.core.config import settings as app_settings
  36. from backend.app.models.print_queue import PrintQueueItem
  37. from backend.app.models.spool_assignment import SpoolAssignment
  38. from backend.app.models.spoolman_slot_assignment import SpoolmanSlotAssignment
  39. from backend.app.services.filament_requirements import extract_filament_requirements
  40. logger = logging.getLogger(__name__)
  41. @dataclass(frozen=True)
  42. class FilamentDeficit:
  43. """One slot's filament shortfall."""
  44. slot_id: int
  45. ams_id: int | None
  46. tray_id: int | None
  47. filament_type: str
  48. required_grams: float
  49. remaining_grams: float | None # None = could not determine
  50. def to_dict(self) -> dict:
  51. return {
  52. "slot_id": self.slot_id,
  53. "ams_id": self.ams_id,
  54. "tray_id": self.tray_id,
  55. "filament_type": self.filament_type,
  56. "required_grams": self.required_grams,
  57. "remaining_grams": self.remaining_grams,
  58. }
  59. def _global_to_ams_key(global_tray_id: int) -> tuple[int, int]:
  60. """Inverse of ``ams_id * 4 + tray_id`` — matches ``usage_tracker``."""
  61. if global_tray_id >= 254:
  62. return (255, global_tray_id - 254)
  63. if global_tray_id >= 128:
  64. return (global_tray_id, 0)
  65. return (global_tray_id // 4, global_tray_id % 4)
  66. def _ams_key_to_global(ams_id: int, tray_id: int) -> int:
  67. """Inverse of ``_global_to_ams_key``.
  68. Mirrors the frontend ``getGlobalTrayId``: external / VT slots (``ams_id``
  69. 255) land at ``254 + tray_id``, AMS-HT units (``ams_id`` >= 128) use the
  70. unit id directly, regular AMS slots use ``ams_id * 4 + tray_id``.
  71. """
  72. if ams_id >= 255:
  73. return 254 + tray_id
  74. if ams_id >= 128:
  75. return ams_id
  76. return ams_id * 4 + tray_id
  77. def _resolve_source_3mf(item: PrintQueueItem) -> Path | None:
  78. """Locate the 3MF file backing this queue item (archive or library).
  79. ``LibraryFile.file_path`` is stored relative to ``base_dir`` (rows written
  80. before that convention hold absolute paths, which is why every reader
  81. guards on ``is_absolute``). Resolving a relative one against the process
  82. working directory finds nothing, and a source that cannot be found is
  83. treated as "nothing to verify" — so this check silently passed every
  84. library-backed item, which is every Slicer Pipeline job and everything
  85. queued from the Library page (#2779).
  86. """
  87. if item.archive is not None and item.archive.file_path:
  88. return app_settings.base_dir / item.archive.file_path
  89. if item.library_file is not None and item.library_file.file_path:
  90. library_path = Path(item.library_file.file_path)
  91. if library_path.is_absolute():
  92. return library_path
  93. # SEC-PATH-OK: file_path is DB-stored and generated by the Library
  94. # ingest (archive/library/files/<uuid>.<ext>), never request input. The
  95. # same value already resolves the file for upload in print_queue.py and
  96. # print_scheduler.py — this check reads what the printer is about to be
  97. # sent, so it must resolve it identically.
  98. return app_settings.base_dir / item.library_file.file_path
  99. return None
  100. async def _spoolman_remaining_grams(spoolman_spool_id: int) -> float | None:
  101. """Live remaining grams for a Spoolman spool, or None if unavailable."""
  102. try:
  103. from backend.app.services.spoolman import (
  104. SpoolmanClientError,
  105. SpoolmanNotFoundError,
  106. get_spoolman_client,
  107. )
  108. except ImportError:
  109. return None
  110. try:
  111. client = await get_spoolman_client()
  112. if client is None:
  113. return None
  114. spool = await client.get_spool(spoolman_spool_id)
  115. except (SpoolmanNotFoundError, SpoolmanClientError):
  116. return None
  117. except Exception as e:
  118. logger.debug("Spoolman fetch failed for spool %s: %s", spoolman_spool_id, e)
  119. return None
  120. if not spool:
  121. return None
  122. # Spoolman exposes either an absolute remaining_weight, or used_weight +
  123. # the spool's net weight (initial_weight, else filament.weight; #3194).
  124. # Either is sufficient — prefer remaining_weight when present (the user
  125. # may have overridden it).
  126. remaining = spool.get("remaining_weight")
  127. if isinstance(remaining, (int, float)) and remaining >= 0:
  128. return float(remaining)
  129. used = spool.get("used_weight")
  130. total = spoolman_net_weight(spool)
  131. if isinstance(used, (int, float)) and isinstance(total, (int, float)) and total > 0:
  132. return max(0.0, float(total) - float(used))
  133. return None
  134. async def _is_spoolman_mode(db: AsyncSession) -> bool:
  135. """Check whether the user has opted in to Spoolman inventory mode."""
  136. try:
  137. from backend.app.api.routes.settings import get_setting
  138. spoolman_enabled = await get_setting(db, "spoolman_enabled")
  139. return bool(spoolman_enabled) and spoolman_enabled.lower() == "true"
  140. except Exception:
  141. return False
  142. async def _warnings_disabled(db: AsyncSession) -> bool:
  143. """Honour the ``disable_filament_warnings`` setting (#720)."""
  144. try:
  145. from backend.app.api.routes.settings import get_setting
  146. disabled = await get_setting(db, "disable_filament_warnings")
  147. return bool(disabled) and disabled.lower() == "true"
  148. except Exception:
  149. return False
  150. def _normalize_color_for_id(raw: str | None) -> str:
  151. """Canonicalise a hex colour for identity comparison.
  152. Strips the leading ``#``, uppercases, and drops the alpha channel when
  153. the hex is 8 chars long (``RRGGBBAA``) so a fully-opaque 8-char hex
  154. matches a 6-char hex of the same RGB. Empty / None → empty string.
  155. Anything that is not a string reads as "no colour" rather than raising.
  156. In Spoolman mode ``raw`` comes straight off the wire as
  157. ``filament.color_hex``, and this runs on the dispatch path — a record
  158. holding a number there would otherwise fail a queue start rather than
  159. merely fail to pool.
  160. """
  161. if not isinstance(raw, str):
  162. raw = None
  163. s = (raw or "").strip().lstrip("#").upper()
  164. if len(s) == 8: # RRGGBBAA → strip alpha
  165. s = s[:6]
  166. return s
  167. def _material_identity_internal(spool) -> str:
  168. """Strict same-material key for backup-peer matching in internal mode.
  169. Requires a Bambu filament preset ID (``slicer_filament``, e.g. ``GFA00``)
  170. AND a matching colour. The preset identifies the filament profile (PETG
  171. HF, PLA Basic, etc.) — same hot-end behaviour — but the firmware's
  172. switch logic also requires the spool to be the same colour (otherwise
  173. every PETG HF spool would back every other PETG HF spool regardless of
  174. colour, which would dye prints mid-run). Spools without a preset
  175. (user-tagged / non-Bambu) get a per-spool unique key so they NEVER
  176. pair with anything else; without the Bambu preset the firmware can't
  177. trust the backup decision.
  178. """
  179. preset = (spool.slicer_filament or "").strip() if spool else ""
  180. if preset:
  181. color = _normalize_color_for_id(spool.rgba if spool else None)
  182. return f"preset:{preset}|color:{color}"
  183. # Unique-per-spool key prevents grouping. Use the spool's primary key so
  184. # the same spool always resolves to the same key within a request.
  185. spool_id = getattr(spool, "id", None) if spool else None
  186. return f"unmatched:{spool_id}"
  187. def _material_identity_spoolman(spool: dict | None) -> str:
  188. """Strict same-material key for backup-peer matching in Spoolman mode.
  189. Two spools pair only when they reference the same Spoolman ``filament``
  190. catalog entry (same ``filament.id``) AND share the same colour. The
  191. catalog entry pins the profile (PETG HF / PLA Basic / ...); the colour
  192. pins the variant. Spools without a resolvable filament id get a
  193. per-spool unique key so they never pair.
  194. """
  195. if not isinstance(spool, dict) or not spool:
  196. return "unmatched:none"
  197. # Both are free-form JSON off the Spoolman API, and this is the dispatch
  198. # path: a wrongly-typed member must cost the slot its pool, never the
  199. # queue its start.
  200. filament = spool.get("filament") or {}
  201. if not isinstance(filament, dict):
  202. filament = {}
  203. fil_id = filament.get("id")
  204. if isinstance(fil_id, (int, str)) and str(fil_id).strip():
  205. # Prefer the per-spool override colour when set (Spoolman lets the user
  206. # tag a spool with a colour distinct from the filament catalog
  207. # default); fall back to the filament catalog colour.
  208. color = _normalize_color_for_id(
  209. (spool.get("color_hex") if isinstance(spool.get("color_hex"), str) else None) or filament.get("color_hex")
  210. )
  211. return f"filament:{fil_id}|color:{color}"
  212. spool_id = spool.get("id")
  213. return f"unmatched:{spool_id}"
  214. def _extruder_side_for_ams(
  215. ams_id: int,
  216. ams_extruder_map: dict[str, int],
  217. is_dual_extruder: bool,
  218. ) -> int:
  219. """Resolve the extruder index (0=right, 1=left) for a given AMS unit.
  220. Single-extruder printers collapse everything to 0. On dual-extruder
  221. printers (H2D / H2C / X2D), the firmware can't cross extruders even with
  222. AMS Filament Backup ON, so the pool must be scoped per-side.
  223. """
  224. if not is_dual_extruder:
  225. return 0
  226. return int(ams_extruder_map.get(str(ams_id), 0))
  227. def _parse_ams_mapping(raw: str | None) -> list[int] | None:
  228. if not raw:
  229. return None
  230. try:
  231. parsed = json.loads(raw)
  232. except (json.JSONDecodeError, TypeError):
  233. return None
  234. if not isinstance(parsed, list):
  235. return None
  236. return [v for v in parsed if isinstance(v, int)]
  237. async def _get_printer_backup_context(
  238. printer_id: int,
  239. ) -> tuple[bool, dict[str, int], bool]:
  240. """Return ``(backup_on, ams_extruder_map, is_dual_extruder)`` for the printer.
  241. Read from the live MQTT state via ``printer_manager`` (no DB round-trip).
  242. Defaults conservatively to ``backup_on=False`` when the state is missing
  243. or the printer is offline — same fallback as today (per-slot deficit
  244. accounting), so an offline printer is never treated as backup-capable.
  245. """
  246. try:
  247. from backend.app.services.printer_manager import printer_manager
  248. from backend.app.utils.printer_models import is_dual_nozzle_model
  249. except ImportError:
  250. return False, {}, False
  251. state = printer_manager.get_status(printer_id)
  252. if state is None:
  253. return False, {}, False
  254. backup_on = state.ams_filament_backup is True
  255. ams_extruder_map = dict(state.ams_extruder_map or {})
  256. model = printer_manager.get_model(printer_id)
  257. is_dual = bool(model and is_dual_nozzle_model(model))
  258. return backup_on, ams_extruder_map, is_dual
  259. @dataclass(frozen=True)
  260. class SlotSpoolIdentity:
  261. """How the spool bound to a slot should be *named*, as opposed to matched.
  262. The printer cannot supply this and never will. A tray record carries no
  263. brand field at all, and ``tray_sub_brands`` stays empty for anything that
  264. isn't a Bambu spool, so a client naming a slot from telemetry alone has
  265. only the type and the colour hex to work with — and turns that hex into
  266. whichever catalogue colour happens to share it. A Devil Design PLA Basic
  267. Orange the operator assigned in Bambuddy reads back as "PLA (Sunflower
  268. Yellow)", because Bambu sell a Sunflower Yellow at the same ``FEC600``.
  269. Only the assignment knows the answer, which is why it is served alongside
  270. the pooling key rather than left to the client to resolve: the identity
  271. rule differs per inventory mode, and the printer card and the print dialog
  272. disagreeing about what is in a slot is the bug this exists to close.
  273. Purely descriptive — nothing here takes part in matching, which stays on
  274. the printer's own telemetry so the dialog and the dispatcher cannot draw
  275. different conclusions from the same slot.
  276. """
  277. brand: str | None
  278. material: str | None
  279. subtype: str | None
  280. color_name: str | None
  281. rgba: str | None
  282. # The rest of the spool's colour, for drawing it rather than naming it
  283. # (#3159). A tray record carries one hex and nothing else, so a two-tone
  284. # or glittery spool renders as its base colour unless the binding says
  285. # otherwise -- and on a farm of third-party spools that is exactly the
  286. # swatch somebody is comparing against the slicer's colour. Same shape the
  287. # inventory row stores: ``extra_colors`` a comma-separated hex list,
  288. # ``effect_type`` one of the client's FilamentEffect names.
  289. extra_colors: str | None = None
  290. effect_type: str | None = None
  291. def to_dict(self) -> dict:
  292. return {
  293. "brand": self.brand,
  294. "material": self.material,
  295. "subtype": self.subtype,
  296. "color_name": self.color_name,
  297. "rgba": self.rgba,
  298. "extra_colors": self.extra_colors,
  299. "effect_type": self.effect_type,
  300. }
  301. def _clean(value) -> str | None:
  302. """Trim a display field, collapsing blanks to None so the client can skip it."""
  303. text = str(value).strip() if value is not None else ""
  304. return text or None
  305. def _identity_from_internal(spool) -> SlotSpoolIdentity:
  306. """Display identity from an internal-inventory ``Spool`` row."""
  307. return SlotSpoolIdentity(
  308. brand=_clean(spool.brand),
  309. material=_clean(spool.material),
  310. subtype=_clean(spool.subtype),
  311. color_name=_clean(spool.color_name),
  312. rgba=_clean(spool.rgba),
  313. extra_colors=_clean(spool.extra_colors),
  314. effect_type=_clean(spool.effect_type),
  315. )
  316. def _identity_from_spoolman(spool_dict: dict) -> SlotSpoolIdentity | None:
  317. """Display identity from a raw Spoolman spool dict, or None if unreadable.
  318. Goes through ``_map_spoolman_spool`` rather than reading the dict directly:
  319. brand lives on the nested vendor, subtype is the filament name with its
  320. material prefix stripped, and ``color_name`` has a three-step read order
  321. Spoolman itself has no field for. Re-deriving any of that here is how the
  322. two modes would drift apart.
  323. """
  324. # Broad on purpose. This is a name for a dropdown, and the caller is on the
  325. # dispatch path -- ``compute_deficit_for_queue_item`` runs it before every
  326. # queue start. ``_map_spoolman_spool`` walks a dozen nested fields off the
  327. # wire (``filament.vendor.name``, ``extra.tag``, ``filament.color_hex``) and
  328. # any of them arriving as the wrong type raises AttributeError rather than
  329. # ValueError, so a narrow catch here would turn one malformed Spoolman
  330. # record into a failed dispatch. Losing the name costs a fallback to
  331. # telemetry, which is what every slot did before this existed.
  332. try:
  333. mapped = _map_spoolman_spool(spool_dict)
  334. except Exception as exc: # noqa: BLE001 - display-only, must never block a dispatch
  335. logger.debug(
  336. "Spoolman spool %r has no usable display identity: %s",
  337. spool_dict.get("id") if isinstance(spool_dict, dict) else spool_dict,
  338. exc,
  339. )
  340. return None
  341. # Spoolman has no colour-name field, so `_map_spoolman_spool` synthesises
  342. # one from the subtype when nothing is stored -- which reads fine in an
  343. # inventory list ("PLA Basic") and badly as a colour ("Devil Design PLA
  344. # Basic (Basic)"). Drop it and let the client's catalogue lookup name the
  345. # hex, which is what an unnamed slot got before this existed.
  346. color_name = None if mapped.get("color_name_is_synthesized") else _clean(mapped.get("color_name"))
  347. return SlotSpoolIdentity(
  348. brand=_clean(mapped.get("brand")),
  349. material=_clean(mapped.get("material")),
  350. subtype=_clean(mapped.get("subtype")),
  351. color_name=color_name,
  352. rgba=_clean(mapped.get("rgba")),
  353. # `_map_spoolman_spool` builds `extra_colors` from Spoolman's own
  354. # multi-colour hexes and pins `effect_type` to None, because Spoolman
  355. # has no field for it -- see the note there.
  356. extra_colors=_clean(mapped.get("extra_colors")),
  357. effect_type=_clean(mapped.get("effect_type")),
  358. )
  359. @dataclass(frozen=True)
  360. class SlotMaterial:
  361. """One inventory-bound AMS slot: what's in it, how much is left, which side."""
  362. ams_id: int
  363. tray_id: int
  364. global_tray_id: int
  365. # Opaque grouping key. Two slots pool for AMS Filament Backup only when
  366. # their keys AND extruder sides match. Never parse it — the format is an
  367. # implementation detail of ``_material_identity_*``.
  368. material_key: str
  369. remaining_grams: float
  370. extruder: int
  371. # Display-only; see SlotSpoolIdentity. None when the binding resolves to a
  372. # spool we cannot describe, which callers render from telemetry as before.
  373. spool: SlotSpoolIdentity | None = None
  374. def to_dict(self) -> dict:
  375. return {
  376. "ams_id": self.ams_id,
  377. "tray_id": self.tray_id,
  378. "global_tray_id": self.global_tray_id,
  379. "material_key": self.material_key,
  380. "remaining_g": self.remaining_grams,
  381. "extruder": self.extruder,
  382. "spool": self.spool.to_dict() if self.spool else None,
  383. }
  384. async def build_slot_materials(db: AsyncSession, printer_id: int) -> list[SlotMaterial]:
  385. """Every inventory-bound slot on ``printer_id``, with identity and remaining grams.
  386. Mode-agnostic: internal inventory resolves via ``SpoolAssignment`` joined to
  387. ``Spool`` (``label_weight`` minus ``weight_used``), Spoolman mode via
  388. ``SpoolmanSlotAssignment`` plus a live ``get_spool`` fetch. Slots whose
  389. remaining weight can't be determined — no spool row, zero label weight,
  390. Spoolman unreachable — are omitted entirely rather than reported as zero, so
  391. a missing binding never manufactures a shortfall.
  392. This is the pool the AMS-Filament-Backup accounting draws on (#1762), and
  393. the same data the PrintModal pre-flight check consumes through
  394. ``GET /printers/{id}/inventory-remain``. Both sides share it so the modal's
  395. warning and the dispatcher's 409 can't disagree about what backs what up.
  396. """
  397. _, ams_extruder_map, is_dual = await _get_printer_backup_context(printer_id)
  398. materials: list[SlotMaterial] = []
  399. def _append(
  400. ams_id: int,
  401. tray_id: int,
  402. material_key: str,
  403. remaining: float,
  404. spool: SlotSpoolIdentity | None = None,
  405. ) -> None:
  406. materials.append(
  407. SlotMaterial(
  408. ams_id=ams_id,
  409. tray_id=tray_id,
  410. global_tray_id=_ams_key_to_global(ams_id, tray_id),
  411. material_key=material_key,
  412. remaining_grams=remaining,
  413. extruder=_extruder_side_for_ams(ams_id, ams_extruder_map, is_dual),
  414. spool=spool,
  415. )
  416. )
  417. if await _is_spoolman_mode(db):
  418. sm_all = await db.execute(select(SpoolmanSlotAssignment).where(SpoolmanSlotAssignment.printer_id == printer_id))
  419. from backend.app.services.spoolman import (
  420. SpoolmanClientError,
  421. SpoolmanNotFoundError,
  422. get_spoolman_client,
  423. )
  424. try:
  425. client = await get_spoolman_client()
  426. except Exception:
  427. client = None
  428. if client is None:
  429. return []
  430. for sa in sm_all.scalars().all():
  431. try:
  432. spool_dict = await client.get_spool(sa.spoolman_spool_id)
  433. except (SpoolmanNotFoundError, SpoolmanClientError):
  434. continue
  435. except Exception as e:
  436. logger.debug("Spoolman pool fetch failed for spool %s: %s", sa.spoolman_spool_id, e)
  437. continue
  438. if not spool_dict:
  439. continue
  440. remaining: float | None = None
  441. rw = spool_dict.get("remaining_weight")
  442. if isinstance(rw, (int, float)) and rw >= 0:
  443. remaining = float(rw)
  444. else:
  445. used = spool_dict.get("used_weight")
  446. total = spoolman_net_weight(spool_dict)
  447. if isinstance(used, (int, float)) and isinstance(total, (int, float)) and total > 0:
  448. remaining = max(0.0, float(total) - float(used))
  449. if remaining is None:
  450. continue
  451. _append(
  452. sa.ams_id,
  453. sa.tray_id,
  454. _material_identity_spoolman(spool_dict),
  455. remaining,
  456. _identity_from_spoolman(spool_dict),
  457. )
  458. return materials
  459. internal_all = await db.execute(
  460. select(SpoolAssignment)
  461. .options(selectinload(SpoolAssignment.spool))
  462. .where(SpoolAssignment.printer_id == printer_id)
  463. )
  464. for assignment in internal_all.scalars().all():
  465. spool = assignment.spool
  466. if spool is None:
  467. continue
  468. label_weight = float(spool.label_weight or 0)
  469. weight_used = float(spool.weight_used or 0)
  470. if label_weight <= 0:
  471. continue
  472. _append(
  473. assignment.ams_id,
  474. assignment.tray_id,
  475. _material_identity_internal(spool),
  476. max(0.0, label_weight - weight_used),
  477. _identity_from_internal(spool),
  478. )
  479. return materials
  480. async def compute_deficit_for_queue_item(
  481. db: AsyncSession,
  482. item: PrintQueueItem,
  483. *,
  484. printer_id: int | None = None,
  485. ams_mapping: list[int] | None = None,
  486. require_known: bool = False,
  487. ) -> list[FilamentDeficit]:
  488. """Return per-slot filament shortfalls for ``item``, or [] when it's safe to dispatch.
  489. ``printer_id`` and ``ams_mapping`` ask about a printer the item is not
  490. assigned to yet, with the mapping it would get there: the model-based
  491. matcher uses them to pass over a printer that would be short (#3137)
  492. before it commits the item to one. Without them, the item's own columns
  493. are read.
  494. ``require_known`` also reports every printed slot whose remaining amount
  495. cannot be determined (no tray mapped, no spool assigned, no label weight,
  496. Spoolman not answering), with ``remaining_grams=None``. Everywhere else an
  497. unknown amount lets the print through; a caller about to *start* a held
  498. job on its own uses this so it only does so on a positive finding.
  499. Returns an empty list whenever any of the following hold:
  500. * The ``disable_filament_warnings`` setting is on.
  501. * The item has no resolved ``printer_id`` (model-based assignment not
  502. yet picked a printer — the scheduler re-runs the check after it does).
  503. * No source 3MF is available, or the 3MF carries no per-slot
  504. requirements (treated as "nothing to verify" rather than an error,
  505. matching the PrintModal behaviour).
  506. * No AMS mapping is set yet — the scheduler computes the mapping just
  507. before dispatch; until it does we cannot map slot → tray.
  508. * Spoolman mode is on but the Spoolman server is unreachable. We do not
  509. wedge the queue on a network blip.
  510. #1762: when the printer reports ``ams_filament_backup=True`` in MQTT
  511. status, available material is pooled across ALL same-material spools on
  512. the printer (within the same extruder side for dual-nozzle models, since
  513. firmware can't cross extruders even with the backup bit set). Per-slot
  514. shortfalls are then only emitted if the POOL is too small for the
  515. print's total required of that material — matching how the printer
  516. actually behaves with Filament Backup ON.
  517. """
  518. if await _warnings_disabled(db):
  519. return []
  520. if printer_id is None:
  521. printer_id = item.printer_id
  522. if printer_id is None:
  523. return []
  524. # Refresh the relationships we need without assuming the caller eagerly
  525. # loaded them — both the route and the scheduler call this from contexts
  526. # with different loading strategies.
  527. refreshed = await db.execute(
  528. select(PrintQueueItem)
  529. .options(
  530. selectinload(PrintQueueItem.archive),
  531. selectinload(PrintQueueItem.library_file),
  532. )
  533. .where(PrintQueueItem.id == item.id)
  534. )
  535. item = refreshed.scalar_one_or_none() or item
  536. source_path = _resolve_source_3mf(item)
  537. if source_path is None:
  538. # No archive and no library file — nothing was ever attached to check.
  539. return []
  540. if not source_path.exists():
  541. # Dispatch is not blocked: the upload that follows needs the same file
  542. # and fails within seconds, where wedging the queue here would strand
  543. # it. But skipping a safety check must leave a trace — a silent skip is
  544. # what hid #2779 for every library-backed item.
  545. logger.warning(
  546. "Filament check skipped for queue item %s: source 3MF not found at %s",
  547. item.id,
  548. source_path,
  549. )
  550. return []
  551. requirements = extract_filament_requirements(source_path, item.plate_id)
  552. if not requirements:
  553. return []
  554. mapping = ams_mapping if ams_mapping is not None else _parse_ams_mapping(item.ams_mapping)
  555. if not mapping:
  556. return []
  557. spoolman_mode = await _is_spoolman_mode(db)
  558. backup_on, ams_extruder_map, is_dual = await _get_printer_backup_context(printer_id)
  559. # ------------------------------------------------------------------ phase 1
  560. # Resolve each requirement to (ams_id, tray_id, identity, remaining_grams).
  561. # Slot identity is the identity of the spool *assigned to that slot*. A
  562. # ``None`` remaining means "couldn't determine" — treated as "no deficit"
  563. # below (preserved from pre-#1762 behaviour for non-backup paths too).
  564. @dataclass
  565. class _ReqRow:
  566. slot_id: int
  567. ams_id: int
  568. tray_id: int
  569. global_tray_id: int
  570. required: float
  571. identity: str
  572. remaining: float | None
  573. filament_type: str
  574. extruder: int
  575. resolved: list[_ReqRow] = []
  576. # Printed slots whose remaining amount is unknown; only returned with
  577. # ``require_known``.
  578. undetermined: list[FilamentDeficit] = []
  579. def _undetermined(req: dict, ams_id: int | None = None, tray_id: int | None = None) -> None:
  580. undetermined.append(
  581. FilamentDeficit(
  582. slot_id=req["slot_id"],
  583. ams_id=ams_id,
  584. tray_id=tray_id,
  585. filament_type=str(req.get("type", "")),
  586. required_grams=float(req["used_grams"]),
  587. remaining_grams=None,
  588. )
  589. )
  590. for req in requirements:
  591. slot_id = req.get("slot_id")
  592. used_grams = req.get("used_grams")
  593. if not isinstance(slot_id, int) or slot_id <= 0:
  594. continue
  595. if not isinstance(used_grams, (int, float)) or used_grams <= 0:
  596. continue
  597. idx = slot_id - 1
  598. if idx >= len(mapping):
  599. _undetermined(req)
  600. continue
  601. global_tray_id = mapping[idx]
  602. if global_tray_id is None or global_tray_id < 0:
  603. _undetermined(req)
  604. continue
  605. ams_id, tray_id = _global_to_ams_key(global_tray_id)
  606. identity = "attrs:|||"
  607. remaining: float | None = None
  608. if spoolman_mode:
  609. sm_result = await db.execute(
  610. select(SpoolmanSlotAssignment).where(
  611. SpoolmanSlotAssignment.printer_id == printer_id,
  612. SpoolmanSlotAssignment.ams_id == ams_id,
  613. SpoolmanSlotAssignment.tray_id == tray_id,
  614. )
  615. )
  616. sm_assignment = sm_result.scalar_one_or_none()
  617. if sm_assignment is None:
  618. _undetermined(req, ams_id, tray_id)
  619. continue
  620. # Live remaining_weight from Spoolman. The fetch also resolves the
  621. # filament identity for pooling (material + colour + name).
  622. from backend.app.services.spoolman import (
  623. SpoolmanClientError,
  624. SpoolmanNotFoundError,
  625. get_spoolman_client,
  626. )
  627. try:
  628. client = await get_spoolman_client()
  629. spool_dict = await client.get_spool(sm_assignment.spoolman_spool_id) if client else None
  630. except (SpoolmanNotFoundError, SpoolmanClientError):
  631. spool_dict = None
  632. except Exception as e:
  633. logger.debug("Spoolman fetch failed for spool %s: %s", sm_assignment.spoolman_spool_id, e)
  634. spool_dict = None
  635. if spool_dict:
  636. identity = _material_identity_spoolman(spool_dict)
  637. rw = spool_dict.get("remaining_weight")
  638. if isinstance(rw, (int, float)) and rw >= 0:
  639. remaining = float(rw)
  640. else:
  641. used = spool_dict.get("used_weight")
  642. total = spoolman_net_weight(spool_dict)
  643. if isinstance(used, (int, float)) and isinstance(total, (int, float)) and total > 0:
  644. remaining = max(0.0, float(total) - float(used))
  645. else:
  646. internal_result = await db.execute(
  647. select(SpoolAssignment)
  648. .options(selectinload(SpoolAssignment.spool))
  649. .where(
  650. SpoolAssignment.printer_id == printer_id,
  651. SpoolAssignment.ams_id == ams_id,
  652. SpoolAssignment.tray_id == tray_id,
  653. )
  654. )
  655. assignment = internal_result.scalar_one_or_none()
  656. if assignment is None or assignment.spool is None:
  657. _undetermined(req, ams_id, tray_id)
  658. continue
  659. spool = assignment.spool
  660. identity = _material_identity_internal(spool)
  661. label_weight = float(spool.label_weight or 0)
  662. weight_used = float(spool.weight_used or 0)
  663. if label_weight <= 0:
  664. _undetermined(req, ams_id, tray_id)
  665. continue
  666. remaining = max(0.0, label_weight - weight_used)
  667. if remaining is None:
  668. # Unable to determine remaining grams — preserve pre-#1762 behaviour
  669. # (don't block on undetermined data).
  670. _undetermined(req, ams_id, tray_id)
  671. continue
  672. resolved.append(
  673. _ReqRow(
  674. slot_id=slot_id,
  675. ams_id=ams_id,
  676. tray_id=tray_id,
  677. global_tray_id=global_tray_id,
  678. required=float(used_grams),
  679. identity=identity,
  680. remaining=remaining,
  681. filament_type=str(req.get("type", "")),
  682. extruder=_extruder_side_for_ams(ams_id, ams_extruder_map, is_dual),
  683. )
  684. )
  685. # ------------------------------------------------------------------ phase 2
  686. # When backup is OFF, fall back to today's per-slot accounting (one-line
  687. # equivalence of the original loop), so this path is a strict no-op
  688. # behaviour-wise vs. the pre-#1762 code.
  689. unknown = undetermined if require_known else []
  690. if not backup_on:
  691. return unknown + [
  692. FilamentDeficit(
  693. slot_id=row.slot_id,
  694. ams_id=row.ams_id,
  695. tray_id=row.tray_id,
  696. filament_type=row.filament_type,
  697. required_grams=row.required,
  698. remaining_grams=row.remaining,
  699. )
  700. for row in resolved
  701. if row.remaining is not None and row.remaining < row.required
  702. ]
  703. # ------------------------------------------------------------------ phase 3
  704. # Backup ON: build (identity, extruder)-keyed pool and required-sum maps
  705. # from EVERY assigned spool on the printer (not just the slots in the
  706. # print's mapping). Then emit deficits only when the pool for a slot's
  707. # material is too small for the print's total required of that material.
  708. pool_by_key: dict[tuple[str, int], float] = defaultdict(float)
  709. required_by_key: dict[tuple[str, int], float] = defaultdict(float)
  710. for slot in await build_slot_materials(db, printer_id):
  711. pool_by_key[(slot.material_key, slot.extruder)] += slot.remaining_grams
  712. for row in resolved:
  713. required_by_key[(row.identity, row.extruder)] += row.required
  714. deficits: list[FilamentDeficit] = []
  715. for row in resolved:
  716. key = (row.identity, row.extruder)
  717. # Pool insufficient for the print's TOTAL required of this material on
  718. # this extruder side → real deficit. The per-slot remaining still gets
  719. # surfaced so the UI can point at the slot the user assigned.
  720. if pool_by_key[key] < required_by_key[key]:
  721. deficits.append(
  722. FilamentDeficit(
  723. slot_id=row.slot_id,
  724. ams_id=row.ams_id,
  725. tray_id=row.tray_id,
  726. filament_type=row.filament_type,
  727. required_grams=row.required,
  728. remaining_grams=row.remaining,
  729. )
  730. )
  731. return unknown + deficits
  732. # Re-export the most useful pieces for callers that just want the data.
  733. __all__ = [
  734. "FilamentDeficit",
  735. "compute_deficit_for_queue_item",
  736. ]