_spoolman_helpers.py 18 KB

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