spoolman.py 65 KB

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