spoolman_inventory.py 74 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760
  1. """Spoolman inventory proxy endpoints.
  2. Translates between Spoolman's data model and Bambuddy's internal
  3. InventorySpool format so the frontend can use a single unified inventory UI
  4. regardless of whether data comes from the local database or Spoolman.
  5. """
  6. from __future__ import annotations
  7. import asyncio
  8. import json
  9. import logging
  10. import re
  11. import time
  12. from contextlib import asynccontextmanager
  13. from fastapi import APIRouter, Body, Depends, HTTPException, Path, Query, Response
  14. from fastapi.responses import JSONResponse
  15. from pydantic import BaseModel, Field, field_validator, model_validator
  16. from sqlalchemy import delete, select, text
  17. from sqlalchemy.exc import IntegrityError
  18. from sqlalchemy.ext.asyncio import AsyncSession
  19. from sqlalchemy.orm import selectinload
  20. from backend.app.api.routes._spoolman_helpers import (
  21. NormalizedFilament,
  22. NormalizedVendorRef,
  23. _map_spoolman_spool,
  24. _safe_float,
  25. _safe_int,
  26. _safe_optional_float,
  27. assert_safe_spoolman_url,
  28. )
  29. from backend.app.core.auth import RequirePermissionIfAuthEnabled
  30. from backend.app.core.database import get_db
  31. from backend.app.core.permissions import Permission
  32. from backend.app.models.ams_label import AmsLabel
  33. from backend.app.models.printer import Printer
  34. from backend.app.models.settings import Settings
  35. from backend.app.models.spoolman_k_profile import SpoolmanKProfile
  36. from backend.app.models.spoolman_slot_assignment import SpoolmanSlotAssignment
  37. from backend.app.models.user import User
  38. from backend.app.schemas.spool import SpoolKProfileBase
  39. from backend.app.schemas.spoolman import SpoolmanFilamentPatch, SpoolmanSlotAssignmentEnriched
  40. from backend.app.services.printer_manager import printer_manager
  41. from backend.app.services.spoolman import (
  42. SpoolmanClient,
  43. SpoolmanClientError,
  44. SpoolmanNotFoundError,
  45. SpoolmanUnavailableError,
  46. get_spoolman_client,
  47. init_spoolman_client,
  48. )
  49. from backend.app.services.spoolman_tracking import get_fallback_spool_tag_for_slot
  50. from backend.app.utils.filament_ids import (
  51. GENERIC_FILAMENT_IDS,
  52. MATERIAL_TEMPS,
  53. normalize_slicer_filament,
  54. )
  55. logger = logging.getLogger(__name__)
  56. router = APIRouter(prefix="/spoolman/inventory", tags=["spoolman-inventory"])
  57. # Cache the last successful health-check timestamp to avoid a round-trip on
  58. # every request. A failed check clears the cache immediately.
  59. _health_check_cache: dict[str, float] = {}
  60. _HEALTH_CHECK_TTL = 30.0 # seconds
  61. def _tag_cleared(val: str | None) -> bool:
  62. """Return True when a PATCH field explicitly removes a tag (null)."""
  63. return val is None
  64. async def _clear_stale_tag_links(
  65. client: SpoolmanClient,
  66. *,
  67. tag: str,
  68. keep_spool_id: int,
  69. log_context: str,
  70. ) -> int:
  71. """Clear extra.tag on OTHER spools still claiming the given tag (#1457).
  72. A given AMS slot tag — whether a real RFID (tray_uuid/tag_uid) or the
  73. deterministic fallback derived from (printer_serial, ams_id, tray_id) for
  74. non-RFID slots — uniquely identifies one physical slot. When a spool is
  75. (re)bound to that slot via Assign or Link, any other Spoolman spool whose
  76. extra.tag still holds the same value is stale and would resurface in the
  77. hover card / fill-level lookup.
  78. Best-effort: per-spool patch failures are logged and skipped, never raised.
  79. Returns the number of spools cleared.
  80. """
  81. if not tag:
  82. return 0
  83. tag_upper = tag.upper()
  84. try:
  85. spools = await client.get_spools()
  86. except (SpoolmanClientError, SpoolmanUnavailableError) as exc:
  87. logger.warning("Could not enumerate spools for stale-tag cleanup: %s", exc)
  88. return 0
  89. cleared = 0
  90. for spool in spools:
  91. spool_id = spool.get("id")
  92. if not spool_id or spool_id == keep_spool_id:
  93. continue
  94. extra = spool.get("extra") or {}
  95. raw_tag = extra.get("tag", "")
  96. if not raw_tag:
  97. continue
  98. clean_tag = raw_tag.strip('"').upper()
  99. if clean_tag != tag_upper:
  100. continue
  101. try:
  102. await client.merge_spool_extra(spool_id, {"tag": json.dumps("")})
  103. cleared += 1
  104. logger.info(
  105. "Cleared stale tag '%s' from Spoolman spool %s (%s; reassigned to spool %s)",
  106. tag_upper[:16],
  107. spool_id,
  108. log_context,
  109. keep_spool_id,
  110. )
  111. except (SpoolmanClientError, SpoolmanUnavailableError, SpoolmanNotFoundError) as exc:
  112. logger.warning(
  113. "Failed to clear stale tag on Spoolman spool %s: %s",
  114. spool_id,
  115. exc,
  116. )
  117. return cleared
  118. async def _clear_stale_slot_fallback_tag_links(
  119. client: SpoolmanClient,
  120. *,
  121. printer_serial: str,
  122. ams_id: int,
  123. tray_id: int,
  124. keep_spool_id: int,
  125. ) -> int:
  126. """Convenience wrapper: compute the slot's fallback tag and clear it from
  127. other spools. Used by the assign route, which identifies the slot by
  128. (printer, ams, tray) rather than by an explicit tag value.
  129. """
  130. fallback_tag = get_fallback_spool_tag_for_slot(printer_serial, ams_id, tray_id)
  131. if not fallback_tag:
  132. return 0
  133. return await _clear_stale_tag_links(
  134. client,
  135. tag=fallback_tag,
  136. keep_spool_id=keep_spool_id,
  137. log_context=f"printer={printer_serial} ams={ams_id} tray={tray_id}",
  138. )
  139. async def _get_client(db: AsyncSession) -> SpoolmanClient:
  140. """Return a validated Spoolman client (URL checked, health-checked) or raise an HTTP error."""
  141. result = await db.execute(select(Settings))
  142. settings: dict[str, str] = {s.key: s.value for s in result.scalars().all()}
  143. enabled = settings.get("spoolman_enabled", "false").lower() == "true"
  144. url = settings.get("spoolman_url", "").strip()
  145. if not enabled:
  146. raise HTTPException(status_code=400, detail="Spoolman integration is not enabled")
  147. if not url:
  148. raise HTTPException(status_code=400, detail="Spoolman URL is not configured")
  149. # SSRF guard: reject dangerous schemes, cloud-metadata IPs (169.254.169.254, 100.100.100.200,
  150. # fd00:ec2::254), multicast and unspecified addresses — loopback and RFC-1918 ranges are
  151. # intentionally permitted (Spoolman commonly runs on the same host or home LAN).
  152. # Raises ValueError with a descriptive message on any violation.
  153. try:
  154. assert_safe_spoolman_url(url)
  155. except ValueError as exc:
  156. raise HTTPException(status_code=400, detail=str(exc)) from exc
  157. # Re-use the cached client when URL is unchanged; reinitialise on URL change (cache invalidation).
  158. client = await get_spoolman_client()
  159. if not client or client.base_url != url.rstrip("/"):
  160. try:
  161. client = await init_spoolman_client(url)
  162. except ValueError as exc:
  163. raise HTTPException(status_code=400, detail=str(exc)) from exc
  164. # Only call health_check() when the cached result has expired.
  165. # Evict stale entries when URL changes (only one Spoolman URL is active at a time).
  166. if url not in _health_check_cache and _health_check_cache:
  167. _health_check_cache.clear()
  168. now = time.monotonic()
  169. last_ok = _health_check_cache.get(url, 0.0)
  170. if now - last_ok > _HEALTH_CHECK_TTL:
  171. if not await client.health_check():
  172. _health_check_cache.pop(url, None)
  173. raise HTTPException(status_code=503, detail="Spoolman server is not reachable")
  174. _health_check_cache[url] = now
  175. return client
  176. @asynccontextmanager
  177. async def _translate_spoolman_errors():
  178. """Translate Spoolman typed exceptions to HTTP errors for all inventory endpoints."""
  179. try:
  180. yield
  181. except SpoolmanNotFoundError as exc:
  182. raise HTTPException(status_code=404, detail="Spool not found in Spoolman") from exc
  183. except SpoolmanClientError as exc:
  184. raise HTTPException(
  185. status_code=502,
  186. detail={
  187. "message": "Spoolman rejected the request",
  188. "upstream_status": exc.status_code,
  189. "upstream_body": getattr(exc, "response_text", ""),
  190. },
  191. ) from exc
  192. except SpoolmanUnavailableError as exc:
  193. raise HTTPException(status_code=503, detail="Spoolman server is not reachable") from exc
  194. def _raise_if_partial_failure(spools: list[dict], results: list, operation: str) -> None:
  195. """Raise HTTP 502 if any gather result is an exception, logging each failure."""
  196. failures = [(s["id"], r) for s, r in zip(spools, results, strict=True) if isinstance(r, BaseException)]
  197. if failures:
  198. logger.error(
  199. "Partial %s failure: %d/%d spools failed: %s",
  200. operation,
  201. len(failures),
  202. len(spools),
  203. [(sid, type(exc).__name__) for sid, exc in failures],
  204. )
  205. raise HTTPException(
  206. status_code=502,
  207. detail=f"{operation} partially applied: {len(spools) - len(failures)}/{len(spools)} spools updated",
  208. )
  209. async def _apply_price_if_set(client: SpoolmanClient, spool: dict, cost_per_kg: float | None) -> tuple[dict, list[str]]:
  210. """Patch the spool price; return (updated_spool, warnings).
  211. Returns the original spool and a non-empty warnings list when the price
  212. update fails, so the caller can return HTTP 207 instead of silently
  213. discarding the price.
  214. """
  215. if cost_per_kg is None:
  216. return spool, []
  217. try:
  218. async with _translate_spoolman_errors():
  219. updated = await client.update_spool_full(spool["id"], price=cost_per_kg)
  220. return updated, []
  221. except HTTPException as exc:
  222. if exc.status_code >= 500:
  223. raise # Propagate network/server errors — don't swallow Spoolman outages
  224. logger.warning(
  225. "Price update failed for spool %d; spool created without price (cost_per_kg=%s, status=%d)",
  226. spool["id"],
  227. cost_per_kg,
  228. exc.status_code,
  229. )
  230. return spool, [f"price_not_set: Spoolman rejected the price update (HTTP {exc.status_code})"]
  231. # ---------------------------------------------------------------------------
  232. # Request / response schemas
  233. # ---------------------------------------------------------------------------
  234. _HEX_RE = re.compile(r"^[0-9A-Fa-f]{6}([0-9A-Fa-f]{2})?$")
  235. def _validate_rgba(v: str | None) -> str | None:
  236. if v is None:
  237. return v
  238. clean = v.removeprefix("#")
  239. if not _HEX_RE.match(clean):
  240. raise ValueError("rgba must be a 6 or 8 character hex string (RRGGBB or RRGGBBAA)")
  241. return clean.upper()
  242. def _validate_storage_location(v: str | None) -> str | None:
  243. if v is not None and any(c in v for c in ("\r", "\n", "\x00")):
  244. raise ValueError("storage_location must not contain control characters")
  245. return v
  246. class SpoolmanInventoryCreate(BaseModel):
  247. # When spoolman_filament_id is provided the caller has already chosen a filament from the
  248. # Spoolman catalog, so material (and other metadata) are optional — the backend skips
  249. # find_or_create_filament() and uses the supplied ID directly.
  250. spoolman_filament_id: int | None = Field(None, gt=0)
  251. material: str | None = Field(None, min_length=1, max_length=64)
  252. subtype: str | None = Field(None, max_length=64)
  253. brand: str | None = Field(None, max_length=128)
  254. color_name: str | None = Field(None, max_length=64)
  255. rgba: str | None = Field(None, max_length=8, description="6-digit hex (RRGGBB) or 8-digit (RRGGBBAA)")
  256. label_weight: int = Field(1000, ge=1, le=100_000)
  257. core_weight: int = Field(
  258. 250, ge=0, le=10_000
  259. ) # Accepted for schema parity but not persisted to Spoolman (stored on filament type, not spool)
  260. weight_used: float = Field(0.0, ge=0.0, le=100_000.0)
  261. note: str | None = Field(None, max_length=1000)
  262. cost_per_kg: float | None = Field(None, ge=0.0, le=1_000_000.0)
  263. storage_location: str | None = Field(None, max_length=255)
  264. # BambuStudio slicer preset for this spool. Spoolman has no native field
  265. # for this, so we persist it under the bambu_slicer_filament[_name] keys
  266. # in the spool's extra dict and read it back in _map_spoolman_spool.
  267. slicer_filament: str | None = Field(None, max_length=128)
  268. slicer_filament_name: str | None = Field(None, max_length=255)
  269. @field_validator("rgba")
  270. @classmethod
  271. def validate_rgba(cls, v: str | None) -> str | None:
  272. return _validate_rgba(v)
  273. @field_validator("storage_location")
  274. @classmethod
  275. def validate_storage_location(cls, v: str | None) -> str | None:
  276. return _validate_storage_location(v)
  277. @model_validator(mode="after")
  278. def validate_weight_consistency(self) -> SpoolmanInventoryCreate:
  279. # material is required only when the caller has not pre-selected a Spoolman filament
  280. if self.spoolman_filament_id is None and not self.material:
  281. raise ValueError("material is required when spoolman_filament_id is not provided")
  282. if self.weight_used > self.label_weight:
  283. raise ValueError("weight_used must not exceed label_weight")
  284. return self
  285. class SpoolmanInventoryUpdate(BaseModel):
  286. material: str | None = Field(None, min_length=1, max_length=64)
  287. subtype: str | None = Field(None, max_length=64)
  288. brand: str | None = Field(None, max_length=128)
  289. color_name: str | None = Field(None, max_length=64)
  290. rgba: str | None = Field(None, max_length=8, description="6-digit hex (RRGGBB) or 8-digit (RRGGBBAA)")
  291. label_weight: int | None = Field(None, ge=1, le=100_000)
  292. core_weight: int | None = Field(
  293. None, ge=0, le=10_000
  294. ) # Accepted for schema parity but not persisted to Spoolman (stored on filament type, not spool)
  295. weight_used: float | None = Field(None, ge=0.0, le=100_000.0)
  296. note: str | None = Field(None, max_length=1000)
  297. cost_per_kg: float | None = Field(None, ge=0.0, le=1_000_000.0)
  298. tag_uid: str | None = Field(None, min_length=8, max_length=30, pattern=r"^[0-9A-Fa-f]+$")
  299. tray_uuid: str | None = Field(None, min_length=32, max_length=32, pattern=r"^[0-9A-Fa-f]+$")
  300. storage_location: str | None = Field(None, max_length=255)
  301. # BambuStudio slicer preset — persisted to Spoolman extra dict (see Create
  302. # schema). Pass an empty string to clear; null/omitted leaves unchanged.
  303. slicer_filament: str | None = Field(None, max_length=128)
  304. slicer_filament_name: str | None = Field(None, max_length=255)
  305. @field_validator("rgba")
  306. @classmethod
  307. def validate_rgba(cls, v: str | None) -> str | None:
  308. return _validate_rgba(v)
  309. @field_validator("storage_location")
  310. @classmethod
  311. def validate_storage_location(cls, v: str | None) -> str | None:
  312. return _validate_storage_location(v)
  313. @model_validator(mode="after")
  314. def validate_tag_fields(self) -> SpoolmanInventoryUpdate:
  315. # null = remove tag; non-null values rejected (use /tag endpoint to write tags)
  316. if self.tag_uid is not None:
  317. raise ValueError("tag_uid cannot be set via this endpoint; use PATCH /spools/{id}/tag to write tags")
  318. if self.tray_uuid is not None:
  319. raise ValueError("tray_uuid cannot be set via this endpoint; use PATCH /spools/{id}/tag to write tags")
  320. return self
  321. @model_validator(mode="after")
  322. def validate_weight_consistency(self) -> SpoolmanInventoryUpdate:
  323. if self.weight_used is not None and self.label_weight is not None:
  324. if self.weight_used > self.label_weight:
  325. raise ValueError("weight_used must not exceed label_weight")
  326. return self
  327. class SpoolmanInventoryBulkCreate(BaseModel):
  328. spool: SpoolmanInventoryCreate
  329. quantity: int = Field(1, ge=1, le=50)
  330. class SpoolWeightUpdate(BaseModel):
  331. weight_grams: float = Field(..., ge=0.0, le=100_000.0)
  332. class SpoolTagLinkRequest(BaseModel):
  333. # Minimum 8 hex chars = 4-byte NFC UID (Bambu Lab hardware tags use 4-byte UIDs).
  334. tag_uid: str | None = Field(None, min_length=8, max_length=30, pattern=r"^[0-9A-Fa-f]+$")
  335. tray_uuid: str | None = Field(None, min_length=32, max_length=32, pattern=r"^[0-9A-Fa-f]+$")
  336. @field_validator("tag_uid")
  337. @classmethod
  338. def tag_uid_not_all_zeros(cls, v: str | None) -> str | None:
  339. if v is not None and all(c in "0" for c in v):
  340. raise ValueError("tag_uid must not be all-zero bytes")
  341. return v
  342. @model_validator(mode="after")
  343. def at_least_one(self) -> SpoolTagLinkRequest:
  344. if not self.tag_uid and not self.tray_uuid:
  345. raise ValueError("tag_uid or tray_uuid is required")
  346. return self
  347. class SpoolSlotAssignmentRequest(BaseModel):
  348. spoolman_spool_id: int = Field(..., gt=0)
  349. printer_id: int = Field(..., gt=0)
  350. # ams_id 0–7 for physical AMS units; 255 = external/virtual spool extruder slot
  351. ams_id: int = Field(..., ge=0, le=255)
  352. tray_id: int = Field(..., ge=0, le=3)
  353. # ---------------------------------------------------------------------------
  354. # Endpoints
  355. # ---------------------------------------------------------------------------
  356. @router.get("/spools")
  357. async def list_spools(
  358. include_archived: bool = Query(False),
  359. db: AsyncSession = Depends(get_db),
  360. _: User | None = RequirePermissionIfAuthEnabled(Permission.INVENTORY_READ),
  361. ) -> list[dict]:
  362. """Return all Spoolman spools in the InventorySpool format."""
  363. client = await _get_client(db)
  364. async with _translate_spoolman_errors():
  365. spools = await client.get_all_spools(allow_archived=include_archived)
  366. mapped: list[dict] = []
  367. spool_ids: list[int] = []
  368. for s in spools:
  369. try:
  370. m = _map_spoolman_spool(s)
  371. mapped.append(m)
  372. spool_ids.append(m["id"])
  373. except ValueError as exc:
  374. logger.warning("Skipping malformed Spoolman spool (id=%r): %s", s.get("id"), exc)
  375. if spool_ids:
  376. kp_result = await db.execute(select(SpoolmanKProfile).where(SpoolmanKProfile.spoolman_spool_id.in_(spool_ids)))
  377. kp_by_spool: dict[int, list[dict]] = {}
  378. for kp in kp_result.scalars().all():
  379. kp_by_spool.setdefault(kp.spoolman_spool_id, []).append(_k_profile_to_dict(kp))
  380. for m in mapped:
  381. m["k_profiles"] = kp_by_spool.get(m["id"], [])
  382. return mapped
  383. @router.get("/spools/{spool_id}")
  384. async def get_spool(
  385. spool_id: int = Path(..., gt=0),
  386. db: AsyncSession = Depends(get_db),
  387. _: User | None = RequirePermissionIfAuthEnabled(Permission.INVENTORY_READ),
  388. ) -> dict:
  389. """Return a single Spoolman spool in the InventorySpool format."""
  390. client = await _get_client(db)
  391. async with _translate_spoolman_errors():
  392. spool = await client.get_spool(spool_id)
  393. try:
  394. mapped = _map_spoolman_spool(spool)
  395. except ValueError as exc:
  396. logger.warning("Malformed Spoolman spool (id=%r): %s", spool_id, exc)
  397. raise HTTPException(status_code=502, detail="Spoolman returned malformed spool data") from exc
  398. kp_result = await db.execute(select(SpoolmanKProfile).where(SpoolmanKProfile.spoolman_spool_id == spool_id))
  399. mapped["k_profiles"] = [_k_profile_to_dict(kp) for kp in kp_result.scalars().all()]
  400. return mapped
  401. async def _resolve_filament_id(data: SpoolmanInventoryCreate, client: SpoolmanClient) -> int:
  402. """Return the Spoolman filament ID for this spool creation request.
  403. If spoolman_filament_id is set the caller pre-selected a catalog entry,
  404. so find_or_create_filament() is skipped and the ID is used directly.
  405. """
  406. if data.spoolman_filament_id is not None:
  407. return data.spoolman_filament_id
  408. # Validator guarantees material is non-None when spoolman_filament_id is None
  409. assert data.material is not None # noqa: S101
  410. color_hex = (data.rgba or "808080FF")[:6]
  411. async with _translate_spoolman_errors():
  412. return await client.find_or_create_filament(
  413. material=data.material,
  414. subtype=data.subtype or "",
  415. brand=data.brand,
  416. color_hex=color_hex,
  417. label_weight=data.label_weight,
  418. color_name=data.color_name,
  419. )
  420. @router.post("/spools")
  421. async def create_spool(
  422. data: SpoolmanInventoryCreate,
  423. db: AsyncSession = Depends(get_db),
  424. _: User | None = RequirePermissionIfAuthEnabled(Permission.INVENTORY_UPDATE),
  425. ) -> dict:
  426. """Create a new spool in Spoolman, auto-creating vendor and filament as needed."""
  427. client = await _get_client(db)
  428. filament_id = await _resolve_filament_id(data, client)
  429. remaining = max(0.0, data.label_weight - data.weight_used)
  430. try:
  431. async with _translate_spoolman_errors():
  432. spool = await client.create_spool(
  433. filament_id=filament_id,
  434. remaining_weight=remaining,
  435. comment=data.note or None,
  436. location=data.storage_location or None,
  437. )
  438. except HTTPException as exc:
  439. if exc.status_code == 404 and data.spoolman_filament_id is not None:
  440. raise HTTPException(
  441. status_code=404,
  442. detail=f"Filament {data.spoolman_filament_id} not found in Spoolman",
  443. ) from exc
  444. raise
  445. spool, price_warnings = await _apply_price_if_set(client, spool, data.cost_per_kg)
  446. # Persist slicer_filament AND color_name under the spool's extra dict
  447. # (mirror update_spool). Spoolman has no `color_name` field on filament
  448. # (#1357) so we own the round-trip ourselves.
  449. if data.slicer_filament is not None or data.slicer_filament_name is not None or data.color_name is not None:
  450. # Ensure extra fields are registered before write.
  451. if data.slicer_filament is not None:
  452. await client.ensure_extra_field("bambu_slicer_filament")
  453. if data.slicer_filament_name is not None:
  454. await client.ensure_extra_field("bambu_slicer_filament_name")
  455. if data.color_name is not None:
  456. await client.ensure_extra_field("bambu_color_name")
  457. new_extra: dict = {}
  458. if data.slicer_filament is not None:
  459. new_extra["bambu_slicer_filament"] = json.dumps(data.slicer_filament)
  460. if data.slicer_filament_name is not None:
  461. new_extra["bambu_slicer_filament_name"] = json.dumps(data.slicer_filament_name)
  462. if data.color_name is not None:
  463. new_extra["bambu_color_name"] = json.dumps(data.color_name)
  464. if new_extra:
  465. try:
  466. async with _translate_spoolman_errors():
  467. spool = await client.merge_spool_extra(spool["id"], new_extra)
  468. except HTTPException:
  469. # Best-effort — the spool already exists, log and continue.
  470. logger.warning(
  471. "Failed to persist slicer_filament/color_name for spool %s",
  472. spool.get("id"),
  473. )
  474. result = _map_spoolman_spool(spool)
  475. if price_warnings:
  476. return JSONResponse(status_code=207, content={**result, "warnings": price_warnings})
  477. return result
  478. @router.post("/spools/bulk")
  479. async def bulk_create_spools(
  480. payload: SpoolmanInventoryBulkCreate,
  481. db: AsyncSession = Depends(get_db),
  482. _: User | None = RequirePermissionIfAuthEnabled(Permission.INVENTORY_UPDATE),
  483. ) -> Response:
  484. """Create multiple identical spools in Spoolman."""
  485. client = await _get_client(db)
  486. data = payload.spool
  487. try:
  488. filament_id = await _resolve_filament_id(data, client)
  489. except HTTPException as exc:
  490. if exc.status_code == 404 and data.spoolman_filament_id is not None:
  491. raise HTTPException(
  492. status_code=404,
  493. detail=f"Filament {data.spoolman_filament_id} not found in Spoolman",
  494. ) from exc
  495. raise
  496. remaining = max(0.0, data.label_weight - data.weight_used)
  497. created: list[dict] = []
  498. failures: list[str] = []
  499. for _ in range(payload.quantity):
  500. try:
  501. spool = await client.create_spool(
  502. filament_id=filament_id,
  503. remaining_weight=remaining,
  504. comment=data.note or None,
  505. location=data.storage_location or None,
  506. )
  507. except (SpoolmanUnavailableError, SpoolmanClientError, SpoolmanNotFoundError) as exc:
  508. logger.warning("Bulk spool creation: one spool failed: %s", exc)
  509. failures.append("spool creation failed")
  510. continue
  511. try:
  512. spool, price_warnings = await _apply_price_if_set(client, spool, data.cost_per_kg)
  513. except HTTPException as exc:
  514. logger.warning(
  515. "Bulk spool %d: price update failed (HTTP %d); spool not added to created list",
  516. spool.get("id", 0),
  517. exc.status_code,
  518. )
  519. failures.append("spool created but price update failed")
  520. continue
  521. if price_warnings:
  522. logger.warning("Bulk spool %s created without price: %s", spool.get("id"), price_warnings)
  523. created.append(_map_spoolman_spool(spool))
  524. if not created:
  525. raise HTTPException(status_code=500, detail="Failed to create any spools in Spoolman")
  526. if len(created) < payload.quantity:
  527. # Some spool creations failed — return 207 Multi-Status so the caller
  528. # can distinguish a full success from a partial one and show a useful message.
  529. return JSONResponse(
  530. status_code=207,
  531. content={
  532. "created": created,
  533. "requested_count": payload.quantity,
  534. "failed_count": payload.quantity - len(created),
  535. "failures": failures,
  536. },
  537. )
  538. return JSONResponse(status_code=200, content=created)
  539. @router.patch("/spools/{spool_id}")
  540. async def update_spool(
  541. *,
  542. spool_id: int = Path(..., gt=0),
  543. data: SpoolmanInventoryUpdate,
  544. db: AsyncSession = Depends(get_db),
  545. _: User | None = RequirePermissionIfAuthEnabled(Permission.INVENTORY_UPDATE),
  546. ) -> dict:
  547. """Update an existing Spoolman spool, re-linking the filament if metadata changed."""
  548. client = await _get_client(db)
  549. async with _translate_spoolman_errors():
  550. current = await client.get_spool(spool_id)
  551. cur_filament: dict = current.get("filament") or {}
  552. cur_vendor: dict = cur_filament.get("vendor") or {}
  553. cur_mat: str = (cur_filament.get("material") or "").strip()
  554. cur_name: str = (cur_filament.get("name") or "").strip()
  555. if cur_mat and cur_name.upper().startswith(cur_mat.upper()):
  556. cur_subtype: str = cur_name[len(cur_mat) :].strip()
  557. else:
  558. cur_subtype = cur_name
  559. # Resolve final values: use request value if provided, else keep current
  560. material = data.material if data.material is not None else cur_mat
  561. subtype = data.subtype if data.subtype is not None else cur_subtype
  562. brand = data.brand if data.brand is not None else (cur_vendor.get("name") or None)
  563. # color_name uses model_fields_set so explicit null (clear) is distinguishable
  564. # from "field omitted" (don't touch). find_or_create_filament's convention:
  565. # None = don't touch, "" = explicit clear, "value" = set.
  566. if "color_name" in data.model_fields_set:
  567. color_name = data.color_name if data.color_name is not None else ""
  568. else:
  569. color_name = cur_filament.get("color_name") or None
  570. cur_color = (cur_filament.get("color_hex") or "808080").upper().removeprefix("#")
  571. rgba = data.rgba if data.rgba is not None else (cur_color + "FF")
  572. label_weight = data.label_weight if data.label_weight is not None else int(cur_filament.get("weight") or 1000)
  573. # Default weight_used from the synthetic mapping (label - remaining) so an
  574. # edit that doesn't touch the weight field preserves Spoolman's real
  575. # remaining_weight after a "Reset usage to 0" — the previous code read
  576. # Spoolman's used_weight directly, which is 0 post-reset, so
  577. # `remaining = label - 0 = 1000` would overwrite the real remaining
  578. # the next time the user edited any other field (#1390).
  579. cur_remaining_raw = current.get("remaining_weight")
  580. if cur_remaining_raw is not None:
  581. synthetic_used = max(0.0, float(label_weight) - float(cur_remaining_raw))
  582. else:
  583. synthetic_used = float(current.get("used_weight") or 0)
  584. weight_used = data.weight_used if data.weight_used is not None else synthetic_used
  585. note = data.note if data.note is not None else current.get("comment")
  586. storage_location_changed = "storage_location" in data.model_fields_set
  587. storage_location = data.storage_location if storage_location_changed else None
  588. color_hex = rgba[:6]
  589. # Resolve which filament this spool should be linked to AFTER the edit.
  590. #
  591. # The old behaviour was always `find_or_create_filament`, which proliferated
  592. # duplicate Spoolman filaments whenever the user changed any field that
  593. # made up the match key (material/subtype/brand/color) — every edit minted
  594. # a fresh row and orphaned the previous one (#1357 follow-up). To match
  595. # internal-mode behaviour ([[feedback_inventory_modes_parity]]: editing a
  596. # spool does not proliferate new entities), prefer PATCHing the current
  597. # filament in place when it's a singleton.
  598. cur_filament_id = cur_filament.get("id")
  599. desired_name = f"{material} {subtype}".strip() if subtype else material
  600. cur_color_norm = (cur_filament.get("color_hex") or "").upper()[:6]
  601. cur_vendor_name = (cur_vendor.get("name") or "").strip()
  602. cur_weight_int = int(cur_filament.get("weight") or 0)
  603. metadata_unchanged = (
  604. cur_filament_id
  605. and (cur_filament.get("name") or "").strip() == desired_name
  606. and (cur_filament.get("material") or "").upper() == material.upper()
  607. and cur_color_norm == color_hex.upper()
  608. and cur_vendor_name.lower() == ((brand or "").strip().lower())
  609. and cur_weight_int == int(label_weight)
  610. )
  611. if metadata_unchanged:
  612. # No filament-side change at all — re-use the existing link, skip
  613. # find_or_create entirely so a no-op edit (e.g. just changing
  614. # weight_used or note) never even touches the filament catalogue.
  615. filament_id = cur_filament_id
  616. else:
  617. async with _translate_spoolman_errors():
  618. shared = await client.is_filament_shared(cur_filament_id, spool_id) if cur_filament_id else False
  619. if cur_filament_id and not shared:
  620. # Singleton filament — PATCH it in place so the user's edit lands
  621. # on the row their spool already points at instead of orphaning it.
  622. patch_body: dict = {
  623. "name": desired_name,
  624. "material": material,
  625. "color_hex": color_hex,
  626. "weight": float(label_weight),
  627. }
  628. if brand:
  629. vendor_id = await client.find_or_create_vendor(brand)
  630. patch_body["vendor_id"] = vendor_id
  631. async with _translate_spoolman_errors():
  632. await client.patch_filament(cur_filament_id, patch_body)
  633. filament_id = cur_filament_id
  634. else:
  635. # Filament is shared with other spools — PATCHing it in place would
  636. # silently rewrite their metadata too. Fall back to find-or-create
  637. # so only this spool's link moves.
  638. async with _translate_spoolman_errors():
  639. filament_id = await client.find_or_create_filament(
  640. material=material,
  641. subtype=subtype or "",
  642. brand=brand,
  643. color_hex=color_hex,
  644. label_weight=label_weight,
  645. color_name=color_name,
  646. )
  647. if not filament_id:
  648. raise HTTPException(status_code=500, detail="Failed to find or create filament in Spoolman")
  649. remaining = max(0.0, label_weight - weight_used)
  650. # Tag removal: clear only the "tag" key so other custom Spoolman extra fields
  651. # set outside Bambuddy are preserved.
  652. tag_nulled = (
  653. ("tag_uid" in data.model_fields_set or "tray_uuid" in data.model_fields_set)
  654. and _tag_cleared(data.tag_uid)
  655. and _tag_cleared(data.tray_uuid)
  656. )
  657. # Serialise tag-clear + PATCH under the per-spool extra lock to prevent a
  658. # concurrent merge_spool_extra call (e.g. NFC write-back) from overwriting
  659. # the tag key between our read and our write.
  660. #
  661. # Spoolman PATCHes extra dicts by MERGING — popping "tag" from a re-fetched
  662. # dict and sending the rest doesn't clear the key (Spoolman keeps the old
  663. # value because the key wasn't in the payload). Explicitly set the tag to
  664. # a JSON-encoded empty string; read-side filters strip the quotes.
  665. async with client.extra_lock(spool_id):
  666. if tag_nulled:
  667. # Re-fetch inside the lock so we work with fresh extra data.
  668. async with _translate_spoolman_errors():
  669. fresh = await client.get_spool(spool_id)
  670. cur_extra = dict(fresh.get("extra") or {})
  671. cur_extra["tag"] = json.dumps("")
  672. extra: dict | None = cur_extra
  673. else:
  674. extra = None
  675. async with _translate_spoolman_errors():
  676. updated = await client.update_spool_full(
  677. spool_id=spool_id,
  678. filament_id=filament_id,
  679. remaining_weight=remaining,
  680. comment=note or "",
  681. price=data.cost_per_kg,
  682. extra=extra,
  683. location=storage_location or None,
  684. clear_location=storage_location_changed and not storage_location,
  685. )
  686. # Persist BambuStudio slicer preset AND color_name under spool.extra.
  687. # Spoolman has no native fields for these — color_name was confirmed
  688. # absent from the FilamentUpdateParameters schema in 0.23.1 (#1357), so
  689. # writing `filament.color_name` was a silent no-op that left every
  690. # edit looking "not saved". They all round-trip via extra and get
  691. # unpacked in _map_spoolman_spool. Only writes when the request
  692. # explicitly set the field — passing null/omitting leaves the existing
  693. # extra entry untouched (write empty string to clear).
  694. sf_set = "slicer_filament" in data.model_fields_set
  695. sfn_set = "slicer_filament_name" in data.model_fields_set
  696. cn_set = "color_name" in data.model_fields_set
  697. if sf_set or sfn_set or cn_set:
  698. # Ensure extra fields are registered (Spoolman rejects PATCHes with
  699. # unknown keys with HTTP 400). Idempotent if startup already ran this.
  700. if sf_set:
  701. await client.ensure_extra_field("bambu_slicer_filament")
  702. if sfn_set:
  703. await client.ensure_extra_field("bambu_slicer_filament_name")
  704. if cn_set:
  705. await client.ensure_extra_field("bambu_color_name")
  706. new_extra: dict = {}
  707. if sf_set:
  708. new_extra["bambu_slicer_filament"] = json.dumps(data.slicer_filament or "")
  709. if sfn_set:
  710. new_extra["bambu_slicer_filament_name"] = json.dumps(data.slicer_filament_name or "")
  711. if cn_set:
  712. new_extra["bambu_color_name"] = json.dumps(data.color_name or "")
  713. async with _translate_spoolman_errors():
  714. updated = await client.merge_spool_extra(spool_id, new_extra)
  715. return _map_spoolman_spool(updated)
  716. @router.delete("/spools/{spool_id}")
  717. async def delete_spool(
  718. spool_id: int = Path(..., gt=0),
  719. db: AsyncSession = Depends(get_db),
  720. _: User | None = RequirePermissionIfAuthEnabled(Permission.INVENTORY_UPDATE),
  721. ) -> dict:
  722. """Permanently delete a spool from Spoolman."""
  723. client = await _get_client(db)
  724. async with _translate_spoolman_errors():
  725. await client.delete_spool(spool_id)
  726. return {"status": "deleted"}
  727. @router.post("/spools/{spool_id}/archive")
  728. async def archive_spool(
  729. spool_id: int = Path(..., gt=0),
  730. db: AsyncSession = Depends(get_db),
  731. _: User | None = RequirePermissionIfAuthEnabled(Permission.INVENTORY_UPDATE),
  732. ) -> dict:
  733. """Archive a spool in Spoolman (soft-delete)."""
  734. client = await _get_client(db)
  735. async with _translate_spoolman_errors():
  736. spool = await client.set_spool_archived(spool_id, archived=True)
  737. try:
  738. return _map_spoolman_spool(spool)
  739. except ValueError as exc:
  740. logger.warning("Malformed Spoolman spool (id=%r): %s", spool_id, exc)
  741. raise HTTPException(status_code=502, detail="Spoolman returned malformed spool data") from exc
  742. @router.post("/spools/{spool_id}/restore")
  743. async def restore_spool(
  744. spool_id: int = Path(..., gt=0),
  745. db: AsyncSession = Depends(get_db),
  746. _: User | None = RequirePermissionIfAuthEnabled(Permission.INVENTORY_UPDATE),
  747. ) -> dict:
  748. """Restore an archived spool in Spoolman."""
  749. client = await _get_client(db)
  750. async with _translate_spoolman_errors():
  751. spool = await client.set_spool_archived(spool_id, archived=False)
  752. try:
  753. return _map_spoolman_spool(spool)
  754. except ValueError as exc:
  755. logger.warning("Malformed Spoolman spool (id=%r): %s", spool_id, exc)
  756. raise HTTPException(status_code=502, detail="Spoolman returned malformed spool data") from exc
  757. @router.post("/spools/{spool_id}/reset-usage")
  758. async def reset_spool_usage(
  759. spool_id: int = Path(..., gt=0),
  760. db: AsyncSession = Depends(get_db),
  761. _: User | None = RequirePermissionIfAuthEnabled(Permission.INVENTORY_UPDATE),
  762. ) -> dict:
  763. """Zero the spool's used_weight in Spoolman without touching anything else."""
  764. client = await _get_client(db)
  765. async with _translate_spoolman_errors():
  766. spool = await client.reset_spool_usage(spool_id)
  767. try:
  768. return _map_spoolman_spool(spool)
  769. except ValueError as exc:
  770. logger.warning("Malformed Spoolman spool (id=%r): %s", spool_id, exc)
  771. raise HTTPException(status_code=502, detail="Spoolman returned malformed spool data") from exc
  772. @router.post("/spools/reset-usage-bulk")
  773. async def bulk_reset_spool_usage(
  774. payload: dict = Body(...),
  775. db: AsyncSession = Depends(get_db),
  776. _: User | None = RequirePermissionIfAuthEnabled(Permission.INVENTORY_UPDATE),
  777. ) -> dict:
  778. """Bulk-reset used_weight to 0 across the given Spoolman spool IDs.
  779. Caller passes an explicit list of IDs — no "reset all" shortcut, since
  780. a typo on a wildcard would wipe the entire inventory's tracking.
  781. Returns the count of spools successfully reset; individual failures are
  782. logged but do not abort the batch.
  783. """
  784. spool_ids = payload.get("spool_ids")
  785. if not isinstance(spool_ids, list) or not spool_ids:
  786. raise HTTPException(status_code=400, detail="spool_ids must be a non-empty list")
  787. if not all(isinstance(sid, int) for sid in spool_ids):
  788. raise HTTPException(status_code=400, detail="spool_ids must contain integers")
  789. client = await _get_client(db)
  790. reset_count = 0
  791. for spool_id in spool_ids:
  792. try:
  793. async with _translate_spoolman_errors():
  794. await client.reset_spool_usage(spool_id)
  795. reset_count += 1
  796. except HTTPException as exc:
  797. logger.warning("Spoolman reset-usage failed for spool %s: %s", spool_id, exc.detail)
  798. return {"reset": reset_count}
  799. @router.patch("/spools/{spool_id}/weight")
  800. async def sync_spool_weight(
  801. *,
  802. spool_id: int = Path(..., gt=0),
  803. data: SpoolWeightUpdate,
  804. db: AsyncSession = Depends(get_db),
  805. _: User | None = RequirePermissionIfAuthEnabled(Permission.INVENTORY_UPDATE),
  806. ) -> dict:
  807. """Update a spool's remaining weight from a measured gross weight.
  808. Computes remaining = gross_weight - tare, where tare = spool.spool_weight
  809. if set, else filament.spool_weight; falls back to 250 g when both unset.
  810. """
  811. client = await _get_client(db)
  812. async with _translate_spoolman_errors():
  813. current = await client.get_spool(spool_id)
  814. cur_filament = current.get("filament") or {}
  815. spool_tare = current.get("spool_weight")
  816. raw_tare = spool_tare if spool_tare is not None else cur_filament.get("spool_weight")
  817. core_weight = _safe_float(raw_tare, 250.0)
  818. remaining = max(0.0, data.weight_grams - core_weight)
  819. async with _translate_spoolman_errors():
  820. updated = await client.update_spool_full(spool_id=spool_id, remaining_weight=remaining)
  821. upd_filament = updated.get("filament") or {}
  822. label_weight = _safe_int(upd_filament.get("weight"), 1000)
  823. weight_used = max(0.0, label_weight - remaining)
  824. return {"status": "ok", "weight_used": weight_used}
  825. @router.patch("/spools/{spool_id}/tag")
  826. async def link_tag_to_spoolman_spool(
  827. *,
  828. spool_id: int = Path(..., gt=0),
  829. data: SpoolTagLinkRequest,
  830. db: AsyncSession = Depends(get_db),
  831. _: User | None = RequirePermissionIfAuthEnabled(Permission.INVENTORY_UPDATE),
  832. ) -> dict:
  833. """Write an NFC tag UID or Bambu tray UUID into Spoolman's extra.tag for a spool.
  834. tray_uuid takes precedence over tag_uid when both are supplied.
  835. Returns 409 if another spool already carries the same tag.
  836. Uses extra_lock to serialise against concurrent extra-field writes.
  837. """
  838. client = await _get_client(db)
  839. tag = (data.tray_uuid or data.tag_uid).upper()
  840. tag_json = json.dumps(tag)
  841. async with client.extra_lock(spool_id):
  842. # Duplicate check: scan all spools for the same tag on a different spool.
  843. async with _translate_spoolman_errors():
  844. all_spools = await client.get_all_spools()
  845. for s in all_spools:
  846. s_tag = (s.get("extra") or {}).get("tag", "")
  847. if s_tag.strip('"').upper() == tag and s.get("id") != spool_id:
  848. raise HTTPException(
  849. status_code=409,
  850. detail=f"Tag is already assigned to spool {s['id']}",
  851. )
  852. # Re-fetch inside the lock so cur_extra reflects any concurrent update.
  853. async with _translate_spoolman_errors():
  854. current = await client.get_spool(spool_id)
  855. cur_extra = dict(current.get("extra") or {})
  856. cur_extra["tag"] = tag_json
  857. async with _translate_spoolman_errors():
  858. updated = await client.update_spool_full(spool_id=spool_id, extra=cur_extra)
  859. logger.info("Linked tag %s to Spoolman spool %s", tag, spool_id)
  860. return _map_spoolman_spool(updated)
  861. @router.get("/slot-assignments/all", response_model=list[SpoolmanSlotAssignmentEnriched])
  862. async def get_all_spoolman_slot_assignments(
  863. printer_id: int | None = Query(None, gt=0),
  864. db: AsyncSession = Depends(get_db),
  865. _: User | None = RequirePermissionIfAuthEnabled(Permission.INVENTORY_READ),
  866. ) -> list[SpoolmanSlotAssignmentEnriched]:
  867. """Return all Spoolman slot assignments enriched with printer_name and ams_label.
  868. ``printer_name`` is null only when the printer relation is missing
  869. (cascade-deleted edge case). ``ams_label`` is null when no AmsLabel row
  870. matches the slot's MQTT serial (or the synthetic ``f"p{pid}a{ams_id}"``
  871. fallback key).
  872. """
  873. query = select(SpoolmanSlotAssignment).options(selectinload(SpoolmanSlotAssignment.printer))
  874. if printer_id is not None:
  875. query = query.where(SpoolmanSlotAssignment.printer_id == printer_id)
  876. result = await db.execute(query)
  877. slots = list(result.scalars().all())
  878. # Build (printer_id, ams_id) -> ams_serial map from live printer states.
  879. # Same pattern as inventory.py:765-806 for the local /assignments endpoint.
  880. printer_ids = {s.printer_id for s in slots}
  881. serial_map: dict[tuple[int, int], str] = {}
  882. all_statuses = printer_manager.get_all_statuses()
  883. for pid in printer_ids:
  884. state = all_statuses.get(pid)
  885. if not (state and state.raw_data):
  886. continue
  887. # Some printer firmware variants wrap the AMS list in an outer dict
  888. # (`{"ams": [...]}`). Mirror the defense used in sync_spoolman_ams_weights
  889. # (line 842-844) so a wrapped payload still resolves to a list.
  890. ams_raw = state.raw_data.get("ams", [])
  891. if isinstance(ams_raw, dict):
  892. ams_raw = ams_raw.get("ams", [])
  893. if not isinstance(ams_raw, list):
  894. continue
  895. for ams_unit in ams_raw:
  896. if not isinstance(ams_unit, dict):
  897. continue
  898. sn = str(ams_unit.get("sn") or ams_unit.get("serial_number") or "")
  899. if not sn:
  900. continue
  901. try:
  902. serial_map[(pid, int(ams_unit.get("id", 0)))] = sn
  903. except (ValueError, TypeError):
  904. continue
  905. # Add synthetic fallback key (f"p{pid}a{ams_id}") for slots without a serial.
  906. all_serials: set[str] = set(serial_map.values())
  907. for s in slots:
  908. if (s.printer_id, s.ams_id) not in serial_map:
  909. all_serials.add(f"p{s.printer_id}a{s.ams_id}")
  910. label_by_serial: dict[str, str] = {}
  911. if all_serials:
  912. lbl_result = await db.execute(select(AmsLabel).where(AmsLabel.ams_serial_number.in_(all_serials)))
  913. for lbl in lbl_result.scalars().all():
  914. label_by_serial[lbl.ams_serial_number] = lbl.label
  915. def _ams_label_for(pid: int, ams_id: int) -> str | None:
  916. sn = serial_map.get((pid, ams_id))
  917. if sn and sn in label_by_serial:
  918. return label_by_serial[sn]
  919. if not sn:
  920. return label_by_serial.get(f"p{pid}a{ams_id}")
  921. return None
  922. enriched: list[SpoolmanSlotAssignmentEnriched] = []
  923. for s in slots:
  924. if s.printer is None:
  925. # FK is ondelete=CASCADE so this should be unreachable in normal
  926. # operation; surface it loudly if a stale row ever appears.
  927. logger.warning(
  928. "Orphaned Spoolman slot assignment: printer_id=%d (ams=%d, tray=%d, spoolman_spool_id=%d) has no Printer row",
  929. s.printer_id,
  930. s.ams_id,
  931. s.tray_id,
  932. s.spoolman_spool_id,
  933. )
  934. enriched.append(
  935. SpoolmanSlotAssignmentEnriched(
  936. printer_id=s.printer_id,
  937. printer_name=s.printer.name if s.printer else None,
  938. ams_id=s.ams_id,
  939. tray_id=s.tray_id,
  940. spoolman_spool_id=s.spoolman_spool_id,
  941. ams_label=_ams_label_for(s.printer_id, s.ams_id),
  942. )
  943. )
  944. return enriched
  945. @router.post("/sync-ams-weights")
  946. async def sync_spoolman_ams_weights(
  947. db: AsyncSession = Depends(get_db),
  948. _: User | None = RequirePermissionIfAuthEnabled(Permission.INVENTORY_UPDATE),
  949. ):
  950. """Sync remaining weight back to Spoolman for all slot-assigned spools.
  951. Reads live AMS remain% from connected printers, computes
  952. remaining = label_weight * remain% / 100, and PATCHes Spoolman.
  953. """
  954. client = await _get_client(db)
  955. # Fetch all non-archived Spoolman spools once for label_weight lookup
  956. async with _translate_spoolman_errors():
  957. raw_spools = await client.get_all_spools(allow_archived=False)
  958. spool_lookup: dict[int, dict] = {s["id"]: s for s in raw_spools if s.get("id") is not None}
  959. result = await db.execute(select(SpoolmanSlotAssignment))
  960. assignments = list(result.scalars().all())
  961. synced = 0
  962. skipped = 0
  963. def _find_tray(ams_data: list, ams_id: int, tray_id: int) -> dict | None:
  964. if not ams_data:
  965. return None
  966. for ams_unit in ams_data:
  967. if _safe_int(ams_unit.get("id"), -1) != ams_id:
  968. continue
  969. for tray in ams_unit.get("tray", []):
  970. if _safe_int(tray.get("id"), -1) == tray_id:
  971. return tray
  972. return None
  973. for assignment in assignments:
  974. spool_dict = spool_lookup.get(assignment.spoolman_spool_id)
  975. if not spool_dict:
  976. logger.debug("Spoolman AMS sync: spool %d not found in Spoolman, skipping", assignment.spoolman_spool_id)
  977. skipped += 1
  978. continue
  979. label_weight = _safe_int((spool_dict.get("filament") or {}).get("weight"), 1000)
  980. if label_weight <= 0:
  981. logger.debug("Spoolman AMS sync: spool %d has no label_weight, skipping", assignment.spoolman_spool_id)
  982. skipped += 1
  983. continue
  984. state = printer_manager.get_status(assignment.printer_id)
  985. if not state or not state.raw_data:
  986. logger.info(
  987. "Spoolman AMS sync: printer %d not connected, skipping spool %d",
  988. assignment.printer_id,
  989. assignment.spoolman_spool_id,
  990. )
  991. skipped += 1
  992. continue
  993. ams_raw = state.raw_data.get("ams", [])
  994. if isinstance(ams_raw, dict):
  995. ams_raw = ams_raw.get("ams", [])
  996. tray = _find_tray(ams_raw, assignment.ams_id, assignment.tray_id)
  997. if not tray:
  998. logger.info(
  999. "Spoolman AMS sync: no tray data for spool %d (printer %d AMS%d-T%d)",
  1000. assignment.spoolman_spool_id,
  1001. assignment.printer_id,
  1002. assignment.ams_id,
  1003. assignment.tray_id,
  1004. )
  1005. skipped += 1
  1006. continue
  1007. remain_raw = tray.get("remain")
  1008. if remain_raw is None:
  1009. logger.debug(
  1010. "Spoolman AMS sync: no remain value for spool %d (tray %d/%d), skipping",
  1011. assignment.spoolman_spool_id,
  1012. assignment.ams_id,
  1013. assignment.tray_id,
  1014. )
  1015. skipped += 1
  1016. continue
  1017. try:
  1018. remain_val = int(remain_raw)
  1019. except (TypeError, ValueError):
  1020. logger.debug(
  1021. "Spoolman AMS sync: non-numeric remain=%r for spool %d, skipping",
  1022. remain_raw,
  1023. assignment.spoolman_spool_id,
  1024. )
  1025. skipped += 1
  1026. continue
  1027. if remain_val < 0 or remain_val > 100:
  1028. logger.debug("Spoolman AMS sync: invalid remain=%s for spool %d", remain_raw, assignment.spoolman_spool_id)
  1029. skipped += 1
  1030. continue
  1031. remaining = round(label_weight * remain_val / 100.0, 1)
  1032. try:
  1033. async with _translate_spoolman_errors():
  1034. await client.update_spool_full(assignment.spoolman_spool_id, remaining_weight=remaining)
  1035. logger.info(
  1036. "Spoolman AMS sync: spool %d remaining set to %s g (remain=%d%%)",
  1037. assignment.spoolman_spool_id,
  1038. remaining,
  1039. remain_val,
  1040. )
  1041. synced += 1
  1042. except HTTPException as exc:
  1043. if exc.status_code == 404:
  1044. logger.warning(
  1045. "Spoolman AMS sync: spool %d not found in Spoolman (404), skipping",
  1046. assignment.spoolman_spool_id,
  1047. )
  1048. else:
  1049. logger.warning(
  1050. "Spoolman AMS sync: failed to update spool %d (HTTP %d)",
  1051. assignment.spoolman_spool_id,
  1052. exc.status_code,
  1053. )
  1054. skipped += 1
  1055. return {"synced": synced, "skipped": skipped}
  1056. @router.post("/slot-assignments")
  1057. async def assign_spoolman_slot(
  1058. body: SpoolSlotAssignmentRequest,
  1059. db: AsyncSession = Depends(get_db),
  1060. _: User | None = RequirePermissionIfAuthEnabled(Permission.INVENTORY_UPDATE),
  1061. ) -> dict:
  1062. """Assign a Spoolman spool to a printer AMS slot (stored in local DB only).
  1063. Raises 404 if the printer does not exist or the spool is not found in Spoolman.
  1064. Spoolman's own ``spool.location`` field is NOT touched — it is user-managed.
  1065. """
  1066. client = await _get_client(db)
  1067. result = await db.execute(select(Printer).where(Printer.id == body.printer_id))
  1068. printer = result.scalar_one_or_none()
  1069. if not printer:
  1070. raise HTTPException(status_code=404, detail="Printer not found")
  1071. # Verify the Spoolman spool exists before committing to local DB.
  1072. # This prevents ghost rows pointing at non-existent spool IDs.
  1073. async with _translate_spoolman_errors():
  1074. spool = await client.get_spool(body.spoolman_spool_id)
  1075. # Spool confirmed in Spoolman — upsert into local slot-assignment table
  1076. # assigned_at is intentionally not refreshed on re-assign (original timestamp preserved)
  1077. try:
  1078. await db.execute(
  1079. text(
  1080. "INSERT INTO spoolman_slot_assignments"
  1081. " (printer_id, ams_id, tray_id, spoolman_spool_id)"
  1082. " VALUES (:printer_id, :ams_id, :tray_id, :spool_id)"
  1083. " ON CONFLICT(printer_id, ams_id, tray_id)"
  1084. " DO UPDATE SET spoolman_spool_id = excluded.spoolman_spool_id"
  1085. ),
  1086. {
  1087. "printer_id": body.printer_id,
  1088. "ams_id": body.ams_id,
  1089. "tray_id": body.tray_id,
  1090. "spool_id": body.spoolman_spool_id,
  1091. },
  1092. )
  1093. await db.commit()
  1094. except Exception as exc:
  1095. await db.rollback()
  1096. logger.error("Failed to persist slot assignment: %s", exc)
  1097. raise HTTPException(status_code=500, detail="Failed to save slot assignment") from exc
  1098. # #1457: clear stale fallback-tag links on OTHER spools still bound to this
  1099. # slot. Without this, a non-RFID slot's deterministic fallback tag stays
  1100. # attached to the previous spool in Spoolman's extra.tag and re-surfaces in
  1101. # the hover card whenever the local slot assignment is removed.
  1102. if printer.serial_number:
  1103. await _clear_stale_slot_fallback_tag_links(
  1104. client,
  1105. printer_serial=printer.serial_number,
  1106. ams_id=body.ams_id,
  1107. tray_id=body.tray_id,
  1108. keep_spool_id=body.spoolman_spool_id,
  1109. )
  1110. mapped = _map_spoolman_spool(spool)
  1111. # Fetch K-profiles before the MQTT try block so we can use async DB access.
  1112. kp_rows_result = await db.execute(
  1113. select(SpoolmanKProfile).where(
  1114. SpoolmanKProfile.spoolman_spool_id == body.spoolman_spool_id,
  1115. SpoolmanKProfile.printer_id == body.printer_id,
  1116. )
  1117. )
  1118. kp_rows = kp_rows_result.scalars().all()
  1119. # Auto-configure AMS slot via MQTT (best-effort; slot assignment is already persisted)
  1120. try:
  1121. mqtt_client = printer_manager.get_client(body.printer_id)
  1122. if mqtt_client:
  1123. tray_type = mapped.get("material") or ""
  1124. brand = mapped.get("brand") or ""
  1125. subtype = mapped.get("subtype") or ""
  1126. if brand:
  1127. tray_sub_brands = f"{brand} {tray_type} {subtype}".strip()
  1128. elif subtype:
  1129. tray_sub_brands = f"{tray_type} {subtype}".strip()
  1130. else:
  1131. tray_sub_brands = tray_type
  1132. tray_color = (mapped.get("rgba") or "808080FF").upper()
  1133. if len(tray_color) == 6:
  1134. tray_color = tray_color + "FF"
  1135. material_upper = tray_type.upper().strip()
  1136. tray_info_idx = (
  1137. GENERIC_FILAMENT_IDS.get(material_upper)
  1138. or GENERIC_FILAMENT_IDS.get(material_upper.split("-")[0].split(" ")[0])
  1139. or ""
  1140. )
  1141. setting_id = ""
  1142. temp_defaults = MATERIAL_TEMPS.get(material_upper, (200, 240))
  1143. temp_min = mapped.get("nozzle_temp_min") or temp_defaults[0]
  1144. temp_max = temp_defaults[1]
  1145. # Pull printer state from printer_manager. The previous
  1146. # `mqtt_client.printer_state` access via hasattr always returned
  1147. # None (the attribute is `state`, not `printer_state`), so the
  1148. # K-profile cascade silently skipped state.kprofiles, defaulted
  1149. # nozzle_diameter to 0.4, and left slot_extruder unset.
  1150. state = printer_manager.get_status(body.printer_id)
  1151. nozzle_diameter = "0.4"
  1152. if state and state.nozzles:
  1153. nd = state.nozzles[0].nozzle_diameter
  1154. if nd:
  1155. nozzle_diameter = nd
  1156. slot_extruder = None
  1157. if state and state.ams_extruder_map:
  1158. if body.ams_id == 255:
  1159. # External slots: ext-L (tray 0) → extruder 1, ext-R (tray 1) → extruder 0
  1160. # tray_id 0→1, 1→0
  1161. slot_extruder = 1 - body.tray_id
  1162. else:
  1163. slot_extruder = state.ams_extruder_map.get(str(body.ams_id))
  1164. # Prefer exact extruder match, fall back to extruder-agnostic kp
  1165. # for the same nozzle. Hard-skipping on mismatch silently dropped
  1166. # valid stored profiles when the AMS-extruder mapping had shifted.
  1167. exact_kp = None
  1168. fallback_kp = None
  1169. for kp in kp_rows:
  1170. if kp.nozzle_diameter != nozzle_diameter or kp.cali_idx is None:
  1171. continue
  1172. if slot_extruder is not None and kp.extruder is not None and kp.extruder == slot_extruder:
  1173. exact_kp = kp
  1174. break
  1175. if fallback_kp is None:
  1176. fallback_kp = kp
  1177. matching_kp = exact_kp or fallback_kp
  1178. # Resolve the printer-side calibration entry by cali_idx so we
  1179. # know the authoritative filament_id (the printer indexes its
  1180. # calibration table by filament_id, not setting_id).
  1181. printer_kp = None
  1182. if matching_kp and state and state.kprofiles:
  1183. for pkp in state.kprofiles:
  1184. if pkp.slot_id == matching_kp.cali_idx and pkp.nozzle_diameter == nozzle_diameter:
  1185. printer_kp = pkp
  1186. break
  1187. if printer_kp is None:
  1188. logger.warning(
  1189. "Spoolman assign: cali_idx=%d not present in printer's "
  1190. "calibration table — stored kp may be stale.",
  1191. matching_kp.cali_idx,
  1192. )
  1193. # Realign the slot's filament context (tray_info_idx + setting_id)
  1194. # to the kp's calibration context. Without this, ams_filament_setting
  1195. # declares the slot under generic PLA while extrusion_cali_sel points
  1196. # the cali_idx at a different preset — the printer can't link them
  1197. # and falls back to the default profile. P-prefix local presets are
  1198. # valid for tray_info_idx; PFUS-prefix cloud-user presets are not
  1199. # (the slicer rejects them).
  1200. effective_tray_info_idx = tray_info_idx
  1201. effective_setting_id = setting_id
  1202. if printer_kp and printer_kp.filament_id:
  1203. if not printer_kp.filament_id.startswith("PFUS"):
  1204. effective_tray_info_idx = printer_kp.filament_id
  1205. if printer_kp.setting_id:
  1206. effective_setting_id = printer_kp.setting_id
  1207. elif matching_kp and matching_kp.setting_id:
  1208. derived = normalize_slicer_filament(matching_kp.setting_id)[0]
  1209. if derived and not derived.startswith("PFUS"):
  1210. effective_tray_info_idx = derived
  1211. effective_setting_id = matching_kp.setting_id
  1212. if effective_tray_info_idx != tray_info_idx or effective_setting_id != setting_id:
  1213. logger.info(
  1214. "Spoolman assign: realigning tray_info_idx %r → %r, setting_id %r → %r (kp_id=%s, source=%s)",
  1215. tray_info_idx,
  1216. effective_tray_info_idx,
  1217. setting_id,
  1218. effective_setting_id,
  1219. matching_kp.id if matching_kp else None,
  1220. "printer" if printer_kp else "stored",
  1221. )
  1222. mqtt_client.ams_set_filament_setting(
  1223. ams_id=body.ams_id,
  1224. tray_id=body.tray_id,
  1225. tray_info_idx=effective_tray_info_idx,
  1226. tray_type=tray_type,
  1227. tray_sub_brands=tray_sub_brands,
  1228. tray_color=tray_color,
  1229. nozzle_temp_min=temp_min,
  1230. nozzle_temp_max=temp_max,
  1231. setting_id=effective_setting_id,
  1232. )
  1233. if matching_kp and matching_kp.cali_idx is not None:
  1234. # Use printer-reported filament_id when available, otherwise
  1235. # fall back to the realigned tray_info_idx so both commands
  1236. # reference the same filament context.
  1237. cali_filament_id = (
  1238. printer_kp.filament_id if printer_kp and printer_kp.filament_id else None
  1239. ) or effective_tray_info_idx
  1240. mqtt_client.extrusion_cali_sel(
  1241. ams_id=body.ams_id,
  1242. tray_id=body.tray_id,
  1243. cali_idx=matching_kp.cali_idx,
  1244. filament_id=cali_filament_id,
  1245. nozzle_diameter=nozzle_diameter,
  1246. )
  1247. logger.info(
  1248. "Spoolman assign: applied K-profile cali_idx=%d "
  1249. "(kp_id=%d, filament_id=%s) for spool %d on printer %d AMS%d-T%d",
  1250. matching_kp.cali_idx,
  1251. matching_kp.id,
  1252. cali_filament_id,
  1253. body.spoolman_spool_id,
  1254. body.printer_id,
  1255. body.ams_id,
  1256. body.tray_id,
  1257. )
  1258. else:
  1259. # No stored K-profile for this spool — always reset the slot to
  1260. # Default K (cali_idx=-1). The live cali_idx belongs to whatever
  1261. # filament was there before, so preserving it would apply the
  1262. # wrong filament's calibration to the new spool.
  1263. mqtt_client.extrusion_cali_sel(
  1264. ams_id=body.ams_id,
  1265. tray_id=body.tray_id,
  1266. cali_idx=-1,
  1267. filament_id=effective_tray_info_idx,
  1268. nozzle_diameter=nozzle_diameter,
  1269. )
  1270. logger.info(
  1271. "No stored K-profile for Spoolman spool %d — reset slot to Default K (cali_idx=-1)",
  1272. body.spoolman_spool_id,
  1273. )
  1274. logger.info(
  1275. "Auto-configured AMS slot ams=%d tray=%d for Spoolman spool %d on printer %d",
  1276. body.ams_id,
  1277. body.tray_id,
  1278. body.spoolman_spool_id,
  1279. body.printer_id,
  1280. )
  1281. except Exception:
  1282. logger.exception(
  1283. "Failed to auto-configure AMS slot for Spoolman spool %d (printer=%d, ams=%d, tray=%d)",
  1284. body.spoolman_spool_id,
  1285. body.printer_id,
  1286. body.ams_id,
  1287. body.tray_id,
  1288. )
  1289. return mapped
  1290. @router.delete("/slot-assignments/{spoolman_spool_id}")
  1291. async def unassign_spoolman_slot(
  1292. spoolman_spool_id: int = Path(..., gt=0),
  1293. db: AsyncSession = Depends(get_db),
  1294. _: User | None = RequirePermissionIfAuthEnabled(Permission.INVENTORY_UPDATE),
  1295. ) -> dict:
  1296. """Remove the local slot assignment for a Spoolman spool.
  1297. Spoolman's own ``spool.location`` field is NOT touched — it is user-managed.
  1298. """
  1299. client = await _get_client(db)
  1300. try:
  1301. await db.execute(
  1302. delete(SpoolmanSlotAssignment).where(SpoolmanSlotAssignment.spoolman_spool_id == spoolman_spool_id)
  1303. )
  1304. await db.commit()
  1305. except Exception as exc:
  1306. await db.rollback()
  1307. logger.error("Failed to delete slot assignment: %s", exc)
  1308. raise HTTPException(status_code=500, detail="Failed to remove slot assignment") from exc
  1309. # Fetch the spool from Spoolman to return in InventorySpool format.
  1310. # If the spool no longer exists in Spoolman, the local unassignment still succeeded.
  1311. try:
  1312. async with _translate_spoolman_errors():
  1313. spool = await client.get_spool(spoolman_spool_id)
  1314. return _map_spoolman_spool(spool)
  1315. except HTTPException as exc:
  1316. if exc.status_code != 404:
  1317. raise
  1318. # Spool no longer exists in Spoolman; unassignment still succeeded.
  1319. return {"id": spoolman_spool_id}
  1320. @router.get("/slot-assignments")
  1321. async def get_spoolman_slot_assignment(
  1322. printer_id: int = Query(..., gt=0),
  1323. ams_id: int = Query(..., ge=0, le=7),
  1324. tray_id: int = Query(..., ge=0, le=3),
  1325. db: AsyncSession = Depends(get_db),
  1326. _: User | None = RequirePermissionIfAuthEnabled(Permission.INVENTORY_READ),
  1327. ) -> dict | None:
  1328. """Return the Spoolman spool assigned to a specific printer slot, or null if unassigned."""
  1329. client = await _get_client(db)
  1330. result = await db.execute(select(Printer).where(Printer.id == printer_id))
  1331. printer = result.scalar_one_or_none()
  1332. if not printer:
  1333. raise HTTPException(status_code=404, detail="Printer not found")
  1334. slot_result = await db.execute(
  1335. select(SpoolmanSlotAssignment).where(
  1336. SpoolmanSlotAssignment.printer_id == printer_id,
  1337. SpoolmanSlotAssignment.ams_id == ams_id,
  1338. SpoolmanSlotAssignment.tray_id == tray_id,
  1339. )
  1340. )
  1341. slot = slot_result.scalar_one_or_none()
  1342. if not slot:
  1343. return None
  1344. try:
  1345. async with _translate_spoolman_errors():
  1346. spool = await client.get_spool(slot.spoolman_spool_id)
  1347. return _map_spoolman_spool(spool)
  1348. except HTTPException as exc:
  1349. if exc.status_code != 404:
  1350. raise
  1351. # Spool deleted in Spoolman — clean up stale assignment.
  1352. # Include spoolman_spool_id in WHERE to avoid a TOCTOU race where a
  1353. # concurrent re-assign changed the slot to a different spool between
  1354. # the GET and this DELETE.
  1355. try:
  1356. await db.execute(
  1357. delete(SpoolmanSlotAssignment).where(
  1358. SpoolmanSlotAssignment.id == slot.id,
  1359. SpoolmanSlotAssignment.spoolman_spool_id == slot.spoolman_spool_id,
  1360. )
  1361. )
  1362. await db.commit()
  1363. except Exception as cleanup_exc:
  1364. await db.rollback()
  1365. logger.warning(
  1366. "Failed to remove stale slot assignment for spool %s: %s",
  1367. slot.spoolman_spool_id,
  1368. cleanup_exc,
  1369. )
  1370. return None
  1371. def _k_profile_to_dict(p: SpoolmanKProfile) -> dict:
  1372. """Manually map SpoolmanKProfile → SpoolKProfileResponse-compatible dict."""
  1373. return {
  1374. "id": p.id,
  1375. "spool_id": p.spoolman_spool_id,
  1376. "printer_id": p.printer_id,
  1377. "extruder": p.extruder,
  1378. "nozzle_diameter": p.nozzle_diameter,
  1379. "nozzle_type": p.nozzle_type,
  1380. "k_value": p.k_value,
  1381. "name": p.name,
  1382. "cali_idx": p.cali_idx,
  1383. "setting_id": p.setting_id,
  1384. "created_at": p.created_at,
  1385. }
  1386. def _normalize_filament(raw: dict) -> NormalizedFilament | None:
  1387. """Normalise a raw Spoolman filament dict for the frontend catalog picker.
  1388. Returns None for entries with missing/zero IDs — those are malformed and
  1389. must be filtered out before returning to the client.
  1390. weight=0 is collapsed to None — 0g is not a valid filament weight.
  1391. """
  1392. filament_id = _safe_int(raw.get("id"), 0)
  1393. if filament_id <= 0:
  1394. logger.warning("Skipping Spoolman filament with missing or invalid id: %r", raw.get("name"))
  1395. return None
  1396. vendor = raw.get("vendor") or {}
  1397. vendor_ref: NormalizedVendorRef | None = None
  1398. if vendor:
  1399. vendor_id = _safe_int(vendor.get("id"), 0)
  1400. if vendor_id <= 0:
  1401. logger.warning("Spoolman filament %d has vendor without valid id — vendor omitted", filament_id)
  1402. else:
  1403. vendor_ref = {"id": vendor_id, "name": str(vendor.get("name") or "").strip() or "Unknown"}
  1404. return NormalizedFilament(
  1405. id=filament_id,
  1406. name=str(raw.get("name") or ""),
  1407. material=raw.get("material") or None,
  1408. color_hex=raw.get("color_hex") or None,
  1409. color_name=raw.get("color_name") or None,
  1410. weight=_safe_int(raw.get("weight"), 0) or None, # 0g is not a valid weight
  1411. spool_weight=_safe_optional_float(raw.get("spool_weight")),
  1412. vendor=vendor_ref,
  1413. )
  1414. @router.get("/filaments")
  1415. async def list_spoolman_filaments(
  1416. db: AsyncSession = Depends(get_db),
  1417. _: User | None = RequirePermissionIfAuthEnabled(Permission.INVENTORY_READ),
  1418. ) -> list[NormalizedFilament]:
  1419. """Return all filaments from Spoolman, normalised for the frontend catalog picker."""
  1420. client = await _get_client(db)
  1421. async with _translate_spoolman_errors():
  1422. raw_filaments = await client.get_filaments()
  1423. if not isinstance(raw_filaments, list):
  1424. logger.warning("Spoolman get_filaments() returned non-list type: %s", type(raw_filaments).__name__)
  1425. return []
  1426. return [f for raw in raw_filaments if (f := _normalize_filament(raw)) is not None]
  1427. @router.patch("/filaments/{filament_id}")
  1428. async def patch_spoolman_filament(
  1429. *,
  1430. filament_id: int = Path(..., gt=0),
  1431. body: SpoolmanFilamentPatch = Body(...),
  1432. db: AsyncSession = Depends(get_db),
  1433. _: User | None = RequirePermissionIfAuthEnabled(Permission.INVENTORY_UPDATE),
  1434. ) -> NormalizedFilament:
  1435. """Update a Spoolman filament's name and/or spool_weight.
  1436. When spool_weight changes, Option A (keep_existing_spools=True) stamps the old
  1437. weight onto spools currently inheriting it (spool.spool_weight is None) so their
  1438. tare calculations are unaffected by the filament change.
  1439. Option B (keep_existing_spools=False, the default): when spool_weight is a
  1440. concrete value, stamps it onto every affected spool explicitly; when spool_weight
  1441. is null, clears per-spool overrides so spools fall back to the filament value.
  1442. """
  1443. client = await _get_client(db)
  1444. async with _translate_spoolman_errors():
  1445. current = await client.get_filament(filament_id)
  1446. patch_data = {k: v for k, v in body.model_dump(exclude_unset=True).items() if k != "keep_existing_spools"}
  1447. if not patch_data:
  1448. normalized = _normalize_filament(current)
  1449. if normalized is None:
  1450. raise HTTPException(status_code=404, detail="Filament not found")
  1451. return normalized
  1452. async with _translate_spoolman_errors():
  1453. updated = await client.patch_filament(filament_id, patch_data)
  1454. if "spool_weight" in body.model_fields_set:
  1455. async with _translate_spoolman_errors():
  1456. all_spools = await client.get_all_spools()
  1457. affected_spools = [s for s in all_spools if (s.get("filament") or {}).get("id") == filament_id]
  1458. if affected_spools:
  1459. if body.keep_existing_spools:
  1460. old_weight = _safe_optional_float(current.get("spool_weight"))
  1461. if old_weight is not None:
  1462. spools_to_fix = [s for s in affected_spools if s.get("spool_weight") is None]
  1463. if spools_to_fix:
  1464. async with _translate_spoolman_errors():
  1465. results = await asyncio.gather(
  1466. *(
  1467. client.update_spool_full(spool_id=s["id"], spool_weight=old_weight)
  1468. for s in spools_to_fix
  1469. ),
  1470. return_exceptions=True,
  1471. )
  1472. _raise_if_partial_failure(spools_to_fix, results, "spool_weight stamp (option A)")
  1473. else:
  1474. new_weight = body.spool_weight
  1475. if new_weight is not None:
  1476. # Stamp the new weight onto every spool of this filament type so
  1477. # each spool carries the value explicitly rather than inheriting.
  1478. async with _translate_spoolman_errors():
  1479. results = await asyncio.gather(
  1480. *(
  1481. client.update_spool_full(spool_id=s["id"], spool_weight=new_weight)
  1482. for s in affected_spools
  1483. ),
  1484. return_exceptions=True,
  1485. )
  1486. _raise_if_partial_failure(affected_spools, results, "spool_weight stamp (option B)")
  1487. else:
  1488. # Filament weight is being cleared — remove any per-spool override
  1489. # so spools fall back to whatever the filament now provides.
  1490. spools_to_clear = [s for s in affected_spools if s.get("spool_weight") is not None]
  1491. if spools_to_clear:
  1492. async with _translate_spoolman_errors():
  1493. results = await asyncio.gather(
  1494. *(
  1495. client.update_spool_full(spool_id=s["id"], clear_spool_weight=True)
  1496. for s in spools_to_clear
  1497. ),
  1498. return_exceptions=True,
  1499. )
  1500. _raise_if_partial_failure(spools_to_clear, results, "spool_weight clear (option B null)")
  1501. normalized = _normalize_filament(updated)
  1502. if normalized is None:
  1503. raise HTTPException(status_code=502, detail="Spoolman returned malformed filament data")
  1504. return normalized
  1505. @router.get("/spools/{spool_id}/k-profiles")
  1506. async def get_spoolman_k_profiles(
  1507. spool_id: int = Path(..., gt=0),
  1508. db: AsyncSession = Depends(get_db),
  1509. _: User | None = RequirePermissionIfAuthEnabled(Permission.INVENTORY_READ),
  1510. ) -> list[dict]:
  1511. """Return all local K-value calibration profiles for a Spoolman spool."""
  1512. await _get_client(db)
  1513. result = await db.execute(select(SpoolmanKProfile).where(SpoolmanKProfile.spoolman_spool_id == spool_id))
  1514. profiles = result.scalars().all()
  1515. return [_k_profile_to_dict(p) for p in profiles]
  1516. @router.put("/spools/{spool_id}/k-profiles")
  1517. async def save_spoolman_k_profiles(
  1518. spool_id: int = Path(..., gt=0),
  1519. profiles: list[SpoolKProfileBase] = Body(...),
  1520. db: AsyncSession = Depends(get_db),
  1521. _: User | None = RequirePermissionIfAuthEnabled(Permission.INVENTORY_UPDATE),
  1522. ) -> list[dict]:
  1523. """Replace all K-value calibration profiles for a Spoolman spool."""
  1524. client = await _get_client(db)
  1525. async with _translate_spoolman_errors():
  1526. await client.get_spool(spool_id)
  1527. saved: list[SpoolmanKProfile] = []
  1528. try:
  1529. await db.execute(delete(SpoolmanKProfile).where(SpoolmanKProfile.spoolman_spool_id == spool_id))
  1530. for profile in profiles:
  1531. obj = SpoolmanKProfile(
  1532. spoolman_spool_id=spool_id,
  1533. printer_id=profile.printer_id,
  1534. extruder=profile.extruder,
  1535. nozzle_diameter=profile.nozzle_diameter,
  1536. nozzle_type=profile.nozzle_type,
  1537. k_value=profile.k_value,
  1538. name=profile.name,
  1539. cali_idx=profile.cali_idx,
  1540. setting_id=profile.setting_id,
  1541. )
  1542. db.add(obj)
  1543. saved.append(obj)
  1544. await db.commit()
  1545. except IntegrityError as exc:
  1546. await db.rollback()
  1547. raise HTTPException(422, "Duplicate or invalid K-profile (check printer_id and nozzle uniqueness)") from exc
  1548. except Exception as exc:
  1549. await db.rollback()
  1550. logger.error("K-profile save for spool %d failed: %s", spool_id, exc)
  1551. raise HTTPException(500, "Failed to save K-profiles") from exc
  1552. for obj in saved:
  1553. await db.refresh(obj)
  1554. return [_k_profile_to_dict(p) for p in saved]