spoolman.py 68 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513
  1. """Spoolman integration service for syncing AMS filament data."""
  2. import asyncio
  3. import json
  4. import logging
  5. import math
  6. import weakref
  7. from dataclasses import dataclass
  8. from datetime import datetime, timezone
  9. from typing import Literal
  10. import httpx
  11. from sqlalchemy.ext.asyncio import AsyncSession
  12. from backend.app.services.color_catalog_lookup import CatalogColor, best_product_line_match, resolve_bambu_color
  13. from backend.app.utils.color_utils import color_match_key, spoolman_color_hex
  14. logger = logging.getLogger(__name__)
  15. BAMBU_RFID_TAG_LENGTH = 32
  16. # Spool.extra key holding the consumed-counter baseline. Defined here rather
  17. # than imported from the routes package so the client does not depend on it;
  18. # _spoolman_helpers declares the same name for the read side (#2906), and
  19. # test_spoolman_reset_baseline_2906.py asserts the two are equal.
  20. BAMBU_WEIGHT_USED_BASELINE_KEY = "bambu_weight_used_baseline"
  21. @dataclass
  22. class SpoolmanSpool:
  23. """Represents a spool in Spoolman."""
  24. id: int
  25. filament_id: int | None
  26. remaining_weight: float | None
  27. used_weight: float
  28. first_used: str | None
  29. last_used: str | None
  30. location: str | None
  31. lot_nr: str | None
  32. comment: str | None
  33. extra: dict | None # Contains tag_uid in extra.tag
  34. @dataclass
  35. class SpoolmanFilament:
  36. """Represents a filament type in Spoolman."""
  37. id: int
  38. name: str
  39. vendor_id: int | None
  40. material: str | None
  41. color_hex: str | None
  42. weight: float | None # Net weight in grams
  43. @dataclass
  44. class AMSTray:
  45. """Represents an AMS tray with filament data from Bambu printer."""
  46. ams_id: int # 0-3 for regular AMS, 128-135 for AMS-HT, 254+ for external spool
  47. tray_id: int # 0-3
  48. tray_type: str # PLA, PETG, ABS, etc.
  49. tray_sub_brands: str # Full name like "PLA Basic", "PETG HF"
  50. tray_color: str # Hex color like "FEC600FF"
  51. remain: int # Remaining percentage (0-100)
  52. tag_uid: str # RFID tag UID
  53. tray_uuid: str # Spool UUID
  54. tray_info_idx: str # Bambu filament preset ID like "GFA00"
  55. tray_weight: int # Spool weight in grams (usually 1000)
  56. class SpoolmanNotFoundError(Exception):
  57. """Raised when a spool ID does not exist in Spoolman (HTTP 404)."""
  58. class SpoolmanUnavailableError(Exception):
  59. """Raised when Spoolman is unreachable or returns a server/network error."""
  60. class SpoolmanClientError(Exception):
  61. """Raised when Spoolman returns a 4xx client error (not 404)."""
  62. def __init__(self, message: str, status_code: int, response_text: str = "") -> None:
  63. super().__init__(message)
  64. self.status_code = status_code
  65. self.response_text = response_text
  66. def _filament_subtype_part(name: str, material: str) -> str:
  67. """Return the subtype portion of a filament name, lowercased.
  68. Mirrors the read-side derivation in
  69. ``backend/app/api/routes/_spoolman_helpers.py::_map_spoolman_spool``:
  70. if the filament name starts with the material prefix (e.g. ``"PLA Glow"``
  71. when material is ``"PLA"``), strip it; otherwise return the name as-is.
  72. Used by ``find_or_create_filament`` so that an existing filament saved by
  73. the AMS-sync path with name ``"Glow"`` still matches a user-driven edit
  74. that composes ``"PLA Glow"`` (#1357).
  75. """
  76. s = (name or "").strip()
  77. m = (material or "").strip()
  78. if m and s.upper().startswith(m.upper() + " "):
  79. return s[len(m) + 1 :].strip().lower()
  80. return s.lower()
  81. class SpoolmanClient:
  82. """Client for interacting with Spoolman API."""
  83. def __init__(self, base_url: str):
  84. """Initialize the Spoolman client."""
  85. self.base_url = base_url.rstrip("/")
  86. self.api_url = f"{self.base_url}/api/v1"
  87. self._client: httpx.AsyncClient | None = None
  88. self._connected = False
  89. # Per-spool locks for atomic read-modify-write in merge_spool_extra.
  90. # WeakValueDictionary: locks are GC'd once no coroutine holds a reference.
  91. self._extra_locks: weakref.WeakValueDictionary[int, asyncio.Lock] = weakref.WeakValueDictionary()
  92. # Extra-field names this client has already registered with Spoolman.
  93. # Bounded by the number of distinct keys Bambuddy writes, so it never
  94. # grows with spool count; scoped to the instance so a client pointed at
  95. # a different Spoolman starts over.
  96. self._ensured_extra_fields: set[str] = set()
  97. # Whether Spoolman's extra-field listing has been read once. Separate
  98. # from the set above because the two answer different questions: the
  99. # set is "which fields are known to exist", this is "have we asked".
  100. # Without it a client registering three brand-new fields re-read the
  101. # whole listing before each one.
  102. self._extra_fields_listed = False
  103. self._ensure_extra_lock = asyncio.Lock()
  104. async def _get_client(self) -> httpx.AsyncClient:
  105. """Get or create the HTTP client with connection pooling limits."""
  106. if self._client is None:
  107. self._client = httpx.AsyncClient(
  108. timeout=httpx.Timeout(connect=5.0, read=10.0, write=5.0, pool=5.0),
  109. follow_redirects=False,
  110. verify=True,
  111. limits=httpx.Limits(
  112. max_keepalive_connections=5,
  113. max_connections=10,
  114. keepalive_expiry=30.0,
  115. ),
  116. )
  117. return self._client
  118. async def close(self):
  119. """Close the HTTP client."""
  120. if self._client:
  121. await self._client.aclose()
  122. self._client = None
  123. async def health_check(self) -> bool:
  124. """Check if Spoolman server is reachable; returns True if healthy."""
  125. try:
  126. client = await self._get_client()
  127. response = await client.get(f"{self.api_url}/health")
  128. self._connected = response.status_code == 200
  129. return self._connected
  130. except Exception as e:
  131. logger.warning(
  132. "Spoolman health check failed (url=%s, type=%s): %s",
  133. self.api_url,
  134. type(e).__name__,
  135. e,
  136. )
  137. self._connected = False
  138. return False
  139. @property
  140. def is_connected(self) -> bool:
  141. """Check if client is connected to Spoolman."""
  142. return self._connected
  143. async def get_spools(self) -> list[dict]:
  144. """Fetch all spools from Spoolman with up to 3 retries on connection errors."""
  145. max_attempts = 3
  146. retry_delay = 0.5 # 500ms
  147. for attempt in range(1, max_attempts + 1):
  148. try:
  149. client = await self._get_client()
  150. response = await client.get(f"{self.api_url}/spool")
  151. response.raise_for_status()
  152. spools = response.json()
  153. if attempt > 1:
  154. logger.info("Successfully fetched %d spools on attempt %d", len(spools), attempt)
  155. return spools
  156. except (httpx.ReadError, httpx.RemoteProtocolError, httpx.ConnectError) as e:
  157. # Connection-related errors - close and recreate client for next attempt
  158. if attempt < max_attempts:
  159. logger.warning(
  160. "Connection error getting spools (attempt %d/%d): %s. Recreating client and retrying in %dms...",
  161. attempt,
  162. max_attempts,
  163. e,
  164. int(retry_delay * 1000),
  165. )
  166. # Close the stale client and recreate it
  167. await self.close()
  168. await asyncio.sleep(retry_delay)
  169. else:
  170. logger.error("Failed to get spools from Spoolman after %d attempts: %s", max_attempts, e)
  171. raise SpoolmanUnavailableError("Cannot reach Spoolman") from e
  172. except Exception as e:
  173. # Other errors (HTTP errors, JSON decode errors, etc.)
  174. if attempt < max_attempts:
  175. logger.warning(
  176. "Failed to get spools from Spoolman (attempt %d/%d): %s. Retrying in %dms...",
  177. attempt,
  178. max_attempts,
  179. e,
  180. int(retry_delay * 1000),
  181. )
  182. await asyncio.sleep(retry_delay)
  183. else:
  184. logger.error("Failed to get spools from Spoolman after %d attempts: %s", max_attempts, e)
  185. raise SpoolmanUnavailableError("Cannot reach Spoolman") from e
  186. async def _get_with_retry(self, path: str, params: dict | None = None) -> list[dict]:
  187. """GET a Spoolman JSON list endpoint with up to 3 retries on connection errors."""
  188. max_attempts = 3
  189. retry_delay = 0.5
  190. url = f"{self.api_url}/{path.lstrip('/')}"
  191. for attempt in range(1, max_attempts + 1):
  192. try:
  193. client = await self._get_client()
  194. response = await client.get(url, params=params or None)
  195. response.raise_for_status()
  196. return response.json()
  197. except (httpx.ReadError, httpx.RemoteProtocolError, httpx.ConnectError) as e:
  198. if attempt < max_attempts:
  199. logger.warning(
  200. "Connection error fetching %s (attempt %d/%d): %s. Recreating client and retrying in %dms...",
  201. path,
  202. attempt,
  203. max_attempts,
  204. e,
  205. int(retry_delay * 1000),
  206. )
  207. await self.close()
  208. await asyncio.sleep(retry_delay)
  209. else:
  210. logger.error("Failed to fetch %s from Spoolman after %d attempts: %s", path, max_attempts, e)
  211. raise SpoolmanUnavailableError("Cannot reach Spoolman") from e
  212. except Exception as e:
  213. if attempt < max_attempts:
  214. logger.warning(
  215. "Failed to fetch %s from Spoolman (attempt %d/%d): %s. Retrying in %dms...",
  216. path,
  217. attempt,
  218. max_attempts,
  219. e,
  220. int(retry_delay * 1000),
  221. )
  222. await asyncio.sleep(retry_delay)
  223. else:
  224. logger.error("Failed to fetch %s from Spoolman after %d attempts: %s", path, max_attempts, e)
  225. raise SpoolmanUnavailableError("Cannot reach Spoolman") from e
  226. async def get_filaments(self) -> list[dict]:
  227. """Fetch all internal filaments from Spoolman."""
  228. try:
  229. client = await self._get_client()
  230. response = await client.get(f"{self.api_url}/filament")
  231. response.raise_for_status()
  232. return response.json()
  233. except Exception as e:
  234. logger.error("Failed to get filaments from Spoolman: %s", e)
  235. raise SpoolmanUnavailableError("Cannot reach Spoolman") from e
  236. async def get_filament(self, filament_id: int) -> dict:
  237. """Fetch a single filament by ID from Spoolman."""
  238. if filament_id <= 0:
  239. raise ValueError(f"Invalid filament_id: {filament_id}")
  240. response = await self._request_filament("GET", filament_id, operation="get_filament")
  241. return response.json()
  242. async def get_external_filaments(self) -> list[dict]:
  243. """Fetch external/library filaments from Spoolman."""
  244. try:
  245. client = await self._get_client()
  246. response = await client.get(f"{self.api_url}/external/filament")
  247. response.raise_for_status()
  248. return response.json()
  249. except Exception as e:
  250. logger.error("Failed to get external filaments from Spoolman: %s", e)
  251. raise SpoolmanUnavailableError("Cannot reach Spoolman") from e
  252. async def get_vendors(self) -> list[dict]:
  253. """Fetch all vendors from Spoolman."""
  254. try:
  255. client = await self._get_client()
  256. response = await client.get(f"{self.api_url}/vendor")
  257. response.raise_for_status()
  258. return response.json()
  259. except Exception as e:
  260. logger.error("Failed to get vendors from Spoolman: %s", e)
  261. raise SpoolmanUnavailableError("Cannot reach Spoolman") from e
  262. async def create_vendor(self, name: str) -> dict:
  263. """Create a new vendor in Spoolman."""
  264. try:
  265. client = await self._get_client()
  266. response = await client.post(f"{self.api_url}/vendor", json={"name": name})
  267. if 400 <= response.status_code < 500:
  268. raise SpoolmanClientError(
  269. f"Spoolman rejected vendor creation (HTTP {response.status_code})",
  270. response.status_code,
  271. )
  272. response.raise_for_status()
  273. return response.json()
  274. except SpoolmanClientError:
  275. raise
  276. except Exception as e:
  277. logger.error("Failed to create vendor in Spoolman: %s", e)
  278. raise SpoolmanUnavailableError("Cannot reach Spoolman") from e
  279. def _get_material_density(self, material: str | None) -> float:
  280. """Return typical density (g/cm³) for the given filament material; defaults to PLA (1.24)."""
  281. # Typical densities for common filament materials
  282. densities = {
  283. "PLA": 1.24,
  284. "PLA-CF": 1.29,
  285. "PLA-S": 1.24,
  286. "PETG": 1.27,
  287. "ABS": 1.04,
  288. "ASA": 1.07,
  289. "TPU": 1.21,
  290. "PA": 1.14, # Nylon
  291. "PA-CF": 1.20,
  292. "PC": 1.20,
  293. "PVA": 1.23,
  294. "HIPS": 1.04,
  295. "PP": 0.90,
  296. "PET": 1.38,
  297. }
  298. if material:
  299. # Try exact match first, then uppercase
  300. mat_upper = material.upper()
  301. for key, density in densities.items():
  302. if key.upper() == mat_upper or mat_upper.startswith(key.upper()):
  303. return density
  304. return 1.24 # Default to PLA density
  305. async def create_filament(
  306. self,
  307. name: str,
  308. vendor_id: int | None = None,
  309. material: str | None = None,
  310. color_hex: str | None = None,
  311. color_name: str | None = None,
  312. weight: float | None = None,
  313. diameter: float = 1.75,
  314. density: float | None = None,
  315. ) -> dict:
  316. """Create a new filament in Spoolman."""
  317. if not name or not name.strip():
  318. raise ValueError("Filament name is required")
  319. if density is None:
  320. density = self._get_material_density(material)
  321. data: dict = {
  322. "name": name.strip(),
  323. "diameter": diameter,
  324. "density": density,
  325. }
  326. if vendor_id:
  327. data["vendor_id"] = vendor_id
  328. if material:
  329. data["material"] = material
  330. if color_hex:
  331. # Every create funnels through here, so this is where the stored shape
  332. # is decided: six characters for an opaque spool, eight only when the
  333. # alpha byte says the filament is translucent. See #2912.
  334. data["color_hex"] = spoolman_color_hex(color_hex) or color_hex
  335. if color_name:
  336. data["color_name"] = color_name
  337. if weight:
  338. data["weight"] = weight
  339. logger.debug("Creating filament in Spoolman: %s", data)
  340. try:
  341. client = await self._get_client()
  342. response = await client.post(f"{self.api_url}/filament", json=data)
  343. if 400 <= response.status_code < 500:
  344. raise SpoolmanClientError(
  345. f"Spoolman rejected filament creation (HTTP {response.status_code})",
  346. response.status_code,
  347. )
  348. response.raise_for_status()
  349. return response.json()
  350. except SpoolmanClientError:
  351. raise
  352. except Exception as e:
  353. logger.error("Failed to create filament in Spoolman: %s", e)
  354. raise SpoolmanUnavailableError("Cannot reach Spoolman") from e
  355. async def patch_filament(self, filament_id: int, data: dict) -> dict:
  356. """PATCH a filament entry in Spoolman (e.g. update name or spool_weight)."""
  357. if filament_id <= 0:
  358. raise ValueError(f"Invalid filament_id: {filament_id}")
  359. response = await self._request_filament("PATCH", filament_id, json_body=data, operation="patch_filament")
  360. return response.json()
  361. async def create_spool(
  362. self,
  363. filament_id: int,
  364. remaining_weight: float | None = None,
  365. location: str | None = None,
  366. lot_nr: str | None = None,
  367. comment: str | None = None,
  368. extra: dict | None = None,
  369. spool_weight: float | None = None,
  370. initial_weight: float | None = None,
  371. ) -> dict:
  372. """Create a new spool in Spoolman."""
  373. data: dict = {"filament_id": filament_id}
  374. # The spool's own net weight. Left off, Spoolman copies the filament's
  375. # weight, which is wrong for a spool of another size (#3194).
  376. if initial_weight is not None:
  377. data["initial_weight"] = initial_weight
  378. if remaining_weight is not None:
  379. data["remaining_weight"] = remaining_weight
  380. if location:
  381. data["location"] = location
  382. if lot_nr:
  383. data["lot_nr"] = lot_nr
  384. if comment:
  385. data["comment"] = comment
  386. # `is not None`, not truthiness: 0 g is a legitimate tare (a spool-less
  387. # coil), and it is not the same answer as "inherit from the filament",
  388. # which is what leaving the field off means to Spoolman.
  389. if spool_weight is not None:
  390. data["spool_weight"] = spool_weight
  391. if extra:
  392. data["extra"] = extra
  393. await self._ensure_extra_fields(extra)
  394. logger.debug("Creating spool in Spoolman: %s", data)
  395. try:
  396. client = await self._get_client()
  397. response = await client.post(f"{self.api_url}/spool", json=data)
  398. if response.status_code == 404:
  399. raise SpoolmanNotFoundError(f"Filament {filament_id} not found in Spoolman")
  400. if 400 <= response.status_code < 500:
  401. raise SpoolmanClientError(
  402. f"Spoolman rejected spool creation (HTTP {response.status_code})",
  403. response.status_code,
  404. )
  405. response.raise_for_status()
  406. result = response.json()
  407. logger.info("Created spool %s in Spoolman", result.get("id"))
  408. return result
  409. except (SpoolmanNotFoundError, SpoolmanClientError):
  410. raise
  411. except Exception as e:
  412. logger.error("Failed to create spool in Spoolman: %s", e)
  413. raise SpoolmanUnavailableError("Cannot reach Spoolman") from e
  414. async def update_spool(
  415. self,
  416. spool_id: int,
  417. remaining_weight: float | None = None,
  418. location: str | None = None,
  419. clear_location: bool = False,
  420. extra: dict | None = None,
  421. ) -> dict:
  422. """Update an existing spool in Spoolman, always setting last_used."""
  423. data: dict = {}
  424. if remaining_weight is not None:
  425. data["remaining_weight"] = remaining_weight
  426. if clear_location:
  427. data["location"] = None
  428. elif location:
  429. data["location"] = location
  430. if extra:
  431. data["extra"] = extra
  432. await self._ensure_extra_fields(extra)
  433. data["last_used"] = datetime.now(timezone.utc).isoformat()
  434. response = await self._request_spool("PATCH", spool_id, json_body=data, operation="update")
  435. return response.json()
  436. async def _request_spool(
  437. self,
  438. method: Literal["GET", "PATCH", "DELETE"],
  439. spool_id: int,
  440. *,
  441. json_body: dict | None = None,
  442. operation: str,
  443. ) -> httpx.Response:
  444. """Perform a spool-scoped HTTP request, translating 404 and errors to named exceptions."""
  445. try:
  446. client = await self._get_client()
  447. response = await client.request(
  448. method,
  449. f"{self.api_url}/spool/{spool_id}",
  450. json=json_body,
  451. )
  452. if response.status_code == 404:
  453. raise SpoolmanNotFoundError(f"Spool {spool_id} not found in Spoolman")
  454. response.raise_for_status()
  455. return response
  456. except SpoolmanNotFoundError:
  457. raise
  458. except httpx.HTTPStatusError as e:
  459. if 400 <= e.response.status_code < 500:
  460. logger.warning(
  461. "Spoolman returned %d for %s spool %s",
  462. e.response.status_code,
  463. operation,
  464. spool_id,
  465. )
  466. raise SpoolmanClientError(
  467. f"Spoolman rejected {operation} for spool {spool_id} (HTTP {e.response.status_code})",
  468. e.response.status_code,
  469. e.response.text[:500],
  470. ) from e
  471. else:
  472. logger.error("Failed to %s spool %s in Spoolman: %s", operation, spool_id, e)
  473. raise SpoolmanUnavailableError(f"Failed to {operation} spool {spool_id}") from e
  474. except Exception as e:
  475. logger.error("Failed to %s spool %s in Spoolman: %s", operation, spool_id, e)
  476. raise SpoolmanUnavailableError(f"Failed to {operation} spool {spool_id}") from e
  477. async def _request_filament(
  478. self,
  479. method: Literal["GET", "PATCH"],
  480. filament_id: int,
  481. *,
  482. json_body: dict | None = None,
  483. operation: str,
  484. ) -> httpx.Response:
  485. """Perform a filament-scoped HTTP request, translating 404 and errors to named exceptions."""
  486. try:
  487. client = await self._get_client()
  488. response = await client.request(
  489. method,
  490. f"{self.api_url}/filament/{filament_id}",
  491. json=json_body,
  492. )
  493. if response.status_code == 404:
  494. raise SpoolmanNotFoundError(f"Filament {filament_id} not found in Spoolman")
  495. response.raise_for_status()
  496. return response
  497. except SpoolmanNotFoundError:
  498. raise
  499. except httpx.HTTPStatusError as e:
  500. if 400 <= e.response.status_code < 500:
  501. logger.warning(
  502. "Spoolman returned %d for %s filament %s",
  503. e.response.status_code,
  504. operation,
  505. filament_id,
  506. )
  507. raise SpoolmanClientError(
  508. f"Spoolman rejected {operation} for filament {filament_id} (HTTP {e.response.status_code})",
  509. e.response.status_code,
  510. e.response.text[:500],
  511. ) from e
  512. else:
  513. logger.error("Failed to %s filament %s in Spoolman: %s", operation, filament_id, e)
  514. raise SpoolmanUnavailableError(f"Failed to {operation} filament {filament_id}") from e
  515. except Exception as e:
  516. logger.error("Failed to %s filament %s in Spoolman: %s", operation, filament_id, e)
  517. raise SpoolmanUnavailableError(f"Failed to {operation} filament {filament_id}") from e
  518. async def get_spool(self, spool_id: int) -> dict:
  519. """Fetch a single spool by ID from Spoolman."""
  520. response = await self._request_spool("GET", spool_id, operation="get")
  521. return response.json()
  522. async def get_all_spools(self, allow_archived: bool = False) -> list[dict]:
  523. """Fetch all spools from Spoolman with retry, optionally including archived ones."""
  524. params: dict = {}
  525. if allow_archived:
  526. params["allow_archived"] = "true"
  527. return await self._get_with_retry("/spool", params=params or None)
  528. async def get_distinct_locations(self) -> list[str]:
  529. """Return distinct location strings currently assigned to Spoolman spools.
  530. Spoolman's `/location` endpoint shape varies across versions: older
  531. releases return `list[str]`, newer ones return `list[dict]` with a
  532. `name` field. Normalize to `list[str]` so callers can iterate without
  533. runtime shape checks.
  534. """
  535. raw = await self._get_with_retry("/location")
  536. if not isinstance(raw, list):
  537. return []
  538. names: list[str] = []
  539. for entry in raw:
  540. if isinstance(entry, str):
  541. names.append(entry)
  542. elif isinstance(entry, dict):
  543. name = entry.get("name")
  544. if isinstance(name, str):
  545. names.append(name)
  546. return names
  547. async def rename_location(self, current_name: str, new_name: str) -> int:
  548. """Bulk-rename a location string on all Spoolman spools.
  549. Tries the bulk `PATCH /location/{name}` endpoint first. Spoolman
  550. versions older than ~0.16 don't expose it and respond 404/405 — in
  551. that case fall back to iterating every spool currently at
  552. ``current_name`` and PATCHing each one's ``location`` field directly.
  553. Returns the number of spools renamed (or 0 if the bulk endpoint
  554. succeeded without enumerating).
  555. """
  556. from urllib.parse import quote
  557. encoded = quote(current_name, safe="")
  558. client = await self._get_client()
  559. try:
  560. response = await client.patch(
  561. f"{self.api_url}/location/{encoded}",
  562. json={"name": new_name},
  563. )
  564. response.raise_for_status()
  565. return 0
  566. except httpx.HTTPStatusError as exc:
  567. if exc.response.status_code not in (404, 405):
  568. raise
  569. logger.info(
  570. "Spoolman bulk-rename endpoint unavailable (status %d); falling back to per-spool PATCH",
  571. exc.response.status_code,
  572. )
  573. # Per-spool fallback: enumerate every spool currently at the old name
  574. # and PATCH each. Keep going on individual failures so a single
  575. # already-deleted spool doesn't strand the rest at the old name —
  576. # collect errors and re-raise as a single SpoolmanClientError if any
  577. # leftover survives.
  578. spools = await self.get_all_spools(allow_archived=True)
  579. renamed = 0
  580. failures: list[str] = []
  581. for spool in spools:
  582. if (spool.get("location") or "").strip() != current_name:
  583. continue
  584. try:
  585. await self._request_spool(
  586. "PATCH",
  587. spool["id"],
  588. json_body={"location": new_name},
  589. operation="rename-location",
  590. )
  591. renamed += 1
  592. except SpoolmanNotFoundError:
  593. continue
  594. except Exception as exc: # noqa: BLE001 — accumulate and re-raise below
  595. failures.append(f"spool {spool.get('id')}: {exc}")
  596. if failures:
  597. raise SpoolmanClientError(
  598. f"Spoolman rename fallback failed for {len(failures)} spool(s): {'; '.join(failures[:3])}",
  599. status_code=502,
  600. )
  601. return renamed
  602. async def delete_spool(self, spool_id: int) -> None:
  603. """Delete a spool from Spoolman."""
  604. await self._request_spool("DELETE", spool_id, operation="delete")
  605. async def is_filament_shared(self, filament_id: int, exclude_spool_id: int) -> bool:
  606. """True if any spool other than ``exclude_spool_id`` is linked to ``filament_id``.
  607. Used by the spool-edit path to decide between PATCHing the existing
  608. filament in place (singleton) and falling back to find_or_create
  609. (shared — re-linking the spool is the only safe option). Includes
  610. archived spools so a shared link doesn't suddenly look singleton just
  611. because the sibling spool was archived.
  612. """
  613. spools = await self.get_all_spools(allow_archived=True)
  614. for s in spools:
  615. if s.get("id") == exclude_spool_id:
  616. continue
  617. if ((s.get("filament") or {}).get("id")) == filament_id:
  618. return True
  619. return False
  620. async def set_spool_archived(self, spool_id: int, archived: bool) -> dict:
  621. """Archive or restore a spool in Spoolman."""
  622. response = await self._request_spool(
  623. "PATCH",
  624. spool_id,
  625. json_body={"archived": archived},
  626. operation="archive/restore",
  627. )
  628. return response.json()
  629. async def reset_spool_consumed_counter(self, spool_id: int) -> dict:
  630. """Zero the displayed consumed counter by recording a baseline in spool.extra.
  631. This used to PATCH ``used_weight = 0``, which is not a baseline: Spoolman
  632. recomputes ``remaining_weight`` as initial minus used, so zeroing used
  633. weight put the spool back to full and threw away the measured remaining
  634. filament. The confirmation copy promised the opposite in all thirteen
  635. locales (#2906).
  636. Writing the baseline into ``extra`` instead touches no native Spoolman
  637. field -- initial, remaining and used all survive -- and is the same
  638. mechanism internal mode has always used for its ``weight_used_baseline``
  639. column, which is what #1644 asked the two modes to share rather than
  640. approximate. ``_map_spoolman_spool`` folds the stored value back into the
  641. baseline it reports.
  642. Serialised on the same per-spool lock as ``merge_spool_extra`` so a
  643. concurrent tag or colour write cannot read the extra dict between this
  644. method's fetch and its PATCH and put the old one back.
  645. """
  646. async with self.extra_lock(spool_id):
  647. current = await self.get_spool(spool_id) # raises on error
  648. used_weight = current.get("used_weight")
  649. try:
  650. baseline = float(used_weight) if used_weight is not None else 0.0
  651. except (TypeError, ValueError):
  652. baseline = 0.0
  653. if not math.isfinite(baseline) or baseline < 0:
  654. baseline = 0.0
  655. merged = {
  656. **(current.get("extra") or {}),
  657. # Stored as a JSON *string*, not a JSON number. Spoolman
  658. # registers an unseen extra key as field_type "text" on first
  659. # write, and its validate_extra_field_value then requires the
  660. # value to decode to a str -- a bare 263.0 is rejected with
  661. # "Value is not a string." and the PATCH 400s, so the reset
  662. # would never land. It has to be right the first time:
  663. # add_or_update_extra_field refuses to change a field's type
  664. # afterwards, so one numeric write would pin the key to text on
  665. # that install permanently. tag, bambu_color_name and both
  666. # slicer keys already store the string form for this reason.
  667. BAMBU_WEIGHT_USED_BASELINE_KEY: json.dumps(str(baseline)),
  668. }
  669. return await self.update_spool_full(spool_id=spool_id, extra=merged)
  670. async def update_spool_full(
  671. self,
  672. spool_id: int,
  673. *,
  674. filament_id: int | None = None,
  675. remaining_weight: float | None = None,
  676. comment: str | None = None,
  677. price: float | None = None,
  678. location: str | None = None,
  679. clear_location: bool = False,
  680. extra: dict | None = None,
  681. spool_weight: float | None = None,
  682. clear_spool_weight: bool = False,
  683. initial_weight: float | None = None,
  684. ) -> dict:
  685. """Update a spool with full field support; unlike update_spool, does not auto-set last_used."""
  686. data: dict = {}
  687. if filament_id is not None:
  688. data["filament_id"] = filament_id
  689. # Spoolman applies the fields in its own schema order, which puts
  690. # initial_weight before remaining_weight, so a remaining_weight in the
  691. # same PATCH is measured against the new net weight.
  692. if initial_weight is not None:
  693. data["initial_weight"] = initial_weight
  694. if remaining_weight is not None:
  695. data["remaining_weight"] = remaining_weight
  696. if comment is not None:
  697. data["comment"] = comment if comment else None
  698. if price is not None:
  699. data["price"] = price
  700. if clear_location:
  701. data["location"] = None
  702. elif location is not None:
  703. data["location"] = location
  704. if extra is not None:
  705. data["extra"] = extra
  706. await self._ensure_extra_fields(extra)
  707. if clear_spool_weight:
  708. data["spool_weight"] = None
  709. elif spool_weight is not None:
  710. data["spool_weight"] = spool_weight
  711. response = await self._request_spool("PATCH", spool_id, json_body=data, operation="update")
  712. return response.json()
  713. def extra_lock(self, spool_id: int) -> asyncio.Lock:
  714. """Return (creating if needed) the per-spool asyncio.Lock used by merge_spool_extra."""
  715. lock = self._extra_locks.get(spool_id)
  716. if lock is None:
  717. lock = asyncio.Lock()
  718. self._extra_locks[spool_id] = lock
  719. return lock
  720. async def merge_spool_extra(self, spool_id: int, new_fields: dict) -> dict:
  721. """Fetch the spool's extra dict, merge new_fields into it, then PATCH back — serialised per spool."""
  722. async with self.extra_lock(spool_id):
  723. current = await self.get_spool(spool_id) # raises on error
  724. current_extra: dict = current.get("extra") or {}
  725. merged = {**current_extra, **new_fields}
  726. return await self.update_spool_full(spool_id=spool_id, extra=merged)
  727. async def find_or_create_vendor(self, name: str) -> int:
  728. """Return the Spoolman vendor ID for the given name, creating the vendor if absent."""
  729. vendors = await self.get_vendors()
  730. name_lower = name.strip().lower()
  731. for vendor in vendors:
  732. if vendor.get("name", "").strip().lower() == name_lower:
  733. return vendor["id"]
  734. created = await self.create_vendor(name.strip())
  735. vendor_id = created.get("id")
  736. if not vendor_id:
  737. raise SpoolmanUnavailableError(f"Spoolman returned vendor without id field: {list(created.keys())}")
  738. return vendor_id
  739. async def find_or_create_filament(
  740. self,
  741. material: str,
  742. subtype: str,
  743. brand: str | None,
  744. color_hex: str,
  745. label_weight: int,
  746. color_name: str | None = None,
  747. ) -> int:
  748. """Return the filament ID matching material/name/brand/color, creating it if absent."""
  749. name = f"{material} {subtype}".strip() if subtype else material
  750. # One value in both roles. `color_match_key` returns the shape the colour
  751. # would be stored as, so the key the loop below compares on and the value
  752. # a new filament is created with are the same string by construction: an
  753. # opaque spool keys and stores as six characters, a translucent one as
  754. # eight, and neither can be conflated with the other (#2912).
  755. color = color_match_key(color_hex)
  756. vendor_id: int | None = None
  757. if brand:
  758. vendor_id = await self.find_or_create_vendor(brand)
  759. # Normalised match keys (case-insensitive). Computed once outside the
  760. # loop so the inner comparison stays simple.
  761. composed_subtype = _filament_subtype_part(name, material)
  762. material_norm = material.upper()
  763. brand_norm = (brand or "").strip().lower()
  764. filaments = await self.get_filaments()
  765. for f in filaments:
  766. f_material = (f.get("material") or "").upper()
  767. f_color = color_match_key(f.get("color_hex"))
  768. f_vendor = f.get("vendor") or {}
  769. f_vendor_name = (f_vendor.get("name") or "").strip().lower()
  770. material_match = f_material == material_norm
  771. # Match on the subtype portion of the filament name. AMS-sync
  772. # auto-create (the underscore-prefixed `_find_or_create_filament`
  773. # used during MQTT tray import) stores the filament as just
  774. # ``tray.tray_sub_brands`` — e.g. ``"Glow"`` — while the
  775. # user-driven edit path here composes ``"<material> <subtype>"``
  776. # — ``"PLA Glow"``. The old literal equality `f_name == name`
  777. # failed to bridge the two shapes, so every edit fell through to
  778. # `create_filament`, leaving a trail of duplicate filaments AND
  779. # leaving the spool either still pointed at the old filament
  780. # whose `color_name` never got patched, or pointed at a new
  781. # filament with the colour while the inventory list kept
  782. # showing the synth fallback from the old one (#1357).
  783. f_subtype_part = _filament_subtype_part(f.get("name") or "", material)
  784. name_match = f_subtype_part == composed_subtype
  785. color_match = f_color == color
  786. vendor_match = (not brand) or f_vendor_name == brand_norm
  787. if material_match and name_match and color_match and vendor_match:
  788. # color_name is intentionally not part of the match key and
  789. # is no longer patched onto the filament here: Spoolman 0.23.1
  790. # has no `color_name` field on Filament (#1357 — confirmed
  791. # against the FilamentUpdateParameters schema). The earlier
  792. # #1319 fix tried to patch it and Spoolman silently dropped
  793. # the key, which is exactly why the user's edit looked "not
  794. # saved". The route now persists color_name via
  795. # spool.extra.bambu_color_name (see _map_spoolman_spool for
  796. # the read side); find_or_create_filament's only job is to
  797. # resolve the right filament_id for the spool link.
  798. return f["id"]
  799. # color_name omitted: Spoolman has no such field on Filament (#1357);
  800. # the user's color_name lands in spool.extra.bambu_color_name via the
  801. # route after find_or_create_filament returns the new id.
  802. filament = await self.create_filament(
  803. name=name,
  804. vendor_id=vendor_id,
  805. material=material,
  806. color_hex=color,
  807. weight=float(label_weight),
  808. )
  809. filament_id = filament.get("id")
  810. if not filament_id:
  811. raise SpoolmanUnavailableError(f"Spoolman returned filament without id field: {list(filament.keys())}")
  812. return filament_id
  813. async def use_spool(self, spool_id: int, used_weight: float) -> dict:
  814. """Record filament usage for a spool via the Spoolman /use endpoint."""
  815. try:
  816. client = await self._get_client()
  817. response = await client.put(
  818. f"{self.api_url}/spool/{spool_id}/use",
  819. json={"use_weight": used_weight},
  820. )
  821. if response.status_code == 404:
  822. raise SpoolmanNotFoundError(f"Spool {spool_id} not found in Spoolman")
  823. if 400 <= response.status_code < 500:
  824. raise SpoolmanClientError(
  825. f"Spoolman rejected use_spool for spool {spool_id} (HTTP {response.status_code})",
  826. response.status_code,
  827. )
  828. response.raise_for_status()
  829. return response.json()
  830. except (SpoolmanNotFoundError, SpoolmanClientError):
  831. raise
  832. except Exception as e:
  833. logger.error("Failed to record spool usage in Spoolman: %s", e)
  834. raise SpoolmanUnavailableError(f"Failed to record usage for spool {spool_id}") from e
  835. async def find_spool_by_tag(self, tag_uid: str, cached_spools: list[dict] | None = None) -> dict | None:
  836. """Return the spool matching the given RFID tag UID, or None if not found."""
  837. # Use cached spools if provided, otherwise fetch from API
  838. spools = cached_spools if cached_spools is not None else await self.get_spools()
  839. # Normalize tag_uid for comparison (uppercase, strip quotes)
  840. search_tag = tag_uid.strip('"').upper()
  841. for spool in spools:
  842. extra = spool.get("extra", {})
  843. if extra:
  844. stored_tag = extra.get("tag", "")
  845. # Normalize stored tag (strip quotes, uppercase)
  846. if stored_tag:
  847. normalized_tag = stored_tag.strip('"').upper()
  848. if normalized_tag == search_tag:
  849. logger.debug("Found spool %s matching tag %s", spool["id"], tag_uid)
  850. return spool
  851. return None
  852. def _find_spool_by_location(self, location: str, cached_spools: list[dict] | None) -> dict | None:
  853. """Return the spool at the exact location string, or None; fallback when RFID is unavailable."""
  854. if not cached_spools:
  855. return None
  856. for spool in cached_spools:
  857. if spool.get("location") == location:
  858. return spool
  859. return None
  860. async def find_spools_by_location_prefix(
  861. self, location_prefix: str, cached_spools: list[dict] | None = None
  862. ) -> list[dict]:
  863. """Return all spools whose location starts with location_prefix."""
  864. # Use cached spools if provided, otherwise fetch from API
  865. spools = cached_spools if cached_spools is not None else await self.get_spools()
  866. matching = []
  867. for spool in spools:
  868. location = spool.get("location", "")
  869. if location and location.startswith(location_prefix):
  870. matching.append(spool)
  871. return matching
  872. async def clear_location_for_removed_spools(
  873. self,
  874. printer_name: str,
  875. current_tray_uuids: set[str],
  876. cached_spools: list[dict] | None = None,
  877. synced_spool_ids: set[int] | None = None,
  878. ) -> int:
  879. """Clear location for Bambu Lab spools at this printer whose tray_uuid is no longer in the AMS."""
  880. location_prefix = f"{printer_name} - "
  881. spools_at_printer = await self.find_spools_by_location_prefix(location_prefix, cached_spools=cached_spools)
  882. cleared_count = 0
  883. for spool in spools_at_printer:
  884. spool_id = spool.get("id")
  885. # Skip spools that were just synced (matched by location or tag)
  886. if synced_spool_ids and spool_id in synced_spool_ids:
  887. continue
  888. # Get the tray_uuid (stored as "tag" in extra field)
  889. extra = spool.get("extra", {}) or {}
  890. stored_tag = extra.get("tag", "")
  891. if stored_tag:
  892. # Normalize: strip quotes and uppercase
  893. spool_uuid = stored_tag.strip('"').upper()
  894. else:
  895. spool_uuid = ""
  896. # Only clear location for Bambu Lab spools (those with a stored 32-character RFID tag).
  897. if len(spool_uuid) != BAMBU_RFID_TAG_LENGTH:
  898. continue
  899. # If this spool's UUID is not in the current AMS, clear its location
  900. if spool_uuid not in current_tray_uuids:
  901. logger.info(
  902. f"Clearing location for spool {spool_id} "
  903. f"(was: {spool.get('location')}, uuid: {spool_uuid[:16] if spool_uuid else 'none'}...)"
  904. )
  905. result = await self.update_spool(spool_id=spool_id, clear_location=True)
  906. if result:
  907. cleared_count += 1
  908. return cleared_count
  909. async def ensure_bambu_vendor(self) -> int | None:
  910. """Return the Bambu Lab vendor ID in Spoolman, creating the vendor if absent."""
  911. vendors = await self.get_vendors()
  912. for vendor in vendors:
  913. if vendor.get("name", "").lower() == "bambu lab":
  914. return vendor["id"]
  915. # Create Bambu Lab vendor if not exists
  916. vendor = await self.create_vendor("Bambu Lab")
  917. return vendor["id"] if vendor else None
  918. async def ensure_tag_extra_field(self) -> bool:
  919. """Register the 'tag' extra field in Spoolman if not present; returns True on success."""
  920. return await self.ensure_extra_field("tag")
  921. async def _load_existing_extra_field_keys(self) -> set[str] | None:
  922. """Keys of the spool extra fields Spoolman already has, or ``None`` when
  923. the listing could not be read.
  924. ``None`` and ``set()`` mean different things and the caller acts on the
  925. difference: an empty set is "Spoolman has no extra fields", which means
  926. every field Bambuddy needs must be created; ``None`` is "we could not
  927. find out", where the only safe move is to fall back to attempting the
  928. write blind.
  929. """
  930. try:
  931. client = await self._get_client()
  932. response = await client.get(f"{self.api_url}/field/spool")
  933. if response.status_code != 200:
  934. logger.debug(
  935. "Spoolman extra-field listing returned %s; falling back to blind registration",
  936. response.status_code,
  937. )
  938. return None
  939. fields = response.json()
  940. except Exception as e: # noqa: BLE001 — registration is best-effort, see _ensure_extra_fields
  941. logger.debug("Could not read Spoolman extra-field listing: %s", e)
  942. return None
  943. if not isinstance(fields, list):
  944. return None
  945. # Match on `key`, not `name`: `key` is the identifier the extra dict is
  946. # written under and the one Bambuddy cares about, while `name` is the
  947. # free-text label a user is free to change in Spoolman's UI.
  948. return {f["key"] for f in fields if isinstance(f, dict) and isinstance(f.get("key"), str)}
  949. async def ensure_extra_field(self, name: str, field_type: str = "text") -> bool:
  950. """Register a custom extra field in Spoolman if not present.
  951. Spoolman rejects PATCH requests that include unknown extra-dict keys
  952. with HTTP 400 ('Unknown extra field <name>.'), so any custom field
  953. Bambuddy persists alongside spools needs to be pre-registered.
  954. Idempotent — returns True if the field already exists.
  955. Existence is read from ``GET /field/spool``, the whole-listing endpoint.
  956. This used to probe ``GET /field/spool/{name}`` for one field at a time,
  957. which Spoolman has never served: it declares only POST and DELETE at
  958. that path, so the probe answered 405 every time and the check could
  959. never succeed (issue #2983, reported by @ngreatorex).
  960. Falling through to the POST on every call was worse than a wasted
  961. request, because that endpoint is an upsert rather than a create. It
  962. answered 200 whether or not the field was already there, so a field a
  963. user had renamed, retyped or given a default in Spoolman's own UI was
  964. silently reset to Bambuddy's version of it on every restart. Reading
  965. the listing first is what lets an existing field be left alone.
  966. """
  967. try:
  968. if name in self._ensured_extra_fields:
  969. return True
  970. if not self._extra_fields_listed:
  971. existing = await self._load_existing_extra_field_keys()
  972. if existing is not None:
  973. # Bank the whole listing: the caller registers several
  974. # fields in a row, and each one it already has is a request
  975. # not sent and a user customisation not overwritten. Read
  976. # once per client — every field created after this point is
  977. # added to the set as it is created, so re-reading would
  978. # only ever confirm what we already know.
  979. self._ensured_extra_fields |= existing
  980. self._extra_fields_listed = True
  981. if name in existing:
  982. logger.debug("Spoolman extra field %r already exists", name)
  983. return True
  984. client = await self._get_client()
  985. field_data = {
  986. "name": name,
  987. "field_type": field_type,
  988. "default_value": None,
  989. }
  990. response = await client.post(f"{self.api_url}/field/spool/{name}", json=field_data)
  991. if response.status_code in (200, 201):
  992. logger.info("Created Spoolman extra field %r", name)
  993. self._ensured_extra_fields.add(name)
  994. return True
  995. logger.warning(
  996. "Failed to create Spoolman extra field %r: %s - %s",
  997. name,
  998. response.status_code,
  999. response.text,
  1000. )
  1001. return False
  1002. except Exception as e:
  1003. logger.warning("Failed to ensure Spoolman extra field %r exists: %s", name, e)
  1004. return False
  1005. async def _ensure_extra_fields(self, extra: dict | None) -> None:
  1006. """Register every extra key an outgoing write declares, once per client.
  1007. Spoolman answers HTTP 400 "Unknown extra field <name>." for any extra
  1008. key that was not registered first, so registration has to happen before
  1009. the write, not before the feature. It used to happen before the feature:
  1010. three hand-maintained lists (the connect route, startup, and two inline
  1011. blocks in the inventory routes) each named the fields they expected to
  1012. be written later. Enabling Spoolman from Settings reaches none of them,
  1013. so the first AMS sync on a fresh Spoolman failed on every slot -- and
  1014. the Connect button that would have registered them is hidden by then,
  1015. because saving the settings initialises the client and the status
  1016. endpoint reads that as "connected" (issue #2903).
  1017. Keying off the payload instead removes the chance to forget: a write
  1018. that carries a key is a write that registers it. ``bambu_color_name``
  1019. is the cautionary case -- it never made it into the connect or startup
  1020. lists, and only works today because two call sites remembered to
  1021. register it by hand.
  1022. Best-effort by design. ``ensure_extra_field`` logs and returns False
  1023. rather than raising, and a failure here must not turn a write that
  1024. might still succeed into one that never happens -- the caller's own
  1025. error handling stays exactly as it was.
  1026. """
  1027. names = [name for name in (extra or {}) if name not in self._ensured_extra_fields]
  1028. if not names:
  1029. return
  1030. async with self._ensure_extra_lock:
  1031. for name in names:
  1032. # Re-check under the lock: a concurrent write may have just
  1033. # registered this one, and two syncs racing to POST the same
  1034. # field is how one of them gets a needless warning logged.
  1035. if name not in self._ensured_extra_fields:
  1036. await self.ensure_extra_field(name)
  1037. def parse_ams_tray(self, ams_id: int, tray_data: dict) -> AMSTray | None:
  1038. """Parse raw MQTT tray data into an AMSTray; returns None for empty or invalid trays."""
  1039. # Skip empty trays - check for valid tray_type
  1040. tray_type = tray_data.get("tray_type", "")
  1041. if not tray_type or tray_type.strip() == "":
  1042. return None
  1043. # Need valid color to create filament
  1044. tray_color = tray_data.get("tray_color", "")
  1045. if not tray_color or tray_color.strip() == "":
  1046. logger.debug("Skipping tray with empty color")
  1047. return None
  1048. # Transparent filament (alpha=00) used to be rewritten to a cream
  1049. # "natural PLA" colour before being stored, because the swatch
  1050. # renderer couldn't show alpha. The swatch now paints a checkerboard
  1051. # underlay for translucent rgbas (see filamentSwatchHelpers.ts), so
  1052. # we pass `00000000` through verbatim — the inventory row keeps the
  1053. # AMS-reported colour and the frontend resolves the name to "Clear"
  1054. # via getColorName (#1545).
  1055. # Get sub_brands, falling back to tray_type
  1056. tray_sub_brands = tray_data.get("tray_sub_brands", "")
  1057. if not tray_sub_brands or tray_sub_brands.strip() == "":
  1058. tray_sub_brands = tray_type
  1059. # Get tag_uid and tray_uuid, filtering out empty/invalid values
  1060. tag_uid = tray_data.get("tag_uid", "")
  1061. if tag_uid in ("", "0000000000000000"):
  1062. tag_uid = ""
  1063. tray_uuid = tray_data.get("tray_uuid", "")
  1064. if tray_uuid in ("", "00000000000000000000000000000000"):
  1065. tray_uuid = ""
  1066. # Get tray_info_idx (Bambu filament preset ID like "GFA00")
  1067. tray_info_idx = tray_data.get("tray_info_idx", "") or ""
  1068. # Get remaining percentage (-1 means unknown/not read by AMS)
  1069. remain = int(tray_data.get("remain", -1))
  1070. return AMSTray(
  1071. ams_id=ams_id,
  1072. tray_id=int(tray_data.get("id", 0)),
  1073. tray_type=tray_type.strip(),
  1074. tray_sub_brands=tray_sub_brands.strip(),
  1075. tray_color=tray_color,
  1076. remain=remain,
  1077. tag_uid=tag_uid,
  1078. tray_uuid=tray_uuid,
  1079. tray_info_idx=tray_info_idx.strip(),
  1080. tray_weight=int(tray_data.get("tray_weight", 1000)),
  1081. )
  1082. def convert_ams_slot_to_location(self, ams_id: int, tray_id: int) -> str:
  1083. """Return a human-readable location string (e.g. "AMS A1") for the given AMS slot."""
  1084. if ams_id >= 254:
  1085. return "External Spool"
  1086. if 128 <= ams_id <= 135:
  1087. # AMS-HT units use IDs 128-135
  1088. ht_letter = chr(ord("A") + (ams_id - 128))
  1089. return f"AMS-HT {ht_letter}{tray_id + 1}"
  1090. ams_letter = chr(ord("A") + ams_id)
  1091. return f"AMS {ams_letter}{tray_id + 1}"
  1092. def is_bambu_lab_spool(self, tray_uuid: str, tag_uid: str = "", tray_info_idx: str = "") -> bool:
  1093. """Return True if tray_uuid or tag_uid identifies a Bambu Lab spool; tray_info_idx is ignored."""
  1094. # Check tray_uuid (preferred - consistent across printer models)
  1095. if tray_uuid:
  1096. uuid = tray_uuid.strip()
  1097. if len(uuid) == 32 and uuid != "00000000000000000000000000000000":
  1098. try:
  1099. int(uuid, 16)
  1100. return True
  1101. except ValueError:
  1102. pass
  1103. # Fallback: check tag_uid (RFID tag - varies between printer readers)
  1104. # Bambu Lab RFID tags are 16 hex characters (8 bytes)
  1105. if tag_uid:
  1106. tag = tag_uid.strip()
  1107. if len(tag) == 16 and tag != "0000000000000000":
  1108. try:
  1109. int(tag, 16)
  1110. logger.debug("Identified Bambu Lab spool via tag_uid fallback: %s", tag)
  1111. return True
  1112. except ValueError:
  1113. pass
  1114. return False
  1115. def calculate_remaining_weight(self, remain_percent: int, spool_weight: int) -> float:
  1116. """Return remaining filament weight in grams given a percentage and total spool weight."""
  1117. return (remain_percent / 100.0) * spool_weight
  1118. async def sync_ams_tray(
  1119. self,
  1120. tray: AMSTray,
  1121. printer_name: str,
  1122. db: AsyncSession,
  1123. disable_weight_sync: bool = False,
  1124. cached_spools: list[dict] | None = None,
  1125. inventory_remaining: float | None = None,
  1126. spoolman_spool_id_hint: int | None = None,
  1127. auto_add_unknown_rfid: bool = True,
  1128. ) -> dict | None:
  1129. """Sync one AMS tray to Spoolman; creates the spool on first sight, updates weight otherwise."""
  1130. logger.debug(
  1131. f"Processing {printer_name} AMS {tray.ams_id} tray {tray.tray_id}: "
  1132. f"type={tray.tray_type}, idx={tray.tray_info_idx or 'none'}, "
  1133. f"uuid={tray.tray_uuid[:16] if tray.tray_uuid else 'none'}, "
  1134. f"tag={tray.tag_uid[:8] if tray.tag_uid else 'none'}..."
  1135. )
  1136. # Determine which identifier to use for Spoolman (prefer tray_uuid, fallback to tag_uid)
  1137. # Zero-filled values mean the AMS hasn't read the RFID tag — treat as no tag
  1138. zero_uuid = "00000000000000000000000000000000"
  1139. zero_tag = "0000000000000000"
  1140. spool_tag = None
  1141. if tray.tray_uuid and tray.tray_uuid != zero_uuid:
  1142. spool_tag = tray.tray_uuid
  1143. elif tray.tag_uid and tray.tag_uid != zero_tag:
  1144. spool_tag = tray.tag_uid
  1145. # Calculate remaining weight
  1146. # Primary: AMS MQTT data (remain percentage + tray_weight)
  1147. # Fallback: Built-in inventory tracked weight (when firmware sends invalid remain/tray_weight)
  1148. if tray.remain >= 0 and tray.tray_weight > 0:
  1149. remaining = self.calculate_remaining_weight(tray.remain, tray.tray_weight)
  1150. elif inventory_remaining is not None:
  1151. remaining = inventory_remaining
  1152. logger.debug(
  1153. "Using inventory weight fallback for %s AMS %s tray %s: %.1fg",
  1154. printer_name,
  1155. tray.ams_id,
  1156. tray.tray_id,
  1157. remaining,
  1158. )
  1159. else:
  1160. remaining = None
  1161. if spool_tag:
  1162. # Primary path: match by RFID tag
  1163. existing = await self.find_spool_by_tag(spool_tag, cached_spools=cached_spools)
  1164. if existing:
  1165. logger.info("Updating existing spool %s for tag %s...", existing["id"], spool_tag[:16])
  1166. return await self.update_spool(
  1167. spool_id=existing["id"],
  1168. remaining_weight=None if disable_weight_sync else remaining,
  1169. )
  1170. # Spool not found by tag - auto-create it, unless the user has
  1171. # opted out of auto-adding unknown RFIDs (settings.auto_add_unknown_rfid).
  1172. # Caller broadcasts unknown_tag on the resulting None so the UI can
  1173. # surface a "+ Add to inventory" affordance on the slot.
  1174. if not auto_add_unknown_rfid:
  1175. logger.info(
  1176. "Auto-add disabled; skipping Spoolman spool create for %s (tag: %s...)",
  1177. tray.tray_sub_brands,
  1178. spool_tag[:16],
  1179. )
  1180. return None
  1181. logger.info("Creating new spool in Spoolman for %s (tag: %s...)", tray.tray_sub_brands, spool_tag[:16])
  1182. catalog_color = None
  1183. if self.is_bambu_lab_spool(tray.tray_uuid, tray.tag_uid, tray.tray_info_idx):
  1184. catalog_color = await resolve_bambu_color(db, tray.tray_color, (tray.tray_sub_brands or "").strip())
  1185. filament = await self._find_or_create_filament(tray, catalog_color)
  1186. filament_id = filament["id"] if filament else None
  1187. else:
  1188. # Non-BL spool with custom RFID: use generic vendor lookup
  1189. brand = tray.tray_sub_brands if tray.tray_sub_brands != tray.tray_type else None
  1190. try:
  1191. filament_id = await self.find_or_create_filament(
  1192. material=tray.tray_type,
  1193. subtype="",
  1194. brand=brand,
  1195. color_hex=tray.tray_color,
  1196. label_weight=tray.tray_weight,
  1197. )
  1198. except (SpoolmanNotFoundError, SpoolmanUnavailableError, SpoolmanClientError):
  1199. logger.warning("Could not find or create filament for non-BL spool %s", tray.tray_sub_brands)
  1200. return None
  1201. if not filament_id:
  1202. logger.error("Failed to find or create filament for %s", tray.tray_sub_brands)
  1203. return None
  1204. extra = {"tag": json.dumps(spool_tag)}
  1205. # The colour name goes where #1357 put it, not into the filament. A
  1206. # Spoolman Filament has no color_name, so the route stores the user's
  1207. # choice under spool.extra.bambu_color_name and _map_spoolman_spool
  1208. # reads that first. This path never wrote it, which is why the colour
  1209. # ended up in filament.name and the product line fell out of subtype.
  1210. # _ensure_extra_fields registers the key from the payload (#2903).
  1211. if catalog_color is not None:
  1212. extra["bambu_color_name"] = json.dumps(catalog_color.name)
  1213. return await self.create_spool(
  1214. filament_id=filament_id,
  1215. remaining_weight=remaining,
  1216. comment="Created by Bambuddy",
  1217. extra=extra,
  1218. )
  1219. # No-RFID fallback: use the spool ID resolved from the local slot-assignment table.
  1220. # Never create new spools without a tag to avoid duplicates.
  1221. if spoolman_spool_id_hint is not None:
  1222. existing = next((s for s in (cached_spools or []) if s.get("id") == spoolman_spool_id_hint), None)
  1223. if existing is None:
  1224. try:
  1225. existing = await self.get_spool(spoolman_spool_id_hint)
  1226. except (SpoolmanNotFoundError, SpoolmanUnavailableError):
  1227. existing = None
  1228. if existing:
  1229. logger.info(
  1230. "Updating spool %s by slot-assignment hint (no RFID tag available)",
  1231. existing["id"],
  1232. )
  1233. return await self.update_spool(
  1234. spool_id=existing["id"],
  1235. remaining_weight=None if disable_weight_sync else remaining,
  1236. )
  1237. logger.info(
  1238. "%s AMS %s tray %s — skipping (no RFID tag and no slot-assignment hint)",
  1239. printer_name,
  1240. tray.ams_id,
  1241. tray.tray_id,
  1242. )
  1243. return None
  1244. async def _find_or_create_filament(self, tray: AMSTray, catalog_color: CatalogColor | None) -> dict | None:
  1245. """Return the Bambu Lab filament for this tray's product line, creating it if absent.
  1246. Material plus colour is not an identity. PLA Basic Black and PLA Matte
  1247. Charcoal are both PLA at ``#000000``, so a Matte roll was linked to the
  1248. Basic filament and inherited its name (#2907). The product line lives in
  1249. ``tray_sub_brands``, so the filament's name carries it -- the same place
  1250. internal mode keeps it, as the subtype _map_spoolman_spool derives from
  1251. the name. The colour name belongs to the spool (``bambu_color_name``),
  1252. which the caller writes from ``catalog_color``.
  1253. ``catalog_color`` is what the colour catalogue says about this roll, or
  1254. None when it has no row. It is resolved by the caller rather than here so
  1255. this client stays pure HTTP and the caller can store the name too.
  1256. """
  1257. bambu_vendor_id = await self.ensure_bambu_vendor()
  1258. material_upper = tray.tray_type.upper()
  1259. # Same single value as the user-driven path: the match key is the stored
  1260. # shape. That is what lets an opaque tray still find the six-character
  1261. # filaments every existing instance is full of, while a clear tray keys
  1262. # to eight and gets its own record (#2912).
  1263. color = color_match_key(tray.tray_color)
  1264. sub_brand = (tray.tray_sub_brands or "").strip()
  1265. color_name = catalog_color.name if catalog_color else None
  1266. # Search internal filaments - only match Bambu Lab vendor
  1267. same_colour = []
  1268. for filament in await self.get_filaments():
  1269. fil_vendor_id = filament.get("vendor_id") or filament.get("vendor", {}).get("id")
  1270. if fil_vendor_id != bambu_vendor_id:
  1271. continue
  1272. fil_material = filament.get("material") or ""
  1273. if fil_material.upper() == material_upper and color_match_key(filament.get("color_hex")) == color:
  1274. same_colour.append(filament)
  1275. existing = best_product_line_match(same_colour, color_name, sub_brand, tray.tray_type)
  1276. if existing is not None:
  1277. return existing
  1278. # Search external filaments (SpoolmanDB) — restrict to Bambu Lab only.
  1279. # The /api/v1/external/filament endpoint returns the full multi-vendor catalog
  1280. # with no server-side filter, so without a manufacturer check the first PLA/black
  1281. # hit is typically 3DJAKE or 3DXTECH, not Bambu Lab.
  1282. external = await self.get_external_filaments()
  1283. bambu_candidates = []
  1284. for filament in external:
  1285. manufacturer = (filament.get("manufacturer") or "").strip().lower()
  1286. ext_id = (filament.get("id") or "").strip().lower()
  1287. if manufacturer != "bambu lab" and not ext_id.startswith("bambulab_"):
  1288. continue
  1289. fil_material = filament.get("material") or ""
  1290. if fil_material.upper() == material_upper and color_match_key(filament.get("color_hex")) == color:
  1291. bambu_candidates.append(filament)
  1292. # SpoolmanDB folds the product line into the name ("Matte Charcoal",
  1293. # "Tough+ Black", "Brown Galaxy") everywhere except PLA Basic and the
  1294. # lines with a base material of their own, which get the bare colour
  1295. # ("Black"). The catalogue keeps the line in its material column and a
  1296. # bare colour in color_name. So neither side's name equals the other's
  1297. # for most lines, and the selector has to rebuild the library's spelling
  1298. # from the line and the colour -- preferring it over the bare colour,
  1299. # which several lines share at the same hex.
  1300. chosen = best_product_line_match(bambu_candidates, color_name, sub_brand, tray.tray_type)
  1301. if chosen is not None:
  1302. return await self._create_filament_from_external(chosen, tray)
  1303. # Either the catalogue has no row for this colour -- it is seeded from
  1304. # Bambu's published list and lags new releases -- or it has one and no
  1305. # external entry carries that name. Both mean the library cannot say what
  1306. # this roll is, so build it from what the printer reported rather than
  1307. # attaching to whichever candidate happened to come first.
  1308. return await self.create_filament(
  1309. name=tray.tray_sub_brands or tray.tray_type,
  1310. vendor_id=bambu_vendor_id,
  1311. material=tray.tray_type,
  1312. color_hex=color,
  1313. weight=tray.tray_weight,
  1314. )
  1315. async def _create_filament_from_external(self, external: dict, tray: AMSTray) -> dict | None:
  1316. """Create an internal Spoolman filament from an external library entry.
  1317. The entry supplies what the printer cannot: density and label weight. The
  1318. name comes from the tray, not the entry, so a filament taken from the
  1319. library is named the same way as one built from scratch and the two modes
  1320. agree on the subtype. The library's own name ("Matte Charcoal") is a
  1321. colour name with the line folded in, and the colour name is stored on the
  1322. spool instead.
  1323. """
  1324. vendor_id = await self.ensure_bambu_vendor()
  1325. return await self.create_filament(
  1326. name=tray.tray_sub_brands or external.get("name") or tray.tray_type,
  1327. vendor_id=vendor_id,
  1328. material=external.get("material", tray.tray_type),
  1329. # `or`, not a two-argument get: an entry that carries the key with an
  1330. # explicit null would hand None to create_filament rather than reach
  1331. # the tray fallback. Only a candidate when the tray colour is empty
  1332. # too, so this is a correctness tidy, not a fix for a live path.
  1333. color_hex=external.get("color_hex") or color_match_key(tray.tray_color),
  1334. weight=external.get("weight", tray.tray_weight),
  1335. density=external.get("density"),
  1336. )
  1337. # Global client instance (initialized when settings are loaded)
  1338. _spoolman_client: SpoolmanClient | None = None
  1339. async def get_spoolman_client() -> SpoolmanClient | None:
  1340. """Return the global SpoolmanClient, or None if not configured."""
  1341. return _spoolman_client
  1342. async def init_spoolman_client(url: str) -> SpoolmanClient:
  1343. """Initialise (or reinitialise) the global SpoolmanClient; raises ValueError if url fails SSRF guard."""
  1344. from backend.app.api.routes._spoolman_helpers import assert_safe_spoolman_url
  1345. assert_safe_spoolman_url(url)
  1346. global _spoolman_client
  1347. if _spoolman_client:
  1348. await _spoolman_client.close()
  1349. _spoolman_client = SpoolmanClient(url)
  1350. return _spoolman_client
  1351. async def close_spoolman_client():
  1352. """Close the global Spoolman client."""
  1353. global _spoolman_client
  1354. if _spoolman_client:
  1355. await _spoolman_client.close()
  1356. _spoolman_client = None