spoolman_inventory.py 70 KB

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