spoolman.py 49 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169
  1. """Spoolman integration service for syncing AMS filament data."""
  2. import asyncio
  3. import logging
  4. import weakref
  5. from dataclasses import dataclass
  6. from datetime import datetime, timezone
  7. from typing import Literal
  8. import httpx
  9. logger = logging.getLogger(__name__)
  10. BAMBU_RFID_TAG_LENGTH = 32
  11. @dataclass
  12. class SpoolmanSpool:
  13. """Represents a spool in Spoolman."""
  14. id: int
  15. filament_id: int | None
  16. remaining_weight: float | None
  17. used_weight: float
  18. first_used: str | None
  19. last_used: str | None
  20. location: str | None
  21. lot_nr: str | None
  22. comment: str | None
  23. extra: dict | None # Contains tag_uid in extra.tag
  24. @dataclass
  25. class SpoolmanFilament:
  26. """Represents a filament type in Spoolman."""
  27. id: int
  28. name: str
  29. vendor_id: int | None
  30. material: str | None
  31. color_hex: str | None
  32. weight: float | None # Net weight in grams
  33. @dataclass
  34. class AMSTray:
  35. """Represents an AMS tray with filament data from Bambu printer."""
  36. ams_id: int # 0-3 for regular AMS, 128-135 for AMS-HT, 254+ for external spool
  37. tray_id: int # 0-3
  38. tray_type: str # PLA, PETG, ABS, etc.
  39. tray_sub_brands: str # Full name like "PLA Basic", "PETG HF"
  40. tray_color: str # Hex color like "FEC600FF"
  41. remain: int # Remaining percentage (0-100)
  42. tag_uid: str # RFID tag UID
  43. tray_uuid: str # Spool UUID
  44. tray_info_idx: str # Bambu filament preset ID like "GFA00"
  45. tray_weight: int # Spool weight in grams (usually 1000)
  46. class SpoolmanNotFoundError(Exception):
  47. """Raised when a spool ID does not exist in Spoolman (HTTP 404)."""
  48. class SpoolmanUnavailableError(Exception):
  49. """Raised when Spoolman is unreachable or returns a server/network error."""
  50. class SpoolmanClientError(Exception):
  51. """Raised when Spoolman returns a 4xx client error (not 404)."""
  52. def __init__(self, message: str, status_code: int, response_text: str = "") -> None:
  53. super().__init__(message)
  54. self.status_code = status_code
  55. self.response_text = response_text
  56. def _filament_subtype_part(name: str, material: str) -> str:
  57. """Return the subtype portion of a filament name, lowercased.
  58. Mirrors the read-side derivation in
  59. ``backend/app/api/routes/_spoolman_helpers.py::_map_spoolman_spool``:
  60. if the filament name starts with the material prefix (e.g. ``"PLA Glow"``
  61. when material is ``"PLA"``), strip it; otherwise return the name as-is.
  62. Used by ``find_or_create_filament`` so that an existing filament saved by
  63. the AMS-sync path with name ``"Glow"`` still matches a user-driven edit
  64. that composes ``"PLA Glow"`` (#1357).
  65. """
  66. s = (name or "").strip()
  67. m = (material or "").strip()
  68. if m and s.upper().startswith(m.upper() + " "):
  69. return s[len(m) + 1 :].strip().lower()
  70. return s.lower()
  71. class SpoolmanClient:
  72. """Client for interacting with Spoolman API."""
  73. def __init__(self, base_url: str):
  74. """Initialize the Spoolman client."""
  75. self.base_url = base_url.rstrip("/")
  76. self.api_url = f"{self.base_url}/api/v1"
  77. self._client: httpx.AsyncClient | None = None
  78. self._connected = False
  79. # Per-spool locks for atomic read-modify-write in merge_spool_extra.
  80. # WeakValueDictionary: locks are GC'd once no coroutine holds a reference.
  81. self._extra_locks: weakref.WeakValueDictionary[int, asyncio.Lock] = weakref.WeakValueDictionary()
  82. async def _get_client(self) -> httpx.AsyncClient:
  83. """Get or create the HTTP client with connection pooling limits."""
  84. if self._client is None:
  85. self._client = httpx.AsyncClient(
  86. timeout=httpx.Timeout(connect=5.0, read=10.0, write=5.0, pool=5.0),
  87. follow_redirects=False,
  88. verify=True,
  89. limits=httpx.Limits(
  90. max_keepalive_connections=5,
  91. max_connections=10,
  92. keepalive_expiry=30.0,
  93. ),
  94. )
  95. return self._client
  96. async def close(self):
  97. """Close the HTTP client."""
  98. if self._client:
  99. await self._client.aclose()
  100. self._client = None
  101. async def health_check(self) -> bool:
  102. """Check if Spoolman server is reachable; returns True if healthy."""
  103. try:
  104. client = await self._get_client()
  105. response = await client.get(f"{self.api_url}/health")
  106. self._connected = response.status_code == 200
  107. return self._connected
  108. except Exception as e:
  109. logger.warning(
  110. "Spoolman health check failed (url=%s, type=%s): %s",
  111. self.api_url,
  112. type(e).__name__,
  113. e,
  114. )
  115. self._connected = False
  116. return False
  117. @property
  118. def is_connected(self) -> bool:
  119. """Check if client is connected to Spoolman."""
  120. return self._connected
  121. async def get_spools(self) -> list[dict]:
  122. """Fetch all spools from Spoolman with up to 3 retries on connection errors."""
  123. max_attempts = 3
  124. retry_delay = 0.5 # 500ms
  125. for attempt in range(1, max_attempts + 1):
  126. try:
  127. client = await self._get_client()
  128. response = await client.get(f"{self.api_url}/spool")
  129. response.raise_for_status()
  130. spools = response.json()
  131. if attempt > 1:
  132. logger.info("Successfully fetched %d spools on attempt %d", len(spools), attempt)
  133. return spools
  134. except (httpx.ReadError, httpx.RemoteProtocolError, httpx.ConnectError) as e:
  135. # Connection-related errors - close and recreate client for next attempt
  136. if attempt < max_attempts:
  137. logger.warning(
  138. "Connection error getting spools (attempt %d/%d): %s. Recreating client and retrying in %dms...",
  139. attempt,
  140. max_attempts,
  141. e,
  142. int(retry_delay * 1000),
  143. )
  144. # Close the stale client and recreate it
  145. await self.close()
  146. await asyncio.sleep(retry_delay)
  147. else:
  148. logger.error("Failed to get spools from Spoolman after %d attempts: %s", max_attempts, e)
  149. raise SpoolmanUnavailableError("Cannot reach Spoolman") from e
  150. except Exception as e:
  151. # Other errors (HTTP errors, JSON decode errors, etc.)
  152. if attempt < max_attempts:
  153. logger.warning(
  154. "Failed to get spools from Spoolman (attempt %d/%d): %s. Retrying in %dms...",
  155. attempt,
  156. max_attempts,
  157. e,
  158. int(retry_delay * 1000),
  159. )
  160. await asyncio.sleep(retry_delay)
  161. else:
  162. logger.error("Failed to get spools from Spoolman after %d attempts: %s", max_attempts, e)
  163. raise SpoolmanUnavailableError("Cannot reach Spoolman") from e
  164. async def _get_with_retry(self, path: str, params: dict | None = None) -> list[dict]:
  165. """GET a Spoolman JSON list endpoint with up to 3 retries on connection errors."""
  166. max_attempts = 3
  167. retry_delay = 0.5
  168. url = f"{self.api_url}/{path.lstrip('/')}"
  169. for attempt in range(1, max_attempts + 1):
  170. try:
  171. client = await self._get_client()
  172. response = await client.get(url, params=params or None)
  173. response.raise_for_status()
  174. return response.json()
  175. except (httpx.ReadError, httpx.RemoteProtocolError, httpx.ConnectError) as e:
  176. if attempt < max_attempts:
  177. logger.warning(
  178. "Connection error fetching %s (attempt %d/%d): %s. Recreating client and retrying in %dms...",
  179. path,
  180. attempt,
  181. max_attempts,
  182. e,
  183. int(retry_delay * 1000),
  184. )
  185. await self.close()
  186. await asyncio.sleep(retry_delay)
  187. else:
  188. logger.error("Failed to fetch %s from Spoolman after %d attempts: %s", path, max_attempts, e)
  189. raise SpoolmanUnavailableError("Cannot reach Spoolman") from e
  190. except Exception as e:
  191. if attempt < max_attempts:
  192. logger.warning(
  193. "Failed to fetch %s from Spoolman (attempt %d/%d): %s. Retrying in %dms...",
  194. path,
  195. attempt,
  196. max_attempts,
  197. e,
  198. int(retry_delay * 1000),
  199. )
  200. await asyncio.sleep(retry_delay)
  201. else:
  202. logger.error("Failed to fetch %s from Spoolman after %d attempts: %s", path, max_attempts, e)
  203. raise SpoolmanUnavailableError("Cannot reach Spoolman") from e
  204. async def get_filaments(self) -> list[dict]:
  205. """Fetch all internal filaments from Spoolman."""
  206. try:
  207. client = await self._get_client()
  208. response = await client.get(f"{self.api_url}/filament")
  209. response.raise_for_status()
  210. return response.json()
  211. except Exception as e:
  212. logger.error("Failed to get filaments from Spoolman: %s", e)
  213. raise SpoolmanUnavailableError("Cannot reach Spoolman") from e
  214. async def get_filament(self, filament_id: int) -> dict:
  215. """Fetch a single filament by ID from Spoolman."""
  216. if filament_id <= 0:
  217. raise ValueError(f"Invalid filament_id: {filament_id}")
  218. response = await self._request_filament("GET", filament_id, operation="get_filament")
  219. return response.json()
  220. async def get_external_filaments(self) -> list[dict]:
  221. """Fetch external/library filaments from Spoolman."""
  222. try:
  223. client = await self._get_client()
  224. response = await client.get(f"{self.api_url}/external/filament")
  225. response.raise_for_status()
  226. return response.json()
  227. except Exception as e:
  228. logger.error("Failed to get external filaments from Spoolman: %s", e)
  229. raise SpoolmanUnavailableError("Cannot reach Spoolman") from e
  230. async def get_vendors(self) -> list[dict]:
  231. """Fetch all vendors from Spoolman."""
  232. try:
  233. client = await self._get_client()
  234. response = await client.get(f"{self.api_url}/vendor")
  235. response.raise_for_status()
  236. return response.json()
  237. except Exception as e:
  238. logger.error("Failed to get vendors from Spoolman: %s", e)
  239. raise SpoolmanUnavailableError("Cannot reach Spoolman") from e
  240. async def create_vendor(self, name: str) -> dict:
  241. """Create a new vendor in Spoolman."""
  242. try:
  243. client = await self._get_client()
  244. response = await client.post(f"{self.api_url}/vendor", json={"name": name})
  245. if 400 <= response.status_code < 500:
  246. raise SpoolmanClientError(
  247. f"Spoolman rejected vendor creation (HTTP {response.status_code})",
  248. response.status_code,
  249. )
  250. response.raise_for_status()
  251. return response.json()
  252. except SpoolmanClientError:
  253. raise
  254. except Exception as e:
  255. logger.error("Failed to create vendor in Spoolman: %s", e)
  256. raise SpoolmanUnavailableError("Cannot reach Spoolman") from e
  257. def _get_material_density(self, material: str | None) -> float:
  258. """Return typical density (g/cm³) for the given filament material; defaults to PLA (1.24)."""
  259. # Typical densities for common filament materials
  260. densities = {
  261. "PLA": 1.24,
  262. "PLA-CF": 1.29,
  263. "PLA-S": 1.24,
  264. "PETG": 1.27,
  265. "ABS": 1.04,
  266. "ASA": 1.07,
  267. "TPU": 1.21,
  268. "PA": 1.14, # Nylon
  269. "PA-CF": 1.20,
  270. "PC": 1.20,
  271. "PVA": 1.23,
  272. "HIPS": 1.04,
  273. "PP": 0.90,
  274. "PET": 1.38,
  275. }
  276. if material:
  277. # Try exact match first, then uppercase
  278. mat_upper = material.upper()
  279. for key, density in densities.items():
  280. if key.upper() == mat_upper or mat_upper.startswith(key.upper()):
  281. return density
  282. return 1.24 # Default to PLA density
  283. async def create_filament(
  284. self,
  285. name: str,
  286. vendor_id: int | None = None,
  287. material: str | None = None,
  288. color_hex: str | None = None,
  289. color_name: str | None = None,
  290. weight: float | None = None,
  291. diameter: float = 1.75,
  292. density: float | None = None,
  293. ) -> dict:
  294. """Create a new filament in Spoolman."""
  295. if not name or not name.strip():
  296. raise ValueError("Filament name is required")
  297. if density is None:
  298. density = self._get_material_density(material)
  299. data: dict = {
  300. "name": name.strip(),
  301. "diameter": diameter,
  302. "density": density,
  303. }
  304. if vendor_id:
  305. data["vendor_id"] = vendor_id
  306. if material:
  307. data["material"] = material
  308. if color_hex:
  309. # Strip alpha channel if present (RRGGBBAA -> RRGGBB)
  310. color_hex = color_hex[:6] if len(color_hex) >= 6 else color_hex
  311. data["color_hex"] = color_hex
  312. if color_name:
  313. data["color_name"] = color_name
  314. if weight:
  315. data["weight"] = weight
  316. logger.debug("Creating filament in Spoolman: %s", data)
  317. try:
  318. client = await self._get_client()
  319. response = await client.post(f"{self.api_url}/filament", json=data)
  320. if 400 <= response.status_code < 500:
  321. raise SpoolmanClientError(
  322. f"Spoolman rejected filament creation (HTTP {response.status_code})",
  323. response.status_code,
  324. )
  325. response.raise_for_status()
  326. return response.json()
  327. except SpoolmanClientError:
  328. raise
  329. except Exception as e:
  330. logger.error("Failed to create filament in Spoolman: %s", e)
  331. raise SpoolmanUnavailableError("Cannot reach Spoolman") from e
  332. async def patch_filament(self, filament_id: int, data: dict) -> dict:
  333. """PATCH a filament entry in Spoolman (e.g. update name or spool_weight)."""
  334. if filament_id <= 0:
  335. raise ValueError(f"Invalid filament_id: {filament_id}")
  336. response = await self._request_filament("PATCH", filament_id, json_body=data, operation="patch_filament")
  337. return response.json()
  338. async def create_spool(
  339. self,
  340. filament_id: int,
  341. remaining_weight: float | None = None,
  342. location: str | None = None,
  343. lot_nr: str | None = None,
  344. comment: str | None = None,
  345. extra: dict | None = None,
  346. ) -> dict:
  347. """Create a new spool in Spoolman."""
  348. data: dict = {"filament_id": filament_id}
  349. if remaining_weight is not None:
  350. data["remaining_weight"] = remaining_weight
  351. if location:
  352. data["location"] = location
  353. if lot_nr:
  354. data["lot_nr"] = lot_nr
  355. if comment:
  356. data["comment"] = comment
  357. if extra:
  358. data["extra"] = extra
  359. logger.debug("Creating spool in Spoolman: %s", data)
  360. try:
  361. client = await self._get_client()
  362. response = await client.post(f"{self.api_url}/spool", json=data)
  363. if response.status_code == 404:
  364. raise SpoolmanNotFoundError(f"Filament {filament_id} not found in Spoolman")
  365. if 400 <= response.status_code < 500:
  366. raise SpoolmanClientError(
  367. f"Spoolman rejected spool creation (HTTP {response.status_code})",
  368. response.status_code,
  369. )
  370. response.raise_for_status()
  371. result = response.json()
  372. logger.info("Created spool %s in Spoolman", result.get("id"))
  373. return result
  374. except (SpoolmanNotFoundError, SpoolmanClientError):
  375. raise
  376. except Exception as e:
  377. logger.error("Failed to create spool in Spoolman: %s", e)
  378. raise SpoolmanUnavailableError("Cannot reach Spoolman") from e
  379. async def update_spool(
  380. self,
  381. spool_id: int,
  382. remaining_weight: float | None = None,
  383. location: str | None = None,
  384. clear_location: bool = False,
  385. extra: dict | None = None,
  386. ) -> dict:
  387. """Update an existing spool in Spoolman, always setting last_used."""
  388. data: dict = {}
  389. if remaining_weight is not None:
  390. data["remaining_weight"] = remaining_weight
  391. if clear_location:
  392. data["location"] = None
  393. elif location:
  394. data["location"] = location
  395. if extra:
  396. data["extra"] = extra
  397. data["last_used"] = datetime.now(timezone.utc).isoformat()
  398. response = await self._request_spool("PATCH", spool_id, json_body=data, operation="update")
  399. return response.json()
  400. async def _request_spool(
  401. self,
  402. method: Literal["GET", "PATCH", "DELETE"],
  403. spool_id: int,
  404. *,
  405. json_body: dict | None = None,
  406. operation: str,
  407. ) -> httpx.Response:
  408. """Perform a spool-scoped HTTP request, translating 404 and errors to named exceptions."""
  409. try:
  410. client = await self._get_client()
  411. response = await client.request(
  412. method,
  413. f"{self.api_url}/spool/{spool_id}",
  414. json=json_body,
  415. )
  416. if response.status_code == 404:
  417. raise SpoolmanNotFoundError(f"Spool {spool_id} not found in Spoolman")
  418. response.raise_for_status()
  419. return response
  420. except SpoolmanNotFoundError:
  421. raise
  422. except httpx.HTTPStatusError as e:
  423. if 400 <= e.response.status_code < 500:
  424. logger.warning(
  425. "Spoolman returned %d for %s spool %s",
  426. e.response.status_code,
  427. operation,
  428. spool_id,
  429. )
  430. raise SpoolmanClientError(
  431. f"Spoolman rejected {operation} for spool {spool_id} (HTTP {e.response.status_code})",
  432. e.response.status_code,
  433. e.response.text[:500],
  434. ) from e
  435. else:
  436. logger.error("Failed to %s spool %s in Spoolman: %s", operation, spool_id, e)
  437. raise SpoolmanUnavailableError(f"Failed to {operation} spool {spool_id}") from e
  438. except Exception as e:
  439. logger.error("Failed to %s spool %s in Spoolman: %s", operation, spool_id, e)
  440. raise SpoolmanUnavailableError(f"Failed to {operation} spool {spool_id}") from e
  441. async def _request_filament(
  442. self,
  443. method: Literal["GET", "PATCH"],
  444. filament_id: int,
  445. *,
  446. json_body: dict | None = None,
  447. operation: str,
  448. ) -> httpx.Response:
  449. """Perform a filament-scoped HTTP request, translating 404 and errors to named exceptions."""
  450. try:
  451. client = await self._get_client()
  452. response = await client.request(
  453. method,
  454. f"{self.api_url}/filament/{filament_id}",
  455. json=json_body,
  456. )
  457. if response.status_code == 404:
  458. raise SpoolmanNotFoundError(f"Filament {filament_id} not found in Spoolman")
  459. response.raise_for_status()
  460. return response
  461. except SpoolmanNotFoundError:
  462. raise
  463. except httpx.HTTPStatusError as e:
  464. if 400 <= e.response.status_code < 500:
  465. logger.warning(
  466. "Spoolman returned %d for %s filament %s",
  467. e.response.status_code,
  468. operation,
  469. filament_id,
  470. )
  471. raise SpoolmanClientError(
  472. f"Spoolman rejected {operation} for filament {filament_id} (HTTP {e.response.status_code})",
  473. e.response.status_code,
  474. e.response.text[:500],
  475. ) from e
  476. else:
  477. logger.error("Failed to %s filament %s in Spoolman: %s", operation, filament_id, e)
  478. raise SpoolmanUnavailableError(f"Failed to {operation} filament {filament_id}") from e
  479. except Exception as e:
  480. logger.error("Failed to %s filament %s in Spoolman: %s", operation, filament_id, e)
  481. raise SpoolmanUnavailableError(f"Failed to {operation} filament {filament_id}") from e
  482. async def get_spool(self, spool_id: int) -> dict:
  483. """Fetch a single spool by ID from Spoolman."""
  484. response = await self._request_spool("GET", spool_id, operation="get")
  485. return response.json()
  486. async def get_all_spools(self, allow_archived: bool = False) -> list[dict]:
  487. """Fetch all spools from Spoolman with retry, optionally including archived ones."""
  488. params: dict = {}
  489. if allow_archived:
  490. params["allow_archived"] = "true"
  491. return await self._get_with_retry("/spool", params=params or None)
  492. async def delete_spool(self, spool_id: int) -> None:
  493. """Delete a spool from Spoolman."""
  494. await self._request_spool("DELETE", spool_id, operation="delete")
  495. async def set_spool_archived(self, spool_id: int, archived: bool) -> dict:
  496. """Archive or restore a spool in Spoolman."""
  497. response = await self._request_spool(
  498. "PATCH",
  499. spool_id,
  500. json_body={"archived": archived},
  501. operation="archive/restore",
  502. )
  503. return response.json()
  504. async def update_spool_full(
  505. self,
  506. spool_id: int,
  507. *,
  508. filament_id: int | None = None,
  509. remaining_weight: float | None = None,
  510. comment: str | None = None,
  511. price: float | None = None,
  512. location: str | None = None,
  513. clear_location: bool = False,
  514. extra: dict | None = None,
  515. spool_weight: float | None = None,
  516. clear_spool_weight: bool = False,
  517. ) -> dict:
  518. """Update a spool with full field support; unlike update_spool, does not auto-set last_used."""
  519. data: dict = {}
  520. if filament_id is not None:
  521. data["filament_id"] = filament_id
  522. if remaining_weight is not None:
  523. data["remaining_weight"] = remaining_weight
  524. if comment is not None:
  525. data["comment"] = comment if comment else None
  526. if price is not None:
  527. data["price"] = price
  528. if clear_location:
  529. data["location"] = None
  530. elif location is not None:
  531. data["location"] = location
  532. if extra is not None:
  533. data["extra"] = extra
  534. if clear_spool_weight:
  535. data["spool_weight"] = None
  536. elif spool_weight is not None:
  537. data["spool_weight"] = spool_weight
  538. response = await self._request_spool("PATCH", spool_id, json_body=data, operation="update")
  539. return response.json()
  540. def extra_lock(self, spool_id: int) -> asyncio.Lock:
  541. """Return (creating if needed) the per-spool asyncio.Lock used by merge_spool_extra."""
  542. lock = self._extra_locks.get(spool_id)
  543. if lock is None:
  544. lock = asyncio.Lock()
  545. self._extra_locks[spool_id] = lock
  546. return lock
  547. async def merge_spool_extra(self, spool_id: int, new_fields: dict) -> dict:
  548. """Fetch the spool's extra dict, merge new_fields into it, then PATCH back — serialised per spool."""
  549. async with self.extra_lock(spool_id):
  550. current = await self.get_spool(spool_id) # raises on error
  551. current_extra: dict = current.get("extra") or {}
  552. merged = {**current_extra, **new_fields}
  553. return await self.update_spool_full(spool_id=spool_id, extra=merged)
  554. async def find_or_create_vendor(self, name: str) -> int:
  555. """Return the Spoolman vendor ID for the given name, creating the vendor if absent."""
  556. vendors = await self.get_vendors()
  557. name_lower = name.strip().lower()
  558. for vendor in vendors:
  559. if vendor.get("name", "").strip().lower() == name_lower:
  560. return vendor["id"]
  561. created = await self.create_vendor(name.strip())
  562. vendor_id = created.get("id")
  563. if not vendor_id:
  564. raise SpoolmanUnavailableError(f"Spoolman returned vendor without id field: {list(created.keys())}")
  565. return vendor_id
  566. async def find_or_create_filament(
  567. self,
  568. material: str,
  569. subtype: str,
  570. brand: str | None,
  571. color_hex: str,
  572. label_weight: int,
  573. color_name: str | None = None,
  574. ) -> int:
  575. """Return the filament ID matching material/name/brand/color, creating it if absent."""
  576. name = f"{material} {subtype}".strip() if subtype else material
  577. color = color_hex[:6].upper() if len(color_hex) >= 6 else color_hex.upper()
  578. vendor_id: int | None = None
  579. if brand:
  580. vendor_id = await self.find_or_create_vendor(brand)
  581. # Normalised match keys (case-insensitive). Computed once outside the
  582. # loop so the inner comparison stays simple.
  583. composed_subtype = _filament_subtype_part(name, material)
  584. material_norm = material.upper()
  585. brand_norm = (brand or "").strip().lower()
  586. filaments = await self.get_filaments()
  587. for f in filaments:
  588. f_material = (f.get("material") or "").upper()
  589. f_color = (f.get("color_hex") or "").upper()[:6]
  590. f_vendor = f.get("vendor") or {}
  591. f_vendor_name = (f_vendor.get("name") or "").strip().lower()
  592. material_match = f_material == material_norm
  593. # Match on the subtype portion of the filament name. AMS-sync
  594. # auto-create (the underscore-prefixed `_find_or_create_filament`
  595. # used during MQTT tray import) stores the filament as just
  596. # ``tray.tray_sub_brands`` — e.g. ``"Glow"`` — while the
  597. # user-driven edit path here composes ``"<material> <subtype>"``
  598. # — ``"PLA Glow"``. The old literal equality `f_name == name`
  599. # failed to bridge the two shapes, so every edit fell through to
  600. # `create_filament`, leaving a trail of duplicate filaments AND
  601. # leaving the spool either still pointed at the old filament
  602. # whose `color_name` never got patched, or pointed at a new
  603. # filament with the colour while the inventory list kept
  604. # showing the synth fallback from the old one (#1357).
  605. f_subtype_part = _filament_subtype_part(f.get("name") or "", material)
  606. name_match = f_subtype_part == composed_subtype
  607. color_match = f_color == color
  608. vendor_match = (not brand) or f_vendor_name == brand_norm
  609. if material_match and name_match and color_match and vendor_match:
  610. # color_name is intentionally not part of the match key and
  611. # is no longer patched onto the filament here: Spoolman 0.23.1
  612. # has no `color_name` field on Filament (#1357 — confirmed
  613. # against the FilamentUpdateParameters schema). The earlier
  614. # #1319 fix tried to patch it and Spoolman silently dropped
  615. # the key, which is exactly why the user's edit looked "not
  616. # saved". The route now persists color_name via
  617. # spool.extra.bambu_color_name (see _map_spoolman_spool for
  618. # the read side); find_or_create_filament's only job is to
  619. # resolve the right filament_id for the spool link.
  620. return f["id"]
  621. # color_name omitted: Spoolman has no such field on Filament (#1357);
  622. # the user's color_name lands in spool.extra.bambu_color_name via the
  623. # route after find_or_create_filament returns the new id.
  624. filament = await self.create_filament(
  625. name=name,
  626. vendor_id=vendor_id,
  627. material=material,
  628. color_hex=color,
  629. weight=float(label_weight),
  630. )
  631. filament_id = filament.get("id")
  632. if not filament_id:
  633. raise SpoolmanUnavailableError(f"Spoolman returned filament without id field: {list(filament.keys())}")
  634. return filament_id
  635. async def use_spool(self, spool_id: int, used_weight: float) -> dict:
  636. """Record filament usage for a spool via the Spoolman /use endpoint."""
  637. try:
  638. client = await self._get_client()
  639. response = await client.put(
  640. f"{self.api_url}/spool/{spool_id}/use",
  641. json={"use_weight": used_weight},
  642. )
  643. if response.status_code == 404:
  644. raise SpoolmanNotFoundError(f"Spool {spool_id} not found in Spoolman")
  645. if 400 <= response.status_code < 500:
  646. raise SpoolmanClientError(
  647. f"Spoolman rejected use_spool for spool {spool_id} (HTTP {response.status_code})",
  648. response.status_code,
  649. )
  650. response.raise_for_status()
  651. return response.json()
  652. except (SpoolmanNotFoundError, SpoolmanClientError):
  653. raise
  654. except Exception as e:
  655. logger.error("Failed to record spool usage in Spoolman: %s", e)
  656. raise SpoolmanUnavailableError(f"Failed to record usage for spool {spool_id}") from e
  657. async def find_spool_by_tag(self, tag_uid: str, cached_spools: list[dict] | None = None) -> dict | None:
  658. """Return the spool matching the given RFID tag UID, or None if not found."""
  659. # Use cached spools if provided, otherwise fetch from API
  660. spools = cached_spools if cached_spools is not None else await self.get_spools()
  661. # Normalize tag_uid for comparison (uppercase, strip quotes)
  662. search_tag = tag_uid.strip('"').upper()
  663. for spool in spools:
  664. extra = spool.get("extra", {})
  665. if extra:
  666. stored_tag = extra.get("tag", "")
  667. # Normalize stored tag (strip quotes, uppercase)
  668. if stored_tag:
  669. normalized_tag = stored_tag.strip('"').upper()
  670. if normalized_tag == search_tag:
  671. logger.debug("Found spool %s matching tag %s", spool["id"], tag_uid)
  672. return spool
  673. return None
  674. def _find_spool_by_location(self, location: str, cached_spools: list[dict] | None) -> dict | None:
  675. """Return the spool at the exact location string, or None; fallback when RFID is unavailable."""
  676. if not cached_spools:
  677. return None
  678. for spool in cached_spools:
  679. if spool.get("location") == location:
  680. return spool
  681. return None
  682. async def find_spools_by_location_prefix(
  683. self, location_prefix: str, cached_spools: list[dict] | None = None
  684. ) -> list[dict]:
  685. """Return all spools whose location starts with location_prefix."""
  686. # Use cached spools if provided, otherwise fetch from API
  687. spools = cached_spools if cached_spools is not None else await self.get_spools()
  688. matching = []
  689. for spool in spools:
  690. location = spool.get("location", "")
  691. if location and location.startswith(location_prefix):
  692. matching.append(spool)
  693. return matching
  694. async def clear_location_for_removed_spools(
  695. self,
  696. printer_name: str,
  697. current_tray_uuids: set[str],
  698. cached_spools: list[dict] | None = None,
  699. synced_spool_ids: set[int] | None = None,
  700. ) -> int:
  701. """Clear location for Bambu Lab spools at this printer whose tray_uuid is no longer in the AMS."""
  702. location_prefix = f"{printer_name} - "
  703. spools_at_printer = await self.find_spools_by_location_prefix(location_prefix, cached_spools=cached_spools)
  704. cleared_count = 0
  705. for spool in spools_at_printer:
  706. spool_id = spool.get("id")
  707. # Skip spools that were just synced (matched by location or tag)
  708. if synced_spool_ids and spool_id in synced_spool_ids:
  709. continue
  710. # Get the tray_uuid (stored as "tag" in extra field)
  711. extra = spool.get("extra", {}) or {}
  712. stored_tag = extra.get("tag", "")
  713. if stored_tag:
  714. # Normalize: strip quotes and uppercase
  715. spool_uuid = stored_tag.strip('"').upper()
  716. else:
  717. spool_uuid = ""
  718. # Only clear location for Bambu Lab spools (those with a stored 32-character RFID tag).
  719. if len(spool_uuid) != BAMBU_RFID_TAG_LENGTH:
  720. continue
  721. # If this spool's UUID is not in the current AMS, clear its location
  722. if spool_uuid not in current_tray_uuids:
  723. logger.info(
  724. f"Clearing location for spool {spool_id} "
  725. f"(was: {spool.get('location')}, uuid: {spool_uuid[:16] if spool_uuid else 'none'}...)"
  726. )
  727. result = await self.update_spool(spool_id=spool_id, clear_location=True)
  728. if result:
  729. cleared_count += 1
  730. return cleared_count
  731. async def ensure_bambu_vendor(self) -> int | None:
  732. """Return the Bambu Lab vendor ID in Spoolman, creating the vendor if absent."""
  733. vendors = await self.get_vendors()
  734. for vendor in vendors:
  735. if vendor.get("name", "").lower() == "bambu lab":
  736. return vendor["id"]
  737. # Create Bambu Lab vendor if not exists
  738. vendor = await self.create_vendor("Bambu Lab")
  739. return vendor["id"] if vendor else None
  740. async def ensure_tag_extra_field(self) -> bool:
  741. """Register the 'tag' extra field in Spoolman if not present; returns True on success."""
  742. return await self.ensure_extra_field("tag")
  743. async def ensure_extra_field(self, name: str, field_type: str = "text") -> bool:
  744. """Register a custom extra field in Spoolman if not present.
  745. Spoolman rejects PATCH requests that include unknown extra-dict keys
  746. with HTTP 400 ('Unknown extra field <name>.'), so any custom field
  747. Bambuddy persists alongside spools needs to be pre-registered.
  748. Idempotent — returns True if the field already exists.
  749. """
  750. try:
  751. client = await self._get_client()
  752. # Check if field already exists
  753. response = await client.get(f"{self.api_url}/field/spool/{name}")
  754. if response.status_code == 200:
  755. logger.debug("Spoolman extra field %r already exists", name)
  756. return True
  757. # Field doesn't exist - create it
  758. field_data = {
  759. "name": name,
  760. "field_type": field_type,
  761. "default_value": None,
  762. }
  763. response = await client.post(f"{self.api_url}/field/spool/{name}", json=field_data)
  764. if response.status_code in (200, 201):
  765. logger.info("Created Spoolman extra field %r", name)
  766. return True
  767. logger.warning(
  768. "Failed to create Spoolman extra field %r: %s - %s",
  769. name,
  770. response.status_code,
  771. response.text,
  772. )
  773. return False
  774. except Exception as e:
  775. logger.warning("Failed to ensure Spoolman extra field %r exists: %s", name, e)
  776. return False
  777. def parse_ams_tray(self, ams_id: int, tray_data: dict) -> AMSTray | None:
  778. """Parse raw MQTT tray data into an AMSTray; returns None for empty or invalid trays."""
  779. # Skip empty trays - check for valid tray_type
  780. tray_type = tray_data.get("tray_type", "")
  781. if not tray_type or tray_type.strip() == "":
  782. return None
  783. # Need valid color to create filament
  784. tray_color = tray_data.get("tray_color", "")
  785. if not tray_color or tray_color.strip() == "":
  786. logger.debug("Skipping tray with empty color")
  787. return None
  788. # Handle transparent/natural filament (RRGGBBAA with alpha=00)
  789. # Replace with cream color that represents how natural PLA actually looks
  790. if tray_color == "00000000":
  791. tray_color = "F5E6D3FF" # Light cream/natural color
  792. # Get sub_brands, falling back to tray_type
  793. tray_sub_brands = tray_data.get("tray_sub_brands", "")
  794. if not tray_sub_brands or tray_sub_brands.strip() == "":
  795. tray_sub_brands = tray_type
  796. # Get tag_uid and tray_uuid, filtering out empty/invalid values
  797. tag_uid = tray_data.get("tag_uid", "")
  798. if tag_uid in ("", "0000000000000000"):
  799. tag_uid = ""
  800. tray_uuid = tray_data.get("tray_uuid", "")
  801. if tray_uuid in ("", "00000000000000000000000000000000"):
  802. tray_uuid = ""
  803. # Get tray_info_idx (Bambu filament preset ID like "GFA00")
  804. tray_info_idx = tray_data.get("tray_info_idx", "") or ""
  805. # Get remaining percentage (-1 means unknown/not read by AMS)
  806. remain = int(tray_data.get("remain", -1))
  807. return AMSTray(
  808. ams_id=ams_id,
  809. tray_id=int(tray_data.get("id", 0)),
  810. tray_type=tray_type.strip(),
  811. tray_sub_brands=tray_sub_brands.strip(),
  812. tray_color=tray_color,
  813. remain=remain,
  814. tag_uid=tag_uid,
  815. tray_uuid=tray_uuid,
  816. tray_info_idx=tray_info_idx.strip(),
  817. tray_weight=int(tray_data.get("tray_weight", 1000)),
  818. )
  819. def convert_ams_slot_to_location(self, ams_id: int, tray_id: int) -> str:
  820. """Return a human-readable location string (e.g. "AMS A1") for the given AMS slot."""
  821. if ams_id >= 254:
  822. return "External Spool"
  823. if 128 <= ams_id <= 135:
  824. # AMS-HT units use IDs 128-135
  825. ht_letter = chr(ord("A") + (ams_id - 128))
  826. return f"AMS-HT {ht_letter}{tray_id + 1}"
  827. ams_letter = chr(ord("A") + ams_id)
  828. return f"AMS {ams_letter}{tray_id + 1}"
  829. def is_bambu_lab_spool(self, tray_uuid: str, tag_uid: str = "", tray_info_idx: str = "") -> bool:
  830. """Return True if tray_uuid or tag_uid identifies a Bambu Lab spool; tray_info_idx is ignored."""
  831. # Check tray_uuid (preferred - consistent across printer models)
  832. if tray_uuid:
  833. uuid = tray_uuid.strip()
  834. if len(uuid) == 32 and uuid != "00000000000000000000000000000000":
  835. try:
  836. int(uuid, 16)
  837. return True
  838. except ValueError:
  839. pass
  840. # Fallback: check tag_uid (RFID tag - varies between printer readers)
  841. # Bambu Lab RFID tags are 16 hex characters (8 bytes)
  842. if tag_uid:
  843. tag = tag_uid.strip()
  844. if len(tag) == 16 and tag != "0000000000000000":
  845. try:
  846. int(tag, 16)
  847. logger.debug("Identified Bambu Lab spool via tag_uid fallback: %s", tag)
  848. return True
  849. except ValueError:
  850. pass
  851. return False
  852. def calculate_remaining_weight(self, remain_percent: int, spool_weight: int) -> float:
  853. """Return remaining filament weight in grams given a percentage and total spool weight."""
  854. return (remain_percent / 100.0) * spool_weight
  855. async def sync_ams_tray(
  856. self,
  857. tray: AMSTray,
  858. printer_name: str,
  859. disable_weight_sync: bool = False,
  860. cached_spools: list[dict] | None = None,
  861. inventory_remaining: float | None = None,
  862. spoolman_spool_id_hint: int | None = None,
  863. ) -> dict | None:
  864. """Sync one AMS tray to Spoolman; creates the spool on first sight, updates weight otherwise."""
  865. logger.debug(
  866. f"Processing {printer_name} AMS {tray.ams_id} tray {tray.tray_id}: "
  867. f"type={tray.tray_type}, idx={tray.tray_info_idx or 'none'}, "
  868. f"uuid={tray.tray_uuid[:16] if tray.tray_uuid else 'none'}, "
  869. f"tag={tray.tag_uid[:8] if tray.tag_uid else 'none'}..."
  870. )
  871. # Determine which identifier to use for Spoolman (prefer tray_uuid, fallback to tag_uid)
  872. # Zero-filled values mean the AMS hasn't read the RFID tag — treat as no tag
  873. zero_uuid = "00000000000000000000000000000000"
  874. zero_tag = "0000000000000000"
  875. spool_tag = None
  876. if tray.tray_uuid and tray.tray_uuid != zero_uuid:
  877. spool_tag = tray.tray_uuid
  878. elif tray.tag_uid and tray.tag_uid != zero_tag:
  879. spool_tag = tray.tag_uid
  880. # Calculate remaining weight
  881. # Primary: AMS MQTT data (remain percentage + tray_weight)
  882. # Fallback: Built-in inventory tracked weight (when firmware sends invalid remain/tray_weight)
  883. if tray.remain >= 0 and tray.tray_weight > 0:
  884. remaining = self.calculate_remaining_weight(tray.remain, tray.tray_weight)
  885. elif inventory_remaining is not None:
  886. remaining = inventory_remaining
  887. logger.debug(
  888. "Using inventory weight fallback for %s AMS %s tray %s: %.1fg",
  889. printer_name,
  890. tray.ams_id,
  891. tray.tray_id,
  892. remaining,
  893. )
  894. else:
  895. remaining = None
  896. if spool_tag:
  897. # Primary path: match by RFID tag
  898. existing = await self.find_spool_by_tag(spool_tag, cached_spools=cached_spools)
  899. if existing:
  900. logger.info("Updating existing spool %s for tag %s...", existing["id"], spool_tag[:16])
  901. return await self.update_spool(
  902. spool_id=existing["id"],
  903. remaining_weight=None if disable_weight_sync else remaining,
  904. )
  905. # Spool not found by tag - auto-create it
  906. logger.info("Creating new spool in Spoolman for %s (tag: %s...)", tray.tray_sub_brands, spool_tag[:16])
  907. if self.is_bambu_lab_spool(tray.tray_uuid, tray.tag_uid, tray.tray_info_idx):
  908. filament = await self._find_or_create_filament(tray)
  909. filament_id = filament["id"] if filament else None
  910. else:
  911. # Non-BL spool with custom RFID: use generic vendor lookup
  912. brand = tray.tray_sub_brands if tray.tray_sub_brands != tray.tray_type else None
  913. try:
  914. filament_id = await self.find_or_create_filament(
  915. material=tray.tray_type,
  916. subtype="",
  917. brand=brand,
  918. color_hex=tray.tray_color[:6],
  919. label_weight=tray.tray_weight,
  920. )
  921. except (SpoolmanNotFoundError, SpoolmanUnavailableError, SpoolmanClientError):
  922. logger.warning("Could not find or create filament for non-BL spool %s", tray.tray_sub_brands)
  923. return None
  924. if not filament_id:
  925. logger.error("Failed to find or create filament for %s", tray.tray_sub_brands)
  926. return None
  927. import json
  928. return await self.create_spool(
  929. filament_id=filament_id,
  930. remaining_weight=remaining,
  931. comment="Created by Bambuddy",
  932. extra={"tag": json.dumps(spool_tag)},
  933. )
  934. # No-RFID fallback: use the spool ID resolved from the local slot-assignment table.
  935. # Never create new spools without a tag to avoid duplicates.
  936. if spoolman_spool_id_hint is not None:
  937. existing = next((s for s in (cached_spools or []) if s.get("id") == spoolman_spool_id_hint), None)
  938. if existing is None:
  939. try:
  940. existing = await self.get_spool(spoolman_spool_id_hint)
  941. except (SpoolmanNotFoundError, SpoolmanUnavailableError):
  942. existing = None
  943. if existing:
  944. logger.info(
  945. "Updating spool %s by slot-assignment hint (no RFID tag available)",
  946. existing["id"],
  947. )
  948. return await self.update_spool(
  949. spool_id=existing["id"],
  950. remaining_weight=None if disable_weight_sync else remaining,
  951. )
  952. logger.info(
  953. "%s AMS %s tray %s — skipping (no RFID tag and no slot-assignment hint)",
  954. printer_name,
  955. tray.ams_id,
  956. tray.tray_id,
  957. )
  958. return None
  959. async def _find_or_create_filament(self, tray: AMSTray) -> dict | None:
  960. """Return a Bambu Lab filament matching the tray's material/color, creating it if absent."""
  961. bambu_vendor_id = await self.ensure_bambu_vendor()
  962. color_hex = tray.tray_color[:6] # Strip alpha channel
  963. material_upper = tray.tray_type.upper()
  964. color_upper = color_hex.upper()
  965. # Search internal filaments - only match Bambu Lab vendor
  966. filaments = await self.get_filaments()
  967. for filament in filaments:
  968. fil_vendor_id = filament.get("vendor_id") or filament.get("vendor", {}).get("id")
  969. if fil_vendor_id != bambu_vendor_id:
  970. continue
  971. fil_material = filament.get("material") or ""
  972. fil_color = filament.get("color_hex") or ""
  973. if fil_material.upper() == material_upper and fil_color.upper() == color_upper:
  974. return filament
  975. # Search external filaments (SpoolmanDB) — restrict to Bambu Lab only.
  976. # The /api/v1/external/filament endpoint returns the full multi-vendor catalog
  977. # with no server-side filter, so without a manufacturer check the first PLA/black
  978. # hit is typically 3DJAKE or 3DXTECH, not Bambu Lab.
  979. external = await self.get_external_filaments()
  980. sub_brand = (tray.tray_sub_brands or "").strip().lower()
  981. bambu_candidates = []
  982. for filament in external:
  983. manufacturer = (filament.get("manufacturer") or "").strip().lower()
  984. ext_id = (filament.get("id") or "").strip().lower()
  985. if manufacturer != "bambu lab" and not ext_id.startswith("bambulab_"):
  986. continue
  987. fil_material = filament.get("material") or ""
  988. fil_color = filament.get("color_hex") or ""
  989. if fil_material.upper() == material_upper and fil_color.upper() == color_upper:
  990. bambu_candidates.append(filament)
  991. if bambu_candidates:
  992. # Prefer the entry whose `name` matches the AMS `tray_sub_brands`
  993. # (e.g. "PLA Basic", "Support for PLA/PETG Black") so the more specific
  994. # variant wins over a generic "Black" entry when both are present.
  995. chosen = next(
  996. (f for f in bambu_candidates if (f.get("name") or "").strip().lower() == sub_brand),
  997. bambu_candidates[0],
  998. )
  999. return await self._create_filament_from_external(chosen, tray)
  1000. # Not found in either source - create a new Bambu Lab filament from scratch.
  1001. return await self.create_filament(
  1002. name=tray.tray_sub_brands or tray.tray_type,
  1003. vendor_id=bambu_vendor_id,
  1004. material=tray.tray_type,
  1005. color_hex=color_hex,
  1006. weight=tray.tray_weight,
  1007. )
  1008. async def _create_filament_from_external(self, external: dict, tray: AMSTray) -> dict | None:
  1009. """Create an internal Spoolman filament from an external library entry."""
  1010. vendor_id = await self.ensure_bambu_vendor()
  1011. return await self.create_filament(
  1012. name=external.get("name", tray.tray_sub_brands),
  1013. vendor_id=vendor_id,
  1014. material=external.get("material", tray.tray_type),
  1015. color_hex=external.get("color_hex", tray.tray_color[:6]),
  1016. weight=external.get("weight", tray.tray_weight),
  1017. density=external.get("density"),
  1018. )
  1019. # Global client instance (initialized when settings are loaded)
  1020. _spoolman_client: SpoolmanClient | None = None
  1021. async def get_spoolman_client() -> SpoolmanClient | None:
  1022. """Return the global SpoolmanClient, or None if not configured."""
  1023. return _spoolman_client
  1024. async def init_spoolman_client(url: str) -> SpoolmanClient:
  1025. """Initialise (or reinitialise) the global SpoolmanClient; raises ValueError if url fails SSRF guard."""
  1026. from backend.app.api.routes._spoolman_helpers import assert_safe_spoolman_url
  1027. assert_safe_spoolman_url(url)
  1028. global _spoolman_client
  1029. if _spoolman_client:
  1030. await _spoolman_client.close()
  1031. _spoolman_client = SpoolmanClient(url)
  1032. return _spoolman_client
  1033. async def close_spoolman_client():
  1034. """Close the global Spoolman client."""
  1035. global _spoolman_client
  1036. if _spoolman_client:
  1037. await _spoolman_client.close()
  1038. _spoolman_client = None