_spoolman_helpers.py 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  1. """Pure helper functions for Spoolman spool mapping.
  2. No heavy dependencies — importable in unit tests without the full backend stack.
  3. """
  4. from __future__ import annotations
  5. import json
  6. import logging
  7. import math
  8. import re
  9. from typing import Any
  10. from typing_extensions import TypedDict
  11. from backend.app.api.routes._url_safety import assert_safe_lan_service_url
  12. logger = logging.getLogger(__name__)
  13. class MappedSpoolFields(TypedDict):
  14. """Full shape of the dict returned by _map_spoolman_spool (InventorySpool-compatible)."""
  15. id: int
  16. material: str | None
  17. subtype: str | None
  18. brand: str | None
  19. color_name: str | None
  20. color_name_is_synthesized: bool
  21. rgba: str | None
  22. extra_colors: str | None
  23. effect_type: None
  24. label_weight: int | None
  25. core_weight: int | None
  26. core_weight_is_inherited: bool
  27. core_weight_catalog_id: None
  28. weight_used: float | None
  29. weight_used_baseline: float | None
  30. weight_locked: bool
  31. last_scale_weight: None
  32. last_weighed_at: None
  33. slicer_filament: None
  34. slicer_filament_name: str | None
  35. nozzle_temp_min: int | None
  36. nozzle_temp_max: None
  37. note: str | None
  38. added_full: None
  39. last_used: str | None
  40. encode_time: str | None
  41. tag_uid: str | None
  42. tray_uuid: str | None
  43. data_origin: str | None
  44. tag_type: str | None
  45. archived_at: str | None
  46. created_at: str | None # None when Spoolman spool has no registered timestamp
  47. updated_at: str | None
  48. cost_per_kg: float | None
  49. # Spoolman's native filament.article_number, surfaced as the internal
  50. # material number (#2870). Read-only in Spoolman mode — the number is
  51. # filament-level there and maintained in Spoolman itself.
  52. material_number: str | None
  53. storage_location: str | None
  54. location_id: int | None
  55. k_profiles: list[Any]
  56. class NormalizedVendorRef(TypedDict):
  57. """Vendor reference embedded in a NormalizedFilament."""
  58. id: int
  59. name: str
  60. class NormalizedFilament(TypedDict):
  61. """Normalised Spoolman filament dict returned by the /filaments catalog endpoint."""
  62. id: int
  63. name: str
  64. material: str | None
  65. color_hex: str | None
  66. color_name: str | None
  67. weight: int | None
  68. spool_weight: float | None
  69. vendor: NormalizedVendorRef | None
  70. def assert_safe_spoolman_url(url: str) -> None:
  71. """Raise ValueError if the Spoolman *url* should be blocked as an SSRF risk.
  72. Thin wrapper over the shared LAN-service policy — see
  73. ``_url_safety.assert_safe_lan_service_url`` for what is and isn't
  74. rejected, and why loopback/RFC-1918 are deliberately permitted (running
  75. Spoolman on the same host or home LAN is THE normal topology).
  76. Kept as a named function because the "Spoolman URL …" wording in its
  77. errors is user-facing and asserted by existing tests.
  78. """
  79. assert_safe_lan_service_url(url, label="Spoolman URL")
  80. # Six characters, or eight when the filament carries an alpha byte. The write
  81. # side stores eight only for genuinely translucent spools (#2912); rejecting
  82. # them here turned every clear spool into neutral grey on read.
  83. _COLOR_HEX_RE = re.compile(r"^[0-9A-Fa-f]{6}(?:[0-9A-Fa-f]{2})?$")
  84. _TAG_HEX_RE = re.compile(r"^[0-9A-F]+$")
  85. def _safe_int(value: object, fallback: int) -> int:
  86. """Convert value to int, returning fallback for None/NaN/Inf/non-numeric."""
  87. try:
  88. f = float(value) # type: ignore[arg-type]
  89. if math.isfinite(f):
  90. return int(f)
  91. except (TypeError, ValueError):
  92. pass
  93. return fallback
  94. def _safe_float(value: object, fallback: float) -> float:
  95. """Convert value to float, returning fallback for None/NaN/Inf/non-numeric."""
  96. try:
  97. f = float(value) # type: ignore[arg-type]
  98. if math.isfinite(f):
  99. return f
  100. except (TypeError, ValueError):
  101. pass
  102. return fallback
  103. def _safe_optional_float(value: object) -> float | None:
  104. """Convert value to finite float, or None if missing/NaN/Infinite/non-numeric.
  105. Used for optional monetary fields (price) to prevent Infinity/NaN from
  106. reaching JSON serialisation, which raises ValueError with allow_nan=False.
  107. """
  108. if value is None:
  109. return None
  110. try:
  111. f = float(value) # type: ignore[arg-type]
  112. if math.isfinite(f):
  113. return f
  114. except (TypeError, ValueError):
  115. pass
  116. return None
  117. # Used only when Spoolman has no empty-spool weight at any level.
  118. SPOOLMAN_FALLBACK_TARE = 250.0
  119. def spoolman_tare(spool: dict) -> tuple[float, str]:
  120. """The empty-spool weight of a Spoolman spool, and where it came from.
  121. Spoolman resolves the tare as the spool's own ``spool_weight``, then the
  122. filament's ``spool_weight``, then the vendor's ``empty_spool_weight``, and
  123. its own ``/measure`` endpoint follows that order. Skipping the vendor
  124. level made a spool whose tare lives only on its vendor weigh against
  125. 250 g instead (#3195). Every Spoolman-mode tare in Bambuddy goes through
  126. here so the weigh endpoints and the displayed core weight cannot drift
  127. apart again.
  128. Returns ``(grams, source)`` with source one of ``"spool"``,
  129. ``"filament"``, ``"vendor"`` or ``"fallback"``. 0 is a real tare, not a
  130. missing one; a value that is not a finite number counts as missing.
  131. """
  132. filament = spool.get("filament") or {}
  133. vendor = filament.get("vendor") or {}
  134. for source, raw in (
  135. ("spool", spool.get("spool_weight")),
  136. ("filament", filament.get("spool_weight")),
  137. ("vendor", vendor.get("empty_spool_weight")),
  138. ):
  139. value = _safe_optional_float(raw)
  140. if value is not None:
  141. return value, source
  142. return SPOOLMAN_FALLBACK_TARE, "fallback"
  143. def spoolman_net_weight(spool: dict) -> float | None:
  144. """The net filament weight of a full Spoolman spool (its label weight).
  145. Spoolman keeps it on the spool as ``initial_weight`` and falls back to the
  146. filament's catalogue ``weight``, so one filament can have spools of
  147. different sizes. Reading only the filament made a 250 g spool of a 1000 g
  148. filament look four times its size (#3194). Like Spoolman, an
  149. ``initial_weight`` of 0 counts as unset.
  150. Returns None when neither level has a finite number (a bool is not
  151. one); the filament's value is otherwise returned as stored, 0 included,
  152. so callers keep their own handling of a filament without a weight.
  153. """
  154. initial = _weight_or_none(spool.get("initial_weight"))
  155. if initial is not None and initial > 0:
  156. return initial
  157. filament = spool.get("filament")
  158. return _weight_or_none(filament.get("weight")) if isinstance(filament, dict) else None
  159. def _weight_or_none(value: object) -> float | None:
  160. # A bool is an int in Python; True must not read as a 1 g spool.
  161. return None if isinstance(value, bool) else _safe_optional_float(value)
  162. def spoolman_price_to_cost_per_kg(price: object, net_weight: float) -> float | None:
  163. """A Spoolman spool price as a price per kilogram.
  164. Spoolman's ``price`` on a spool is what that spool cost, not a rate; the
  165. spool form edits a rate, so the two are converted at the spool's net
  166. weight. They agree only for 1000 g spools (#3194). Rounded to 4 places so
  167. a round trip through the form doesn't drift by float noise.
  168. """
  169. value = _safe_optional_float(price)
  170. if value is None or net_weight <= 0:
  171. return None
  172. return round(value * 1000.0 / net_weight, 4)
  173. def cost_per_kg_to_spoolman_price(cost_per_kg: float, net_weight: float) -> float:
  174. """Inverse of spoolman_price_to_cost_per_kg: the price of a spool of ``net_weight`` grams."""
  175. return round(cost_per_kg * net_weight / 1000.0, 4)
  176. def spoolman_price_weight(spool: dict) -> float:
  177. """The weight a Spoolman spool's price is converted at.
  178. The spool's exact net weight, not the whole-gram label weight, so a
  179. 250.7 g spool is not re-priced by a no-op edit. A spool with no usable
  180. weight keeps the old 1:1 reading of its price as a per-kg rate.
  181. """
  182. net = spoolman_net_weight(spool)
  183. return net if net is not None and net > 0 else 1000.0
  184. def spoolman_cost_per_kg(spool: dict) -> float | None:
  185. """The spool form's cost per kg for a Spoolman spool (#3194)."""
  186. return spoolman_price_to_cost_per_kg(spool.get("price"), spoolman_price_weight(spool))
  187. def _extract_extra_str(extra: dict, key: str) -> str:
  188. """Extract a JSON-encoded string from a Spoolman extra dict.
  189. Spoolman stores extra values as JSON-stringified text — a stored string
  190. "GFL05" appears as `'"GFL05"'` (six chars including the quotes). This
  191. unwraps that, returning the bare string. Returns "" for missing keys,
  192. non-strings, or invalid JSON.
  193. """
  194. raw = extra.get(key)
  195. if not isinstance(raw, str):
  196. return ""
  197. try:
  198. decoded = json.loads(raw)
  199. except (json.JSONDecodeError, ValueError):
  200. # Tolerate bare-string values written without JSON encoding.
  201. return raw
  202. return decoded if isinstance(decoded, str) else ""
  203. # Spool.extra key holding the consumed-counter baseline: the value Spoolman's
  204. # used_weight had when the user last pressed "Reset usage to 0". Displayed
  205. # consumed is used_weight minus this, which is what internal mode has always
  206. # done with its own weight_used_baseline column (#1644). See #2906.
  207. BAMBU_WEIGHT_USED_BASELINE_KEY = "bambu_weight_used_baseline"
  208. def _extract_extra_float(extra: dict, key: str) -> float | None:
  209. """Extract a JSON-encoded number from a Spoolman extra dict.
  210. Same storage convention as :func:`_extract_extra_str` — Spoolman keeps
  211. extra values as JSON text, so 250.0 is stored as ``"250.0"``. Returns
  212. ``None`` for missing keys, non-finite values, or anything that does not
  213. decode to a number, so the caller can tell "no baseline recorded" from
  214. "a baseline of zero".
  215. """
  216. raw = extra.get(key)
  217. if raw is None:
  218. return None
  219. if isinstance(raw, (int, float)) and not isinstance(raw, bool):
  220. value = float(raw)
  221. elif isinstance(raw, str):
  222. try:
  223. decoded = json.loads(raw)
  224. except (json.JSONDecodeError, ValueError):
  225. return None
  226. if isinstance(decoded, str):
  227. # Spoolman's extra fields default to field_type "text", which only
  228. # accepts values that decode to a str, so numbers written through
  229. # it arrive as '"263.0"' rather than '263.0'. Both spellings have
  230. # to read back the same, or a baseline written by the reset would
  231. # be invisible to the code that subtracts it.
  232. try:
  233. value = float(decoded)
  234. except ValueError:
  235. return None
  236. elif isinstance(decoded, (int, float)) and not isinstance(decoded, bool):
  237. value = float(decoded)
  238. else:
  239. return None
  240. else:
  241. return None
  242. return value if math.isfinite(value) else None
  243. def parse_spoolman_multi_colors(filament: dict) -> list[str]:
  244. """Spoolman's ``multi_color_hexes`` as a list of bare 6/8-char hex tokens.
  245. Spoolman stores the extra stops of a gradient / dual / multi-colour
  246. filament here, and writes the field as a comma-separated string in some
  247. releases and a list in others -- both shapes are accepted. Tokens keep the
  248. case they arrived in and lose any leading ``#``, which is the form
  249. ``Spool.extra_colors`` stores and ``parseStops`` on the client expects.
  250. Shared with the label renderer rather than parsed twice: the two read the
  251. same field for the same purpose, and a swatch on a printer card that
  252. disagreed with the swatch on the printed label would be worse than either
  253. being wrong on its own.
  254. """
  255. raw = filament.get("multi_color_hexes")
  256. if isinstance(raw, str):
  257. tokens = raw.split(",")
  258. elif isinstance(raw, list):
  259. tokens = [str(token) for token in raw]
  260. else:
  261. return []
  262. return [cleaned for token in tokens if (cleaned := token.strip().lstrip("#"))]
  263. def _map_spoolman_spool(spool: dict) -> MappedSpoolFields:
  264. """Convert a raw Spoolman spool dict to the InventorySpool-compatible format.
  265. Fields not supported by Spoolman (k_profiles, slicer_filament, …) are
  266. returned as None / empty so the frontend can still render them without
  267. errors. The ``data_origin`` field is set to ``"spoolman"`` so UI code can
  268. distinguish these spools from local ones.
  269. """
  270. raw_id = spool.get("id")
  271. if raw_id is None:
  272. raise ValueError("Spoolman spool is missing required 'id' field")
  273. try:
  274. spool_id: int = int(raw_id)
  275. except (TypeError, ValueError):
  276. raise ValueError(f"Spoolman spool 'id' is not a valid integer: {raw_id!r}")
  277. if spool_id <= 0:
  278. raise ValueError(f"Spoolman spool 'id' must be a positive integer, got {spool_id}")
  279. filament: dict = spool.get("filament") or {}
  280. if not filament:
  281. logger.warning(
  282. "Spoolman spool %s has no filament data — all filament fields will use defaults",
  283. spool_id,
  284. )
  285. vendor: dict = filament.get("vendor") or {}
  286. extra: dict = spool.get("extra") or {}
  287. # RFID tag stored as JSON-encoded string in Spoolman extra.tag.
  288. # 32-char hex → Bambu Lab tray UUID; 8–30-char hex → NFC tag UID.
  289. # Accepting the full realistic UID range (4-byte = 8 chars, 7-byte = 14 chars,
  290. # 10-byte = 20 chars) avoids silently dropping valid SpoolBuddy-written tags.
  291. raw_tag: str = (extra.get("tag") or "").strip('"').upper()
  292. _raw_is_hex = bool(_TAG_HEX_RE.match(raw_tag))
  293. tag_uid = raw_tag if _raw_is_hex and 8 <= len(raw_tag) <= 30 else None
  294. tray_uuid = raw_tag if _raw_is_hex and len(raw_tag) == 32 else None
  295. # Subtype = filament name with material prefix stripped
  296. material: str = (filament.get("material") or "").strip()
  297. filament_name: str = (filament.get("name") or "").strip()
  298. if material and filament_name.upper().startswith(material.upper()):
  299. subtype: str | None = filament_name[len(material) :].strip() or None
  300. else:
  301. subtype = filament_name or None
  302. # Colour: validate as 6- or 8-char hex; fall back to neutral grey for invalid
  303. # values. An 8-char value already carries its alpha, so appending the opaque
  304. # byte would push it to ten and lose the translucency it was stored to keep.
  305. raw_color = (filament.get("color_hex") or "").upper().removeprefix("#")
  306. color_hex: str = raw_color if _COLOR_HEX_RE.match(raw_color) else "808080"
  307. rgba: str = color_hex if len(color_hex) == 8 else color_hex + "FF"
  308. # Spoolman carries the extra stops but has no concept of a surface effect
  309. # -- its only neighbouring field is `multi_color_direction`, which says how
  310. # the stops are laid out, not that the filament is silk or glitter. So a
  311. # Spoolman spool can render its gradient and never an effect overlay, and
  312. # `effect_type` is pinned to None rather than guessed at.
  313. extra_stops = parse_spoolman_multi_colors(filament)
  314. extra_colors: str | None = ",".join(extra_stops) if extra_stops else None
  315. label_weight: int = _safe_int(spoolman_net_weight(spool), 1000)
  316. real_used_weight: float = _safe_float(spool.get("used_weight"), 0.0)
  317. # Parity with internal mode (#1390): the InventorySpool shape lets the
  318. # frontend compute `remaining = label_weight - weight_used` and
  319. # `consumed = weight_used - weight_used_baseline`. Map Spoolman's two
  320. # independent fields (used_weight, remaining_weight) onto that shape:
  321. # weight_used = label_weight - remaining_weight (so remaining matches)
  322. # baseline = weight_used - used_weight (so consumed matches)
  323. # When remaining_weight is unset (legacy spools, or filament linked but
  324. # never primed), fall back to the old behaviour: weight_used =
  325. # used_weight, baseline = 0.
  326. #
  327. # A "Reset usage to 0" records the then-current used_weight under
  328. # BAMBU_WEIGHT_USED_BASELINE_KEY in spool.extra and touches nothing else,
  329. # so displayed consumed is used_weight minus that. It is folded into the
  330. # baseline here because the frontend's arithmetic is fixed: it always shows
  331. # `weight_used - weight_used_baseline`. The reset used to PATCH Spoolman's
  332. # used_weight to 0 instead, and Spoolman recomputes remaining from initial
  333. # minus used, so the spool jumped back to full (#2906).
  334. # `or 0.0` would collapse None and 0.0, which is the one distinction
  335. # _extract_extra_float exists to preserve; spell the absent case out.
  336. # Clamp the low end too: the write side already refuses a negative
  337. # baseline, and without the same rule here a negative value stored by hand
  338. # would be subtracted, inflating the displayed counter. min() below only
  339. # bounds the other end.
  340. stored = _extract_extra_float(extra, BAMBU_WEIGHT_USED_BASELINE_KEY)
  341. stored_baseline = 0.0 if stored is None else max(0.0, stored)
  342. remaining_raw = spool.get("remaining_weight")
  343. if remaining_raw is not None:
  344. remaining_weight: float = _safe_float(remaining_raw, 0.0)
  345. used_weight: float = max(0.0, float(label_weight) - remaining_weight)
  346. weight_used_baseline: float = max(0.0, used_weight - real_used_weight + stored_baseline)
  347. else:
  348. used_weight = real_used_weight
  349. weight_used_baseline = stored_baseline # already clamped to >= 0 above
  350. # Never let the displayed counter read negative. used_weight can fall below
  351. # the baseline after a reset without anyone touching it: the AMS sync
  352. # writes remaining_weight from the tray percentage, and Spoolman derives
  353. # used_weight from that. The counter then reads 0 until consumption climbs
  354. # past the baseline again. Internal mode behaves the same way, and it is
  355. # far better than before #2906, when the next sync undid the reset outright.
  356. weight_used_baseline = min(weight_used_baseline, used_weight)
  357. # Archived state – Spoolman uses a boolean ``archived`` field
  358. archived: bool = spool.get("archived", False)
  359. archived_at: str | None = None
  360. if archived:
  361. archived_at = spool.get("last_used") or spool.get("registered") or None
  362. created_at: str | None = spool.get("registered") or None
  363. # Spoolman has no `color_name` field on Filament — confirmed against the
  364. # FilamentUpdateParameters schema in 0.23.1: name/vendor_id/material/price/
  365. # density/diameter/weight/spool_weight/article_number/comment/extruder_temp/
  366. # bed_temp/color_hex/multi_color_hexes/multi_color_direction/external_id/
  367. # extra, no color_name (#1357). The previous attempt (b8e350c3) was
  368. # PATCHing a key Spoolman silently discards, which is why color_name
  369. # never actually persisted from the user's edits.
  370. #
  371. # We persist it ourselves under spool.extra.bambu_color_name (JSON-encoded
  372. # string, same pattern as bambu_slicer_filament). Read order:
  373. # 1. spool.extra.bambu_color_name (the canonical store)
  374. # 2. filament.color_name (forward-compat — picks up the value if a
  375. # future Spoolman release adds the field, or if an admin populated
  376. # it via a custom extra-field they registered themselves)
  377. # 3. subtype (synth fallback so the inventory list isn't a sea of
  378. # "Unknown color" entries on installs with neither field set)
  379. #
  380. # color_name_is_synthesized = True only when we fell back to subtype.
  381. # The edit form uses it to leave the input blank, so the user doesn't
  382. # round-trip the synth value back as if they had set it.
  383. extra_color_name = _extract_extra_str(extra, "bambu_color_name") or None
  384. stored_color_name = extra_color_name or (filament.get("color_name") or None)
  385. color_name: str | None = stored_color_name or subtype or None
  386. color_name_is_synthesized: bool = stored_color_name is None and color_name is not None
  387. nozzle_temp_raw = filament.get("settings_extruder_temp")
  388. nozzle_temp_min: int | None = _safe_int(nozzle_temp_raw, 0) or None
  389. return {
  390. "id": spool_id,
  391. "material": material,
  392. "subtype": subtype,
  393. "color_name": color_name,
  394. "color_name_is_synthesized": color_name_is_synthesized,
  395. "rgba": rgba,
  396. "extra_colors": extra_colors,
  397. "effect_type": None,
  398. "brand": vendor.get("name") or None,
  399. "label_weight": label_weight,
  400. "core_weight": _safe_int(spoolman_tare(spool)[0], 250),
  401. # True when the spool has no spool_weight of its own and core_weight is
  402. # the filament type's, the vendor's or the 250 g fallback. The spool form needs it
  403. # to copy a spool without dropping an own tare or stamping an
  404. # inherited one (#2908).
  405. "core_weight_is_inherited": spool.get("spool_weight") is None,
  406. "core_weight_catalog_id": None,
  407. "weight_used": used_weight,
  408. "weight_used_baseline": weight_used_baseline,
  409. "weight_locked": False,
  410. "last_scale_weight": None,
  411. "last_weighed_at": None,
  412. # BambuStudio slicer preset — Spoolman has no native field, so the
  413. # update endpoint persists these under bambu_slicer_filament[_name]
  414. # in the spool's extra dict. Values are JSON-encoded strings; an
  415. # empty string ("") means cleared. Falls back to Spoolman's
  416. # filament_name for slicer_filament_name when nothing is stored.
  417. "slicer_filament": (_extract_extra_str(extra, "bambu_slicer_filament") or None),
  418. "slicer_filament_name": (_extract_extra_str(extra, "bambu_slicer_filament_name") or (filament_name or None)),
  419. "nozzle_temp_min": nozzle_temp_min,
  420. "nozzle_temp_max": None,
  421. "note": spool.get("comment") or None,
  422. "added_full": None,
  423. "last_used": spool.get("last_used"),
  424. # encode_time semantics differ: local records NFC write time; Spoolman first_used
  425. # records first print use — different events; using first_used as best available proxy.
  426. "encode_time": spool.get("first_used"),
  427. "tag_uid": tag_uid,
  428. "tray_uuid": tray_uuid,
  429. "data_origin": "spoolman",
  430. "tag_type": "spoolman",
  431. "archived_at": archived_at,
  432. "created_at": created_at,
  433. # Spoolman has no updated_at field; use registered timestamp as best available proxy
  434. "updated_at": created_at,
  435. # Spoolman's spool price is what the whole spool cost (#3194).
  436. "cost_per_kg": spoolman_cost_per_kg(spool),
  437. # Spoolman's filament.article_number maps 1:1 onto the internal
  438. # material number (#2870): both identify the purchasable product.
  439. # Trimmed for the same reason the schema validator trims the internal
  440. # one — the filter chip builds its options from trimmed values and
  441. # matches exactly, so a padded number would list and match nothing.
  442. "material_number": ((filament.get("article_number") or "").strip() or None),
  443. "storage_location": spool.get("location") or None,
  444. "location_id": None,
  445. "k_profiles": [],
  446. }