cloud.py 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174
  1. """
  2. Bambu Lab Cloud API Routes
  3. Handles authentication and profile management with Bambu Cloud.
  4. """
  5. import json
  6. import logging
  7. from pathlib import Path
  8. from typing import Literal
  9. from fastapi import APIRouter, Body, Depends, Header, HTTPException, Request
  10. from fastapi.security import HTTPAuthorizationCredentials
  11. from sqlalchemy import select
  12. from sqlalchemy.ext.asyncio import AsyncSession
  13. from backend.app.core.auth import (
  14. RequirePermissionIfAuthEnabled,
  15. _user_from_api_key,
  16. _validate_api_key,
  17. require_permission_if_auth_enabled,
  18. security,
  19. )
  20. from backend.app.core.database import get_db
  21. from backend.app.core.permissions import Permission
  22. from backend.app.models.api_key import APIKey
  23. from backend.app.models.settings import Settings
  24. from backend.app.models.user import User
  25. from backend.app.schemas.cloud import (
  26. CloudAuthStatus,
  27. CloudDevice,
  28. CloudLoginRequest,
  29. CloudLoginResponse,
  30. CloudTokenRequest,
  31. CloudVerifyRequest,
  32. FirmwareUpdateInfo,
  33. FirmwareUpdatesResponse,
  34. SlicerSetting,
  35. SlicerSettingCreate,
  36. SlicerSettingDeleteResponse,
  37. SlicerSettingsResponse,
  38. SlicerSettingUpdate,
  39. )
  40. from backend.app.services.bambu_cloud import (
  41. _SLICER_API_VERSION,
  42. BambuCloudAuthError,
  43. BambuCloudError,
  44. BambuCloudService,
  45. )
  46. from backend.app.utils.filament_ids import filament_id_to_setting_id
  47. logger = logging.getLogger(__name__)
  48. async def _cloud_api_key_gate(
  49. request: Request,
  50. credentials: HTTPAuthorizationCredentials | None = Depends(security),
  51. x_api_key: str | None = Header(default=None, alias="X-API-Key"),
  52. db: AsyncSession = Depends(get_db),
  53. ) -> None:
  54. """Router-level dependency: enforce API-key cloud-access fences (#1182).
  55. Runs before every /cloud/* handler. JWT-authed and anonymous callers are
  56. no-ops — their access is gated by the per-route ``Permission.CLOUD_AUTH``
  57. / ``Permission.FILAMENTS_READ`` / etc. dependency. API-keyed callers
  58. must have an owner and ``can_access_cloud=True``; legacy ownerless keys
  59. and keys without the cloud scope are rejected here.
  60. On a successful API-keyed request the owner User is stashed on
  61. ``request.state.api_key_owner`` so route handlers can resolve it via
  62. ``cloud_caller`` (the auth gate returns None for API keys to avoid a
  63. wider behaviour change in non-cloud routes — see auth.py).
  64. The dep duplicates the API-key validation done by the regular auth gate
  65. (which runs as a route-level dep, *after* router-level deps). The cost
  66. is one extra ``SELECT FROM api_keys`` per /cloud/* request — bounded and
  67. cheap (key_prefix is indexed).
  68. """
  69. api_key_value: str | None = None
  70. if x_api_key:
  71. api_key_value = x_api_key
  72. elif credentials and credentials.credentials.startswith("bb_"):
  73. api_key_value = credentials.credentials
  74. if api_key_value is None:
  75. return # JWT or anonymous — no-op
  76. api_key = await _validate_api_key(db, api_key_value)
  77. if api_key is None:
  78. # Invalid key — let the route-level auth gate produce the 401 so the
  79. # error matches what every other route returns for a bad key.
  80. return
  81. _assert_api_key_can_access_cloud(api_key)
  82. # All fences passed. Stash the owner so cloud routes can resolve their
  83. # caller User without going through the auth gate (which intentionally
  84. # returns None for API keys to keep #1182 surface-bounded to /cloud/*).
  85. request.state.api_key_owner = await _user_from_api_key(db, api_key)
  86. def cloud_caller(*permissions: Permission):
  87. """Route-level dep factory for /cloud/* handlers.
  88. Returns a Depends that resolves to:
  89. - the JWT-authenticated User (when a JWT is present and the route's
  90. permission set is satisfied), OR
  91. - the API-key owner User stashed by the router-level gate
  92. (``request.state.api_key_owner``), OR
  93. - None when auth is disabled.
  94. Replaces the direct ``RequirePermissionIfAuthEnabled(...)`` dep on cloud
  95. routes so API-keyed callers get the *owner* in ``current_user`` rather
  96. than None — without that the route falls back to the global Settings
  97. cloud_token, which is empty in auth-enabled deployments.
  98. """
  99. base_dep = require_permission_if_auth_enabled(*permissions)
  100. async def resolved(
  101. request: Request,
  102. base_user: User | None = Depends(base_dep),
  103. ) -> User | None:
  104. if base_user is not None:
  105. return base_user
  106. return getattr(request.state, "api_key_owner", None)
  107. return Depends(resolved)
  108. async def resolve_api_key_cloud_owner(
  109. credentials: HTTPAuthorizationCredentials | None = Depends(security),
  110. x_api_key: str | None = Header(default=None, alias="X-API-Key"),
  111. db: AsyncSession = Depends(get_db),
  112. ) -> User | None:
  113. """Route-level dep for non-/cloud/* endpoints that need to read the
  114. caller's stored Bambu Cloud token (e.g. the slice path resolving cloud
  115. presets — #1182 follow-up).
  116. Returns the API key's owner User when the caller is an API-keyed
  117. request *and* the key has ``can_access_cloud=True``; returns None for
  118. JWT, anonymous, or API keys without the cloud scope. The caller is
  119. expected to fall back to the JWT-authed ``current_user`` first and use
  120. this dep's result only when ``current_user`` is None.
  121. Unlike ``_cloud_api_key_gate`` (which 403s legacy/non-cloud keys at the
  122. router level), this dep is permissive: it returns None instead of
  123. raising, so a slice request via an API key without cloud scope still
  124. runs against local presets. The downstream cloud-token check in
  125. ``preset_resolver._resolve_cloud`` produces the right 400 if the
  126. request actually selects a cloud preset.
  127. """
  128. api_key_value: str | None = None
  129. if x_api_key:
  130. api_key_value = x_api_key
  131. elif credentials and credentials.credentials.startswith("bb_"):
  132. api_key_value = credentials.credentials
  133. if api_key_value is None:
  134. return None
  135. api_key = await _validate_api_key(db, api_key_value)
  136. if api_key is None or api_key.user_id is None or not api_key.can_access_cloud:
  137. return None
  138. return await _user_from_api_key(db, api_key)
  139. router = APIRouter(prefix="/cloud", tags=["cloud"], dependencies=[Depends(_cloud_api_key_gate)])
  140. # Keys for storing cloud credentials in settings
  141. CLOUD_TOKEN_KEY = "bambu_cloud_token"
  142. CLOUD_EMAIL_KEY = "bambu_cloud_email"
  143. CLOUD_REGION_KEY = "bambu_cloud_region"
  144. def _normalise_region(region: str | None) -> str:
  145. """Treat NULL/empty as 'global' for legacy rows that predate the region column."""
  146. return region if region in ("global", "china") else "global"
  147. async def get_stored_token(db: AsyncSession, user: User | None = None) -> tuple[str | None, str | None, str]:
  148. """Get stored cloud token, email, and region.
  149. When a user is provided (auth enabled), returns that user's per-user credentials.
  150. When user is None (auth disabled), falls back to global Settings table.
  151. Region defaults to ``"global"`` when unset (including for rows that predate
  152. the ``cloud_region`` column).
  153. """
  154. if user is not None:
  155. return user.cloud_token, user.cloud_email, _normalise_region(user.cloud_region)
  156. # Fallback: global storage (auth disabled)
  157. result = await db.execute(
  158. select(Settings).where(Settings.key.in_([CLOUD_TOKEN_KEY, CLOUD_EMAIL_KEY, CLOUD_REGION_KEY]))
  159. )
  160. settings = {s.key: s.value for s in result.scalars().all()}
  161. return (
  162. settings.get(CLOUD_TOKEN_KEY),
  163. settings.get(CLOUD_EMAIL_KEY),
  164. _normalise_region(settings.get(CLOUD_REGION_KEY)),
  165. )
  166. async def store_token(db: AsyncSession, token: str, email: str, region: str, user: User | None = None) -> None:
  167. """Store cloud token, email, and region.
  168. When a user is provided (auth enabled), stores on the user record.
  169. When user is None (auth disabled), stores in global Settings table.
  170. """
  171. region = _normalise_region(region)
  172. if user is not None:
  173. # User object is from the auth dependency's session (detached),
  174. # so use a direct UPDATE via the route's db session.
  175. from sqlalchemy import update
  176. await db.execute(
  177. update(User).where(User.id == user.id).values(cloud_token=token, cloud_email=email, cloud_region=region)
  178. )
  179. await db.commit()
  180. return
  181. # Fallback: global storage (auth disabled)
  182. for key, value in [(CLOUD_TOKEN_KEY, token), (CLOUD_EMAIL_KEY, email), (CLOUD_REGION_KEY, region)]:
  183. result = await db.execute(select(Settings).where(Settings.key == key))
  184. setting = result.scalar_one_or_none()
  185. if setting:
  186. setting.value = value
  187. else:
  188. db.add(Settings(key=key, value=value))
  189. await db.commit()
  190. async def clear_token(db: AsyncSession, user: User | None = None) -> None:
  191. """Clear stored cloud token, email, and region.
  192. When a user is provided (auth enabled), clears that user's credentials.
  193. When user is None (auth disabled), clears from global Settings table.
  194. """
  195. if user is not None:
  196. from sqlalchemy import update
  197. await db.execute(
  198. update(User).where(User.id == user.id).values(cloud_token=None, cloud_email=None, cloud_region=None)
  199. )
  200. await db.commit()
  201. return
  202. # Fallback: global storage (auth disabled)
  203. result = await db.execute(
  204. select(Settings).where(Settings.key.in_([CLOUD_TOKEN_KEY, CLOUD_EMAIL_KEY, CLOUD_REGION_KEY]))
  205. )
  206. for setting in result.scalars().all():
  207. await db.delete(setting)
  208. await db.commit()
  209. def _assert_api_key_can_access_cloud(api_key: APIKey) -> None:
  210. """Reject API keys that aren't authorised to read cloud data.
  211. Three independent fences for API keys (#1182):
  212. 1. user_id IS NOT NULL — legacy keys created before per-user ownership
  213. have no owner whose cloud_token we could read; force recreate.
  214. 2. can_access_cloud=True — opt-in scope so existing automation doesn't
  215. start reading cloud data without the operator explicitly enabling it.
  216. 3. owner has stored cloud_token — enforced separately at the route
  217. level via ``build_authenticated_cloud`` returning None.
  218. """
  219. if api_key.user_id is None:
  220. raise HTTPException(
  221. status_code=401,
  222. detail=(
  223. "This API key was created before per-user cloud access was supported. "
  224. "Recreate it from Settings → API Keys to use /cloud/* endpoints."
  225. ),
  226. )
  227. if not api_key.can_access_cloud:
  228. raise HTTPException(
  229. status_code=403,
  230. detail=(
  231. "This API key is not authorised to access Bambu Cloud data. "
  232. "Enable 'Allow cloud access' on the key in Settings → API Keys."
  233. ),
  234. )
  235. async def build_authenticated_cloud(db: AsyncSession, user: User | None) -> BambuCloudService | None:
  236. """Build a per-request cloud service seeded with the caller's stored token + region.
  237. Returns ``None`` when no token is stored, so callers can 401 without constructing
  238. (and then closing) a useless client. Caller is responsible for ``await cloud.close()``.
  239. """
  240. token, _email, region = await get_stored_token(db, user)
  241. if not token:
  242. return None
  243. cloud = BambuCloudService(region=region)
  244. cloud.set_token(token)
  245. return cloud
  246. @router.get("/status", response_model=CloudAuthStatus)
  247. async def get_auth_status(
  248. db: AsyncSession = Depends(get_db),
  249. current_user: User | None = cloud_caller(Permission.CLOUD_AUTH),
  250. ):
  251. """Get current cloud authentication status.
  252. Reads the stored credentials in one DB round-trip (we used to call
  253. ``get_stored_token`` twice — once here and once inside
  254. ``build_authenticated_cloud``). ``region`` is exposed so the frontend can
  255. show "Connected (China)" after a reload without relying on local state.
  256. """
  257. token, email, region = await get_stored_token(db, current_user)
  258. if not token:
  259. return CloudAuthStatus(is_authenticated=False, email=None, region=None)
  260. cloud = BambuCloudService(region=region)
  261. cloud.set_token(token)
  262. try:
  263. authenticated = cloud.is_authenticated
  264. return CloudAuthStatus(
  265. is_authenticated=authenticated,
  266. email=email if authenticated else None,
  267. region=region if authenticated else None,
  268. )
  269. finally:
  270. await cloud.close()
  271. @router.post("/login", response_model=CloudLoginResponse)
  272. async def login(
  273. request: CloudLoginRequest,
  274. db: AsyncSession = Depends(get_db),
  275. current_user: User | None = cloud_caller(Permission.CLOUD_AUTH),
  276. ):
  277. """
  278. Initiate login to Bambu Cloud.
  279. This will trigger either:
  280. - Email verification: A code is sent to the user's email
  281. - TOTP verification: User enters code from their authenticator app
  282. After receiving/generating the code, call /cloud/verify to complete the login.
  283. For TOTP, include the tfa_key from this response in the verify request.
  284. """
  285. cloud = BambuCloudService(region=request.region)
  286. try:
  287. result = await cloud.login_request(request.email, request.password)
  288. if result.get("success") and cloud.access_token:
  289. # Direct login succeeded (rare)
  290. await store_token(db, cloud.access_token, request.email, request.region, current_user)
  291. return CloudLoginResponse(
  292. success=result.get("success", False),
  293. needs_verification=result.get("needs_verification", False),
  294. message=result.get("message", "Unknown error"),
  295. verification_type=result.get("verification_type"),
  296. tfa_key=result.get("tfa_key"),
  297. )
  298. except BambuCloudAuthError as e:
  299. raise HTTPException(status_code=401, detail=str(e))
  300. except BambuCloudError as e:
  301. raise HTTPException(status_code=500, detail=str(e))
  302. finally:
  303. await cloud.close()
  304. @router.post("/verify", response_model=CloudLoginResponse)
  305. async def verify_code(
  306. request: CloudVerifyRequest,
  307. db: AsyncSession = Depends(get_db),
  308. current_user: User | None = cloud_caller(Permission.CLOUD_AUTH),
  309. ):
  310. """
  311. Complete login with verification code (email or TOTP).
  312. For email verification:
  313. - After calling /cloud/login, the user receives an email with a 6-digit code
  314. - Submit the code with email address
  315. For TOTP verification:
  316. - The user enters the 6-digit code from their authenticator app
  317. - Include the tfa_key from the /cloud/login response
  318. ``request.region`` must match the region used in /cloud/login so that the
  319. TOTP call hits the correct TFA endpoint (bambulab.com vs bambulab.cn).
  320. """
  321. cloud = BambuCloudService(region=request.region)
  322. try:
  323. # Use TOTP verification if tfa_key is provided
  324. if request.tfa_key:
  325. result = await cloud.verify_totp(request.tfa_key, request.code)
  326. else:
  327. result = await cloud.verify_code(request.email, request.code)
  328. if result.get("success") and cloud.access_token:
  329. await store_token(db, cloud.access_token, request.email, request.region, current_user)
  330. return CloudLoginResponse(
  331. success=result.get("success", False),
  332. needs_verification=False,
  333. message=result.get("message", "Unknown error"),
  334. )
  335. except BambuCloudAuthError as e:
  336. raise HTTPException(status_code=401, detail=str(e))
  337. except BambuCloudError as e:
  338. raise HTTPException(status_code=500, detail=str(e))
  339. finally:
  340. await cloud.close()
  341. @router.post("/token", response_model=CloudAuthStatus)
  342. async def set_token(
  343. request: CloudTokenRequest,
  344. db: AsyncSession = Depends(get_db),
  345. current_user: User | None = cloud_caller(Permission.CLOUD_AUTH),
  346. ):
  347. """
  348. Set access token directly.
  349. For users who already have a token (e.g., from Bambu Studio). The
  350. selected ``region`` is persisted alongside the token so every subsequent
  351. request hits the right Bambu API endpoint, including after a restart.
  352. """
  353. cloud = BambuCloudService(region=request.region)
  354. cloud.set_token(request.access_token)
  355. try:
  356. # Verify token works by trying to get profile
  357. await cloud.get_user_profile()
  358. await store_token(db, request.access_token, "token-auth", request.region, current_user)
  359. return CloudAuthStatus(is_authenticated=True, email="token-auth")
  360. except BambuCloudError:
  361. raise HTTPException(status_code=401, detail="Invalid token")
  362. finally:
  363. await cloud.close()
  364. @router.post("/logout")
  365. async def logout(
  366. db: AsyncSession = Depends(get_db),
  367. current_user: User | None = cloud_caller(Permission.CLOUD_AUTH),
  368. ):
  369. """Log out of Bambu Cloud."""
  370. await clear_token(db, current_user)
  371. return {"success": True}
  372. @router.get("/settings", response_model=SlicerSettingsResponse)
  373. async def get_slicer_settings(
  374. version: str = _SLICER_API_VERSION,
  375. db: AsyncSession = Depends(get_db),
  376. current_user: User | None = cloud_caller(Permission.CLOUD_AUTH),
  377. ):
  378. """
  379. Get all slicer settings (filament, printer, process presets).
  380. Requires authentication.
  381. """
  382. cloud = await build_authenticated_cloud(db, current_user)
  383. if cloud is None or not cloud.is_authenticated:
  384. raise HTTPException(status_code=401, detail="Not authenticated")
  385. try:
  386. data = await cloud.get_slicer_settings(version)
  387. result = SlicerSettingsResponse()
  388. # Map API keys to our types (API uses 'print' for process presets)
  389. type_mapping = {
  390. "filament": "filament",
  391. "printer": "printer",
  392. "print": "process", # API calls it 'print', we call it 'process'
  393. }
  394. for api_key, our_type in type_mapping.items():
  395. type_data = data.get(api_key, {})
  396. private_settings = type_data.get("private", [])
  397. public_settings = type_data.get("public", [])
  398. parsed = []
  399. # Private (custom) presets first
  400. for s in private_settings:
  401. parsed.append(
  402. SlicerSetting(
  403. setting_id=s.get("setting_id", s.get("id", "")),
  404. name=s.get("name", "Unknown"),
  405. type=our_type,
  406. version=s.get("version"),
  407. user_id=s.get("user_id"),
  408. updated_time=s.get("updated_time"),
  409. is_custom=True,
  410. )
  411. )
  412. # Public (default) presets
  413. for s in public_settings:
  414. parsed.append(
  415. SlicerSetting(
  416. setting_id=s.get("setting_id", s.get("id", "")),
  417. name=s.get("name", "Unknown"),
  418. type=our_type,
  419. version=s.get("version"),
  420. user_id=s.get("user_id"),
  421. updated_time=s.get("updated_time"),
  422. is_custom=False,
  423. )
  424. )
  425. setattr(result, our_type, parsed)
  426. return result
  427. except BambuCloudAuthError:
  428. await clear_token(db, current_user)
  429. raise HTTPException(status_code=401, detail="Authentication expired")
  430. except BambuCloudError as e:
  431. raise HTTPException(status_code=500, detail=str(e))
  432. finally:
  433. await cloud.close()
  434. @router.get("/settings/{setting_id}")
  435. async def get_setting_detail(
  436. setting_id: str,
  437. db: AsyncSession = Depends(get_db),
  438. current_user: User | None = cloud_caller(Permission.CLOUD_AUTH),
  439. ):
  440. """
  441. Get detailed information for a specific setting/preset.
  442. Returns the full preset configuration.
  443. """
  444. cloud = await build_authenticated_cloud(db, current_user)
  445. if cloud is None or not cloud.is_authenticated:
  446. raise HTTPException(status_code=401, detail="Not authenticated")
  447. try:
  448. data = await cloud.get_setting_detail(setting_id)
  449. return data
  450. except BambuCloudAuthError:
  451. await clear_token(db, current_user)
  452. raise HTTPException(status_code=401, detail="Authentication expired")
  453. except BambuCloudError as e:
  454. raise HTTPException(status_code=500, detail=str(e))
  455. finally:
  456. await cloud.close()
  457. @router.get("/filaments", response_model=list[SlicerSetting])
  458. async def get_filament_presets(
  459. version: str = _SLICER_API_VERSION,
  460. db: AsyncSession = Depends(get_db),
  461. current_user: User | None = cloud_caller(Permission.FILAMENTS_READ),
  462. ):
  463. """
  464. Get just filament presets (convenience endpoint).
  465. Returns all filament presets with custom presets first.
  466. Uses the same cache as get_slicer_settings.
  467. """
  468. settings = await get_slicer_settings(version=version, db=db, current_user=current_user)
  469. return settings.filament
  470. # Cache for filament preset info (setting_id -> {name, k})
  471. _filament_cache: dict[str, dict] = {}
  472. _filament_cache_time: float = 0
  473. FILAMENT_CACHE_TTL = 300 # 5 minutes
  474. # Built-in filament ID → name mapping (fallback when cloud API and local profiles
  475. # don't have the entry). Based on Bambu Lab's known filament catalogue.
  476. _BUILTIN_FILAMENT_NAMES: dict[str, str] = {
  477. "GFA00": "Bambu PLA Basic",
  478. "GFA01": "Bambu PLA Matte",
  479. "GFA02": "Bambu PLA Metal",
  480. "GFA05": "Bambu PLA Silk",
  481. "GFA06": "Bambu PLA Silk+",
  482. "GFA07": "Bambu PLA Marble",
  483. "GFA08": "Bambu PLA Sparkle",
  484. "GFA09": "Bambu PLA Tough",
  485. "GFA11": "Bambu PLA Aero",
  486. "GFA12": "Bambu PLA Glow",
  487. "GFA13": "Bambu PLA Dynamic",
  488. "GFA15": "Bambu PLA Galaxy",
  489. "GFA16": "Bambu PLA Wood",
  490. "GFA50": "Bambu PLA-CF",
  491. "GFB00": "Bambu ABS",
  492. "GFB01": "Bambu ASA",
  493. "GFB02": "Bambu ASA-Aero",
  494. "GFB50": "Bambu ABS-GF",
  495. "GFB51": "Bambu ASA-CF",
  496. "GFB60": "PolyLite ABS",
  497. "GFB61": "PolyLite ASA",
  498. "GFB98": "Generic ASA",
  499. "GFB99": "Generic ABS",
  500. "GFC00": "Bambu PC",
  501. "GFC01": "Bambu PC FR",
  502. "GFC99": "Generic PC",
  503. "GFG00": "Bambu PETG Basic",
  504. "GFG01": "Bambu PETG Translucent",
  505. "GFG02": "Bambu PETG HF",
  506. "GFG50": "Bambu PETG-CF",
  507. "GFG60": "PolyLite PETG",
  508. "GFG96": "Generic PETG HF",
  509. "GFG97": "Generic PCTG",
  510. "GFG98": "Generic PETG-CF",
  511. "GFG99": "Generic PETG",
  512. "GFL00": "PolyLite PLA",
  513. "GFL01": "PolyTerra PLA",
  514. "GFL03": "eSUN PLA+",
  515. "GFL04": "Overture PLA",
  516. "GFL05": "Overture Matte PLA",
  517. "GFL06": "Fiberon PETG-ESD",
  518. "GFL50": "Fiberon PA6-CF",
  519. "GFL51": "Fiberon PA6-GF",
  520. "GFL52": "Fiberon PA12-CF",
  521. "GFL53": "Fiberon PA612-CF",
  522. "GFL54": "Fiberon PET-CF",
  523. "GFL55": "Fiberon PETG-rCF",
  524. "GFL95": "Generic PLA High Speed",
  525. "GFL96": "Generic PLA Silk",
  526. "GFL98": "Generic PLA-CF",
  527. "GFL99": "Generic PLA",
  528. "GFN03": "Bambu PA-CF",
  529. "GFN04": "Bambu PAHT-CF",
  530. "GFN05": "Bambu PA6-CF",
  531. "GFN06": "Bambu PPA-CF",
  532. "GFN08": "Bambu PA6-GF",
  533. "GFN96": "Generic PPA-GF",
  534. "GFN97": "Generic PPA-CF",
  535. "GFN98": "Generic PA-CF",
  536. "GFN99": "Generic PA",
  537. "GFP95": "Generic PP-GF",
  538. "GFP96": "Generic PP-CF",
  539. "GFP97": "Generic PP",
  540. "GFP98": "Generic PE-CF",
  541. "GFP99": "Generic PE",
  542. "GFR98": "Generic PHA",
  543. "GFR99": "Generic EVA",
  544. "GFS00": "Bambu Support W",
  545. "GFS01": "Bambu Support G",
  546. "GFS02": "Bambu Support For PLA",
  547. "GFS03": "Bambu Support For PA/PET",
  548. "GFS04": "Bambu PVA",
  549. "GFS05": "Bambu Support For PLA/PETG",
  550. "GFS06": "Bambu Support for ABS",
  551. "GFS97": "Generic BVOH",
  552. "GFS98": "Generic HIPS",
  553. "GFS99": "Generic PVA",
  554. "GFT01": "Bambu PET-CF",
  555. "GFT02": "Bambu PPS-CF",
  556. "GFT97": "Generic PPS",
  557. "GFT98": "Generic PPS-CF",
  558. "GFU00": "Bambu TPU 95A HF",
  559. "GFU01": "Bambu TPU 95A",
  560. "GFU02": "Bambu TPU for AMS",
  561. "GFU98": "Generic TPU for AMS",
  562. "GFU99": "Generic TPU",
  563. }
  564. async def _enrich_from_local_presets(
  565. unresolved_ids: list[str],
  566. result: dict,
  567. db: AsyncSession,
  568. ) -> dict:
  569. """Fall back to local profiles for filament IDs not resolved by cloud.
  570. Matches by checking the setting_id field inside the local preset's
  571. resolved JSON blob (stored in the 'setting' column).
  572. """
  573. from sqlalchemy import text
  574. from backend.app.models.local_preset import LocalPreset
  575. # Build lookup: converted setting_id -> original filament_id
  576. id_map: dict[str, str] = {}
  577. for fid in unresolved_ids:
  578. converted = _filament_id_to_setting_id(fid)
  579. id_map[converted] = fid
  580. # Also map the original in case the JSON uses that form
  581. id_map[fid] = fid
  582. try:
  583. # Query filament presets that have a setting_id matching any of our IDs
  584. from backend.app.core.db_dialect import is_sqlite
  585. if is_sqlite():
  586. json_filter = text("json_extract(setting, '$.setting_id') IS NOT NULL")
  587. else:
  588. json_filter = text("(setting::jsonb->>'setting_id') IS NOT NULL")
  589. candidates = await db.execute(
  590. select(LocalPreset).where(
  591. LocalPreset.preset_type == "filament",
  592. json_filter,
  593. )
  594. )
  595. for preset in candidates.scalars().all():
  596. try:
  597. setting_data = json.loads(preset.setting) if isinstance(preset.setting, str) else preset.setting
  598. preset_setting_id = setting_data.get("setting_id", "")
  599. if preset_setting_id in id_map:
  600. original_id = id_map[preset_setting_id]
  601. info = {"name": preset.name, "k": None}
  602. # Try to extract K value from the local preset
  603. pa = setting_data.get("pressure_advance")
  604. if pa is not None:
  605. try:
  606. k_val = float(pa[0]) if isinstance(pa, list) else float(pa)
  607. info["k"] = k_val
  608. except (ValueError, TypeError, IndexError):
  609. pass
  610. _filament_cache[original_id] = info
  611. result[original_id] = info
  612. except Exception:
  613. continue
  614. except Exception as e:
  615. logger.warning("Failed to search local presets for filament info: %s", e)
  616. # Phase 4: Fall back to built-in filament name table for any still without a name
  617. for fid in unresolved_ids:
  618. if fid not in result or not result[fid].get("name"):
  619. name = _BUILTIN_FILAMENT_NAMES.get(fid, "")
  620. if name:
  621. # Preserve K value from earlier phases if available
  622. existing_k = result.get(fid, {}).get("k")
  623. info = {"name": name, "k": existing_k}
  624. _filament_cache[fid] = info
  625. result[fid] = info
  626. # Fill remaining unresolved with empty entries
  627. for fid in unresolved_ids:
  628. if fid not in result:
  629. _filament_cache[fid] = {"name": "", "k": None}
  630. result[fid] = {"name": "", "k": None}
  631. return result
  632. # _filament_id_to_setting_id is now imported from backend.app.utils.filament_ids
  633. _filament_id_to_setting_id = filament_id_to_setting_id
  634. @router.post("/filament-info")
  635. async def get_filament_info(
  636. setting_ids: list[str] = Body(...),
  637. db: AsyncSession = Depends(get_db),
  638. current_user: User | None = cloud_caller(Permission.FILAMENTS_READ),
  639. ):
  640. """
  641. Get filament preset info (name and K value) for multiple setting IDs.
  642. Used to enrich AMS tray and nozzle rack tooltips with preset data.
  643. Lookup order: cache → cloud → local profiles → built-in table → empty fallback.
  644. """
  645. import time
  646. logger.info("get_filament_info called with %s IDs: %s", len(setting_ids), setting_ids)
  647. global _filament_cache, _filament_cache_time
  648. # Clear stale cache
  649. if time.time() - _filament_cache_time > FILAMENT_CACHE_TTL:
  650. _filament_cache = {}
  651. _filament_cache_time = time.time()
  652. result = {}
  653. unresolved_ids: list[str] = []
  654. # Phase 1: Check cache
  655. for setting_id in setting_ids:
  656. if not setting_id:
  657. continue
  658. if setting_id in _filament_cache:
  659. result[setting_id] = _filament_cache[setting_id]
  660. else:
  661. unresolved_ids.append(setting_id)
  662. # Phase 2: Try cloud for uncached IDs
  663. if unresolved_ids:
  664. cloud = await build_authenticated_cloud(db, current_user)
  665. if cloud is not None and cloud.is_authenticated:
  666. try:
  667. still_unresolved: list[str] = []
  668. for setting_id in unresolved_ids:
  669. try:
  670. api_setting_id = _filament_id_to_setting_id(setting_id)
  671. data = await cloud.get_setting_detail(api_setting_id)
  672. setting = data.get("setting", {})
  673. name = data.get("name", "")
  674. k_value = setting.get("pressure_advance")
  675. if k_value is not None:
  676. try:
  677. k_value = float(k_value)
  678. except (ValueError, TypeError):
  679. k_value = None
  680. info = {"name": name, "k": k_value}
  681. _filament_cache[setting_id] = info
  682. result[setting_id] = info
  683. if not name:
  684. still_unresolved.append(setting_id)
  685. except Exception as e:
  686. logger.warning(
  687. f"Failed to get cloud preset {setting_id} "
  688. f"(API ID: {_filament_id_to_setting_id(setting_id)}): {e}"
  689. )
  690. still_unresolved.append(setting_id)
  691. unresolved_ids = still_unresolved
  692. finally:
  693. await cloud.close()
  694. elif cloud is not None:
  695. await cloud.close()
  696. # Phase 3: Try local profiles for any IDs still without a name
  697. if unresolved_ids:
  698. result = await _enrich_from_local_presets(unresolved_ids, result, db)
  699. return result
  700. @router.get("/devices", response_model=list[CloudDevice])
  701. async def get_devices(
  702. db: AsyncSession = Depends(get_db),
  703. current_user: User | None = cloud_caller(Permission.PRINTERS_READ),
  704. ):
  705. """
  706. Get list of bound printer devices.
  707. Returns printers registered to the user's Bambu account.
  708. """
  709. cloud = await build_authenticated_cloud(db, current_user)
  710. if cloud is None or not cloud.is_authenticated:
  711. raise HTTPException(status_code=401, detail="Not authenticated")
  712. try:
  713. data = await cloud.get_devices()
  714. devices = data.get("devices", [])
  715. return [
  716. CloudDevice(
  717. dev_id=d.get("dev_id", ""),
  718. name=d.get("name", "Unknown"),
  719. dev_model_name=d.get("dev_model_name"),
  720. dev_product_name=d.get("dev_product_name"),
  721. online=d.get("online", False),
  722. )
  723. for d in devices
  724. ]
  725. except BambuCloudAuthError:
  726. await clear_token(db, current_user)
  727. raise HTTPException(status_code=401, detail="Authentication expired")
  728. except BambuCloudError as e:
  729. raise HTTPException(status_code=500, detail=str(e))
  730. finally:
  731. await cloud.close()
  732. @router.get("/firmware-updates", response_model=FirmwareUpdatesResponse)
  733. async def get_firmware_updates(
  734. db: AsyncSession = Depends(get_db),
  735. current_user: User | None = cloud_caller(Permission.FIRMWARE_READ),
  736. ):
  737. """
  738. Check for firmware updates for all bound devices.
  739. Returns firmware version info for each device including:
  740. - Current installed version
  741. - Latest available version
  742. - Whether an update is available
  743. - Release notes for the latest version
  744. Requires cloud authentication.
  745. """
  746. cloud = await build_authenticated_cloud(db, current_user)
  747. if cloud is None or not cloud.is_authenticated:
  748. raise HTTPException(status_code=401, detail="Not authenticated")
  749. try:
  750. # First get list of bound devices
  751. devices_data = await cloud.get_devices()
  752. devices = devices_data.get("devices", [])
  753. updates = []
  754. updates_available = 0
  755. # Check firmware for each device
  756. for device in devices:
  757. device_id = device.get("dev_id", "")
  758. device_name = device.get("name", "Unknown")
  759. try:
  760. firmware_info = await cloud.get_firmware_version(device_id)
  761. update_available = firmware_info.get("update_available", False)
  762. if update_available:
  763. updates_available += 1
  764. updates.append(
  765. FirmwareUpdateInfo(
  766. device_id=device_id,
  767. device_name=device_name,
  768. current_version=firmware_info.get("current_version"),
  769. latest_version=firmware_info.get("latest_version"),
  770. update_available=update_available,
  771. release_notes=firmware_info.get("release_notes"),
  772. )
  773. )
  774. except BambuCloudError as e:
  775. logger.warning("Failed to get firmware info for %s: %s", device_name, e)
  776. # Still include device but with unknown firmware status
  777. updates.append(
  778. FirmwareUpdateInfo(
  779. device_id=device_id,
  780. device_name=device_name,
  781. current_version=None,
  782. latest_version=None,
  783. update_available=False,
  784. release_notes=None,
  785. )
  786. )
  787. return FirmwareUpdatesResponse(updates=updates, updates_available=updates_available)
  788. except BambuCloudAuthError:
  789. await clear_token(db, current_user)
  790. raise HTTPException(status_code=401, detail="Authentication expired")
  791. except BambuCloudError as e:
  792. raise HTTPException(status_code=500, detail=str(e))
  793. finally:
  794. await cloud.close()
  795. @router.post("/settings")
  796. async def create_setting(
  797. request: SlicerSettingCreate,
  798. db: AsyncSession = Depends(get_db),
  799. current_user: User | None = cloud_caller(Permission.CLOUD_AUTH),
  800. ):
  801. """
  802. Create a new slicer preset/setting.
  803. Creates a new preset on Bambu Cloud. The preset inherits from a base preset
  804. and only stores the delta (modified values).
  805. Type should be: 'filament', 'print', or 'printer'
  806. """
  807. cloud = await build_authenticated_cloud(db, current_user)
  808. if cloud is None or not cloud.is_authenticated:
  809. raise HTTPException(status_code=401, detail="Not authenticated")
  810. try:
  811. data = await cloud.create_setting(
  812. preset_type=request.type,
  813. name=request.name,
  814. base_id=request.base_id,
  815. setting=request.setting,
  816. version=request.version,
  817. )
  818. return data
  819. except BambuCloudAuthError:
  820. await clear_token(db, current_user)
  821. raise HTTPException(status_code=401, detail="Authentication expired")
  822. except BambuCloudError as e:
  823. raise HTTPException(status_code=500, detail=str(e))
  824. finally:
  825. await cloud.close()
  826. @router.put("/settings/{setting_id}")
  827. async def update_setting(
  828. setting_id: str,
  829. request: SlicerSettingUpdate,
  830. db: AsyncSession = Depends(get_db),
  831. current_user: User | None = cloud_caller(Permission.CLOUD_AUTH),
  832. ):
  833. """
  834. Update an existing slicer preset/setting.
  835. Updates the preset's name and/or settings on Bambu Cloud.
  836. """
  837. cloud = await build_authenticated_cloud(db, current_user)
  838. if cloud is None or not cloud.is_authenticated:
  839. raise HTTPException(status_code=401, detail="Not authenticated")
  840. try:
  841. data = await cloud.update_setting(
  842. setting_id=setting_id,
  843. name=request.name,
  844. setting=request.setting,
  845. )
  846. return data
  847. except BambuCloudAuthError:
  848. await clear_token(db, current_user)
  849. raise HTTPException(status_code=401, detail="Authentication expired")
  850. except BambuCloudError as e:
  851. raise HTTPException(status_code=500, detail=str(e))
  852. finally:
  853. await cloud.close()
  854. @router.delete("/settings/{setting_id}", response_model=SlicerSettingDeleteResponse)
  855. async def delete_setting(
  856. setting_id: str,
  857. db: AsyncSession = Depends(get_db),
  858. current_user: User | None = cloud_caller(Permission.CLOUD_AUTH),
  859. ):
  860. """
  861. Delete a slicer preset/setting.
  862. Removes the preset from Bambu Cloud. This cannot be undone.
  863. """
  864. cloud = await build_authenticated_cloud(db, current_user)
  865. if cloud is None or not cloud.is_authenticated:
  866. raise HTTPException(status_code=401, detail="Not authenticated")
  867. try:
  868. result = await cloud.delete_setting(setting_id)
  869. return SlicerSettingDeleteResponse(
  870. success=result.get("success", True),
  871. message=result.get("message", "Setting deleted"),
  872. )
  873. except BambuCloudAuthError:
  874. await clear_token(db, current_user)
  875. raise HTTPException(status_code=401, detail="Authentication expired")
  876. except BambuCloudError as e:
  877. raise HTTPException(status_code=500, detail=str(e))
  878. finally:
  879. await cloud.close()
  880. # Path to field definition files
  881. FIELDS_DATA_DIR = Path(__file__).parent.parent.parent / "data"
  882. # Cache for field definitions (loaded once)
  883. _fields_cache: dict[str, dict] = {}
  884. def _load_fields(preset_type: str) -> dict:
  885. """Load field definitions from JSON file."""
  886. if preset_type in _fields_cache:
  887. return _fields_cache[preset_type]
  888. # Map API type names to file names
  889. file_map = {
  890. "filament": "filament_fields.json",
  891. "print": "process_fields.json",
  892. "process": "process_fields.json",
  893. "printer": "printer_fields.json",
  894. }
  895. filename = file_map.get(preset_type)
  896. if not filename:
  897. raise HTTPException(status_code=400, detail=f"Unknown preset type: {preset_type}")
  898. file_path = FIELDS_DATA_DIR / filename
  899. if not file_path.exists():
  900. raise HTTPException(status_code=404, detail=f"Field definitions not found for: {preset_type}")
  901. with open(file_path) as f:
  902. data = json.load(f)
  903. _fields_cache[preset_type] = data
  904. return data
  905. @router.get("/builtin-filaments")
  906. async def get_builtin_filaments(
  907. _: User | None = RequirePermissionIfAuthEnabled(Permission.FILAMENTS_READ),
  908. ):
  909. """
  910. Get built-in filament names as a fallback source.
  911. Returns the static _BUILTIN_FILAMENT_NAMES table as a list of
  912. {filament_id, name} objects. Used by the frontend when cloud
  913. and local profiles are unavailable.
  914. """
  915. return [{"filament_id": fid, "name": name} for fid, name in _BUILTIN_FILAMENT_NAMES.items()]
  916. # Cache for filament_id → name mapping (resolved from cloud preset details)
  917. _filament_id_name_cache: dict[str, str] = {}
  918. _filament_id_name_cache_time: float = 0
  919. @router.get("/filament-id-map")
  920. async def get_filament_id_map(
  921. db: AsyncSession = Depends(get_db),
  922. current_user: User | None = cloud_caller(Permission.FILAMENTS_READ),
  923. ):
  924. """
  925. Get filament_id → name mapping for user cloud presets.
  926. K-profiles store a filament_id (e.g., "P4d64437") which is different from
  927. the cloud preset setting_id (e.g., "PFUS9ac902733670a9"). This endpoint
  928. fetches details for all custom presets and returns the mapping.
  929. Cached for 5 minutes.
  930. """
  931. import time
  932. global _filament_id_name_cache, _filament_id_name_cache_time
  933. if _filament_id_name_cache and time.time() - _filament_id_name_cache_time < FILAMENT_CACHE_TTL:
  934. return _filament_id_name_cache
  935. cloud = await build_authenticated_cloud(db, current_user)
  936. if cloud is None or not cloud.is_authenticated:
  937. if cloud is not None:
  938. await cloud.close()
  939. return _filament_id_name_cache or {}
  940. try:
  941. data = await cloud.get_slicer_settings()
  942. custom_presets = data.get("filament", {}).get("private", [])
  943. result: dict[str, str] = {}
  944. for preset in custom_presets:
  945. setting_id = preset.get("setting_id", "")
  946. if not setting_id:
  947. continue
  948. try:
  949. detail = await cloud.get_setting_detail(setting_id)
  950. fid = detail.get("filament_id", "")
  951. name = detail.get("name", "")
  952. if fid and name:
  953. # Strip printer/nozzle suffix: "Devil Design PLA Basic @Bambu Lab H2D 0.4 nozzle" → "Devil Design PLA Basic"
  954. clean_name = name.split(" @")[0].strip() if " @" in name else name
  955. result[fid] = clean_name
  956. except Exception:
  957. pass
  958. _filament_id_name_cache = result
  959. _filament_id_name_cache_time = time.time()
  960. return result
  961. except Exception:
  962. return _filament_id_name_cache or {}
  963. finally:
  964. await cloud.close()
  965. @router.get("/fields/{preset_type}")
  966. async def get_preset_fields(
  967. preset_type: Literal["filament", "print", "process", "printer"],
  968. _: User | None = RequirePermissionIfAuthEnabled(Permission.CLOUD_AUTH),
  969. ):
  970. """
  971. Get field definitions for a preset type.
  972. Returns a list of field definitions including:
  973. - key: The setting key name
  974. - label: Human-readable label
  975. - type: Field type (text, number, boolean, select)
  976. - category: Grouping category
  977. - description: Field description
  978. - options: For select fields, available options
  979. - unit: Unit of measurement (if applicable)
  980. - min/max/step: For number fields, validation constraints
  981. """
  982. data = _load_fields(preset_type)
  983. return data
  984. @router.get("/fields")
  985. async def get_all_preset_fields(
  986. _: User | None = RequirePermissionIfAuthEnabled(Permission.CLOUD_AUTH),
  987. ):
  988. """
  989. Get all field definitions for all preset types.
  990. Returns field definitions organized by type.
  991. """
  992. return {
  993. "filament": _load_fields("filament"),
  994. "process": _load_fields("process"),
  995. "printer": _load_fields("printer"),
  996. }