printers.py 98 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628
  1. import asyncio
  2. import logging
  3. import re
  4. import zipfile
  5. from fastapi import APIRouter, Depends, HTTPException, Query
  6. from fastapi.responses import Response
  7. from sqlalchemy import func, select
  8. from sqlalchemy.ext.asyncio import AsyncSession
  9. from backend.app.core.auth import RequirePermissionIfAuthEnabled
  10. from backend.app.core.config import settings
  11. from backend.app.core.database import get_db
  12. from backend.app.core.permissions import Permission
  13. from backend.app.models.ams_label import AmsLabel
  14. from backend.app.models.printer import Printer
  15. from backend.app.models.slot_preset import SlotPresetMapping
  16. from backend.app.schemas.printer import (
  17. AmsLabelBody,
  18. AMSTray,
  19. AMSUnit,
  20. HMSErrorResponse,
  21. NozzleInfoResponse,
  22. NozzleRackSlot,
  23. PrinterCreate,
  24. PrinterResponse,
  25. PrinterStatus,
  26. PrinterUpdate,
  27. PrintOptionsResponse,
  28. )
  29. from backend.app.services.bambu_ftp import (
  30. delete_file_async,
  31. download_file_bytes_async,
  32. download_file_try_paths_async,
  33. get_storage_info_async,
  34. list_files_async,
  35. )
  36. from backend.app.services.printer_manager import get_derived_status_name, printer_manager, supports_chamber_temp
  37. logger = logging.getLogger(__name__)
  38. router = APIRouter(prefix="/printers", tags=["printers"])
  39. @router.get("/", response_model=list[PrinterResponse])
  40. async def list_printers(
  41. _=RequirePermissionIfAuthEnabled(Permission.PRINTERS_READ),
  42. db: AsyncSession = Depends(get_db),
  43. ):
  44. """List all configured printers."""
  45. result = await db.execute(select(Printer).order_by(Printer.name))
  46. return list(result.scalars().all())
  47. @router.post("/", response_model=PrinterResponse)
  48. async def create_printer(
  49. printer_data: PrinterCreate,
  50. _=RequirePermissionIfAuthEnabled(Permission.PRINTERS_CREATE),
  51. db: AsyncSession = Depends(get_db),
  52. ):
  53. """Add a new printer."""
  54. # Check if serial number already exists
  55. result = await db.execute(select(Printer).where(Printer.serial_number == printer_data.serial_number))
  56. if result.scalar_one_or_none():
  57. raise HTTPException(400, "Printer with this serial number already exists")
  58. printer = Printer(**printer_data.model_dump())
  59. db.add(printer)
  60. await db.commit()
  61. await db.refresh(printer)
  62. # Connect to the printer
  63. if printer.is_active:
  64. await printer_manager.connect_printer(printer)
  65. return printer
  66. @router.get("/usb-cameras")
  67. async def list_usb_cameras(
  68. _=RequirePermissionIfAuthEnabled(Permission.PRINTERS_READ),
  69. ):
  70. """List available USB cameras connected to the system.
  71. Returns a list of detected V4L2 video devices with their info.
  72. Only works on Linux systems with V4L2 support.
  73. Returns:
  74. List of dicts with {device: str, name: str, capabilities: list, formats?: list}
  75. """
  76. from backend.app.services.external_camera import list_usb_cameras
  77. cameras = list_usb_cameras()
  78. return {"cameras": cameras}
  79. @router.get("/available-filaments")
  80. async def get_available_filaments(
  81. model: str = Query(..., description="Target printer model"),
  82. location: str | None = Query(None, description="Optional location filter"),
  83. _=RequirePermissionIfAuthEnabled(Permission.QUEUE_CREATE),
  84. db: AsyncSession = Depends(get_db),
  85. ):
  86. """Get deduplicated list of filaments loaded across all active printers of a given model.
  87. Used by the frontend to offer filament override options for model-based queue assignment.
  88. """
  89. from backend.app.utils.printer_models import normalize_printer_model, normalize_printer_model_id
  90. # Normalize model name
  91. normalized_model = normalize_printer_model(model) or normalize_printer_model_id(model) or model
  92. query = (
  93. select(Printer).where(func.lower(Printer.model) == normalized_model.lower()).where(Printer.is_active == True) # noqa: E712
  94. )
  95. if location:
  96. query = query.where(Printer.location == location)
  97. result = await db.execute(query)
  98. printers_list = list(result.scalars().all())
  99. if not printers_list:
  100. return []
  101. # Collect filaments from all matching printers
  102. # Dedup key includes extruder_id so same color on different nozzles appears separately
  103. seen: set[tuple[str, str, int | None]] = set() # (type_upper, color_normalized, extruder_id)
  104. filaments = []
  105. for printer in printers_list:
  106. status = printer_manager.get_status(printer.id)
  107. if not status:
  108. continue
  109. # Get ams_extruder_map for dual-nozzle printers
  110. ams_extruder_map = status.raw_data.get("ams_extruder_map", {})
  111. # AMS trays
  112. for ams_unit in status.raw_data.get("ams", []):
  113. ams_id = str(ams_unit.get("id", 0))
  114. extruder_id = ams_extruder_map.get(ams_id)
  115. for tray in ams_unit.get("tray", []):
  116. tray_type = tray.get("tray_type")
  117. if not tray_type:
  118. continue
  119. tray_color = tray.get("tray_color", "")
  120. # Normalize color: remove alpha, add hash
  121. hex_color = tray_color.replace("#", "")[:6] if tray_color else "808080"
  122. color = f"#{hex_color}"
  123. tray_info_idx = tray.get("tray_info_idx", "")
  124. key = (tray_type.upper(), hex_color.lower(), extruder_id)
  125. if key not in seen:
  126. seen.add(key)
  127. filaments.append(
  128. {
  129. "type": tray_type,
  130. "color": color,
  131. "tray_info_idx": tray_info_idx,
  132. "extruder_id": extruder_id,
  133. }
  134. )
  135. # External spools (vt_tray)
  136. for vt in status.raw_data.get("vt_tray") or []:
  137. vt_type = vt.get("tray_type")
  138. if not vt_type:
  139. continue
  140. vt_color = vt.get("tray_color", "")
  141. hex_color = vt_color.replace("#", "")[:6] if vt_color else "808080"
  142. color = f"#{hex_color}"
  143. tray_info_idx = vt.get("tray_info_idx", "")
  144. vt_id = int(vt.get("id", 254))
  145. extruder_id = (255 - vt_id) if ams_extruder_map else None
  146. key = (vt_type.upper(), hex_color.lower(), extruder_id)
  147. if key not in seen:
  148. seen.add(key)
  149. filaments.append(
  150. {
  151. "type": vt_type,
  152. "color": color,
  153. "tray_info_idx": tray_info_idx,
  154. "extruder_id": extruder_id,
  155. }
  156. )
  157. return filaments
  158. @router.get("/developer-mode-warnings")
  159. async def get_developer_mode_warnings(
  160. _=RequirePermissionIfAuthEnabled(Permission.PRINTERS_READ),
  161. db: AsyncSession = Depends(get_db),
  162. ):
  163. """Check if any connected printer lacks developer LAN mode."""
  164. result = await db.execute(select(Printer).where(Printer.is_active == True)) # noqa: E712
  165. printers = result.scalars().all()
  166. statuses = printer_manager.get_all_statuses()
  167. warnings = []
  168. for printer in printers:
  169. state = statuses.get(printer.id)
  170. if state and state.connected and state.developer_mode is False:
  171. warnings.append(
  172. {
  173. "printer_id": printer.id,
  174. "name": printer.name,
  175. }
  176. )
  177. return warnings
  178. @router.get("/{printer_id}", response_model=PrinterResponse)
  179. async def get_printer(
  180. printer_id: int,
  181. _=RequirePermissionIfAuthEnabled(Permission.PRINTERS_READ),
  182. db: AsyncSession = Depends(get_db),
  183. ):
  184. """Get a specific printer."""
  185. result = await db.execute(select(Printer).where(Printer.id == printer_id))
  186. printer = result.scalar_one_or_none()
  187. if not printer:
  188. raise HTTPException(404, "Printer not found")
  189. return printer
  190. @router.patch("/{printer_id}", response_model=PrinterResponse)
  191. async def update_printer(
  192. printer_id: int,
  193. printer_data: PrinterUpdate,
  194. _=RequirePermissionIfAuthEnabled(Permission.PRINTERS_UPDATE),
  195. db: AsyncSession = Depends(get_db),
  196. ):
  197. """Update a printer."""
  198. result = await db.execute(select(Printer).where(Printer.id == printer_id))
  199. printer = result.scalar_one_or_none()
  200. if not printer:
  201. raise HTTPException(404, "Printer not found")
  202. update_data = printer_data.model_dump(exclude_unset=True)
  203. # Handle nested ROI object - flatten to individual columns
  204. if "plate_detection_roi" in update_data:
  205. roi = update_data.pop("plate_detection_roi")
  206. if roi:
  207. update_data["plate_detection_roi_x"] = roi.get("x")
  208. update_data["plate_detection_roi_y"] = roi.get("y")
  209. update_data["plate_detection_roi_w"] = roi.get("w")
  210. update_data["plate_detection_roi_h"] = roi.get("h")
  211. else:
  212. # Clear ROI if set to null
  213. update_data["plate_detection_roi_x"] = None
  214. update_data["plate_detection_roi_y"] = None
  215. update_data["plate_detection_roi_w"] = None
  216. update_data["plate_detection_roi_h"] = None
  217. for field, value in update_data.items():
  218. setattr(printer, field, value)
  219. await db.commit()
  220. await db.refresh(printer)
  221. # Reconnect if connection settings changed
  222. if any(k in update_data for k in ["ip_address", "access_code", "is_active"]):
  223. printer_manager.disconnect_printer(printer_id)
  224. if printer.is_active:
  225. await printer_manager.connect_printer(printer)
  226. return printer
  227. @router.delete("/{printer_id}")
  228. async def delete_printer(
  229. printer_id: int,
  230. delete_archives: bool = True,
  231. _=RequirePermissionIfAuthEnabled(Permission.PRINTERS_DELETE),
  232. db: AsyncSession = Depends(get_db),
  233. ):
  234. """Delete a printer.
  235. Args:
  236. printer_id: ID of the printer to delete
  237. delete_archives: If True (default), delete all print archives for this printer.
  238. If False, keep archives but remove their printer association.
  239. """
  240. from sqlalchemy import delete as sql_delete
  241. from backend.app.models.archive import PrintArchive
  242. from backend.app.models.maintenance import MaintenanceHistory, PrinterMaintenance
  243. result = await db.execute(select(Printer).where(Printer.id == printer_id))
  244. printer = result.scalar_one_or_none()
  245. if not printer:
  246. raise HTTPException(404, "Printer not found")
  247. printer_manager.disconnect_printer(printer_id)
  248. if delete_archives:
  249. # Delete all archives for this printer
  250. await db.execute(sql_delete(PrintArchive).where(PrintArchive.printer_id == printer_id))
  251. else:
  252. # Orphan the archives instead of deleting them
  253. from sqlalchemy import update
  254. await db.execute(update(PrintArchive).where(PrintArchive.printer_id == printer_id).values(printer_id=None))
  255. # Delete maintenance history and items for this printer
  256. # (SQLite doesn't enforce FK cascades, so do it explicitly)
  257. maintenance_ids = (
  258. (await db.execute(select(PrinterMaintenance.id).where(PrinterMaintenance.printer_id == printer_id)))
  259. .scalars()
  260. .all()
  261. )
  262. if maintenance_ids:
  263. await db.execute(
  264. sql_delete(MaintenanceHistory).where(MaintenanceHistory.printer_maintenance_id.in_(maintenance_ids))
  265. )
  266. await db.execute(sql_delete(PrinterMaintenance).where(PrinterMaintenance.printer_id == printer_id))
  267. await db.delete(printer)
  268. await db.commit()
  269. return {"status": "deleted", "archives_deleted": delete_archives}
  270. @router.get("/{printer_id}/status", response_model=PrinterStatus)
  271. async def get_printer_status(
  272. printer_id: int,
  273. _=RequirePermissionIfAuthEnabled(Permission.PRINTERS_READ),
  274. db: AsyncSession = Depends(get_db),
  275. ):
  276. """Get real-time status of a printer."""
  277. result = await db.execute(select(Printer).where(Printer.id == printer_id))
  278. printer = result.scalar_one_or_none()
  279. if not printer:
  280. raise HTTPException(404, "Printer not found")
  281. state = printer_manager.get_status(printer_id)
  282. if not state:
  283. return PrinterStatus(
  284. id=printer_id,
  285. name=printer.name,
  286. connected=False,
  287. )
  288. # Determine cover URL if there's an active print (including paused)
  289. cover_url = None
  290. if state.state in ("RUNNING", "PAUSE") and state.gcode_file:
  291. cover_url = f"/api/v1/printers/{printer_id}/cover"
  292. # Convert HMS errors to response format
  293. hms_errors = [
  294. HMSErrorResponse(code=e.code, attr=e.attr, module=e.module, severity=e.severity)
  295. for e in (state.hms_errors or [])
  296. ]
  297. # Parse AMS data from raw_data
  298. ams_units = []
  299. vt_tray = []
  300. ams_exists = False
  301. raw_data = state.raw_data or {}
  302. # Build K-profile lookup map: cali_idx -> k_value
  303. # This allows looking up the calibrated K value for each AMS slot
  304. kprofile_map: dict[int, float] = {}
  305. for kp in state.kprofiles or []:
  306. if kp.slot_id is not None and kp.k_value:
  307. try:
  308. kprofile_map[kp.slot_id] = float(kp.k_value)
  309. except (ValueError, TypeError):
  310. pass # Skip K-profile entries with unparseable values
  311. if "ams" in raw_data and isinstance(raw_data["ams"], list):
  312. ams_exists = True
  313. for ams_data in raw_data["ams"]:
  314. # Skip if ams_data is not a dict (defensive check)
  315. if not isinstance(ams_data, dict):
  316. continue
  317. trays = []
  318. for tray_data in ams_data.get("tray", []):
  319. # Filter out empty/invalid tag values
  320. tag_uid = tray_data.get("tag_uid", "")
  321. if tag_uid in ("", "0000000000000000"):
  322. tag_uid = None
  323. tray_uuid = tray_data.get("tray_uuid", "")
  324. if tray_uuid in ("", "00000000000000000000000000000000"):
  325. tray_uuid = None
  326. # Get K value: first try tray's k field, then lookup from K-profiles
  327. k_value = tray_data.get("k")
  328. cali_idx = tray_data.get("cali_idx")
  329. if k_value is None and cali_idx is not None and cali_idx in kprofile_map:
  330. k_value = kprofile_map[cali_idx]
  331. trays.append(
  332. AMSTray(
  333. id=tray_data.get("id", 0),
  334. tray_color=tray_data.get("tray_color"),
  335. tray_type=tray_data.get("tray_type"),
  336. tray_sub_brands=tray_data.get("tray_sub_brands"),
  337. tray_id_name=tray_data.get("tray_id_name"),
  338. tray_info_idx=tray_data.get("tray_info_idx"),
  339. remain=tray_data.get("remain", 0),
  340. k=k_value,
  341. cali_idx=cali_idx,
  342. tag_uid=tag_uid,
  343. tray_uuid=tray_uuid,
  344. nozzle_temp_min=tray_data.get("nozzle_temp_min"),
  345. nozzle_temp_max=tray_data.get("nozzle_temp_max"),
  346. )
  347. )
  348. # Prefer humidity_raw (percentage) over humidity (index 1-5)
  349. # humidity_raw is the actual percentage value from the sensor
  350. humidity_raw = ams_data.get("humidity_raw")
  351. humidity_idx = ams_data.get("humidity")
  352. humidity_value = None
  353. if humidity_raw is not None:
  354. try:
  355. humidity_value = int(humidity_raw)
  356. except (ValueError, TypeError):
  357. pass # Skip unparseable humidity; will try index fallback
  358. if humidity_value is None and humidity_idx is not None:
  359. try:
  360. humidity_value = int(humidity_idx)
  361. except (ValueError, TypeError):
  362. pass # Skip unparseable humidity index; humidity remains None
  363. # AMS-HT has 1 tray, regular AMS has 4 trays
  364. is_ams_ht = len(trays) == 1
  365. ams_units.append(
  366. AMSUnit(
  367. id=ams_data.get("id", 0),
  368. humidity=humidity_value,
  369. temp=ams_data.get("temp"),
  370. is_ams_ht=is_ams_ht,
  371. tray=trays,
  372. # Serial number: Bambu MQTT uses "sn" key on AMS unit objects
  373. serial_number=str(ams_data.get("sn") or ams_data.get("serial_number") or ""),
  374. # Firmware version: populated by _handle_version_info from info.module ams/* entries
  375. sw_ver=str(ams_data.get("sw_ver") or ""),
  376. )
  377. )
  378. # Virtual tray (external spool holder) - comes from vt_tray in raw_data (list)
  379. if "vt_tray" in raw_data:
  380. for vt_data in raw_data["vt_tray"]:
  381. # Filter out empty/invalid tag values for vt_tray
  382. vt_tag_uid = vt_data.get("tag_uid", "")
  383. if vt_tag_uid in ("", "0000000000000000"):
  384. vt_tag_uid = None
  385. vt_tray_uuid = vt_data.get("tray_uuid", "")
  386. if vt_tray_uuid in ("", "00000000000000000000000000000000"):
  387. vt_tray_uuid = None
  388. # Get K value: first try tray's k field, then lookup from K-profiles
  389. vt_k_value = vt_data.get("k")
  390. vt_cali_idx = vt_data.get("cali_idx")
  391. if vt_k_value is None and vt_cali_idx is not None and vt_cali_idx in kprofile_map:
  392. vt_k_value = kprofile_map[vt_cali_idx]
  393. tray_id = int(vt_data.get("id", 254))
  394. vt_tray.append(
  395. AMSTray(
  396. id=tray_id,
  397. tray_color=vt_data.get("tray_color"),
  398. tray_type=vt_data.get("tray_type"),
  399. tray_sub_brands=vt_data.get("tray_sub_brands"),
  400. tray_id_name=vt_data.get("tray_id_name"),
  401. tray_info_idx=vt_data.get("tray_info_idx"),
  402. remain=vt_data.get("remain", 0),
  403. k=vt_k_value,
  404. cali_idx=vt_cali_idx,
  405. tag_uid=vt_tag_uid,
  406. tray_uuid=vt_tray_uuid,
  407. nozzle_temp_min=vt_data.get("nozzle_temp_min"),
  408. nozzle_temp_max=vt_data.get("nozzle_temp_max"),
  409. )
  410. )
  411. # Convert nozzle info to response format
  412. nozzles = [
  413. NozzleInfoResponse(
  414. nozzle_type=n.nozzle_type,
  415. nozzle_diameter=n.nozzle_diameter,
  416. )
  417. for n in (state.nozzles or [])
  418. ]
  419. # H2C nozzle rack (tool-changer dock positions)
  420. nozzle_rack = [
  421. NozzleRackSlot(
  422. id=n.get("id", 0),
  423. nozzle_type=n.get("type", ""),
  424. nozzle_diameter=n.get("diameter", ""),
  425. wear=n.get("wear"),
  426. stat=n.get("stat"),
  427. max_temp=n.get("max_temp", 0),
  428. serial_number=n.get("serial_number", ""),
  429. filament_color=n.get("filament_color", ""),
  430. filament_id=n.get("filament_id", ""),
  431. filament_type=n.get("filament_type", ""),
  432. )
  433. for n in (state.nozzle_rack or [])
  434. ]
  435. # Convert print options to response format
  436. print_options = PrintOptionsResponse(
  437. spaghetti_detector=state.print_options.spaghetti_detector,
  438. print_halt=state.print_options.print_halt,
  439. halt_print_sensitivity=state.print_options.halt_print_sensitivity,
  440. first_layer_inspector=state.print_options.first_layer_inspector,
  441. printing_monitor=state.print_options.printing_monitor,
  442. buildplate_marker_detector=state.print_options.buildplate_marker_detector,
  443. allow_skip_parts=state.print_options.allow_skip_parts,
  444. nozzle_clumping_detector=state.print_options.nozzle_clumping_detector,
  445. nozzle_clumping_sensitivity=state.print_options.nozzle_clumping_sensitivity,
  446. pileup_detector=state.print_options.pileup_detector,
  447. pileup_sensitivity=state.print_options.pileup_sensitivity,
  448. airprint_detector=state.print_options.airprint_detector,
  449. airprint_sensitivity=state.print_options.airprint_sensitivity,
  450. auto_recovery_step_loss=state.print_options.auto_recovery_step_loss,
  451. filament_tangle_detect=state.print_options.filament_tangle_detect,
  452. )
  453. # Get AMS mapping from raw_data (which AMS is connected to which nozzle)
  454. ams_mapping = raw_data.get("ams_mapping", [])
  455. # Get per-AMS extruder map from state attribute (not raw_data, to avoid race condition
  456. # where raw_data gets replaced during MQTT updates and ams_extruder_map is temporarily missing)
  457. ams_extruder_map = state.ams_extruder_map or {}
  458. logger.debug("API returning ams_mapping: %s, ams_extruder_map: %s", ams_mapping, ams_extruder_map)
  459. # tray_now from MQTT is already a global tray ID: (ams_id * 4) + slot_id
  460. # Per OpenBambuAPI docs: 254 = external spool, 255 = no filament, otherwise global tray ID
  461. # No conversion needed - just use the raw value directly
  462. tray_now = state.tray_now
  463. logger.debug("Using tray_now directly as global ID: %s", tray_now)
  464. # Filter out chamber temp for models that don't have a real sensor
  465. # P1P, P1S, A1, A1Mini report meaningless chamber_temper values
  466. temperatures = state.temperatures
  467. if not supports_chamber_temp(printer.model):
  468. temperatures = {
  469. k: v for k, v in temperatures.items() if k not in ("chamber", "chamber_target", "chamber_heating")
  470. }
  471. return PrinterStatus(
  472. id=printer_id,
  473. name=printer.name,
  474. connected=state.connected,
  475. state=state.state,
  476. current_print=state.current_print,
  477. subtask_name=state.subtask_name,
  478. gcode_file=state.gcode_file,
  479. progress=state.progress,
  480. remaining_time=state.remaining_time,
  481. layer_num=state.layer_num,
  482. total_layers=state.total_layers,
  483. temperatures=temperatures,
  484. cover_url=cover_url,
  485. hms_errors=hms_errors,
  486. ams=ams_units,
  487. ams_exists=ams_exists,
  488. vt_tray=vt_tray,
  489. sdcard=state.sdcard,
  490. store_to_sdcard=state.store_to_sdcard,
  491. timelapse=state.timelapse,
  492. ipcam=state.ipcam,
  493. wifi_signal=state.wifi_signal,
  494. wired_network=state.wired_network,
  495. nozzles=nozzles,
  496. nozzle_rack=nozzle_rack,
  497. print_options=print_options,
  498. stg_cur=state.stg_cur,
  499. stg_cur_name=get_derived_status_name(state, printer.model),
  500. stg=state.stg,
  501. airduct_mode=state.airduct_mode,
  502. speed_level=state.speed_level,
  503. chamber_light=state.chamber_light,
  504. active_extruder=state.active_extruder,
  505. ams_mapping=ams_mapping,
  506. ams_extruder_map=ams_extruder_map,
  507. tray_now=tray_now,
  508. ams_status_main=state.ams_status_main,
  509. ams_status_sub=state.ams_status_sub,
  510. mc_print_sub_stage=state.mc_print_sub_stage,
  511. last_ams_update=state.last_ams_update,
  512. printable_objects_count=len(state.printable_objects),
  513. cooling_fan_speed=state.cooling_fan_speed,
  514. big_fan1_speed=state.big_fan1_speed,
  515. big_fan2_speed=state.big_fan2_speed,
  516. heatbreak_fan_speed=state.heatbreak_fan_speed,
  517. firmware_version=state.firmware_version,
  518. developer_mode=state.developer_mode if state else None,
  519. plate_cleared=printer_manager.is_plate_cleared(printer_id),
  520. )
  521. @router.get("/{printer_id}/current-print-user")
  522. async def get_current_print_user(
  523. printer_id: int,
  524. _=RequirePermissionIfAuthEnabled(Permission.PRINTERS_READ),
  525. db: AsyncSession = Depends(get_db),
  526. ):
  527. """Get the user who started the current print (for reprint tracking).
  528. Returns user info if available, empty object otherwise.
  529. This tracks users for reprints (which bypass the queue).
  530. For queue-based prints, use the queue item's created_by field instead.
  531. """
  532. result = await db.execute(select(Printer).where(Printer.id == printer_id))
  533. printer = result.scalar_one_or_none()
  534. if not printer:
  535. raise HTTPException(404, "Printer not found")
  536. user_info = printer_manager.get_current_print_user(printer_id)
  537. return user_info or {}
  538. @router.post("/{printer_id}/refresh-status")
  539. async def refresh_printer_status(
  540. printer_id: int,
  541. _=RequirePermissionIfAuthEnabled(Permission.PRINTERS_READ),
  542. db: AsyncSession = Depends(get_db),
  543. ):
  544. """Request a full status refresh from the printer (sends pushall command)."""
  545. result = await db.execute(select(Printer).where(Printer.id == printer_id))
  546. printer = result.scalar_one_or_none()
  547. if not printer:
  548. raise HTTPException(404, "Printer not found")
  549. success = printer_manager.request_status_update(printer_id)
  550. if not success:
  551. raise HTTPException(400, "Printer not connected")
  552. return {"status": "refresh_requested"}
  553. @router.post("/{printer_id}/connect")
  554. async def connect_printer(
  555. printer_id: int,
  556. _=RequirePermissionIfAuthEnabled(Permission.PRINTERS_CONTROL),
  557. db: AsyncSession = Depends(get_db),
  558. ):
  559. """Manually connect to a printer."""
  560. result = await db.execute(select(Printer).where(Printer.id == printer_id))
  561. printer = result.scalar_one_or_none()
  562. if not printer:
  563. raise HTTPException(404, "Printer not found")
  564. success = await printer_manager.connect_printer(printer)
  565. return {"connected": success}
  566. @router.post("/{printer_id}/disconnect")
  567. async def disconnect_printer(
  568. printer_id: int,
  569. _=RequirePermissionIfAuthEnabled(Permission.PRINTERS_CONTROL),
  570. db: AsyncSession = Depends(get_db),
  571. ):
  572. """Manually disconnect from a printer."""
  573. result = await db.execute(select(Printer).where(Printer.id == printer_id))
  574. printer = result.scalar_one_or_none()
  575. if not printer:
  576. raise HTTPException(404, "Printer not found")
  577. printer_manager.disconnect_printer(printer_id)
  578. return {"connected": False}
  579. @router.post("/test")
  580. async def test_printer_connection(
  581. ip_address: str,
  582. serial_number: str,
  583. access_code: str,
  584. _=RequirePermissionIfAuthEnabled(Permission.PRINTERS_CREATE),
  585. ):
  586. """Test connection to a printer without saving."""
  587. result = await printer_manager.test_connection(
  588. ip_address=ip_address,
  589. serial_number=serial_number,
  590. access_code=access_code,
  591. )
  592. return result
  593. # Cache for cover images (printer_id -> {(subtask_name, plate_num, view) -> image_bytes})
  594. _cover_cache: dict[int, dict[tuple[str, str], bytes]] = {}
  595. def clear_cover_cache(printer_id: int) -> None:
  596. """Clear cached cover images for a printer. Call on print start to avoid stale thumbnails."""
  597. _cover_cache.pop(printer_id, None)
  598. @router.get("/{printer_id}/cover")
  599. async def get_printer_cover(
  600. printer_id: int,
  601. view: str | None = None,
  602. db: AsyncSession = Depends(get_db),
  603. ):
  604. # Note: No auth required - this is an image asset loaded via <img src> which can't send auth headers
  605. """Get the cover image for the current print job.
  606. Args:
  607. view: Optional view type. Use "top" for top-down build plate view (useful for skip objects).
  608. Default returns angled 3D perspective view.
  609. """
  610. result = await db.execute(select(Printer).where(Printer.id == printer_id))
  611. printer = result.scalar_one_or_none()
  612. if not printer:
  613. raise HTTPException(404, "Printer not found")
  614. state = printer_manager.get_status(printer_id)
  615. if not state:
  616. raise HTTPException(404, "Printer not connected")
  617. # Use subtask_name as the 3MF filename (gcode_file is the path inside the 3MF)
  618. subtask_name = state.subtask_name
  619. if not subtask_name:
  620. raise HTTPException(404, f"No subtask_name in printer state (state={state.state})")
  621. # Extract plate number from gcode_file (e.g., "/data/Metadata/plate_12.gcode" -> 12)
  622. plate_num = 1
  623. gcode_file = state.gcode_file
  624. if gcode_file:
  625. match = re.search(r"plate_(\d+)\.gcode", gcode_file)
  626. if match:
  627. plate_num = int(match.group(1))
  628. logger.info("Detected plate number %s from gcode_file: %s", plate_num, gcode_file)
  629. # Normalize view parameter
  630. view_key = view or "default"
  631. # Check cache - include plate_num in cache key for multi-plate projects
  632. if printer_id in _cover_cache:
  633. cache_key = (subtask_name, plate_num, view_key)
  634. if cache_key in _cover_cache[printer_id]:
  635. return Response(content=_cover_cache[printer_id][cache_key], media_type="image/png")
  636. # Build possible 3MF filenames from subtask_name
  637. # Bambu printers may store files as "name.gcode.3mf" (sliced via Bambu Studio)
  638. # or just "name.3mf" (uploaded directly)
  639. possible_filenames = []
  640. if subtask_name.endswith(".3mf"):
  641. possible_filenames.append(subtask_name)
  642. else:
  643. # Try both naming patterns
  644. possible_filenames.append(f"{subtask_name}.gcode.3mf")
  645. possible_filenames.append(f"{subtask_name}.3mf")
  646. # Also try with spaces converted to underscores (Bambu Studio may normalize filenames)
  647. if " " in subtask_name:
  648. normalized = subtask_name.replace(" ", "_")
  649. if normalized.endswith(".3mf"):
  650. possible_filenames.append(normalized)
  651. else:
  652. possible_filenames.append(f"{normalized}.gcode.3mf")
  653. possible_filenames.append(f"{normalized}.3mf")
  654. # Build list of all remote paths to try
  655. remote_paths = []
  656. for filename in possible_filenames:
  657. remote_paths.extend(
  658. [
  659. f"/{filename}", # Root directory (most common)
  660. f"/cache/{filename}",
  661. f"/model/{filename}",
  662. f"/data/{filename}",
  663. ]
  664. )
  665. # Use first filename for temp path (will be reused)
  666. temp_filename = possible_filenames[0]
  667. temp_path = settings.archive_dir / "temp" / f"cover_{printer_id}_{temp_filename}"
  668. temp_path.parent.mkdir(parents=True, exist_ok=True)
  669. logger.info(
  670. f"Trying to download cover for '{subtask_name}' from {printer.ip_address} (trying {len(remote_paths)} paths)"
  671. )
  672. # Retry logic for transient FTP failures
  673. max_retries = 2
  674. last_error = None
  675. downloaded = False
  676. for attempt in range(max_retries + 1):
  677. try:
  678. downloaded = await download_file_try_paths_async(
  679. printer.ip_address,
  680. printer.access_code,
  681. remote_paths,
  682. temp_path,
  683. printer_model=printer.model,
  684. )
  685. if downloaded:
  686. break
  687. except Exception as e:
  688. last_error = e
  689. if attempt < max_retries:
  690. logger.warning("FTP download attempt %s failed: %s, retrying...", attempt + 1, e)
  691. await asyncio.sleep(0.5 * (attempt + 1)) # Brief backoff
  692. else:
  693. logger.error("FTP download failed after %s attempts: %s", max_retries + 1, e)
  694. if last_error and not downloaded:
  695. raise HTTPException(503, f"FTP download temporarily unavailable: {last_error}")
  696. if not downloaded:
  697. raise HTTPException(
  698. 404,
  699. f"Could not download 3MF file for '{subtask_name}' from printer {printer.ip_address}. Tried: {possible_filenames}",
  700. )
  701. # Verify file actually exists and has content
  702. if not temp_path.exists():
  703. raise HTTPException(500, f"Download reported success but file not found: {temp_path}")
  704. file_size = temp_path.stat().st_size
  705. logger.info("Downloaded file size: %s bytes", file_size)
  706. if file_size == 0:
  707. temp_path.unlink()
  708. raise HTTPException(500, f"Downloaded file is empty for '{subtask_name}'")
  709. try:
  710. # Extract thumbnail from 3MF (which is a ZIP file)
  711. try:
  712. zf = zipfile.ZipFile(temp_path, "r")
  713. except zipfile.BadZipFile:
  714. raise HTTPException(500, "Downloaded file is not a valid 3MF/ZIP archive")
  715. except OSError as e:
  716. logger.error("Failed to open 3MF file: %s", e, exc_info=True)
  717. raise HTTPException(500, "Failed to open 3MF file. Check server logs for details.")
  718. try:
  719. # Try common thumbnail paths in 3MF files
  720. # Use plate_num to get the correct plate's thumbnail for multi-plate projects
  721. # Use top-down view if requested (better for skip objects modal)
  722. if view == "top":
  723. thumbnail_paths = [
  724. f"Metadata/top_{plate_num}.png",
  725. # Fall back to plate 1 if specific plate not found
  726. "Metadata/top_1.png",
  727. f"Metadata/plate_{plate_num}.png",
  728. "Metadata/plate_1.png",
  729. "Metadata/thumbnail.png",
  730. ]
  731. else:
  732. thumbnail_paths = [
  733. f"Metadata/plate_{plate_num}.png",
  734. # Fall back to plate 1 if specific plate not found
  735. "Metadata/plate_1.png",
  736. "Metadata/thumbnail.png",
  737. f"Metadata/plate_{plate_num}_small.png",
  738. "Metadata/plate_1_small.png",
  739. "Thumbnails/thumbnail.png",
  740. "thumbnail.png",
  741. ]
  742. for thumb_path in thumbnail_paths:
  743. try:
  744. image_data = zf.read(thumb_path)
  745. # Cache the result - include plate_num in cache key
  746. if printer_id not in _cover_cache:
  747. _cover_cache[printer_id] = {}
  748. _cover_cache[printer_id][(subtask_name, plate_num, view_key)] = image_data
  749. return Response(content=image_data, media_type="image/png")
  750. except KeyError:
  751. continue
  752. # If no specific thumbnail found, try any PNG in Metadata
  753. for name in zf.namelist():
  754. if name.startswith("Metadata/") and name.endswith(".png"):
  755. image_data = zf.read(name)
  756. if printer_id not in _cover_cache:
  757. _cover_cache[printer_id] = {}
  758. _cover_cache[printer_id][(subtask_name, plate_num, view_key)] = image_data
  759. return Response(content=image_data, media_type="image/png")
  760. raise HTTPException(404, "No thumbnail found in 3MF file")
  761. finally:
  762. zf.close()
  763. finally:
  764. if temp_path.exists():
  765. temp_path.unlink()
  766. # ============================================
  767. # File Manager Endpoints
  768. # ============================================
  769. @router.get("/{printer_id}/files")
  770. async def list_printer_files(
  771. printer_id: int,
  772. path: str = "/",
  773. _=RequirePermissionIfAuthEnabled(Permission.PRINTERS_FILES),
  774. db: AsyncSession = Depends(get_db),
  775. ):
  776. """List files on the printer at the specified path."""
  777. result = await db.execute(select(Printer).where(Printer.id == printer_id))
  778. printer = result.scalar_one_or_none()
  779. if not printer:
  780. raise HTTPException(404, "Printer not found")
  781. files = await list_files_async(printer.ip_address, printer.access_code, path, printer_model=printer.model)
  782. # Add full path to each file
  783. for f in files:
  784. f["path"] = f"{path.rstrip('/')}/{f['name']}" if path != "/" else f"/{f['name']}"
  785. return {
  786. "path": path,
  787. "files": files,
  788. }
  789. @router.get("/{printer_id}/files/download")
  790. async def download_printer_file(
  791. printer_id: int,
  792. path: str,
  793. _=RequirePermissionIfAuthEnabled(Permission.PRINTERS_FILES),
  794. db: AsyncSession = Depends(get_db),
  795. ):
  796. """Download a file from the printer."""
  797. result = await db.execute(select(Printer).where(Printer.id == printer_id))
  798. printer = result.scalar_one_or_none()
  799. if not printer:
  800. raise HTTPException(404, "Printer not found")
  801. data = await download_file_bytes_async(printer.ip_address, printer.access_code, path, printer_model=printer.model)
  802. if data is None:
  803. raise HTTPException(404, f"File not found: {path}")
  804. # Determine content type based on extension
  805. filename = path.split("/")[-1]
  806. ext = filename.lower().split(".")[-1] if "." in filename else ""
  807. content_types = {
  808. "3mf": "application/vnd.ms-package.3dmanufacturing-3dmodel+xml",
  809. "gcode": "text/plain",
  810. "mp4": "video/mp4",
  811. "avi": "video/x-msvideo",
  812. "png": "image/png",
  813. "jpg": "image/jpeg",
  814. "jpeg": "image/jpeg",
  815. "json": "application/json",
  816. "txt": "text/plain",
  817. }
  818. content_type = content_types.get(ext, "application/octet-stream")
  819. return Response(
  820. content=data,
  821. media_type=content_type,
  822. headers={"Content-Disposition": f'attachment; filename="{filename}"'},
  823. )
  824. @router.get("/{printer_id}/files/gcode")
  825. async def get_printer_file_gcode(
  826. printer_id: int,
  827. path: str,
  828. _=RequirePermissionIfAuthEnabled(Permission.PRINTERS_FILES),
  829. db: AsyncSession = Depends(get_db),
  830. ):
  831. """Get gcode for a file stored on a printer (for preview)."""
  832. import io
  833. # Validate printer
  834. result = await db.execute(select(Printer).where(Printer.id == printer_id))
  835. printer = result.scalar_one_or_none()
  836. if not printer:
  837. raise HTTPException(404, "Printer not found")
  838. data = await download_file_bytes_async(printer.ip_address, printer.access_code, path, printer_model=printer.model)
  839. if data is None:
  840. raise HTTPException(404, f"File not found: {path}")
  841. filename = path.split("/")[-1]
  842. lower = filename.lower()
  843. if lower.endswith(".gcode"):
  844. return Response(content=data, media_type="text/plain")
  845. if lower.endswith(".3mf"):
  846. try:
  847. with zipfile.ZipFile(io.BytesIO(data), "r") as zf:
  848. gcode_files = [n for n in zf.namelist() if n.endswith(".gcode")]
  849. if not gcode_files:
  850. raise HTTPException(status_code=404, detail="No gcode found in 3MF file")
  851. gcode_content = zf.read(gcode_files[0])
  852. return Response(content=gcode_content, media_type="text/plain")
  853. except zipfile.BadZipFile:
  854. raise HTTPException(status_code=400, detail="Invalid 3MF file")
  855. raise HTTPException(status_code=400, detail="Unsupported file type")
  856. @router.get("/{printer_id}/files/plates")
  857. async def get_printer_file_plates(
  858. printer_id: int,
  859. path: str = Query(..., description="Full path to the 3MF file on the printer"),
  860. _=RequirePermissionIfAuthEnabled(Permission.PRINTERS_FILES),
  861. db: AsyncSession = Depends(get_db),
  862. ):
  863. """Get available plates from a multi-plate 3MF file stored on a printer."""
  864. import io
  865. import json
  866. import defusedxml.ElementTree as ET
  867. # Validate printer
  868. result = await db.execute(select(Printer).where(Printer.id == printer_id))
  869. printer = result.scalar_one_or_none()
  870. if not printer:
  871. raise HTTPException(404, "Printer not found")
  872. filename = path.split("/")[-1]
  873. if not filename.lower().endswith(".3mf"):
  874. return {
  875. "printer_id": printer_id,
  876. "path": path,
  877. "filename": filename,
  878. "plates": [],
  879. "is_multi_plate": False,
  880. }
  881. data = await download_file_bytes_async(printer.ip_address, printer.access_code, path, printer_model=printer.model)
  882. if data is None:
  883. raise HTTPException(404, f"File not found: {path}")
  884. plates = []
  885. try:
  886. with zipfile.ZipFile(io.BytesIO(data), "r") as zf:
  887. namelist = zf.namelist()
  888. # Find all plate gcode files to determine available plates
  889. gcode_files = [n for n in namelist if n.startswith("Metadata/plate_") and n.endswith(".gcode")]
  890. # If no gcode is present (source-only or unsliced), fall back to plate JSON/PNG
  891. plate_indices: list[int] = []
  892. if gcode_files:
  893. for gf in gcode_files:
  894. try:
  895. plate_str = gf[15:-6] # Remove "Metadata/plate_" and ".gcode"
  896. plate_indices.append(int(plate_str))
  897. except ValueError:
  898. pass # Skip gcode files with non-numeric plate indices
  899. else:
  900. plate_json_files = [n for n in namelist if n.startswith("Metadata/plate_") and n.endswith(".json")]
  901. plate_png_files = [
  902. n
  903. for n in namelist
  904. if n.startswith("Metadata/plate_")
  905. and n.endswith(".png")
  906. and "_small" not in n
  907. and "no_light" not in n
  908. ]
  909. plate_name_candidates = plate_json_files + plate_png_files
  910. plate_re = re.compile(r"^Metadata/plate_(\d+)\.(json|png)$")
  911. seen_indices: set[int] = set()
  912. for name in plate_name_candidates:
  913. match = plate_re.match(name)
  914. if match:
  915. try:
  916. index = int(match.group(1))
  917. except ValueError:
  918. continue
  919. if index in seen_indices:
  920. continue
  921. seen_indices.add(index)
  922. plate_indices.append(index)
  923. if not plate_indices:
  924. return {
  925. "printer_id": printer_id,
  926. "path": path,
  927. "filename": filename,
  928. "plates": [],
  929. "is_multi_plate": False,
  930. }
  931. plate_indices.sort()
  932. # Parse model_settings.config for plate names
  933. plate_names = {}
  934. if "Metadata/model_settings.config" in namelist:
  935. try:
  936. model_content = zf.read("Metadata/model_settings.config").decode()
  937. model_root = ET.fromstring(model_content)
  938. for plate_elem in model_root.findall(".//plate"):
  939. plater_id = None
  940. plater_name = None
  941. for meta in plate_elem.findall("metadata"):
  942. key = meta.get("key")
  943. value = meta.get("value")
  944. if key == "plater_id" and value:
  945. try:
  946. plater_id = int(value)
  947. except ValueError:
  948. pass # Skip plate with unparseable ID
  949. elif key == "plater_name" and value:
  950. plater_name = value.strip()
  951. if plater_id is not None and plater_name:
  952. plate_names[plater_id] = plater_name
  953. except Exception:
  954. pass # Plate names are optional; continue without them
  955. # Parse slice_info.config for plate metadata
  956. plate_metadata = {}
  957. if "Metadata/slice_info.config" in namelist:
  958. content = zf.read("Metadata/slice_info.config").decode()
  959. root = ET.fromstring(content)
  960. for plate_elem in root.findall(".//plate"):
  961. plate_info = {"filaments": [], "prediction": None, "weight": None, "name": None, "objects": []}
  962. plate_index = None
  963. for meta in plate_elem.findall("metadata"):
  964. key = meta.get("key")
  965. value = meta.get("value")
  966. if key == "index" and value:
  967. try:
  968. plate_index = int(value)
  969. except ValueError:
  970. pass # Skip plate with unparseable index
  971. elif key == "prediction" and value:
  972. try:
  973. plate_info["prediction"] = int(value)
  974. except ValueError:
  975. pass # Skip unparseable prediction; leave as None
  976. elif key == "weight" and value:
  977. try:
  978. plate_info["weight"] = float(value)
  979. except ValueError:
  980. pass # Skip unparseable weight; leave as None
  981. # Get filaments used in this plate
  982. for filament_elem in plate_elem.findall("filament"):
  983. filament_id = filament_elem.get("id")
  984. filament_type = filament_elem.get("type", "")
  985. filament_color = filament_elem.get("color", "")
  986. used_g = filament_elem.get("used_g", "0")
  987. used_m = filament_elem.get("used_m", "0")
  988. try:
  989. used_grams = float(used_g)
  990. except (ValueError, TypeError):
  991. used_grams = 0
  992. if used_grams > 0 and filament_id:
  993. plate_info["filaments"].append(
  994. {
  995. "slot_id": int(filament_id),
  996. "type": filament_type,
  997. "color": filament_color,
  998. "used_grams": round(used_grams, 1),
  999. "used_meters": float(used_m) if used_m else 0,
  1000. }
  1001. )
  1002. plate_info["filaments"].sort(key=lambda x: x["slot_id"])
  1003. # Collect object names
  1004. for obj_elem in plate_elem.findall("object"):
  1005. obj_name = obj_elem.get("name")
  1006. if obj_name and obj_name not in plate_info["objects"]:
  1007. plate_info["objects"].append(obj_name)
  1008. # Set plate name
  1009. if plate_index is not None:
  1010. custom_name = plate_names.get(plate_index)
  1011. if custom_name:
  1012. plate_info["name"] = custom_name
  1013. elif plate_info["objects"]:
  1014. plate_info["name"] = plate_info["objects"][0]
  1015. plate_metadata[plate_index] = plate_info
  1016. # Parse plate_*.json for object lists when slice_info is missing
  1017. plate_json_objects: dict[int, list[str]] = {}
  1018. for name in namelist:
  1019. match = re.match(r"^Metadata/plate_(\d+)\.json$", name)
  1020. if not match:
  1021. continue
  1022. try:
  1023. plate_index = int(match.group(1))
  1024. except ValueError:
  1025. continue
  1026. try:
  1027. payload = json.loads(zf.read(name).decode())
  1028. bbox_objects = payload.get("bbox_objects", [])
  1029. names: list[str] = []
  1030. for obj in bbox_objects:
  1031. obj_name = obj.get("name") if isinstance(obj, dict) else None
  1032. if obj_name and obj_name not in names:
  1033. names.append(obj_name)
  1034. if names:
  1035. plate_json_objects[plate_index] = names
  1036. except Exception:
  1037. continue
  1038. # Build plate list
  1039. for idx in plate_indices:
  1040. meta = plate_metadata.get(idx, {})
  1041. has_thumbnail = f"Metadata/plate_{idx}.png" in namelist
  1042. objects = meta.get("objects", [])
  1043. if not objects:
  1044. objects = plate_json_objects.get(idx, [])
  1045. plate_name = meta.get("name")
  1046. if not plate_name:
  1047. plate_name = plate_names.get(idx)
  1048. if not plate_name and objects:
  1049. plate_name = objects[0]
  1050. plates.append(
  1051. {
  1052. "index": idx,
  1053. "name": plate_name,
  1054. "objects": objects,
  1055. "object_count": len(objects),
  1056. "has_thumbnail": has_thumbnail,
  1057. "thumbnail_url": f"/api/v1/printers/{printer_id}/files/plate-thumbnail/{idx}?path={path}",
  1058. "print_time_seconds": meta.get("prediction"),
  1059. "filament_used_grams": meta.get("weight"),
  1060. "filaments": meta.get("filaments", []),
  1061. }
  1062. )
  1063. except Exception as e:
  1064. logger.warning("Failed to parse plates from printer file %s: %s", path, e)
  1065. return {
  1066. "printer_id": printer_id,
  1067. "path": path,
  1068. "filename": filename,
  1069. "plates": plates,
  1070. "is_multi_plate": len(plates) > 1,
  1071. }
  1072. @router.get("/{printer_id}/files/plate-thumbnail/{plate_index}")
  1073. async def get_printer_file_plate_thumbnail(
  1074. printer_id: int,
  1075. plate_index: int,
  1076. path: str = Query(..., description="Full path to the 3MF file on the printer"),
  1077. _=RequirePermissionIfAuthEnabled(Permission.PRINTERS_FILES),
  1078. db: AsyncSession = Depends(get_db),
  1079. ):
  1080. """Get a plate thumbnail image from a printer-stored 3MF file."""
  1081. import io
  1082. result = await db.execute(select(Printer).where(Printer.id == printer_id))
  1083. printer = result.scalar_one_or_none()
  1084. if not printer:
  1085. raise HTTPException(404, "Printer not found")
  1086. data = await download_file_bytes_async(printer.ip_address, printer.access_code, path, printer_model=printer.model)
  1087. if data is None:
  1088. raise HTTPException(404, f"File not found: {path}")
  1089. try:
  1090. with zipfile.ZipFile(io.BytesIO(data), "r") as zf:
  1091. thumb_path = f"Metadata/plate_{plate_index}.png"
  1092. if thumb_path in zf.namelist():
  1093. image_data = zf.read(thumb_path)
  1094. return Response(content=image_data, media_type="image/png")
  1095. except Exception:
  1096. pass # Corrupt or unreadable 3MF; fall through to 404
  1097. raise HTTPException(status_code=404, detail=f"Thumbnail for plate {plate_index} not found")
  1098. @router.post("/{printer_id}/files/download-zip")
  1099. async def download_printer_files_as_zip(
  1100. printer_id: int,
  1101. request: dict,
  1102. _=RequirePermissionIfAuthEnabled(Permission.PRINTERS_FILES),
  1103. db: AsyncSession = Depends(get_db),
  1104. ):
  1105. """Download multiple files from the printer as a ZIP archive."""
  1106. import io
  1107. paths = request.get("paths", [])
  1108. if not paths:
  1109. raise HTTPException(400, "No files specified")
  1110. result = await db.execute(select(Printer).where(Printer.id == printer_id))
  1111. printer = result.scalar_one_or_none()
  1112. if not printer:
  1113. raise HTTPException(404, "Printer not found")
  1114. # Create ZIP in memory
  1115. zip_buffer = io.BytesIO()
  1116. with zipfile.ZipFile(zip_buffer, "w", zipfile.ZIP_DEFLATED) as zf:
  1117. for path in paths:
  1118. try:
  1119. data = await download_file_bytes_async(
  1120. printer.ip_address, printer.access_code, path, printer_model=printer.model
  1121. )
  1122. if data:
  1123. filename = path.split("/")[-1]
  1124. zf.writestr(filename, data)
  1125. except Exception as e:
  1126. logging.warning("Failed to add %s to ZIP: %s", path, e)
  1127. continue
  1128. zip_buffer.seek(0)
  1129. zip_data = zip_buffer.read()
  1130. if len(zip_data) == 0:
  1131. raise HTTPException(404, "No files could be downloaded")
  1132. return Response(
  1133. content=zip_data,
  1134. media_type="application/zip",
  1135. headers={"Content-Disposition": 'attachment; filename="printer-files.zip"'},
  1136. )
  1137. @router.delete("/{printer_id}/files")
  1138. async def delete_printer_file(
  1139. printer_id: int,
  1140. path: str,
  1141. _=RequirePermissionIfAuthEnabled(Permission.PRINTERS_FILES),
  1142. db: AsyncSession = Depends(get_db),
  1143. ):
  1144. """Delete a file from the printer."""
  1145. result = await db.execute(select(Printer).where(Printer.id == printer_id))
  1146. printer = result.scalar_one_or_none()
  1147. if not printer:
  1148. raise HTTPException(404, "Printer not found")
  1149. success = await delete_file_async(printer.ip_address, printer.access_code, path, printer_model=printer.model)
  1150. if not success:
  1151. raise HTTPException(500, f"Failed to delete file: {path}")
  1152. return {"status": "deleted", "path": path}
  1153. @router.get("/{printer_id}/storage")
  1154. async def get_printer_storage(
  1155. printer_id: int,
  1156. _=RequirePermissionIfAuthEnabled(Permission.PRINTERS_READ),
  1157. db: AsyncSession = Depends(get_db),
  1158. ):
  1159. """Get storage information from the printer."""
  1160. result = await db.execute(select(Printer).where(Printer.id == printer_id))
  1161. printer = result.scalar_one_or_none()
  1162. if not printer:
  1163. raise HTTPException(404, "Printer not found")
  1164. storage_info = await get_storage_info_async(printer.ip_address, printer.access_code, printer_model=printer.model)
  1165. return storage_info or {"used_bytes": None, "free_bytes": None}
  1166. # ============================================
  1167. # MQTT Debug Logging Endpoints
  1168. # ============================================
  1169. @router.post("/{printer_id}/logging/enable")
  1170. async def enable_mqtt_logging(
  1171. printer_id: int,
  1172. _=RequirePermissionIfAuthEnabled(Permission.PRINTERS_CONTROL),
  1173. db: AsyncSession = Depends(get_db),
  1174. ):
  1175. """Enable MQTT message logging for a printer."""
  1176. result = await db.execute(select(Printer).where(Printer.id == printer_id))
  1177. printer = result.scalar_one_or_none()
  1178. if not printer:
  1179. raise HTTPException(404, "Printer not found")
  1180. success = printer_manager.enable_logging(printer_id, True)
  1181. if not success:
  1182. raise HTTPException(400, "Printer not connected")
  1183. return {"logging_enabled": True}
  1184. @router.post("/{printer_id}/logging/disable")
  1185. async def disable_mqtt_logging(
  1186. printer_id: int,
  1187. _=RequirePermissionIfAuthEnabled(Permission.PRINTERS_CONTROL),
  1188. db: AsyncSession = Depends(get_db),
  1189. ):
  1190. """Disable MQTT message logging for a printer."""
  1191. result = await db.execute(select(Printer).where(Printer.id == printer_id))
  1192. printer = result.scalar_one_or_none()
  1193. if not printer:
  1194. raise HTTPException(404, "Printer not found")
  1195. success = printer_manager.enable_logging(printer_id, False)
  1196. if not success:
  1197. raise HTTPException(400, "Printer not connected")
  1198. return {"logging_enabled": False}
  1199. @router.get("/{printer_id}/logging")
  1200. async def get_mqtt_logs(
  1201. printer_id: int,
  1202. _=RequirePermissionIfAuthEnabled(Permission.PRINTERS_READ),
  1203. db: AsyncSession = Depends(get_db),
  1204. ):
  1205. """Get MQTT message logs for a printer."""
  1206. result = await db.execute(select(Printer).where(Printer.id == printer_id))
  1207. printer = result.scalar_one_or_none()
  1208. if not printer:
  1209. raise HTTPException(404, "Printer not found")
  1210. logs = printer_manager.get_logs(printer_id)
  1211. return {
  1212. "logging_enabled": printer_manager.is_logging_enabled(printer_id),
  1213. "logs": [
  1214. {
  1215. "timestamp": log.timestamp,
  1216. "topic": log.topic,
  1217. "direction": log.direction,
  1218. "payload": log.payload,
  1219. }
  1220. for log in logs
  1221. ],
  1222. }
  1223. @router.delete("/{printer_id}/logging")
  1224. async def clear_mqtt_logs(
  1225. printer_id: int,
  1226. _=RequirePermissionIfAuthEnabled(Permission.PRINTERS_CONTROL),
  1227. db: AsyncSession = Depends(get_db),
  1228. ):
  1229. """Clear MQTT message logs for a printer."""
  1230. result = await db.execute(select(Printer).where(Printer.id == printer_id))
  1231. printer = result.scalar_one_or_none()
  1232. if not printer:
  1233. raise HTTPException(404, "Printer not found")
  1234. printer_manager.clear_logs(printer_id)
  1235. return {"status": "cleared"}
  1236. # ============================================
  1237. # Print Options (AI Detection) Endpoints
  1238. # ============================================
  1239. @router.post("/{printer_id}/print-options")
  1240. async def set_print_option(
  1241. printer_id: int,
  1242. module_name: str,
  1243. enabled: bool,
  1244. print_halt: bool = True,
  1245. sensitivity: str = "medium",
  1246. _=RequirePermissionIfAuthEnabled(Permission.PRINTERS_CONTROL),
  1247. db: AsyncSession = Depends(get_db),
  1248. ):
  1249. """Set an AI detection / print option on the printer.
  1250. Valid module_name values:
  1251. - spaghetti_detector: Spaghetti detection
  1252. - first_layer_inspector: First layer inspection
  1253. - printing_monitor: AI print quality monitoring
  1254. - buildplate_marker_detector: Build plate marker detection
  1255. - allow_skip_parts: Allow skipping failed parts
  1256. """
  1257. result = await db.execute(select(Printer).where(Printer.id == printer_id))
  1258. printer = result.scalar_one_or_none()
  1259. if not printer:
  1260. raise HTTPException(404, "Printer not found")
  1261. client = printer_manager.get_client(printer_id)
  1262. if not client or not client.state.connected:
  1263. raise HTTPException(400, "Printer not connected")
  1264. # Validate module_name
  1265. valid_modules = [
  1266. "spaghetti_detector",
  1267. "first_layer_inspector",
  1268. "printing_monitor",
  1269. "buildplate_marker_detector",
  1270. "allow_skip_parts",
  1271. "pileup_detector",
  1272. "clump_detector",
  1273. "airprint_detector",
  1274. "auto_recovery_step_loss",
  1275. ]
  1276. if module_name not in valid_modules:
  1277. raise HTTPException(400, f"Invalid module_name. Must be one of: {valid_modules}")
  1278. # Validate sensitivity
  1279. valid_sensitivities = ["low", "medium", "high", "never_halt"]
  1280. if sensitivity not in valid_sensitivities:
  1281. raise HTTPException(400, f"Invalid sensitivity. Must be one of: {valid_sensitivities}")
  1282. success = client.set_xcam_option(
  1283. module_name=module_name,
  1284. enabled=enabled,
  1285. print_halt=print_halt,
  1286. sensitivity=sensitivity,
  1287. )
  1288. if not success:
  1289. raise HTTPException(500, "Failed to send command to printer")
  1290. return {
  1291. "success": True,
  1292. "module_name": module_name,
  1293. "enabled": enabled,
  1294. "print_halt": print_halt,
  1295. "sensitivity": sensitivity,
  1296. }
  1297. # ============================================
  1298. # Calibration
  1299. # ============================================
  1300. @router.post("/{printer_id}/calibration")
  1301. async def start_calibration(
  1302. printer_id: int,
  1303. bed_leveling: bool = False,
  1304. vibration: bool = False,
  1305. motor_noise: bool = False,
  1306. nozzle_offset: bool = False,
  1307. high_temp_heatbed: bool = False,
  1308. _=RequirePermissionIfAuthEnabled(Permission.PRINTERS_CONTROL),
  1309. db: AsyncSession = Depends(get_db),
  1310. ):
  1311. """Start printer calibration with selected options.
  1312. At least one option must be selected.
  1313. Options:
  1314. - bed_leveling: Run bed leveling calibration
  1315. - vibration: Run vibration compensation calibration
  1316. - motor_noise: Run motor noise cancellation calibration
  1317. - nozzle_offset: Run nozzle offset calibration (dual nozzle printers)
  1318. - high_temp_heatbed: Run high-temperature heatbed calibration
  1319. """
  1320. result = await db.execute(select(Printer).where(Printer.id == printer_id))
  1321. printer = result.scalar_one_or_none()
  1322. if not printer:
  1323. raise HTTPException(404, "Printer not found")
  1324. client = printer_manager.get_client(printer_id)
  1325. if not client or not client.state.connected:
  1326. raise HTTPException(400, "Printer not connected")
  1327. # Check that at least one option is selected
  1328. if not any([bed_leveling, vibration, motor_noise, nozzle_offset, high_temp_heatbed]):
  1329. raise HTTPException(400, "At least one calibration option must be selected")
  1330. success = client.start_calibration(
  1331. bed_leveling=bed_leveling,
  1332. vibration=vibration,
  1333. motor_noise=motor_noise,
  1334. nozzle_offset=nozzle_offset,
  1335. high_temp_heatbed=high_temp_heatbed,
  1336. )
  1337. if not success:
  1338. raise HTTPException(500, "Failed to send calibration command to printer")
  1339. return {
  1340. "success": True,
  1341. "bed_leveling": bed_leveling,
  1342. "vibration": vibration,
  1343. "motor_noise": motor_noise,
  1344. "nozzle_offset": nozzle_offset,
  1345. "high_temp_heatbed": high_temp_heatbed,
  1346. }
  1347. # ============================================================================
  1348. # Slot Preset Mapping Endpoints
  1349. # ============================================================================
  1350. @router.get("/{printer_id}/slot-presets")
  1351. async def get_slot_presets(
  1352. printer_id: int,
  1353. _=RequirePermissionIfAuthEnabled(Permission.PRINTERS_READ),
  1354. db: AsyncSession = Depends(get_db),
  1355. ):
  1356. """Get all saved slot-to-preset mappings for a printer."""
  1357. result = await db.execute(select(SlotPresetMapping).where(SlotPresetMapping.printer_id == printer_id))
  1358. mappings = result.scalars().all()
  1359. return {
  1360. mapping.ams_id * 4 + mapping.tray_id: {
  1361. "ams_id": mapping.ams_id,
  1362. "tray_id": mapping.tray_id,
  1363. "preset_id": mapping.preset_id,
  1364. "preset_name": mapping.preset_name,
  1365. }
  1366. for mapping in mappings
  1367. }
  1368. @router.get("/{printer_id}/slot-presets/{ams_id}/{tray_id}")
  1369. async def get_slot_preset(
  1370. printer_id: int,
  1371. ams_id: int,
  1372. tray_id: int,
  1373. _=RequirePermissionIfAuthEnabled(Permission.PRINTERS_READ),
  1374. db: AsyncSession = Depends(get_db),
  1375. ):
  1376. """Get the saved preset for a specific slot."""
  1377. result = await db.execute(
  1378. select(SlotPresetMapping).where(
  1379. SlotPresetMapping.printer_id == printer_id,
  1380. SlotPresetMapping.ams_id == ams_id,
  1381. SlotPresetMapping.tray_id == tray_id,
  1382. )
  1383. )
  1384. mapping = result.scalar_one_or_none()
  1385. if not mapping:
  1386. return None
  1387. return {
  1388. "ams_id": mapping.ams_id,
  1389. "tray_id": mapping.tray_id,
  1390. "preset_id": mapping.preset_id,
  1391. "preset_name": mapping.preset_name,
  1392. }
  1393. @router.put("/{printer_id}/slot-presets/{ams_id}/{tray_id}")
  1394. async def save_slot_preset(
  1395. printer_id: int,
  1396. ams_id: int,
  1397. tray_id: int,
  1398. preset_id: str,
  1399. preset_name: str,
  1400. preset_source: str = "cloud",
  1401. _=RequirePermissionIfAuthEnabled(Permission.PRINTERS_UPDATE),
  1402. db: AsyncSession = Depends(get_db),
  1403. ):
  1404. """Save a preset mapping for a specific slot."""
  1405. # Check printer exists
  1406. result = await db.execute(select(Printer).where(Printer.id == printer_id))
  1407. if not result.scalar_one_or_none():
  1408. raise HTTPException(404, "Printer not found")
  1409. # Check for existing mapping
  1410. result = await db.execute(
  1411. select(SlotPresetMapping).where(
  1412. SlotPresetMapping.printer_id == printer_id,
  1413. SlotPresetMapping.ams_id == ams_id,
  1414. SlotPresetMapping.tray_id == tray_id,
  1415. )
  1416. )
  1417. mapping = result.scalar_one_or_none()
  1418. if mapping:
  1419. # Update existing
  1420. mapping.preset_id = preset_id
  1421. mapping.preset_name = preset_name
  1422. mapping.preset_source = preset_source
  1423. else:
  1424. # Create new
  1425. mapping = SlotPresetMapping(
  1426. printer_id=printer_id,
  1427. ams_id=ams_id,
  1428. tray_id=tray_id,
  1429. preset_id=preset_id,
  1430. preset_name=preset_name,
  1431. preset_source=preset_source,
  1432. )
  1433. db.add(mapping)
  1434. await db.commit()
  1435. await db.refresh(mapping)
  1436. return {
  1437. "ams_id": mapping.ams_id,
  1438. "tray_id": mapping.tray_id,
  1439. "preset_id": mapping.preset_id,
  1440. "preset_name": mapping.preset_name,
  1441. "preset_source": mapping.preset_source,
  1442. }
  1443. @router.delete("/{printer_id}/slot-presets/{ams_id}/{tray_id}")
  1444. async def delete_slot_preset(
  1445. printer_id: int,
  1446. ams_id: int,
  1447. tray_id: int,
  1448. _=RequirePermissionIfAuthEnabled(Permission.PRINTERS_UPDATE),
  1449. db: AsyncSession = Depends(get_db),
  1450. ):
  1451. """Delete a saved preset mapping for a slot."""
  1452. result = await db.execute(
  1453. select(SlotPresetMapping).where(
  1454. SlotPresetMapping.printer_id == printer_id,
  1455. SlotPresetMapping.ams_id == ams_id,
  1456. SlotPresetMapping.tray_id == tray_id,
  1457. )
  1458. )
  1459. mapping = result.scalar_one_or_none()
  1460. if mapping:
  1461. await db.delete(mapping)
  1462. await db.commit()
  1463. return {"success": True}
  1464. @router.post("/{printer_id}/slots/{ams_id}/{tray_id}/configure")
  1465. async def configure_ams_slot(
  1466. printer_id: int,
  1467. ams_id: int,
  1468. tray_id: int,
  1469. tray_info_idx: str = Query(...),
  1470. tray_type: str = Query(...),
  1471. tray_sub_brands: str = Query(...),
  1472. tray_color: str = Query(...),
  1473. nozzle_temp_min: int = Query(...),
  1474. nozzle_temp_max: int = Query(...),
  1475. cali_idx: int = Query(-1),
  1476. nozzle_diameter: str = Query("0.4"),
  1477. setting_id: str = Query(""),
  1478. kprofile_filament_id: str = Query(""),
  1479. kprofile_setting_id: str = Query(""),
  1480. k_value: float = Query(0.0),
  1481. _=RequirePermissionIfAuthEnabled(Permission.PRINTERS_CONTROL),
  1482. ):
  1483. """Configure an AMS slot with a specific filament setting and K profile.
  1484. This sends two commands to the printer:
  1485. 1. ams_filament_setting - sets filament type, color, temperature
  1486. 2. extrusion_cali_sel - sets the K profile (pressure advance value)
  1487. Args:
  1488. printer_id: Database ID of the printer
  1489. ams_id: AMS unit ID (0-3 for regular AMS, 128-135 for HT AMS)
  1490. tray_id: Tray ID within the AMS (0-3)
  1491. tray_info_idx: Filament ID short format (e.g., "GFL05") or user preset ID
  1492. tray_type: Filament type (e.g., "PLA", "PETG")
  1493. tray_sub_brands: Sub-brand/profile name (e.g., "PLA Basic", "PETG HF")
  1494. tray_color: Color in RRGGBBAA hex format (e.g., "FFFF00FF")
  1495. nozzle_temp_min: Minimum nozzle temperature
  1496. nozzle_temp_max: Maximum nozzle temperature
  1497. cali_idx: K profile calibration index (-1 for default 0.020)
  1498. nozzle_diameter: Nozzle diameter string (e.g., "0.4")
  1499. setting_id: Full setting ID with version (e.g., "GFSL05_07") - optional
  1500. kprofile_filament_id: K profile's filament_id for proper K profile linking
  1501. k_value: Direct K value to set (0.0 to skip direct K value setting)
  1502. """
  1503. logger = logging.getLogger(__name__)
  1504. logger.info("[configure_ams_slot] printer_id=%s, ams_id=%s, tray_id=%s", printer_id, ams_id, tray_id)
  1505. logger.info(
  1506. f"[configure_ams_slot] tray_info_idx={tray_info_idx!r}, tray_type={tray_type!r}, tray_sub_brands={tray_sub_brands!r}"
  1507. )
  1508. logger.info(
  1509. f"[configure_ams_slot] setting_id={setting_id!r}, kprofile_filament_id={kprofile_filament_id!r}, kprofile_setting_id={kprofile_setting_id!r}"
  1510. )
  1511. # Get MQTT client for this printer
  1512. client = printer_manager.get_client(printer_id)
  1513. if not client:
  1514. raise HTTPException(status_code=400, detail="Printer not connected")
  1515. # Resolve tray_info_idx for the MQTT command.
  1516. # Priority:
  1517. # 1. Use the provided tray_info_idx if set (including cloud-synced
  1518. # custom presets like PFUS* / P*).
  1519. # 2. Reuse the slot's existing tray_info_idx if it's a specific
  1520. # (non-generic) preset for the same material.
  1521. # 3. Fall back to a generic Bambu filament ID.
  1522. _GENERIC_FILAMENT_IDS = {
  1523. "PLA": "GFL99",
  1524. "PETG": "GFG99",
  1525. "ABS": "GFB99",
  1526. "ASA": "GFB98",
  1527. "PC": "GFC99",
  1528. "PA": "GFN99",
  1529. "NYLON": "GFN99",
  1530. "TPU": "GFU99",
  1531. "PVA": "GFS99",
  1532. "HIPS": "GFS98",
  1533. "PLA-CF": "GFL98",
  1534. "PETG-CF": "GFG98",
  1535. "PA-CF": "GFN98",
  1536. "PETG HF": "GFG96",
  1537. }
  1538. _GENERIC_ID_VALUES = set(_GENERIC_FILAMENT_IDS.values())
  1539. effective_tray_info_idx = tray_info_idx
  1540. if not tray_info_idx:
  1541. # No preset provided — try slot reuse or generic fallback
  1542. current_tray_info_idx = ""
  1543. current_tray_type = ""
  1544. state = printer_manager.get_status(printer_id)
  1545. if state and state.raw_data:
  1546. from backend.app.api.routes.inventory import _find_tray_in_ams_data
  1547. if ams_id == 255:
  1548. vt_tray = state.raw_data.get("vt_tray") or []
  1549. ext_id = tray_id + 254
  1550. for vt in vt_tray:
  1551. if isinstance(vt, dict) and int(vt.get("id", 254)) == ext_id:
  1552. current_tray_info_idx = vt.get("tray_info_idx", "")
  1553. current_tray_type = vt.get("tray_type", "")
  1554. break
  1555. else:
  1556. ams_data = state.raw_data.get("ams", {})
  1557. ams_list = (
  1558. ams_data.get("ams", [])
  1559. if isinstance(ams_data, dict)
  1560. else ams_data
  1561. if isinstance(ams_data, list)
  1562. else []
  1563. )
  1564. cur_tray = _find_tray_in_ams_data(ams_list, ams_id, tray_id)
  1565. if cur_tray:
  1566. current_tray_info_idx = cur_tray.get("tray_info_idx", "")
  1567. current_tray_type = cur_tray.get("tray_type", "")
  1568. if (
  1569. current_tray_info_idx
  1570. and current_tray_info_idx not in _GENERIC_ID_VALUES
  1571. and current_tray_type
  1572. and current_tray_type.upper() == tray_type.upper()
  1573. ):
  1574. logger.info(
  1575. "[configure_ams_slot] Reusing slot's existing tray_info_idx=%r (same material %r)",
  1576. current_tray_info_idx,
  1577. tray_type,
  1578. )
  1579. effective_tray_info_idx = current_tray_info_idx
  1580. elif tray_type:
  1581. material = tray_type.upper().strip()
  1582. generic = (
  1583. _GENERIC_FILAMENT_IDS.get(material)
  1584. or _GENERIC_FILAMENT_IDS.get(material.split("-")[0].split(" ")[0])
  1585. or ""
  1586. )
  1587. if generic:
  1588. logger.info("[configure_ams_slot] Falling back to generic %r for material %r", generic, tray_type)
  1589. effective_tray_info_idx = generic
  1590. # Send filament setting + K-profile commands
  1591. filament_id_for_kprofile = kprofile_filament_id if kprofile_filament_id else effective_tray_info_idx
  1592. # Always send ams_set_filament_setting — the user explicitly clicked
  1593. # "Configure Slot", so honor that. Previous versions skipped this for
  1594. # RFID-tagged slots to preserve the slicer eye icon, but printers cache
  1595. # stale tag_uid/tray_uuid after a BL spool is removed, causing the check
  1596. # to false-positive on non-RFID slots and silently drop the command.
  1597. success = client.ams_set_filament_setting(
  1598. ams_id=ams_id,
  1599. tray_id=tray_id,
  1600. tray_info_idx=effective_tray_info_idx,
  1601. tray_type=tray_type,
  1602. tray_sub_brands=tray_sub_brands,
  1603. tray_color=tray_color,
  1604. nozzle_temp_min=nozzle_temp_min,
  1605. nozzle_temp_max=nozzle_temp_max,
  1606. setting_id=setting_id,
  1607. )
  1608. if not success:
  1609. raise HTTPException(status_code=500, detail="Failed to send filament configuration command")
  1610. # Method 1: Select existing calibration profile by cali_idx
  1611. # Do NOT include setting_id — BambuStudio never sends it in extrusion_cali_sel,
  1612. # and including it causes the firmware to mislink the profile on X1C/P1S.
  1613. client.extrusion_cali_sel(
  1614. ams_id=ams_id,
  1615. tray_id=tray_id,
  1616. cali_idx=cali_idx,
  1617. filament_id=filament_id_for_kprofile,
  1618. nozzle_diameter=nozzle_diameter,
  1619. )
  1620. # Method 2: Only send extrusion_cali_set when NO existing profile was selected
  1621. # (cali_idx == -1). When cali_idx >= 0, extrusion_cali_sel already selected the
  1622. # correct profile. Sending extrusion_cali_set with the same cali_idx would MODIFY
  1623. # the existing profile's metadata (extruder_id, nozzle_id, name, setting_id),
  1624. # corrupting it — e.g., overwriting a High Flow extruder 1 profile with
  1625. # hardcoded extruder_id=0 and nozzle_id=HS00.
  1626. if k_value > 0 and cali_idx < 0:
  1627. # Calculate global tray ID for extrusion_cali_set
  1628. if ams_id <= 3:
  1629. global_tray_id = ams_id * 4 + tray_id
  1630. elif ams_id >= 128 and ams_id <= 135:
  1631. global_tray_id = (ams_id - 128) * 4 + tray_id
  1632. else:
  1633. global_tray_id = tray_id
  1634. client.extrusion_cali_set(
  1635. tray_id=global_tray_id,
  1636. k_value=k_value,
  1637. nozzle_diameter=nozzle_diameter,
  1638. nozzle_temp=nozzle_temp_max,
  1639. filament_id=filament_id_for_kprofile,
  1640. setting_id=kprofile_setting_id or "",
  1641. name=tray_sub_brands or "",
  1642. cali_idx=cali_idx,
  1643. )
  1644. # Request fresh status push from printer so frontend gets updated data via WebSocket
  1645. logger.info("[configure_ams_slot] Requesting status update from printer")
  1646. update_result = client.request_status_update()
  1647. logger.info("[configure_ams_slot] Status update request result: %s", update_result)
  1648. return {
  1649. "success": True,
  1650. "message": f"Configured AMS {ams_id} tray {tray_id} with {tray_sub_brands}",
  1651. }
  1652. @router.post("/{printer_id}/ams/{ams_id}/tray/{tray_id}/reset")
  1653. async def reset_ams_slot(
  1654. printer_id: int,
  1655. ams_id: int,
  1656. tray_id: int,
  1657. db: AsyncSession = Depends(get_db),
  1658. _=RequirePermissionIfAuthEnabled(Permission.PRINTERS_CONTROL),
  1659. ):
  1660. """Reset an AMS slot to empty/unconfigured state.
  1661. This clears the filament configuration from the slot.
  1662. """
  1663. # Get MQTT client for this printer
  1664. client = printer_manager.get_client(printer_id)
  1665. if not client:
  1666. raise HTTPException(status_code=400, detail="Printer not connected")
  1667. # Reset the slot
  1668. success = client.reset_ams_slot(ams_id=ams_id, tray_id=tray_id)
  1669. if not success:
  1670. raise HTTPException(status_code=500, detail="Failed to send reset command")
  1671. # Also delete any saved slot preset mapping
  1672. result = await db.execute(
  1673. select(SlotPresetMapping).where(
  1674. SlotPresetMapping.printer_id == printer_id,
  1675. SlotPresetMapping.ams_id == ams_id,
  1676. SlotPresetMapping.tray_id == tray_id,
  1677. )
  1678. )
  1679. mapping = result.scalar_one_or_none()
  1680. if mapping:
  1681. await db.delete(mapping)
  1682. await db.commit()
  1683. # Request fresh status push from printer so frontend gets updated data via WebSocket
  1684. client.request_status_update()
  1685. return {
  1686. "success": True,
  1687. "message": f"Reset AMS {ams_id} tray {tray_id}",
  1688. }
  1689. @router.get("/{printer_id}/ams-labels")
  1690. async def get_ams_labels(
  1691. printer_id: int,
  1692. _=RequirePermissionIfAuthEnabled(Permission.PRINTERS_READ),
  1693. db: AsyncSession = Depends(get_db),
  1694. ):
  1695. """Get all user-defined AMS labels for a printer, keyed by AMS unit ID.
  1696. Labels are stored by AMS serial number. This endpoint resolves the current
  1697. serial-to-ams_id mapping from the live printer state so the response is still
  1698. keyed by ams_id for UI compatibility.
  1699. """
  1700. # Build serial -> ams_id map from live printer state
  1701. serial_to_ams_id: dict[str, int] = {}
  1702. state = printer_manager.get_status(printer_id)
  1703. if state and state.raw_data:
  1704. for ams_unit in state.raw_data.get("ams", []):
  1705. sn = str(ams_unit.get("sn") or ams_unit.get("serial_number") or "")
  1706. if sn:
  1707. serial_to_ams_id[sn] = int(ams_unit.get("id", 0))
  1708. # Collect all known serials for this printer (live + synthetic fallback keys)
  1709. serials_to_query = set(serial_to_ams_id.keys())
  1710. # Fetch labels for all known serials
  1711. labels: dict[int, str] = {}
  1712. if serials_to_query:
  1713. result = await db.execute(
  1714. select(AmsLabel).where(AmsLabel.ams_serial_number.in_(serials_to_query))
  1715. )
  1716. for lbl in result.scalars().all():
  1717. aid = serial_to_ams_id.get(lbl.ams_serial_number)
  1718. if aid is not None:
  1719. labels[aid] = lbl.label
  1720. # Also fetch labels stored under synthetic keys for this printer (backward compat)
  1721. # Collect all synthetic keys first, then query with a single IN clause.
  1722. if state and state.raw_data:
  1723. synthetic_key_to_aid: dict[str, int] = {
  1724. f"p{printer_id}a{int(ams_unit.get('id', 0))}": int(ams_unit.get("id", 0))
  1725. for ams_unit in state.raw_data.get("ams", [])
  1726. if int(ams_unit.get("id", 0)) not in labels
  1727. }
  1728. if synthetic_key_to_aid:
  1729. result = await db.execute(
  1730. select(AmsLabel).where(AmsLabel.ams_serial_number.in_(synthetic_key_to_aid.keys()))
  1731. )
  1732. for lbl in result.scalars().all():
  1733. aid = synthetic_key_to_aid.get(lbl.ams_serial_number)
  1734. if aid is not None:
  1735. labels[aid] = lbl.label
  1736. return labels
  1737. @router.put("/{printer_id}/ams-labels/{ams_id}")
  1738. async def save_ams_label(
  1739. printer_id: int,
  1740. ams_id: int,
  1741. body: AmsLabelBody,
  1742. _=RequirePermissionIfAuthEnabled(Permission.PRINTERS_UPDATE),
  1743. db: AsyncSession = Depends(get_db),
  1744. ):
  1745. """Create or update the friendly name for a specific AMS unit.
  1746. When ``ams_serial`` is provided the label is stored under that serial number so
  1747. it survives the AMS being moved to a different printer. When it is absent (e.g.
  1748. older firmware that does not report a serial) a synthetic key based on the
  1749. printer_id and ams_id is used as a fallback.
  1750. """
  1751. # Verify printer exists
  1752. result = await db.execute(select(Printer).where(Printer.id == printer_id))
  1753. if not result.scalar_one_or_none():
  1754. raise HTTPException(404, "Printer not found")
  1755. # Determine the serial key to store under
  1756. stripped = body.ams_serial.strip() if body.ams_serial else ""
  1757. serial_key = stripped if stripped else f"p{printer_id}a{ams_id}"
  1758. result = await db.execute(
  1759. select(AmsLabel).where(AmsLabel.ams_serial_number == serial_key)
  1760. )
  1761. existing = result.scalar_one_or_none()
  1762. if existing:
  1763. existing.label = body.label
  1764. existing.ams_id = ams_id
  1765. else:
  1766. db.add(AmsLabel(ams_serial_number=serial_key, ams_id=ams_id, label=body.label))
  1767. await db.commit()
  1768. return {"ams_id": ams_id, "label": body.label}
  1769. @router.delete("/{printer_id}/ams-labels/{ams_id}")
  1770. async def delete_ams_label(
  1771. printer_id: int,
  1772. ams_id: int,
  1773. ams_serial: str = Query(default="", max_length=50),
  1774. _=RequirePermissionIfAuthEnabled(Permission.PRINTERS_UPDATE),
  1775. db: AsyncSession = Depends(get_db),
  1776. ):
  1777. """Delete the friendly name for a specific AMS unit, reverting to the auto label."""
  1778. stripped = ams_serial.strip() if ams_serial else ""
  1779. serial_key = stripped if stripped else f"p{printer_id}a{ams_id}"
  1780. result = await db.execute(
  1781. select(AmsLabel).where(AmsLabel.ams_serial_number == serial_key)
  1782. )
  1783. existing = result.scalar_one_or_none()
  1784. if existing:
  1785. await db.delete(existing)
  1786. await db.commit()
  1787. return {"success": True}
  1788. @router.post("/{printer_id}/debug/simulate-print-complete")
  1789. async def debug_simulate_print_complete(
  1790. printer_id: int,
  1791. db: AsyncSession = Depends(get_db),
  1792. _=RequirePermissionIfAuthEnabled(Permission.PRINTERS_CONTROL),
  1793. ):
  1794. """DEBUG: Simulate print completion to test freeze behavior.
  1795. This triggers the same code path as a real print completion,
  1796. without needing to wait for an actual print to finish.
  1797. """
  1798. from backend.app.main import _active_prints, on_print_complete
  1799. from backend.app.models.archive import PrintArchive
  1800. # Get the most recent archive for this printer
  1801. result = await db.execute(
  1802. select(PrintArchive)
  1803. .where(PrintArchive.printer_id == printer_id)
  1804. .order_by(PrintArchive.created_at.desc())
  1805. .limit(1)
  1806. )
  1807. archive = result.scalar_one_or_none()
  1808. if not archive:
  1809. raise HTTPException(status_code=404, detail="No archives found for this printer")
  1810. # Register this archive as "active" so on_print_complete can find it
  1811. filename = archive.file_path.split("/")[-1] if archive.file_path else "test.3mf"
  1812. subtask_name = archive.print_name or "Test Print"
  1813. _active_prints[(printer_id, filename)] = archive.id
  1814. _active_prints[(printer_id, subtask_name)] = archive.id
  1815. # Simulate print completion data
  1816. data = {
  1817. "status": "completed",
  1818. "filename": filename,
  1819. "subtask_name": subtask_name,
  1820. "timelapse_was_active": False,
  1821. }
  1822. logger.info("Simulating print complete for printer %s, archive %s", printer_id, archive.id)
  1823. # Call the actual on_print_complete handler
  1824. await on_print_complete(printer_id, data)
  1825. return {"success": True, "archive_id": archive.id, "message": "Print completion simulated"}
  1826. # =============================================================================
  1827. # Print Control Endpoints
  1828. # =============================================================================
  1829. @router.post("/{printer_id}/print/stop")
  1830. async def stop_print(
  1831. printer_id: int,
  1832. _=RequirePermissionIfAuthEnabled(Permission.PRINTERS_CONTROL),
  1833. db: AsyncSession = Depends(get_db),
  1834. ):
  1835. """Stop/cancel the current print job."""
  1836. result = await db.execute(select(Printer).where(Printer.id == printer_id))
  1837. printer = result.scalar_one_or_none()
  1838. if not printer:
  1839. raise HTTPException(404, "Printer not found")
  1840. client = printer_manager.get_client(printer_id)
  1841. if not client:
  1842. raise HTTPException(400, "Printer not connected")
  1843. success = client.stop_print()
  1844. if not success:
  1845. raise HTTPException(500, "Failed to stop print")
  1846. return {"success": True, "message": "Print stop command sent"}
  1847. @router.post("/{printer_id}/clear-plate")
  1848. async def clear_plate(
  1849. printer_id: int,
  1850. _=RequirePermissionIfAuthEnabled(Permission.PRINTERS_CLEAR_PLATE),
  1851. db: AsyncSession = Depends(get_db),
  1852. ):
  1853. """Acknowledge that the build plate has been cleared after a finished/failed print.
  1854. Sets a plate-cleared flag so the scheduler can start the next queued print.
  1855. No MQTT command is sent to the printer — the scheduler's start_print command
  1856. will override the FINISH/FAILED state when it sends the next job.
  1857. """
  1858. result = await db.execute(select(Printer).where(Printer.id == printer_id))
  1859. printer = result.scalar_one_or_none()
  1860. if not printer:
  1861. raise HTTPException(404, "Printer not found")
  1862. if not printer_manager.is_connected(printer_id):
  1863. raise HTTPException(400, "Printer not connected")
  1864. state = printer_manager.get_status(printer_id)
  1865. if not state or state.state not in ("FINISH", "FAILED"):
  1866. raise HTTPException(
  1867. 400, f"Printer is not in FINISH or FAILED state (current: {state.state if state else 'unknown'})"
  1868. )
  1869. printer_manager.set_plate_cleared(printer_id)
  1870. return {"success": True, "message": "Plate cleared, next print will start shortly"}
  1871. @router.post("/{printer_id}/print/pause")
  1872. async def pause_print(
  1873. printer_id: int,
  1874. _=RequirePermissionIfAuthEnabled(Permission.PRINTERS_CONTROL),
  1875. db: AsyncSession = Depends(get_db),
  1876. ):
  1877. """Pause the current print job."""
  1878. result = await db.execute(select(Printer).where(Printer.id == printer_id))
  1879. printer = result.scalar_one_or_none()
  1880. if not printer:
  1881. raise HTTPException(404, "Printer not found")
  1882. client = printer_manager.get_client(printer_id)
  1883. if not client:
  1884. raise HTTPException(400, "Printer not connected")
  1885. success = client.pause_print()
  1886. if not success:
  1887. raise HTTPException(500, "Failed to pause print")
  1888. return {"success": True, "message": "Print pause command sent"}
  1889. @router.post("/{printer_id}/print/resume")
  1890. async def resume_print(
  1891. printer_id: int,
  1892. _=RequirePermissionIfAuthEnabled(Permission.PRINTERS_CONTROL),
  1893. db: AsyncSession = Depends(get_db),
  1894. ):
  1895. """Resume a paused print job."""
  1896. result = await db.execute(select(Printer).where(Printer.id == printer_id))
  1897. printer = result.scalar_one_or_none()
  1898. if not printer:
  1899. raise HTTPException(404, "Printer not found")
  1900. client = printer_manager.get_client(printer_id)
  1901. if not client:
  1902. raise HTTPException(400, "Printer not connected")
  1903. success = client.resume_print()
  1904. if not success:
  1905. raise HTTPException(500, "Failed to resume print")
  1906. return {"success": True, "message": "Print resume command sent"}
  1907. @router.post("/{printer_id}/chamber-light")
  1908. async def set_chamber_light(
  1909. printer_id: int,
  1910. on: bool = Query(..., description="True to turn on, False to turn off"),
  1911. _=RequirePermissionIfAuthEnabled(Permission.PRINTERS_CONTROL),
  1912. db: AsyncSession = Depends(get_db),
  1913. ):
  1914. """Turn the chamber light on or off."""
  1915. result = await db.execute(select(Printer).where(Printer.id == printer_id))
  1916. printer = result.scalar_one_or_none()
  1917. if not printer:
  1918. raise HTTPException(404, "Printer not found")
  1919. client = printer_manager.get_client(printer_id)
  1920. if not client:
  1921. raise HTTPException(400, "Printer not connected")
  1922. success = client.set_chamber_light(on)
  1923. if not success:
  1924. raise HTTPException(500, "Failed to control chamber light")
  1925. return {"success": True, "message": f"Chamber light {'on' if on else 'off'}"}
  1926. @router.post("/{printer_id}/hms/clear")
  1927. async def clear_hms_errors(
  1928. printer_id: int,
  1929. _=RequirePermissionIfAuthEnabled(Permission.PRINTERS_CONTROL),
  1930. db: AsyncSession = Depends(get_db),
  1931. ):
  1932. """Clear HMS/print errors on the printer."""
  1933. result = await db.execute(select(Printer).where(Printer.id == printer_id))
  1934. printer = result.scalar_one_or_none()
  1935. if not printer:
  1936. raise HTTPException(404, "Printer not found")
  1937. client = printer_manager.get_client(printer_id)
  1938. if not client:
  1939. raise HTTPException(400, "Printer not connected")
  1940. success = client.clear_hms_errors()
  1941. if not success:
  1942. raise HTTPException(500, "Failed to clear HMS errors")
  1943. return {"success": True, "message": "HMS errors cleared"}
  1944. @router.get("/{printer_id}/print/objects")
  1945. async def get_printable_objects(
  1946. printer_id: int,
  1947. reload: bool = False,
  1948. _=RequirePermissionIfAuthEnabled(Permission.PRINTERS_READ),
  1949. db: AsyncSession = Depends(get_db),
  1950. ):
  1951. """Get the list of printable objects for the current print.
  1952. Returns a list of objects with id, name, position (if available), and skip status.
  1953. Objects that have already been skipped are marked in the skipped_objects list.
  1954. Args:
  1955. reload: If True, reload objects from the archive file (useful after restart)
  1956. """
  1957. result = await db.execute(select(Printer).where(Printer.id == printer_id))
  1958. printer = result.scalar_one_or_none()
  1959. if not printer:
  1960. raise HTTPException(404, "Printer not found")
  1961. client = printer_manager.get_client(printer_id)
  1962. if not client:
  1963. raise HTTPException(400, "Printer not connected")
  1964. # Reload objects from 3MF if requested or no objects loaded
  1965. if reload or not client.state.printable_objects:
  1966. subtask_name = client.state.subtask_name
  1967. if subtask_name:
  1968. from backend.app.services.archive import extract_printable_objects_from_3mf
  1969. from backend.app.services.bambu_ftp import download_file_try_paths_async
  1970. # Build possible 3MF filenames (try both .gcode.3mf and .3mf)
  1971. possible_filenames = []
  1972. if subtask_name.endswith(".3mf"):
  1973. possible_filenames.append(subtask_name)
  1974. else:
  1975. possible_filenames.append(f"{subtask_name}.gcode.3mf")
  1976. possible_filenames.append(f"{subtask_name}.3mf")
  1977. # Also try with spaces converted to underscores (Bambu Studio may normalize filenames)
  1978. if " " in subtask_name:
  1979. normalized = subtask_name.replace(" ", "_")
  1980. if normalized.endswith(".3mf"):
  1981. possible_filenames.append(normalized)
  1982. else:
  1983. possible_filenames.append(f"{normalized}.gcode.3mf")
  1984. possible_filenames.append(f"{normalized}.3mf")
  1985. # Download 3MF from printer
  1986. temp_path = settings.archive_dir / "temp" / f"objects_{printer_id}_{possible_filenames[0]}"
  1987. temp_path.parent.mkdir(parents=True, exist_ok=True)
  1988. # Build list of all remote paths to try
  1989. remote_paths = []
  1990. for filename in possible_filenames:
  1991. remote_paths.extend([f"/{filename}", f"/cache/{filename}", f"/model/{filename}"])
  1992. try:
  1993. downloaded = await download_file_try_paths_async(
  1994. printer.ip_address,
  1995. printer.access_code,
  1996. remote_paths,
  1997. temp_path,
  1998. printer_model=printer.model,
  1999. )
  2000. if downloaded and temp_path.exists():
  2001. with open(temp_path, "rb") as f:
  2002. data = f.read()
  2003. objects, bbox_all = extract_printable_objects_from_3mf(data, include_positions=True)
  2004. if objects:
  2005. client.state.printable_objects = objects
  2006. client.state.printable_objects_bbox_all = bbox_all
  2007. logger.info("Reloaded %s objects for printer %s", len(objects), printer_id)
  2008. except Exception as e:
  2009. logger.debug("Failed to reload objects from printer: %s", e)
  2010. finally:
  2011. if temp_path.exists():
  2012. temp_path.unlink()
  2013. # Return objects with their skip status and position data
  2014. objects = []
  2015. for obj_id, obj_data in client.state.printable_objects.items():
  2016. # Handle both old format (string name) and new format (dict with name, x, y)
  2017. if isinstance(obj_data, dict):
  2018. obj_entry = {
  2019. "id": obj_id,
  2020. "name": obj_data.get("name", f"Object {obj_id}"),
  2021. "x": obj_data.get("x"),
  2022. "y": obj_data.get("y"),
  2023. "skipped": obj_id in client.state.skipped_objects,
  2024. }
  2025. else:
  2026. # Legacy format: obj_data is just the name string
  2027. obj_entry = {
  2028. "id": obj_id,
  2029. "name": obj_data,
  2030. "x": None,
  2031. "y": None,
  2032. "skipped": obj_id in client.state.skipped_objects,
  2033. }
  2034. objects.append(obj_entry)
  2035. return {
  2036. "objects": objects,
  2037. "total": len(objects),
  2038. "skipped_count": len(client.state.skipped_objects),
  2039. "is_printing": client.state.state in ("RUNNING", "PAUSE"),
  2040. "bbox_all": getattr(client.state, "printable_objects_bbox_all", None),
  2041. }
  2042. @router.post("/{printer_id}/print/skip-objects")
  2043. async def skip_objects(
  2044. printer_id: int,
  2045. object_ids: list[int],
  2046. _=RequirePermissionIfAuthEnabled(Permission.PRINTERS_CONTROL),
  2047. db: AsyncSession = Depends(get_db),
  2048. ):
  2049. """Skip specific objects during the current print.
  2050. Args:
  2051. object_ids: List of object identify_id values to skip
  2052. """
  2053. result = await db.execute(select(Printer).where(Printer.id == printer_id))
  2054. printer = result.scalar_one_or_none()
  2055. if not printer:
  2056. raise HTTPException(404, "Printer not found")
  2057. client = printer_manager.get_client(printer_id)
  2058. if not client:
  2059. raise HTTPException(400, "Printer not connected")
  2060. if not object_ids:
  2061. raise HTTPException(400, "No object IDs provided")
  2062. # Validate object IDs exist in printable_objects
  2063. invalid_ids = [oid for oid in object_ids if oid not in client.state.printable_objects]
  2064. if invalid_ids:
  2065. raise HTTPException(400, f"Invalid object IDs: {invalid_ids}")
  2066. success = client.skip_objects(object_ids)
  2067. if not success:
  2068. raise HTTPException(500, "Failed to skip objects")
  2069. # Get names of skipped objects for response (handle both old and new format)
  2070. skipped_names = []
  2071. for oid in object_ids:
  2072. obj_data = client.state.printable_objects.get(oid, str(oid))
  2073. if isinstance(obj_data, dict):
  2074. skipped_names.append(obj_data.get("name", str(oid)))
  2075. else:
  2076. skipped_names.append(obj_data)
  2077. return {
  2078. "success": True,
  2079. "message": f"Skipped {len(object_ids)} object(s): {', '.join(skipped_names)}",
  2080. "skipped_objects": object_ids,
  2081. }
  2082. # =============================================================================
  2083. # AMS Control Endpoints
  2084. # =============================================================================
  2085. @router.post("/{printer_id}/ams/{ams_id}/slot/{slot_id}/refresh")
  2086. async def refresh_ams_slot(
  2087. printer_id: int,
  2088. ams_id: int,
  2089. slot_id: int,
  2090. _=RequirePermissionIfAuthEnabled(Permission.PRINTERS_AMS_RFID),
  2091. db: AsyncSession = Depends(get_db),
  2092. ):
  2093. """Re-read RFID for an AMS slot (triggers filament info refresh)."""
  2094. result = await db.execute(select(Printer).where(Printer.id == printer_id))
  2095. printer = result.scalar_one_or_none()
  2096. if not printer:
  2097. raise HTTPException(404, "Printer not found")
  2098. client = printer_manager.get_client(printer_id)
  2099. if not client:
  2100. raise HTTPException(400, "Printer not connected")
  2101. success, message = client.ams_refresh_tray(ams_id, slot_id)
  2102. if not success:
  2103. raise HTTPException(400, message)
  2104. # Apply PA profile after delay (RFID re-read takes a few seconds)
  2105. asyncio.create_task(_apply_pa_after_refresh(printer_id, ams_id, slot_id))
  2106. return {"success": True, "message": message}
  2107. async def _apply_pa_after_refresh(printer_id: int, ams_id: int, slot_id: int):
  2108. """Apply PA profile after RFID re-read completes.
  2109. Waits for the printer to finish processing the RFID data, then selects
  2110. the K-profile via extrusion_cali_sel. Does NOT re-send ams_set_filament_setting
  2111. because that would overwrite the RFID-provided filament data.
  2112. """
  2113. await asyncio.sleep(5)
  2114. try:
  2115. from backend.app.api.routes.inventory import _find_tray_in_ams_data
  2116. from backend.app.core.database import async_session
  2117. from backend.app.models.spool import Spool
  2118. from backend.app.models.spool_assignment import SpoolAssignment as SA
  2119. from backend.app.services.spool_tag_matcher import is_bambu_tag
  2120. client = printer_manager.get_client(printer_id)
  2121. if not client:
  2122. return
  2123. state = printer_manager.get_status(printer_id)
  2124. if not state or not state.raw_data:
  2125. return
  2126. # Find current tray data (should have RFID data by now)
  2127. ams_data = state.raw_data.get("ams", {})
  2128. ams_list = (
  2129. ams_data.get("ams", []) if isinstance(ams_data, dict) else ams_data if isinstance(ams_data, list) else []
  2130. )
  2131. tray = _find_tray_in_ams_data(ams_list, ams_id, slot_id)
  2132. if not tray or not tray.get("tray_type"):
  2133. logger.debug("PA re-apply: no tray data for AMS%d-T%d", ams_id, slot_id)
  2134. return
  2135. tag_uid = tray.get("tag_uid", "")
  2136. tray_uuid = tray.get("tray_uuid", "")
  2137. tray_info_idx = tray.get("tray_info_idx", "")
  2138. if not is_bambu_tag(tag_uid, tray_uuid, tray_info_idx):
  2139. return
  2140. async with async_session() as db:
  2141. from sqlalchemy import select as sa_select
  2142. from sqlalchemy.orm import selectinload
  2143. result = await db.execute(
  2144. sa_select(SA)
  2145. .options(selectinload(SA.spool).selectinload(Spool.k_profiles))
  2146. .where(SA.printer_id == printer_id, SA.ams_id == ams_id, SA.tray_id == slot_id)
  2147. )
  2148. assignment = result.scalar_one_or_none()
  2149. if not assignment or not assignment.spool or not assignment.spool.k_profiles:
  2150. return
  2151. spool = assignment.spool
  2152. nozzle_diameter = "0.4"
  2153. if state.nozzles:
  2154. nd = state.nozzles[0].nozzle_diameter
  2155. if nd:
  2156. nozzle_diameter = nd
  2157. # Determine slot's extruder from ams_extruder_map
  2158. slot_extruder = None
  2159. if state.ams_extruder_map:
  2160. if ams_id == 255:
  2161. # External slots: ext-L (tray 0) → extruder 1, ext-R (tray 1) → extruder 0
  2162. slot_extruder = 1 - slot_id # 0→1, 1→0
  2163. else:
  2164. slot_extruder = state.ams_extruder_map.get(str(ams_id))
  2165. matching_kp = None
  2166. for kp in spool.k_profiles:
  2167. if kp.printer_id == printer_id and kp.nozzle_diameter == nozzle_diameter:
  2168. if slot_extruder is not None and kp.extruder_id is not None and kp.extruder_id != slot_extruder:
  2169. continue
  2170. matching_kp = kp
  2171. break
  2172. if not matching_kp or matching_kp.cali_idx is None:
  2173. return
  2174. # The filament_id in extrusion_cali_sel must match the filament preset
  2175. # under which the K-profile was calibrated. Use spool.slicer_filament
  2176. # (the preset assigned in inventory), falling back to tray's RFID value.
  2177. kp_filament_id = spool.slicer_filament or tray_info_idx
  2178. logger.info(
  2179. "PA re-apply AMS%d-T%d: cali_idx=%d, filament_id=%s",
  2180. ams_id,
  2181. slot_id,
  2182. matching_kp.cali_idx,
  2183. kp_filament_id,
  2184. )
  2185. # 1. Select K-profile
  2186. # NOTE: Do NOT send ams_set_filament_setting here — it tells the firmware
  2187. # "this is a manual config" which destroys the RFID-detected spool state
  2188. # (changes eye icon to pen icon in slicer).
  2189. client.extrusion_cali_sel(
  2190. ams_id=ams_id,
  2191. tray_id=slot_id,
  2192. cali_idx=matching_kp.cali_idx,
  2193. filament_id=kp_filament_id,
  2194. nozzle_diameter=nozzle_diameter,
  2195. )
  2196. # NOTE: Do NOT send extrusion_cali_set here. extrusion_cali_sel already
  2197. # selected the correct profile by cali_idx. Sending extrusion_cali_set with
  2198. # the same cali_idx would MODIFY the existing profile's metadata (extruder_id,
  2199. # nozzle_id, name), corrupting it.
  2200. logger.info(
  2201. "Applied PA profile cali_idx=%d k=%.3f to printer %d AMS%d-T%d",
  2202. matching_kp.cali_idx,
  2203. matching_kp.k_value or 0,
  2204. printer_id,
  2205. ams_id,
  2206. slot_id,
  2207. )
  2208. except Exception as e:
  2209. logger.warning("Failed to apply PA profile after RFID re-read: %s", e)
  2210. @router.get("/{printer_id}/runtime-debug")
  2211. async def get_runtime_debug(
  2212. printer_id: int,
  2213. _=RequirePermissionIfAuthEnabled(Permission.PRINTERS_READ),
  2214. db: AsyncSession = Depends(get_db),
  2215. ):
  2216. """Debug endpoint: Get runtime tracking status for a printer."""
  2217. result = await db.execute(select(Printer).where(Printer.id == printer_id))
  2218. printer = result.scalar_one_or_none()
  2219. if not printer:
  2220. raise HTTPException(404, "Printer not found")
  2221. state = printer_manager.get_status(printer_id)
  2222. return {
  2223. "printer_name": printer.name,
  2224. "runtime_seconds": printer.runtime_seconds,
  2225. "runtime_hours": printer.runtime_seconds / 3600.0 if printer.runtime_seconds else 0,
  2226. "print_hours_offset": printer.print_hours_offset,
  2227. "total_hours": (printer.runtime_seconds / 3600.0 if printer.runtime_seconds else 0)
  2228. + (printer.print_hours_offset or 0),
  2229. "last_runtime_update": printer.last_runtime_update.isoformat() if printer.last_runtime_update else None,
  2230. "mqtt_state": {
  2231. "connected": state.connected if state else False,
  2232. "state": state.state if state else None,
  2233. "progress": state.progress if state else None,
  2234. "gcode_file": state.gcode_file if state else None,
  2235. }
  2236. if state
  2237. else None,
  2238. "is_active": printer.is_active,
  2239. }