printers.py 133 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419
  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 RequireCameraStreamTokenIfAuthEnabled, 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. DiagnosticRequest,
  21. FilaSwitchResponse,
  22. HMSErrorResponse,
  23. NozzleInfoResponse,
  24. NozzleRackSlot,
  25. PrinterCreate,
  26. PrinterDiagnosticResult,
  27. PrinterResponse,
  28. PrinterStatus,
  29. PrinterUpdate,
  30. PrintOptionsResponse,
  31. )
  32. from backend.app.services.bambu_ftp import (
  33. cache_3mf_download,
  34. delete_file_async,
  35. download_file_bytes_async,
  36. download_file_try_paths_async,
  37. get_cached_3mf,
  38. get_storage_info_async,
  39. list_files_async,
  40. )
  41. from backend.app.services.printer_diagnostic import run_connection_diagnostic
  42. from backend.app.services.printer_manager import (
  43. get_derived_status_name,
  44. printer_manager,
  45. resolve_plate_id,
  46. supports_chamber_temp,
  47. supports_drying,
  48. )
  49. from backend.app.utils.http import build_content_disposition
  50. logger = logging.getLogger(__name__)
  51. router = APIRouter(prefix="/printers", tags=["printers"])
  52. @router.get("/", response_model=list[PrinterResponse])
  53. async def list_printers(
  54. _=RequirePermissionIfAuthEnabled(Permission.PRINTERS_READ),
  55. db: AsyncSession = Depends(get_db),
  56. ):
  57. """List all configured printers."""
  58. result = await db.execute(select(Printer).order_by(Printer.name))
  59. return list(result.scalars().all())
  60. @router.post("/", response_model=PrinterResponse)
  61. async def create_printer(
  62. printer_data: PrinterCreate,
  63. _=RequirePermissionIfAuthEnabled(Permission.PRINTERS_CREATE),
  64. db: AsyncSession = Depends(get_db),
  65. ):
  66. """Add a new printer.
  67. Verifies the MQTT connection succeeds before persisting. A wrong access
  68. code or unreachable IP would otherwise create a printer row that shows
  69. as an empty / never-connecting card on the dashboard — those reports
  70. were turning into support tickets that all traced back to a mistyped
  71. access code.
  72. """
  73. # Check if serial number already exists
  74. result = await db.execute(select(Printer).where(Printer.serial_number == printer_data.serial_number))
  75. if result.scalar_one_or_none():
  76. raise HTTPException(400, "Printer with this serial number already exists")
  77. test_result = await printer_manager.test_connection(
  78. ip_address=printer_data.ip_address,
  79. serial_number=printer_data.serial_number,
  80. access_code=printer_data.access_code,
  81. )
  82. if not test_result.get("success"):
  83. # The frontend renders the user-facing message via i18n on `code`;
  84. # `message` is an English fallback for non-UI clients (curl / scripts).
  85. raise HTTPException(
  86. status_code=400,
  87. detail={
  88. "code": "printer_connection_failed",
  89. "message": (
  90. "Could not connect to the printer. Verify IP address, serial number, "
  91. "and access code, and confirm LAN-only mode is enabled. "
  92. "The printer was not added."
  93. ),
  94. },
  95. )
  96. printer = Printer(**printer_data.model_dump())
  97. db.add(printer)
  98. await db.commit()
  99. await db.refresh(printer)
  100. # Connect to the printer
  101. if printer.is_active:
  102. await printer_manager.connect_printer(printer)
  103. return printer
  104. @router.get("/usb-cameras")
  105. async def list_usb_cameras(
  106. _=RequirePermissionIfAuthEnabled(Permission.PRINTERS_READ),
  107. ):
  108. """List available USB cameras connected to the system.
  109. Returns a list of detected V4L2 video devices with their info.
  110. Only works on Linux systems with V4L2 support.
  111. Returns:
  112. List of dicts with {device: str, name: str, capabilities: list, formats?: list}
  113. """
  114. from backend.app.services.external_camera import list_usb_cameras
  115. cameras = list_usb_cameras()
  116. return {"cameras": cameras}
  117. @router.get("/available-filaments")
  118. async def get_available_filaments(
  119. model: str = Query(..., description="Target printer model"),
  120. location: str | None = Query(None, description="Optional location filter"),
  121. _=RequirePermissionIfAuthEnabled(Permission.QUEUE_CREATE),
  122. db: AsyncSession = Depends(get_db),
  123. ):
  124. """Get deduplicated list of filaments loaded across all active printers of a given model.
  125. Used by the frontend to offer filament override options for model-based queue assignment.
  126. """
  127. from backend.app.utils.printer_models import normalize_printer_model, normalize_printer_model_id
  128. # Normalize model name
  129. normalized_model = normalize_printer_model(model) or normalize_printer_model_id(model) or model
  130. query = (
  131. select(Printer).where(func.lower(Printer.model) == normalized_model.lower()).where(Printer.is_active == True) # noqa: E712
  132. )
  133. if location:
  134. query = query.where(Printer.location == location)
  135. result = await db.execute(query)
  136. printers_list = list(result.scalars().all())
  137. if not printers_list:
  138. return []
  139. # Collect filaments from all matching printers
  140. # Dedup key includes extruder_id and tray_sub_brands so "PLA Basic" and "PLA Matte" appear separately
  141. seen: set[tuple[str, str, str, int | None]] = set() # (type_upper, color_normalized, sub_brands_upper, extruder_id)
  142. filaments = []
  143. for printer in printers_list:
  144. status = printer_manager.get_status(printer.id)
  145. if not status:
  146. continue
  147. # Get ams_extruder_map for dual-nozzle printers
  148. ams_extruder_map = status.raw_data.get("ams_extruder_map", {})
  149. # AMS trays
  150. for ams_unit in status.raw_data.get("ams", []):
  151. ams_id = str(ams_unit.get("id", 0))
  152. extruder_id = ams_extruder_map.get(ams_id)
  153. for tray in ams_unit.get("tray", []):
  154. tray_type = tray.get("tray_type")
  155. if not tray_type:
  156. continue
  157. tray_color = tray.get("tray_color", "")
  158. # Normalize color: remove alpha, add hash
  159. hex_color = tray_color.replace("#", "")[:6] if tray_color else "808080"
  160. color = f"#{hex_color}"
  161. tray_info_idx = tray.get("tray_info_idx", "")
  162. tray_sub_brands = tray.get("tray_sub_brands", "") or ""
  163. key = (tray_type.upper(), hex_color.lower(), tray_sub_brands.upper(), extruder_id)
  164. if key not in seen:
  165. seen.add(key)
  166. filaments.append(
  167. {
  168. "type": tray_type,
  169. "color": color,
  170. "tray_info_idx": tray_info_idx,
  171. "tray_sub_brands": tray_sub_brands,
  172. "extruder_id": extruder_id,
  173. }
  174. )
  175. # External spools (vt_tray)
  176. for vt in status.raw_data.get("vt_tray") or []:
  177. vt_type = vt.get("tray_type")
  178. if not vt_type:
  179. continue
  180. vt_color = vt.get("tray_color", "")
  181. hex_color = vt_color.replace("#", "")[:6] if vt_color else "808080"
  182. color = f"#{hex_color}"
  183. tray_info_idx = vt.get("tray_info_idx", "")
  184. tray_sub_brands = vt.get("tray_sub_brands", "") or ""
  185. vt_id = int(vt.get("id", 254))
  186. extruder_id = (255 - vt_id) if ams_extruder_map else None
  187. key = (vt_type.upper(), hex_color.lower(), tray_sub_brands.upper(), extruder_id)
  188. if key not in seen:
  189. seen.add(key)
  190. filaments.append(
  191. {
  192. "type": vt_type,
  193. "color": color,
  194. "tray_info_idx": tray_info_idx,
  195. "tray_sub_brands": tray_sub_brands,
  196. "extruder_id": extruder_id,
  197. }
  198. )
  199. return filaments
  200. @router.get("/developer-mode-warnings")
  201. async def get_developer_mode_warnings(
  202. _=RequirePermissionIfAuthEnabled(Permission.PRINTERS_READ),
  203. db: AsyncSession = Depends(get_db),
  204. ):
  205. """Check if any connected printer lacks developer LAN mode."""
  206. result = await db.execute(select(Printer).where(Printer.is_active == True)) # noqa: E712
  207. printers = result.scalars().all()
  208. statuses = printer_manager.get_all_statuses()
  209. warnings = []
  210. for printer in printers:
  211. state = statuses.get(printer.id)
  212. if state and state.connected and state.developer_mode is False:
  213. warnings.append(
  214. {
  215. "printer_id": printer.id,
  216. "name": printer.name,
  217. }
  218. )
  219. return warnings
  220. @router.get("/{printer_id}", response_model=PrinterResponse)
  221. async def get_printer(
  222. printer_id: int,
  223. _=RequirePermissionIfAuthEnabled(Permission.PRINTERS_READ),
  224. db: AsyncSession = Depends(get_db),
  225. ):
  226. """Get a specific printer."""
  227. result = await db.execute(select(Printer).where(Printer.id == printer_id))
  228. printer = result.scalar_one_or_none()
  229. if not printer:
  230. raise HTTPException(404, "Printer not found")
  231. return printer
  232. @router.patch("/{printer_id}", response_model=PrinterResponse)
  233. async def update_printer(
  234. printer_id: int,
  235. printer_data: PrinterUpdate,
  236. _=RequirePermissionIfAuthEnabled(Permission.PRINTERS_UPDATE),
  237. db: AsyncSession = Depends(get_db),
  238. ):
  239. """Update a printer."""
  240. result = await db.execute(select(Printer).where(Printer.id == printer_id))
  241. printer = result.scalar_one_or_none()
  242. if not printer:
  243. raise HTTPException(404, "Printer not found")
  244. update_data = printer_data.model_dump(exclude_unset=True)
  245. # Handle nested ROI object - flatten to individual columns
  246. if "plate_detection_roi" in update_data:
  247. roi = update_data.pop("plate_detection_roi")
  248. if roi:
  249. update_data["plate_detection_roi_x"] = roi.get("x")
  250. update_data["plate_detection_roi_y"] = roi.get("y")
  251. update_data["plate_detection_roi_w"] = roi.get("w")
  252. update_data["plate_detection_roi_h"] = roi.get("h")
  253. else:
  254. # Clear ROI if set to null
  255. update_data["plate_detection_roi_x"] = None
  256. update_data["plate_detection_roi_y"] = None
  257. update_data["plate_detection_roi_w"] = None
  258. update_data["plate_detection_roi_h"] = None
  259. for field, value in update_data.items():
  260. setattr(printer, field, value)
  261. await db.commit()
  262. await db.refresh(printer)
  263. # Reconnect if connection settings changed
  264. if any(k in update_data for k in ["ip_address", "access_code", "is_active"]):
  265. printer_manager.disconnect_printer(printer_id)
  266. if printer.is_active:
  267. await printer_manager.connect_printer(printer)
  268. return printer
  269. @router.delete("/{printer_id}")
  270. async def delete_printer(
  271. printer_id: int,
  272. delete_archives: bool = True,
  273. _=RequirePermissionIfAuthEnabled(Permission.PRINTERS_DELETE),
  274. db: AsyncSession = Depends(get_db),
  275. ):
  276. """Delete a printer.
  277. Args:
  278. printer_id: ID of the printer to delete
  279. delete_archives: If True (default), delete all print archives for this printer.
  280. If False, keep archives but remove their printer association.
  281. """
  282. from sqlalchemy import delete as sql_delete
  283. from backend.app.models.archive import PrintArchive
  284. from backend.app.models.maintenance import MaintenanceHistory, PrinterMaintenance
  285. from backend.app.models.spoolman_slot_assignment import SpoolmanSlotAssignment
  286. result = await db.execute(select(Printer).where(Printer.id == printer_id))
  287. printer = result.scalar_one_or_none()
  288. if not printer:
  289. raise HTTPException(404, "Printer not found")
  290. printer_manager.disconnect_printer(printer_id)
  291. if delete_archives:
  292. # Delete all archives for this printer
  293. await db.execute(sql_delete(PrintArchive).where(PrintArchive.printer_id == printer_id))
  294. else:
  295. # Orphan the archives instead of deleting them
  296. from sqlalchemy import update
  297. await db.execute(update(PrintArchive).where(PrintArchive.printer_id == printer_id).values(printer_id=None))
  298. # Delete slot assignments for this printer (SQLite doesn't enforce FK cascades)
  299. await db.execute(sql_delete(SpoolmanSlotAssignment).where(SpoolmanSlotAssignment.printer_id == printer_id))
  300. # Delete maintenance history and items for this printer
  301. # (SQLite doesn't enforce FK cascades, so do it explicitly)
  302. maintenance_ids = (
  303. (await db.execute(select(PrinterMaintenance.id).where(PrinterMaintenance.printer_id == printer_id)))
  304. .scalars()
  305. .all()
  306. )
  307. if maintenance_ids:
  308. await db.execute(
  309. sql_delete(MaintenanceHistory).where(MaintenanceHistory.printer_maintenance_id.in_(maintenance_ids))
  310. )
  311. await db.execute(sql_delete(PrinterMaintenance).where(PrinterMaintenance.printer_id == printer_id))
  312. await db.delete(printer)
  313. await db.commit()
  314. return {"status": "deleted", "archives_deleted": delete_archives}
  315. @router.get("/{printer_id}/status", response_model=PrinterStatus)
  316. async def get_printer_status(
  317. printer_id: int,
  318. _=RequirePermissionIfAuthEnabled(Permission.PRINTERS_READ),
  319. db: AsyncSession = Depends(get_db),
  320. ):
  321. """Get real-time status of a printer."""
  322. result = await db.execute(select(Printer).where(Printer.id == printer_id))
  323. printer = result.scalar_one_or_none()
  324. if not printer:
  325. raise HTTPException(404, "Printer not found")
  326. state = printer_manager.get_status(printer_id)
  327. if not state:
  328. return PrinterStatus(
  329. id=printer_id,
  330. name=printer.name,
  331. connected=False,
  332. )
  333. # Determine cover URL if there's an active print (including paused)
  334. cover_url = None
  335. if state.state in ("RUNNING", "PAUSE") and state.gcode_file:
  336. cover_url = f"/api/v1/printers/{printer_id}/cover"
  337. # Convert HMS errors to response format
  338. hms_errors = [
  339. HMSErrorResponse(code=e.code, attr=e.attr, module=e.module, severity=e.severity)
  340. for e in (state.hms_errors or [])
  341. ]
  342. # Parse AMS data from raw_data
  343. ams_units = []
  344. vt_tray = []
  345. ams_exists = False
  346. raw_data = state.raw_data or {}
  347. # Build K-profile lookup map: cali_idx -> k_value
  348. # This allows looking up the calibrated K value for each AMS slot
  349. kprofile_map: dict[int, float] = {}
  350. for kp in state.kprofiles or []:
  351. if kp.slot_id is not None and kp.k_value:
  352. try:
  353. kprofile_map[kp.slot_id] = float(kp.k_value)
  354. except (ValueError, TypeError):
  355. pass # Skip K-profile entries with unparseable values
  356. if "ams" in raw_data and isinstance(raw_data["ams"], list):
  357. ams_exists = True
  358. for ams_data in raw_data["ams"]:
  359. # Skip if ams_data is not a dict (defensive check)
  360. if not isinstance(ams_data, dict):
  361. continue
  362. trays = []
  363. for tray_data in ams_data.get("tray", []):
  364. # Filter out empty/invalid tag values
  365. tag_uid = tray_data.get("tag_uid", "")
  366. if tag_uid in ("", "0000000000000000"):
  367. tag_uid = None
  368. tray_uuid = tray_data.get("tray_uuid", "")
  369. if tray_uuid in ("", "00000000000000000000000000000000"):
  370. tray_uuid = None
  371. # Get K value: first try tray's k field, then lookup from K-profiles
  372. k_value = tray_data.get("k")
  373. cali_idx = tray_data.get("cali_idx")
  374. if k_value is None and cali_idx is not None and cali_idx in kprofile_map:
  375. k_value = kprofile_map[cali_idx]
  376. trays.append(
  377. AMSTray(
  378. id=tray_data.get("id", 0),
  379. tray_color=tray_data.get("tray_color"),
  380. tray_type=tray_data.get("tray_type"),
  381. tray_sub_brands=tray_data.get("tray_sub_brands"),
  382. tray_id_name=tray_data.get("tray_id_name"),
  383. tray_info_idx=tray_data.get("tray_info_idx"),
  384. remain=tray_data.get("remain", 0),
  385. k=k_value,
  386. cali_idx=cali_idx,
  387. tag_uid=tag_uid,
  388. tray_uuid=tray_uuid,
  389. nozzle_temp_min=tray_data.get("nozzle_temp_min"),
  390. nozzle_temp_max=tray_data.get("nozzle_temp_max"),
  391. drying_temp=tray_data.get("drying_temp"),
  392. drying_time=tray_data.get("drying_time"),
  393. state=tray_data.get("state"),
  394. )
  395. )
  396. # Prefer humidity_raw (percentage) over humidity (index 1-5)
  397. # humidity_raw is the actual percentage value from the sensor
  398. humidity_raw = ams_data.get("humidity_raw")
  399. humidity_idx = ams_data.get("humidity")
  400. humidity_value = None
  401. if humidity_raw is not None:
  402. try:
  403. humidity_value = int(humidity_raw)
  404. except (ValueError, TypeError):
  405. pass # Skip unparseable humidity; will try index fallback
  406. if humidity_value is None and humidity_idx is not None:
  407. try:
  408. humidity_value = int(humidity_idx)
  409. except (ValueError, TypeError):
  410. pass # Skip unparseable humidity index; humidity remains None
  411. # AMS-HT has 1 tray, regular AMS has 4 trays
  412. is_ams_ht = len(trays) == 1
  413. ams_units.append(
  414. AMSUnit(
  415. id=ams_data.get("id", 0),
  416. humidity=humidity_value,
  417. temp=ams_data.get("temp"),
  418. is_ams_ht=is_ams_ht,
  419. tray=trays,
  420. # Serial number: Bambu MQTT uses "sn" key on AMS unit objects
  421. serial_number=str(ams_data.get("sn") or ams_data.get("serial_number") or ""),
  422. # Firmware version: populated by _handle_version_info from info.module ams/* entries
  423. sw_ver=str(ams_data.get("sw_ver") or ""),
  424. # Drying: dry_time > 0 means drying is active (minutes remaining)
  425. dry_time=int(ams_data.get("dry_time") or 0),
  426. module_type=str(ams_data.get("module_type") or ""),
  427. )
  428. )
  429. # Virtual tray (external spool holder) - comes from vt_tray in raw_data (list)
  430. if "vt_tray" in raw_data:
  431. for vt_data in raw_data["vt_tray"]:
  432. # Filter out empty/invalid tag values for vt_tray
  433. vt_tag_uid = vt_data.get("tag_uid", "")
  434. if vt_tag_uid in ("", "0000000000000000"):
  435. vt_tag_uid = None
  436. vt_tray_uuid = vt_data.get("tray_uuid", "")
  437. if vt_tray_uuid in ("", "00000000000000000000000000000000"):
  438. vt_tray_uuid = None
  439. # Get K value: first try tray's k field, then lookup from K-profiles
  440. vt_k_value = vt_data.get("k")
  441. vt_cali_idx = vt_data.get("cali_idx")
  442. if vt_k_value is None and vt_cali_idx is not None and vt_cali_idx in kprofile_map:
  443. vt_k_value = kprofile_map[vt_cali_idx]
  444. tray_id = int(vt_data.get("id", 254))
  445. vt_tray.append(
  446. AMSTray(
  447. id=tray_id,
  448. tray_color=vt_data.get("tray_color"),
  449. tray_type=vt_data.get("tray_type"),
  450. tray_sub_brands=vt_data.get("tray_sub_brands"),
  451. tray_id_name=vt_data.get("tray_id_name"),
  452. tray_info_idx=vt_data.get("tray_info_idx"),
  453. remain=vt_data.get("remain", 0),
  454. k=vt_k_value,
  455. cali_idx=vt_cali_idx,
  456. tag_uid=vt_tag_uid,
  457. tray_uuid=vt_tray_uuid,
  458. nozzle_temp_min=vt_data.get("nozzle_temp_min"),
  459. nozzle_temp_max=vt_data.get("nozzle_temp_max"),
  460. )
  461. )
  462. # Convert nozzle info to response format
  463. nozzles = [
  464. NozzleInfoResponse(
  465. nozzle_type=n.nozzle_type,
  466. nozzle_diameter=n.nozzle_diameter,
  467. )
  468. for n in (state.nozzles or [])
  469. ]
  470. # H2C nozzle rack (tool-changer dock positions)
  471. nozzle_rack = [
  472. NozzleRackSlot(
  473. id=n.get("id", 0),
  474. nozzle_type=n.get("type", ""),
  475. nozzle_diameter=n.get("diameter", ""),
  476. wear=n.get("wear"),
  477. stat=n.get("stat"),
  478. max_temp=n.get("max_temp", 0),
  479. serial_number=n.get("serial_number", ""),
  480. filament_color=n.get("filament_color", ""),
  481. filament_id=n.get("filament_id", ""),
  482. filament_type=n.get("filament_type", ""),
  483. )
  484. for n in (state.nozzle_rack or [])
  485. ]
  486. # Convert print options to response format
  487. print_options = PrintOptionsResponse(
  488. spaghetti_detector=state.print_options.spaghetti_detector,
  489. print_halt=state.print_options.print_halt,
  490. halt_print_sensitivity=state.print_options.halt_print_sensitivity,
  491. first_layer_inspector=state.print_options.first_layer_inspector,
  492. printing_monitor=state.print_options.printing_monitor,
  493. buildplate_marker_detector=state.print_options.buildplate_marker_detector,
  494. allow_skip_parts=state.print_options.allow_skip_parts,
  495. nozzle_clumping_detector=state.print_options.nozzle_clumping_detector,
  496. nozzle_clumping_sensitivity=state.print_options.nozzle_clumping_sensitivity,
  497. pileup_detector=state.print_options.pileup_detector,
  498. pileup_sensitivity=state.print_options.pileup_sensitivity,
  499. airprint_detector=state.print_options.airprint_detector,
  500. airprint_sensitivity=state.print_options.airprint_sensitivity,
  501. auto_recovery_step_loss=state.print_options.auto_recovery_step_loss,
  502. filament_tangle_detect=state.print_options.filament_tangle_detect,
  503. )
  504. # Get AMS mapping from raw_data (which AMS is connected to which nozzle)
  505. ams_mapping = raw_data.get("ams_mapping", [])
  506. # Get per-AMS extruder map from state attribute (not raw_data, to avoid race condition
  507. # where raw_data gets replaced during MQTT updates and ams_extruder_map is temporarily missing)
  508. ams_extruder_map = state.ams_extruder_map or {}
  509. logger.debug("API returning ams_mapping: %s, ams_extruder_map: %s", ams_mapping, ams_extruder_map)
  510. # tray_now from MQTT is already a global tray ID: (ams_id * 4) + slot_id
  511. # Per OpenBambuAPI docs: 254 = external spool, 255 = no filament, otherwise global tray ID
  512. # No conversion needed - just use the raw value directly
  513. tray_now = state.tray_now
  514. logger.debug("Using tray_now directly as global ID: %s", tray_now)
  515. # Filter out chamber temp for models that don't have a real sensor
  516. # P1P, P1S, A1, A1Mini report meaningless chamber_temper values
  517. temperatures = state.temperatures
  518. if not supports_chamber_temp(printer.model):
  519. temperatures = {
  520. k: v for k, v in temperatures.items() if k not in ("chamber", "chamber_target", "chamber_heating")
  521. }
  522. # Resolve the active print's archive + plate (#881 follow-up): lets the
  523. # printer card show the actual plate name for multi-plate 3MFs instead of
  524. # just the 3MF filename. Only attempted for active prints, since subtask_id
  525. # is only meaningful then.
  526. current_archive_id: int | None = None
  527. current_plate_id: int | None = None
  528. if state.state in ("RUNNING", "PAUSE"):
  529. current_plate_id = resolve_plate_id(state)
  530. if state.subtask_id:
  531. from backend.app.models.archive import PrintArchive
  532. archive_row = await db.execute(
  533. select(PrintArchive.id)
  534. .where(PrintArchive.subtask_id == state.subtask_id)
  535. .where(PrintArchive.printer_id == printer_id)
  536. .order_by(PrintArchive.created_at.desc())
  537. .limit(1)
  538. )
  539. current_archive_id = archive_row.scalar_one_or_none()
  540. return PrinterStatus(
  541. id=printer_id,
  542. name=printer.name,
  543. connected=state.connected,
  544. state=state.state,
  545. current_print=state.current_print,
  546. subtask_name=state.subtask_name,
  547. gcode_file=state.gcode_file,
  548. progress=state.progress,
  549. remaining_time=state.remaining_time,
  550. layer_num=state.layer_num,
  551. total_layers=state.total_layers,
  552. temperatures=temperatures,
  553. cover_url=cover_url,
  554. hms_errors=hms_errors,
  555. ams=ams_units,
  556. ams_exists=ams_exists,
  557. vt_tray=vt_tray,
  558. sdcard=state.sdcard,
  559. store_to_sdcard=state.store_to_sdcard,
  560. timelapse=state.timelapse,
  561. ipcam=state.ipcam,
  562. wifi_signal=state.wifi_signal,
  563. wired_network=state.wired_network,
  564. door_open=state.door_open,
  565. nozzles=nozzles,
  566. nozzle_rack=nozzle_rack,
  567. print_options=print_options,
  568. stg_cur=state.stg_cur,
  569. stg_cur_name=get_derived_status_name(state, printer.model),
  570. stg=state.stg,
  571. airduct_mode=state.airduct_mode,
  572. speed_level=state.speed_level,
  573. chamber_light=state.chamber_light,
  574. active_extruder=state.active_extruder,
  575. ams_mapping=ams_mapping,
  576. ams_extruder_map=ams_extruder_map,
  577. tray_now=tray_now,
  578. ams_status_main=state.ams_status_main,
  579. ams_status_sub=state.ams_status_sub,
  580. mc_print_sub_stage=state.mc_print_sub_stage,
  581. last_ams_update=state.last_ams_update,
  582. printable_objects_count=len(state.printable_objects),
  583. cooling_fan_speed=state.cooling_fan_speed,
  584. big_fan1_speed=state.big_fan1_speed,
  585. big_fan2_speed=state.big_fan2_speed,
  586. heatbreak_fan_speed=state.heatbreak_fan_speed,
  587. firmware_version=state.firmware_version,
  588. developer_mode=state.developer_mode if state else None,
  589. awaiting_plate_clear=printer_manager.is_awaiting_plate_clear(printer_id),
  590. supports_drying=supports_drying(printer.model, state.firmware_version),
  591. current_archive_id=current_archive_id,
  592. current_plate_id=current_plate_id,
  593. fila_switch=(
  594. FilaSwitchResponse(
  595. installed=state.fila_switch.installed,
  596. in_slots=list(state.fila_switch.in_slots),
  597. out_extruders=list(state.fila_switch.out_extruders),
  598. stat=state.fila_switch.stat,
  599. info=state.fila_switch.info,
  600. )
  601. if state.fila_switch and state.fila_switch.installed
  602. else None
  603. ),
  604. )
  605. @router.get("/{printer_id}/current-print-user")
  606. async def get_current_print_user(
  607. printer_id: int,
  608. _=RequirePermissionIfAuthEnabled(Permission.PRINTERS_READ),
  609. db: AsyncSession = Depends(get_db),
  610. ):
  611. """Get the user who started the current print (for reprint tracking).
  612. Returns user info if available, empty object otherwise.
  613. This tracks users for reprints (which bypass the queue).
  614. For queue-based prints, use the queue item's created_by field instead.
  615. """
  616. result = await db.execute(select(Printer).where(Printer.id == printer_id))
  617. printer = result.scalar_one_or_none()
  618. if not printer:
  619. raise HTTPException(404, "Printer not found")
  620. user_info = printer_manager.get_current_print_user(printer_id)
  621. return user_info or {}
  622. @router.post("/{printer_id}/refresh-status")
  623. async def refresh_printer_status(
  624. printer_id: int,
  625. _=RequirePermissionIfAuthEnabled(Permission.PRINTERS_READ),
  626. db: AsyncSession = Depends(get_db),
  627. ):
  628. """Request a full status refresh from the printer (sends pushall command)."""
  629. result = await db.execute(select(Printer).where(Printer.id == printer_id))
  630. printer = result.scalar_one_or_none()
  631. if not printer:
  632. raise HTTPException(404, "Printer not found")
  633. success = printer_manager.request_status_update(printer_id)
  634. if not success:
  635. raise HTTPException(400, "Printer not connected")
  636. return {"status": "refresh_requested"}
  637. @router.post("/{printer_id}/connect")
  638. async def connect_printer(
  639. printer_id: int,
  640. _=RequirePermissionIfAuthEnabled(Permission.PRINTERS_CONTROL),
  641. db: AsyncSession = Depends(get_db),
  642. ):
  643. """Manually connect to a printer."""
  644. result = await db.execute(select(Printer).where(Printer.id == printer_id))
  645. printer = result.scalar_one_or_none()
  646. if not printer:
  647. raise HTTPException(404, "Printer not found")
  648. success = await printer_manager.connect_printer(printer)
  649. return {"connected": success}
  650. @router.post("/{printer_id}/disconnect")
  651. async def disconnect_printer(
  652. printer_id: int,
  653. _=RequirePermissionIfAuthEnabled(Permission.PRINTERS_CONTROL),
  654. db: AsyncSession = Depends(get_db),
  655. ):
  656. """Manually disconnect from a printer."""
  657. result = await db.execute(select(Printer).where(Printer.id == printer_id))
  658. printer = result.scalar_one_or_none()
  659. if not printer:
  660. raise HTTPException(404, "Printer not found")
  661. printer_manager.disconnect_printer(printer_id)
  662. return {"connected": False}
  663. @router.post("/test")
  664. async def test_printer_connection(
  665. ip_address: str,
  666. serial_number: str,
  667. access_code: str,
  668. _=RequirePermissionIfAuthEnabled(Permission.PRINTERS_CREATE),
  669. ):
  670. """Test connection to a printer without saving."""
  671. result = await printer_manager.test_connection(
  672. ip_address=ip_address,
  673. serial_number=serial_number,
  674. access_code=access_code,
  675. )
  676. return result
  677. @router.post("/diagnostic", response_model=PrinterDiagnosticResult)
  678. async def diagnose_connection(
  679. req: DiagnosticRequest,
  680. _=RequirePermissionIfAuthEnabled(Permission.PRINTERS_CREATE),
  681. ):
  682. """Run connection diagnostics for the Add-Printer flow (printer not yet saved).
  683. When serial_number + access_code are supplied the MQTT credential check
  684. also runs; otherwise only the network-level checks are performed.
  685. """
  686. return await run_connection_diagnostic(
  687. req.ip_address,
  688. serial_number=req.serial_number or None,
  689. access_code=req.access_code or None,
  690. )
  691. @router.get("/{printer_id}/diagnostic", response_model=PrinterDiagnosticResult)
  692. async def diagnose_printer(
  693. printer_id: int,
  694. _=RequirePermissionIfAuthEnabled(Permission.PRINTERS_READ),
  695. db: AsyncSession = Depends(get_db),
  696. ):
  697. """Run connection diagnostics for an existing saved printer."""
  698. result = await db.execute(select(Printer).where(Printer.id == printer_id))
  699. printer = result.scalar_one_or_none()
  700. if not printer:
  701. raise HTTPException(404, "Printer not found")
  702. return await run_connection_diagnostic(printer.ip_address, printer=printer)
  703. # Cache for cover images (printer_id -> {(subtask_name, view_key) -> image_bytes}).
  704. # Cleared on every print start by main.py::on_print_start, so re-dispatches with
  705. # different plates always fetch a fresh thumbnail without needing plate in the key.
  706. _cover_cache: dict[int, dict[tuple[str, str], bytes]] = {}
  707. # Negative cache (#1420): when a cover lookup exhausts every FTP path with 550
  708. # (file sliced on SD card, not on printer storage), remember the failure so the
  709. # next request short-circuits to 404 instead of re-hammering FTP 8 paths deep.
  710. # Cleared on print start alongside _cover_cache.
  711. _cover_404_cache: dict[int, set[tuple[str, str]]] = {}
  712. def clear_cover_cache(printer_id: int) -> None:
  713. """Clear cached cover images for a printer. Call on print start to avoid stale thumbnails."""
  714. _cover_cache.pop(printer_id, None)
  715. _cover_404_cache.pop(printer_id, None)
  716. @router.get("/{printer_id}/cover")
  717. async def get_printer_cover(
  718. printer_id: int,
  719. view: str | None = None,
  720. db: AsyncSession = Depends(get_db),
  721. _: None = RequireCameraStreamTokenIfAuthEnabled,
  722. ):
  723. """Get the cover image for the current print job.
  724. Args:
  725. view: Optional view type. Use "top" for top-down build plate view (useful for skip objects).
  726. Default returns angled 3D perspective view.
  727. """
  728. result = await db.execute(select(Printer).where(Printer.id == printer_id))
  729. printer = result.scalar_one_or_none()
  730. if not printer:
  731. raise HTTPException(404, "Printer not found")
  732. state = printer_manager.get_status(printer_id)
  733. if not state:
  734. raise HTTPException(404, "Printer not connected")
  735. # Use subtask_name as the 3MF filename (gcode_file is the path inside the 3MF)
  736. subtask_name = state.subtask_name
  737. if not subtask_name:
  738. raise HTTPException(404, f"No subtask_name in printer state (state={state.state})")
  739. # Resolve the active plate. Precedence (#1166):
  740. # 1. The plate Bambuddy dispatched (authoritative when we sent the print)
  741. # 2. plate_(\d+)\.gcode regex on state.gcode_file (works on firmware that
  742. # reflects the full path, e.g. some X1C builds)
  743. # 3. Scan the downloaded 3MF for a unique Metadata/plate_*.gcode (covers
  744. # per-plate archives sliced separately in Bambu Studio, where the
  745. # printer's gcode_file echo is just the .3mf filename)
  746. # 4. Fall back to plate 1
  747. # The 3MF-scan fallback runs later — after the file is on disk.
  748. plate_num = resolve_plate_id(state)
  749. if plate_num is not None:
  750. logger.info("Cover: resolved plate %s before download (subtask=%s)", plate_num, subtask_name)
  751. # Normalize view parameter
  752. view_key = view or "default"
  753. # Check cache. Cache by (subtask_name, view_key) only — clear_cover_cache()
  754. # runs on every print start, so a re-dispatch with a different plate gets
  755. # a fresh image regardless. Pre-#1166 the key included plate_num, but with
  756. # late plate resolution the cache check would always miss.
  757. cache_key = (subtask_name, view_key)
  758. if printer_id in _cover_cache and cache_key in _cover_cache[printer_id]:
  759. return Response(content=_cover_cache[printer_id][cache_key], media_type="image/png")
  760. # Negative-cache short-circuit (#1420): if a prior lookup for this same
  761. # subtask + view already failed, don't replay 8 FTP retries on every page
  762. # refresh. _cover_404_cache is cleared on print start.
  763. if printer_id in _cover_404_cache and cache_key in _cover_404_cache[printer_id]:
  764. raise HTTPException(404, f"No cover available for '{subtask_name}' (cached)")
  765. # Build possible 3MF filenames from subtask_name
  766. # Bambu printers may store files as "name.gcode.3mf" (sliced via Bambu Studio)
  767. # or just "name.3mf" (uploaded directly)
  768. possible_filenames = []
  769. if subtask_name.endswith(".3mf"):
  770. possible_filenames.append(subtask_name)
  771. else:
  772. # Try both naming patterns
  773. possible_filenames.append(f"{subtask_name}.gcode.3mf")
  774. possible_filenames.append(f"{subtask_name}.3mf")
  775. # Also try with spaces converted to underscores (Bambu Studio may normalize filenames)
  776. if " " in subtask_name:
  777. normalized = subtask_name.replace(" ", "_")
  778. if normalized.endswith(".3mf"):
  779. possible_filenames.append(normalized)
  780. else:
  781. possible_filenames.append(f"{normalized}.gcode.3mf")
  782. possible_filenames.append(f"{normalized}.3mf")
  783. # Build list of all remote paths to try
  784. remote_paths = []
  785. for filename in possible_filenames:
  786. remote_paths.extend(
  787. [
  788. f"/{filename}", # Root directory (most common)
  789. f"/cache/{filename}",
  790. f"/model/{filename}",
  791. f"/data/{filename}",
  792. ]
  793. )
  794. # Use first filename for temp path (will be reused)
  795. temp_filename = possible_filenames[0]
  796. temp_path = settings.archive_dir / "temp" / f"cover_{printer_id}_{temp_filename}"
  797. temp_path.parent.mkdir(parents=True, exist_ok=True)
  798. # Cache check (#972): the archive-metadata flow in main.py may have already
  799. # downloaded this 3MF during the print-start handler. Reusing that file
  800. # avoids a second 36MB transfer competing with the printer's single FTP
  801. # socket (which produces the 425 errors that feed the retry storm).
  802. downloaded = False
  803. using_cached = False
  804. for candidate_name in possible_filenames:
  805. cached = get_cached_3mf(printer_id, candidate_name)
  806. if cached:
  807. logger.info("Cover using cached 3MF from %s (avoided duplicate FTP)", cached)
  808. temp_path = cached
  809. downloaded = True
  810. using_cached = True
  811. break
  812. if not downloaded:
  813. logger.info(
  814. f"Trying to download cover for '{subtask_name}' from {printer.ip_address} (trying {len(remote_paths)} paths)"
  815. )
  816. # Retry logic for transient FTP failures
  817. max_retries = 2
  818. last_error = None
  819. for attempt in range(max_retries + 1):
  820. try:
  821. downloaded = await download_file_try_paths_async(
  822. printer.ip_address,
  823. printer.access_code,
  824. remote_paths,
  825. temp_path,
  826. printer_model=printer.model,
  827. )
  828. if downloaded:
  829. break
  830. except Exception as e:
  831. last_error = e
  832. if attempt < max_retries:
  833. logger.warning("FTP download attempt %s failed: %s, retrying...", attempt + 1, e)
  834. await asyncio.sleep(0.5 * (attempt + 1)) # Brief backoff
  835. else:
  836. logger.error("FTP download failed after %s attempts: %s", max_retries + 1, e)
  837. if last_error and not downloaded:
  838. raise HTTPException(503, f"FTP download temporarily unavailable: {last_error}")
  839. if not downloaded:
  840. # Remember this failure so subsequent requests for the same print
  841. # skip the 8-path FTP fan-out (#1420).
  842. _cover_404_cache.setdefault(printer_id, set()).add(cache_key)
  843. raise HTTPException(
  844. 404,
  845. f"Could not download 3MF file for '{subtask_name}' from printer {printer.ip_address}. Tried: {possible_filenames}",
  846. )
  847. # Share the fresh download with the archive flow.
  848. cache_3mf_download(printer_id, temp_filename, temp_path)
  849. # Verify file actually exists and has content
  850. if not temp_path.exists():
  851. raise HTTPException(500, f"Download reported success but file not found: {temp_path}")
  852. file_size = temp_path.stat().st_size
  853. logger.info("Downloaded file size: %s bytes", file_size)
  854. if file_size == 0:
  855. if not using_cached:
  856. temp_path.unlink()
  857. raise HTTPException(500, f"Downloaded file is empty for '{subtask_name}'")
  858. try:
  859. # Extract thumbnail from 3MF (which is a ZIP file)
  860. try:
  861. zf = zipfile.ZipFile(temp_path, "r")
  862. except zipfile.BadZipFile:
  863. raise HTTPException(500, "Downloaded file is not a valid 3MF/ZIP archive")
  864. except OSError as e:
  865. logger.error("Failed to open 3MF file: %s", e, exc_info=True)
  866. raise HTTPException(500, "Failed to open 3MF file. Check server logs for details.")
  867. try:
  868. # 3MF-scan fallback for plate detection (#1166). Per-plate archives
  869. # sliced separately in Bambu Studio contain a single
  870. # Metadata/plate_N.gcode for the active plate, even though
  871. # thumbnails for all plates are bundled. Using that gcode's plate
  872. # number prevents falling back to plate_1.png.
  873. if plate_num is None:
  874. plate_gcodes = [name for name in zf.namelist() if re.match(r"^Metadata/plate_\d+\.gcode$", name)]
  875. if len(plate_gcodes) == 1:
  876. match = re.search(r"plate_(\d+)\.gcode", plate_gcodes[0])
  877. if match:
  878. plate_num = int(match.group(1))
  879. logger.info("Cover: detected plate %s from 3MF contents", plate_num)
  880. if plate_num is None:
  881. plate_num = 1
  882. # Try common thumbnail paths in 3MF files
  883. # Use plate_num to get the correct plate's thumbnail for multi-plate projects
  884. # Use top-down view if requested (better for skip objects modal)
  885. if view == "top":
  886. thumbnail_paths = [
  887. f"Metadata/top_{plate_num}.png",
  888. # Fall back to plate 1 if specific plate not found
  889. "Metadata/top_1.png",
  890. f"Metadata/plate_{plate_num}.png",
  891. "Metadata/plate_1.png",
  892. "Metadata/thumbnail.png",
  893. ]
  894. else:
  895. thumbnail_paths = [
  896. f"Metadata/plate_{plate_num}.png",
  897. # Fall back to plate 1 if specific plate not found
  898. "Metadata/plate_1.png",
  899. "Metadata/thumbnail.png",
  900. f"Metadata/plate_{plate_num}_small.png",
  901. "Metadata/plate_1_small.png",
  902. "Thumbnails/thumbnail.png",
  903. "thumbnail.png",
  904. ]
  905. for thumb_path in thumbnail_paths:
  906. try:
  907. image_data = zf.read(thumb_path)
  908. if printer_id not in _cover_cache:
  909. _cover_cache[printer_id] = {}
  910. _cover_cache[printer_id][(subtask_name, view_key)] = image_data
  911. return Response(content=image_data, media_type="image/png")
  912. except KeyError:
  913. continue
  914. # If no specific thumbnail found, try any PNG in Metadata
  915. for name in zf.namelist():
  916. if name.startswith("Metadata/") and name.endswith(".png"):
  917. image_data = zf.read(name)
  918. if printer_id not in _cover_cache:
  919. _cover_cache[printer_id] = {}
  920. _cover_cache[printer_id][(subtask_name, view_key)] = image_data
  921. return Response(content=image_data, media_type="image/png")
  922. _cover_404_cache.setdefault(printer_id, set()).add(cache_key)
  923. raise HTTPException(404, "No thumbnail found in 3MF file")
  924. finally:
  925. zf.close()
  926. finally:
  927. # Only delete when this invocation owns the file. A cached path is
  928. # shared with the archive flow — removing it would force a refetch
  929. # the next time either flow needs the 3MF.
  930. if not using_cached and temp_path.exists():
  931. temp_path.unlink()
  932. # ============================================
  933. # File Manager Endpoints
  934. # ============================================
  935. @router.get("/{printer_id}/files")
  936. async def list_printer_files(
  937. printer_id: int,
  938. path: str = "/",
  939. _=RequirePermissionIfAuthEnabled(Permission.PRINTERS_FILES),
  940. db: AsyncSession = Depends(get_db),
  941. ):
  942. """List files on the printer at the specified path."""
  943. result = await db.execute(select(Printer).where(Printer.id == printer_id))
  944. printer = result.scalar_one_or_none()
  945. if not printer:
  946. raise HTTPException(404, "Printer not found")
  947. files = await list_files_async(printer.ip_address, printer.access_code, path, printer_model=printer.model)
  948. # Add full path to each file
  949. for f in files:
  950. f["path"] = f"{path.rstrip('/')}/{f['name']}" if path != "/" else f"/{f['name']}"
  951. return {
  952. "path": path,
  953. "files": files,
  954. }
  955. @router.get("/{printer_id}/files/download")
  956. async def download_printer_file(
  957. printer_id: int,
  958. path: str,
  959. _=RequirePermissionIfAuthEnabled(Permission.PRINTERS_FILES),
  960. db: AsyncSession = Depends(get_db),
  961. ):
  962. """Download a file from the printer."""
  963. result = await db.execute(select(Printer).where(Printer.id == printer_id))
  964. printer = result.scalar_one_or_none()
  965. if not printer:
  966. raise HTTPException(404, "Printer not found")
  967. data = await download_file_bytes_async(printer.ip_address, printer.access_code, path, printer_model=printer.model)
  968. if data is None:
  969. raise HTTPException(404, f"File not found: {path}")
  970. # Determine content type based on extension
  971. filename = path.split("/")[-1]
  972. ext = filename.lower().split(".")[-1] if "." in filename else ""
  973. content_types = {
  974. "3mf": "application/vnd.ms-package.3dmanufacturing-3dmodel+xml",
  975. "gcode": "text/plain",
  976. "mp4": "video/mp4",
  977. "avi": "video/x-msvideo",
  978. "png": "image/png",
  979. "jpg": "image/jpeg",
  980. "jpeg": "image/jpeg",
  981. "json": "application/json",
  982. "txt": "text/plain",
  983. }
  984. content_type = content_types.get(ext, "application/octet-stream")
  985. return Response(
  986. content=data,
  987. media_type=content_type,
  988. headers={"Content-Disposition": build_content_disposition(filename)},
  989. )
  990. @router.get("/{printer_id}/files/gcode")
  991. async def get_printer_file_gcode(
  992. printer_id: int,
  993. path: str,
  994. _=RequirePermissionIfAuthEnabled(Permission.PRINTERS_FILES),
  995. db: AsyncSession = Depends(get_db),
  996. ):
  997. """Get gcode for a file stored on a printer (for preview)."""
  998. import io
  999. # Validate printer
  1000. result = await db.execute(select(Printer).where(Printer.id == printer_id))
  1001. printer = result.scalar_one_or_none()
  1002. if not printer:
  1003. raise HTTPException(404, "Printer not found")
  1004. data = await download_file_bytes_async(printer.ip_address, printer.access_code, path, printer_model=printer.model)
  1005. if data is None:
  1006. raise HTTPException(404, f"File not found: {path}")
  1007. filename = path.split("/")[-1]
  1008. lower = filename.lower()
  1009. if lower.endswith(".gcode"):
  1010. return Response(content=data, media_type="text/plain")
  1011. if lower.endswith(".3mf"):
  1012. try:
  1013. with zipfile.ZipFile(io.BytesIO(data), "r") as zf:
  1014. gcode_files = [n for n in zf.namelist() if n.endswith(".gcode")]
  1015. if not gcode_files:
  1016. raise HTTPException(status_code=404, detail="No gcode found in 3MF file")
  1017. gcode_content = zf.read(gcode_files[0])
  1018. return Response(content=gcode_content, media_type="text/plain")
  1019. except zipfile.BadZipFile:
  1020. raise HTTPException(status_code=400, detail="Invalid 3MF file")
  1021. raise HTTPException(status_code=400, detail="Unsupported file type")
  1022. @router.get("/{printer_id}/files/plates")
  1023. async def get_printer_file_plates(
  1024. printer_id: int,
  1025. path: str = Query(..., description="Full path to the 3MF file on the printer"),
  1026. _=RequirePermissionIfAuthEnabled(Permission.PRINTERS_FILES),
  1027. db: AsyncSession = Depends(get_db),
  1028. ):
  1029. """Get available plates from a multi-plate 3MF file stored on a printer."""
  1030. import io
  1031. import json
  1032. import defusedxml.ElementTree as ET
  1033. # Validate printer
  1034. result = await db.execute(select(Printer).where(Printer.id == printer_id))
  1035. printer = result.scalar_one_or_none()
  1036. if not printer:
  1037. raise HTTPException(404, "Printer not found")
  1038. filename = path.split("/")[-1]
  1039. if not filename.lower().endswith(".3mf"):
  1040. return {
  1041. "printer_id": printer_id,
  1042. "path": path,
  1043. "filename": filename,
  1044. "plates": [],
  1045. "is_multi_plate": False,
  1046. }
  1047. data = await download_file_bytes_async(printer.ip_address, printer.access_code, path, printer_model=printer.model)
  1048. if data is None:
  1049. raise HTTPException(404, f"File not found: {path}")
  1050. plates = []
  1051. try:
  1052. with zipfile.ZipFile(io.BytesIO(data), "r") as zf:
  1053. namelist = zf.namelist()
  1054. # Find all plate gcode files to determine available plates
  1055. gcode_files = [n for n in namelist if n.startswith("Metadata/plate_") and n.endswith(".gcode")]
  1056. # If no gcode is present (source-only or unsliced), fall back to plate JSON/PNG
  1057. plate_indices: list[int] = []
  1058. if gcode_files:
  1059. for gf in gcode_files:
  1060. try:
  1061. plate_str = gf[15:-6] # Remove "Metadata/plate_" and ".gcode"
  1062. plate_indices.append(int(plate_str))
  1063. except ValueError:
  1064. pass # Skip gcode files with non-numeric plate indices
  1065. else:
  1066. plate_json_files = [n for n in namelist if n.startswith("Metadata/plate_") and n.endswith(".json")]
  1067. plate_png_files = [
  1068. n
  1069. for n in namelist
  1070. if n.startswith("Metadata/plate_")
  1071. and n.endswith(".png")
  1072. and "_small" not in n
  1073. and "no_light" not in n
  1074. ]
  1075. plate_name_candidates = plate_json_files + plate_png_files
  1076. plate_re = re.compile(r"^Metadata/plate_(\d+)\.(json|png)$")
  1077. seen_indices: set[int] = set()
  1078. for name in plate_name_candidates:
  1079. match = plate_re.match(name)
  1080. if match:
  1081. try:
  1082. index = int(match.group(1))
  1083. except ValueError:
  1084. continue
  1085. if index in seen_indices:
  1086. continue
  1087. seen_indices.add(index)
  1088. plate_indices.append(index)
  1089. if not plate_indices:
  1090. return {
  1091. "printer_id": printer_id,
  1092. "path": path,
  1093. "filename": filename,
  1094. "plates": [],
  1095. "is_multi_plate": False,
  1096. }
  1097. plate_indices.sort()
  1098. # Parse model_settings.config for plate names
  1099. plate_names = {}
  1100. if "Metadata/model_settings.config" in namelist:
  1101. try:
  1102. model_content = zf.read("Metadata/model_settings.config").decode()
  1103. model_root = ET.fromstring(model_content)
  1104. for plate_elem in model_root.findall(".//plate"):
  1105. plater_id = None
  1106. plater_name = None
  1107. for meta in plate_elem.findall("metadata"):
  1108. key = meta.get("key")
  1109. value = meta.get("value")
  1110. if key == "plater_id" and value:
  1111. try:
  1112. plater_id = int(value)
  1113. except ValueError:
  1114. pass # Skip plate with unparseable ID
  1115. elif key == "plater_name" and value:
  1116. plater_name = value.strip()
  1117. if plater_id is not None and plater_name:
  1118. plate_names[plater_id] = plater_name
  1119. except Exception:
  1120. pass # Plate names are optional; continue without them
  1121. # Parse slice_info.config for plate metadata
  1122. plate_metadata = {}
  1123. if "Metadata/slice_info.config" in namelist:
  1124. content = zf.read("Metadata/slice_info.config").decode()
  1125. root = ET.fromstring(content)
  1126. for plate_elem in root.findall(".//plate"):
  1127. plate_info = {"filaments": [], "prediction": None, "weight": None, "name": None, "objects": []}
  1128. plate_index = None
  1129. for meta in plate_elem.findall("metadata"):
  1130. key = meta.get("key")
  1131. value = meta.get("value")
  1132. if key == "index" and value:
  1133. try:
  1134. plate_index = int(value)
  1135. except ValueError:
  1136. pass # Skip plate with unparseable index
  1137. elif key == "prediction" and value:
  1138. try:
  1139. plate_info["prediction"] = int(value)
  1140. except ValueError:
  1141. pass # Skip unparseable prediction; leave as None
  1142. elif key == "weight" and value:
  1143. try:
  1144. plate_info["weight"] = float(value)
  1145. except ValueError:
  1146. pass # Skip unparseable weight; leave as None
  1147. # Get filaments used in this plate
  1148. for filament_elem in plate_elem.findall("filament"):
  1149. filament_id = filament_elem.get("id")
  1150. filament_type = filament_elem.get("type", "")
  1151. filament_color = filament_elem.get("color", "")
  1152. used_g = filament_elem.get("used_g", "0")
  1153. used_m = filament_elem.get("used_m", "0")
  1154. try:
  1155. used_grams = float(used_g)
  1156. except (ValueError, TypeError):
  1157. used_grams = 0
  1158. if used_grams > 0 and filament_id:
  1159. plate_info["filaments"].append(
  1160. {
  1161. "slot_id": int(filament_id),
  1162. "type": filament_type,
  1163. "color": filament_color,
  1164. "used_grams": round(used_grams, 1),
  1165. "used_meters": float(used_m) if used_m else 0,
  1166. }
  1167. )
  1168. plate_info["filaments"].sort(key=lambda x: x["slot_id"])
  1169. # Collect object names
  1170. for obj_elem in plate_elem.findall("object"):
  1171. obj_name = obj_elem.get("name")
  1172. if obj_name and obj_name not in plate_info["objects"]:
  1173. plate_info["objects"].append(obj_name)
  1174. # Set plate name
  1175. if plate_index is not None:
  1176. custom_name = plate_names.get(plate_index)
  1177. if custom_name:
  1178. plate_info["name"] = custom_name
  1179. elif plate_info["objects"]:
  1180. plate_info["name"] = plate_info["objects"][0]
  1181. plate_metadata[plate_index] = plate_info
  1182. # Parse plate_*.json for object lists when slice_info is missing
  1183. plate_json_objects: dict[int, list[str]] = {}
  1184. for name in namelist:
  1185. match = re.match(r"^Metadata/plate_(\d+)\.json$", name)
  1186. if not match:
  1187. continue
  1188. try:
  1189. plate_index = int(match.group(1))
  1190. except ValueError:
  1191. continue
  1192. try:
  1193. payload = json.loads(zf.read(name).decode())
  1194. bbox_objects = payload.get("bbox_objects", [])
  1195. names: list[str] = []
  1196. for obj in bbox_objects:
  1197. obj_name = obj.get("name") if isinstance(obj, dict) else None
  1198. if obj_name and obj_name not in names:
  1199. names.append(obj_name)
  1200. if names:
  1201. plate_json_objects[plate_index] = names
  1202. except Exception:
  1203. continue
  1204. # Build plate list
  1205. for idx in plate_indices:
  1206. meta = plate_metadata.get(idx, {})
  1207. has_thumbnail = f"Metadata/plate_{idx}.png" in namelist
  1208. objects = meta.get("objects", [])
  1209. if not objects:
  1210. objects = plate_json_objects.get(idx, [])
  1211. plate_name = meta.get("name")
  1212. if not plate_name:
  1213. plate_name = plate_names.get(idx)
  1214. if not plate_name and objects:
  1215. plate_name = objects[0]
  1216. plates.append(
  1217. {
  1218. "index": idx,
  1219. "name": plate_name,
  1220. "objects": objects,
  1221. "object_count": len(objects),
  1222. "has_thumbnail": has_thumbnail,
  1223. "thumbnail_url": f"/api/v1/printers/{printer_id}/files/plate-thumbnail/{idx}?path={path}",
  1224. "print_time_seconds": meta.get("prediction"),
  1225. "filament_used_grams": meta.get("weight"),
  1226. "filaments": meta.get("filaments", []),
  1227. }
  1228. )
  1229. except Exception as e:
  1230. logger.warning("Failed to parse plates from printer file %s: %s", path, e)
  1231. return {
  1232. "printer_id": printer_id,
  1233. "path": path,
  1234. "filename": filename,
  1235. "plates": plates,
  1236. "is_multi_plate": len(plates) > 1,
  1237. }
  1238. @router.get("/{printer_id}/files/plate-thumbnail/{plate_index}")
  1239. async def get_printer_file_plate_thumbnail(
  1240. printer_id: int,
  1241. plate_index: int,
  1242. path: str = Query(..., description="Full path to the 3MF file on the printer"),
  1243. _=RequirePermissionIfAuthEnabled(Permission.PRINTERS_FILES),
  1244. db: AsyncSession = Depends(get_db),
  1245. ):
  1246. """Get a plate thumbnail image from a printer-stored 3MF file."""
  1247. import io
  1248. result = await db.execute(select(Printer).where(Printer.id == printer_id))
  1249. printer = result.scalar_one_or_none()
  1250. if not printer:
  1251. raise HTTPException(404, "Printer not found")
  1252. data = await download_file_bytes_async(printer.ip_address, printer.access_code, path, printer_model=printer.model)
  1253. if data is None:
  1254. raise HTTPException(404, f"File not found: {path}")
  1255. try:
  1256. with zipfile.ZipFile(io.BytesIO(data), "r") as zf:
  1257. thumb_path = f"Metadata/plate_{plate_index}.png"
  1258. if thumb_path in zf.namelist():
  1259. image_data = zf.read(thumb_path)
  1260. return Response(content=image_data, media_type="image/png")
  1261. except Exception:
  1262. pass # Corrupt or unreadable 3MF; fall through to 404
  1263. raise HTTPException(status_code=404, detail=f"Thumbnail for plate {plate_index} not found")
  1264. @router.post("/{printer_id}/files/download-zip")
  1265. async def download_printer_files_as_zip(
  1266. printer_id: int,
  1267. request: dict,
  1268. _=RequirePermissionIfAuthEnabled(Permission.PRINTERS_FILES),
  1269. db: AsyncSession = Depends(get_db),
  1270. ):
  1271. """Download multiple files from the printer as a ZIP archive."""
  1272. import io
  1273. paths = request.get("paths", [])
  1274. if not paths:
  1275. raise HTTPException(400, "No files specified")
  1276. result = await db.execute(select(Printer).where(Printer.id == printer_id))
  1277. printer = result.scalar_one_or_none()
  1278. if not printer:
  1279. raise HTTPException(404, "Printer not found")
  1280. # Create ZIP in memory
  1281. zip_buffer = io.BytesIO()
  1282. with zipfile.ZipFile(zip_buffer, "w", zipfile.ZIP_DEFLATED) as zf:
  1283. for path in paths:
  1284. try:
  1285. data = await download_file_bytes_async(
  1286. printer.ip_address, printer.access_code, path, printer_model=printer.model
  1287. )
  1288. if data:
  1289. filename = path.split("/")[-1]
  1290. zf.writestr(filename, data)
  1291. except Exception as e:
  1292. logging.warning("Failed to add %s to ZIP: %s", path, e)
  1293. continue
  1294. zip_buffer.seek(0)
  1295. zip_data = zip_buffer.read()
  1296. if len(zip_data) == 0:
  1297. raise HTTPException(404, "No files could be downloaded")
  1298. return Response(
  1299. content=zip_data,
  1300. media_type="application/zip",
  1301. headers={"Content-Disposition": 'attachment; filename="printer-files.zip"'},
  1302. )
  1303. @router.delete("/{printer_id}/files")
  1304. async def delete_printer_file(
  1305. printer_id: int,
  1306. path: str,
  1307. _=RequirePermissionIfAuthEnabled(Permission.PRINTERS_FILES),
  1308. db: AsyncSession = Depends(get_db),
  1309. ):
  1310. """Delete a file from the printer."""
  1311. result = await db.execute(select(Printer).where(Printer.id == printer_id))
  1312. printer = result.scalar_one_or_none()
  1313. if not printer:
  1314. raise HTTPException(404, "Printer not found")
  1315. success = await delete_file_async(printer.ip_address, printer.access_code, path, printer_model=printer.model)
  1316. if not success:
  1317. raise HTTPException(500, f"Failed to delete file: {path}")
  1318. return {"status": "deleted", "path": path}
  1319. @router.get("/{printer_id}/storage")
  1320. async def get_printer_storage(
  1321. printer_id: int,
  1322. _=RequirePermissionIfAuthEnabled(Permission.PRINTERS_READ),
  1323. db: AsyncSession = Depends(get_db),
  1324. ):
  1325. """Get storage information from the printer."""
  1326. result = await db.execute(select(Printer).where(Printer.id == printer_id))
  1327. printer = result.scalar_one_or_none()
  1328. if not printer:
  1329. raise HTTPException(404, "Printer not found")
  1330. storage_info = await get_storage_info_async(printer.ip_address, printer.access_code, printer_model=printer.model)
  1331. return storage_info or {"used_bytes": None, "free_bytes": None}
  1332. # ============================================
  1333. # MQTT Debug Logging Endpoints
  1334. # ============================================
  1335. @router.post("/{printer_id}/logging/enable")
  1336. async def enable_mqtt_logging(
  1337. printer_id: int,
  1338. _=RequirePermissionIfAuthEnabled(Permission.PRINTERS_CONTROL),
  1339. db: AsyncSession = Depends(get_db),
  1340. ):
  1341. """Enable MQTT message logging for a printer."""
  1342. result = await db.execute(select(Printer).where(Printer.id == printer_id))
  1343. printer = result.scalar_one_or_none()
  1344. if not printer:
  1345. raise HTTPException(404, "Printer not found")
  1346. success = printer_manager.enable_logging(printer_id, True)
  1347. if not success:
  1348. raise HTTPException(400, "Printer not connected")
  1349. return {"logging_enabled": True}
  1350. @router.post("/{printer_id}/logging/disable")
  1351. async def disable_mqtt_logging(
  1352. printer_id: int,
  1353. _=RequirePermissionIfAuthEnabled(Permission.PRINTERS_CONTROL),
  1354. db: AsyncSession = Depends(get_db),
  1355. ):
  1356. """Disable MQTT message logging for a printer."""
  1357. result = await db.execute(select(Printer).where(Printer.id == printer_id))
  1358. printer = result.scalar_one_or_none()
  1359. if not printer:
  1360. raise HTTPException(404, "Printer not found")
  1361. success = printer_manager.enable_logging(printer_id, False)
  1362. if not success:
  1363. raise HTTPException(400, "Printer not connected")
  1364. return {"logging_enabled": False}
  1365. @router.get("/{printer_id}/logging")
  1366. async def get_mqtt_logs(
  1367. printer_id: int,
  1368. _=RequirePermissionIfAuthEnabled(Permission.PRINTERS_READ),
  1369. db: AsyncSession = Depends(get_db),
  1370. ):
  1371. """Get MQTT message logs for a printer."""
  1372. result = await db.execute(select(Printer).where(Printer.id == printer_id))
  1373. printer = result.scalar_one_or_none()
  1374. if not printer:
  1375. raise HTTPException(404, "Printer not found")
  1376. logs = printer_manager.get_logs(printer_id)
  1377. return {
  1378. "logging_enabled": printer_manager.is_logging_enabled(printer_id),
  1379. "logs": [
  1380. {
  1381. "timestamp": log.timestamp,
  1382. "topic": log.topic,
  1383. "direction": log.direction,
  1384. "payload": log.payload,
  1385. }
  1386. for log in logs
  1387. ],
  1388. }
  1389. @router.delete("/{printer_id}/logging")
  1390. async def clear_mqtt_logs(
  1391. printer_id: int,
  1392. _=RequirePermissionIfAuthEnabled(Permission.PRINTERS_CONTROL),
  1393. db: AsyncSession = Depends(get_db),
  1394. ):
  1395. """Clear MQTT message logs for a printer."""
  1396. result = await db.execute(select(Printer).where(Printer.id == printer_id))
  1397. printer = result.scalar_one_or_none()
  1398. if not printer:
  1399. raise HTTPException(404, "Printer not found")
  1400. printer_manager.clear_logs(printer_id)
  1401. return {"status": "cleared"}
  1402. # ============================================
  1403. # AMS Drying Endpoints
  1404. # ============================================
  1405. @router.post("/{printer_id}/drying/start")
  1406. async def start_drying(
  1407. printer_id: int,
  1408. ams_id: int,
  1409. temp: int = 45,
  1410. duration: int = 4,
  1411. filament: str = "",
  1412. rotate_tray: bool = False,
  1413. _=RequirePermissionIfAuthEnabled(Permission.PRINTERS_CONTROL),
  1414. db: AsyncSession = Depends(get_db),
  1415. ):
  1416. """Send AMS drying start command. temp=45-85, duration=hours."""
  1417. result = await db.execute(select(Printer).where(Printer.id == printer_id))
  1418. printer = result.scalar_one_or_none()
  1419. if not printer:
  1420. raise HTTPException(404, "Printer not found")
  1421. # Server-side guard: reject if this model/firmware doesn't support drying
  1422. live_state = printer_manager.get_status(printer_id)
  1423. firmware = live_state.firmware_version if live_state else None
  1424. if not supports_drying(printer.model, firmware):
  1425. raise HTTPException(400, "Drying not supported for this printer model or firmware version")
  1426. if temp < 45 or temp > 85:
  1427. raise HTTPException(400, "Temperature must be 45-85°C")
  1428. if duration < 1 or duration > 24:
  1429. raise HTTPException(400, "Duration must be 1-24 hours")
  1430. # Inspect the live AMS unit: surface blocking dry_sf_reasons (otherwise the
  1431. # firmware silently ignores the command — #971) and backfill an empty
  1432. # filament field from the first loaded tray so the printer doesn't reject
  1433. # the payload.
  1434. target_ams: dict | None = None
  1435. for unit in (live_state.raw_data.get("ams") if live_state else None) or []:
  1436. try:
  1437. if int(unit.get("id", -1)) == ams_id:
  1438. target_ams = unit
  1439. break
  1440. except (TypeError, ValueError):
  1441. continue
  1442. if target_ams is not None:
  1443. reason_messages = {
  1444. 0: "Printer is busy",
  1445. 1: "Insufficient power — too many AMS drying or external PSU required",
  1446. 2: "AMS is busy",
  1447. 3: "Filament is at the AMS outlet — retract it first",
  1448. 4: "AMS is already starting a drying cycle",
  1449. 5: "Not supported in 2D mode",
  1450. 6: "AMS is already drying",
  1451. 7: "AMS firmware is upgrading",
  1452. 8: "Plug in the external AMS power adapter to start drying",
  1453. }
  1454. for code in target_ams.get("dry_sf_reason") or []:
  1455. try:
  1456. code_int = int(code)
  1457. except (TypeError, ValueError):
  1458. continue
  1459. if code_int in reason_messages:
  1460. raise HTTPException(409, reason_messages[code_int])
  1461. if not filament:
  1462. for tray in target_ams.get("tray") or []:
  1463. tray_type = tray.get("tray_type")
  1464. if tray_type:
  1465. filament = str(tray_type)
  1466. break
  1467. if not filament:
  1468. filament = "PLA"
  1469. success = printer_manager.send_drying_command(
  1470. printer_id, ams_id, temp, duration, mode=1, filament=filament, rotate_tray=rotate_tray
  1471. )
  1472. if not success:
  1473. raise HTTPException(400, "Printer not connected")
  1474. return {"status": "drying_started", "ams_id": ams_id, "temp": temp, "duration": duration}
  1475. @router.post("/{printer_id}/drying/stop")
  1476. async def stop_drying(
  1477. printer_id: int,
  1478. ams_id: int,
  1479. _=RequirePermissionIfAuthEnabled(Permission.PRINTERS_CONTROL),
  1480. db: AsyncSession = Depends(get_db),
  1481. ):
  1482. """Send AMS drying stop command."""
  1483. result = await db.execute(select(Printer).where(Printer.id == printer_id))
  1484. printer = result.scalar_one_or_none()
  1485. if not printer:
  1486. raise HTTPException(404, "Printer not found")
  1487. success = printer_manager.send_drying_command(printer_id, ams_id, temp=0, duration=0, mode=0)
  1488. if not success:
  1489. raise HTTPException(400, "Printer not connected")
  1490. return {"status": "drying_stopped", "ams_id": ams_id}
  1491. # ============================================
  1492. # Print Options (AI Detection) Endpoints
  1493. # ============================================
  1494. @router.post("/{printer_id}/print-options")
  1495. async def set_print_option(
  1496. printer_id: int,
  1497. module_name: str,
  1498. enabled: bool,
  1499. print_halt: bool = True,
  1500. sensitivity: str = "medium",
  1501. _=RequirePermissionIfAuthEnabled(Permission.PRINTERS_CONTROL),
  1502. db: AsyncSession = Depends(get_db),
  1503. ):
  1504. """Set an AI detection / print option on the printer.
  1505. Valid module_name values:
  1506. - spaghetti_detector: Spaghetti detection
  1507. - first_layer_inspector: First layer inspection
  1508. - printing_monitor: AI print quality monitoring
  1509. - buildplate_marker_detector: Build plate marker detection
  1510. - allow_skip_parts: Allow skipping failed parts
  1511. """
  1512. result = await db.execute(select(Printer).where(Printer.id == printer_id))
  1513. printer = result.scalar_one_or_none()
  1514. if not printer:
  1515. raise HTTPException(404, "Printer not found")
  1516. client = printer_manager.get_client(printer_id)
  1517. if not client or not client.state.connected:
  1518. raise HTTPException(400, "Printer not connected")
  1519. # Validate module_name
  1520. valid_modules = [
  1521. "spaghetti_detector",
  1522. "first_layer_inspector",
  1523. "printing_monitor",
  1524. "buildplate_marker_detector",
  1525. "allow_skip_parts",
  1526. "pileup_detector",
  1527. "clump_detector",
  1528. "airprint_detector",
  1529. "auto_recovery_step_loss",
  1530. ]
  1531. if module_name not in valid_modules:
  1532. raise HTTPException(400, f"Invalid module_name. Must be one of: {valid_modules}")
  1533. # Validate sensitivity
  1534. valid_sensitivities = ["low", "medium", "high", "never_halt"]
  1535. if sensitivity not in valid_sensitivities:
  1536. raise HTTPException(400, f"Invalid sensitivity. Must be one of: {valid_sensitivities}")
  1537. success = client.set_xcam_option(
  1538. module_name=module_name,
  1539. enabled=enabled,
  1540. print_halt=print_halt,
  1541. sensitivity=sensitivity,
  1542. )
  1543. if not success:
  1544. raise HTTPException(500, "Failed to send command to printer")
  1545. return {
  1546. "success": True,
  1547. "module_name": module_name,
  1548. "enabled": enabled,
  1549. "print_halt": print_halt,
  1550. "sensitivity": sensitivity,
  1551. }
  1552. # ============================================
  1553. # Calibration
  1554. # ============================================
  1555. @router.post("/{printer_id}/calibration")
  1556. async def start_calibration(
  1557. printer_id: int,
  1558. bed_leveling: bool = False,
  1559. vibration: bool = False,
  1560. motor_noise: bool = False,
  1561. nozzle_offset: bool = False,
  1562. high_temp_heatbed: bool = False,
  1563. _=RequirePermissionIfAuthEnabled(Permission.PRINTERS_CONTROL),
  1564. db: AsyncSession = Depends(get_db),
  1565. ):
  1566. """Start printer calibration with selected options.
  1567. At least one option must be selected.
  1568. Options:
  1569. - bed_leveling: Run bed leveling calibration
  1570. - vibration: Run vibration compensation calibration
  1571. - motor_noise: Run motor noise cancellation calibration
  1572. - nozzle_offset: Run nozzle offset calibration (dual nozzle printers)
  1573. - high_temp_heatbed: Run high-temperature heatbed calibration
  1574. """
  1575. result = await db.execute(select(Printer).where(Printer.id == printer_id))
  1576. printer = result.scalar_one_or_none()
  1577. if not printer:
  1578. raise HTTPException(404, "Printer not found")
  1579. client = printer_manager.get_client(printer_id)
  1580. if not client or not client.state.connected:
  1581. raise HTTPException(400, "Printer not connected")
  1582. # Check that at least one option is selected
  1583. if not any([bed_leveling, vibration, motor_noise, nozzle_offset, high_temp_heatbed]):
  1584. raise HTTPException(400, "At least one calibration option must be selected")
  1585. success = client.start_calibration(
  1586. bed_leveling=bed_leveling,
  1587. vibration=vibration,
  1588. motor_noise=motor_noise,
  1589. nozzle_offset=nozzle_offset,
  1590. high_temp_heatbed=high_temp_heatbed,
  1591. )
  1592. if not success:
  1593. raise HTTPException(500, "Failed to send calibration command to printer")
  1594. return {
  1595. "success": True,
  1596. "bed_leveling": bed_leveling,
  1597. "vibration": vibration,
  1598. "motor_noise": motor_noise,
  1599. "nozzle_offset": nozzle_offset,
  1600. "high_temp_heatbed": high_temp_heatbed,
  1601. }
  1602. # ============================================================================
  1603. # Slot Preset Mapping Endpoints
  1604. # ============================================================================
  1605. def _slot_preset_key(ams_id: int, tray_id: int) -> int:
  1606. # Mirrors frontend getGlobalTrayId (amsHelpers.ts): AMS-HT (128-135) is keyed
  1607. # by ams_id since each unit has a single slot and shares its global ID with
  1608. # the unit itself. Regular AMS and external (255) use ams_id*4+tray_id.
  1609. if 128 <= ams_id <= 135:
  1610. return ams_id
  1611. return ams_id * 4 + tray_id
  1612. @router.get("/{printer_id}/slot-presets")
  1613. async def get_slot_presets(
  1614. printer_id: int,
  1615. _=RequirePermissionIfAuthEnabled(Permission.PRINTERS_READ),
  1616. db: AsyncSession = Depends(get_db),
  1617. ):
  1618. """Get all saved slot-to-preset mappings for a printer."""
  1619. result = await db.execute(select(SlotPresetMapping).where(SlotPresetMapping.printer_id == printer_id))
  1620. mappings = result.scalars().all()
  1621. return {
  1622. _slot_preset_key(mapping.ams_id, mapping.tray_id): {
  1623. "ams_id": mapping.ams_id,
  1624. "tray_id": mapping.tray_id,
  1625. "preset_id": mapping.preset_id,
  1626. "preset_name": mapping.preset_name,
  1627. }
  1628. for mapping in mappings
  1629. }
  1630. @router.get("/{printer_id}/slot-presets/{ams_id}/{tray_id}")
  1631. async def get_slot_preset(
  1632. printer_id: int,
  1633. ams_id: int,
  1634. tray_id: int,
  1635. _=RequirePermissionIfAuthEnabled(Permission.PRINTERS_READ),
  1636. db: AsyncSession = Depends(get_db),
  1637. ):
  1638. """Get the saved preset for a specific slot."""
  1639. result = await db.execute(
  1640. select(SlotPresetMapping).where(
  1641. SlotPresetMapping.printer_id == printer_id,
  1642. SlotPresetMapping.ams_id == ams_id,
  1643. SlotPresetMapping.tray_id == tray_id,
  1644. )
  1645. )
  1646. mapping = result.scalar_one_or_none()
  1647. if not mapping:
  1648. return None
  1649. return {
  1650. "ams_id": mapping.ams_id,
  1651. "tray_id": mapping.tray_id,
  1652. "preset_id": mapping.preset_id,
  1653. "preset_name": mapping.preset_name,
  1654. }
  1655. @router.put("/{printer_id}/slot-presets/{ams_id}/{tray_id}")
  1656. async def save_slot_preset(
  1657. printer_id: int,
  1658. ams_id: int,
  1659. tray_id: int,
  1660. preset_id: str,
  1661. preset_name: str,
  1662. preset_source: str = "cloud",
  1663. _=RequirePermissionIfAuthEnabled(Permission.PRINTERS_UPDATE),
  1664. db: AsyncSession = Depends(get_db),
  1665. ):
  1666. """Save a preset mapping for a specific slot."""
  1667. # Check printer exists
  1668. result = await db.execute(select(Printer).where(Printer.id == printer_id))
  1669. if not result.scalar_one_or_none():
  1670. raise HTTPException(404, "Printer not found")
  1671. # Check for existing 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. # Update existing
  1682. mapping.preset_id = preset_id
  1683. mapping.preset_name = preset_name
  1684. mapping.preset_source = preset_source
  1685. else:
  1686. # Create new
  1687. mapping = SlotPresetMapping(
  1688. printer_id=printer_id,
  1689. ams_id=ams_id,
  1690. tray_id=tray_id,
  1691. preset_id=preset_id,
  1692. preset_name=preset_name,
  1693. preset_source=preset_source,
  1694. )
  1695. db.add(mapping)
  1696. await db.commit()
  1697. await db.refresh(mapping)
  1698. return {
  1699. "ams_id": mapping.ams_id,
  1700. "tray_id": mapping.tray_id,
  1701. "preset_id": mapping.preset_id,
  1702. "preset_name": mapping.preset_name,
  1703. "preset_source": mapping.preset_source,
  1704. }
  1705. @router.delete("/{printer_id}/slot-presets/{ams_id}/{tray_id}")
  1706. async def delete_slot_preset(
  1707. printer_id: int,
  1708. ams_id: int,
  1709. tray_id: int,
  1710. _=RequirePermissionIfAuthEnabled(Permission.PRINTERS_UPDATE),
  1711. db: AsyncSession = Depends(get_db),
  1712. ):
  1713. """Delete a saved preset mapping for a slot."""
  1714. result = await db.execute(
  1715. select(SlotPresetMapping).where(
  1716. SlotPresetMapping.printer_id == printer_id,
  1717. SlotPresetMapping.ams_id == ams_id,
  1718. SlotPresetMapping.tray_id == tray_id,
  1719. )
  1720. )
  1721. mapping = result.scalar_one_or_none()
  1722. if mapping:
  1723. await db.delete(mapping)
  1724. await db.commit()
  1725. return {"success": True}
  1726. @router.post("/{printer_id}/slots/{ams_id}/{tray_id}/configure")
  1727. async def configure_ams_slot(
  1728. printer_id: int,
  1729. ams_id: int,
  1730. tray_id: int,
  1731. tray_info_idx: str = Query(...),
  1732. tray_type: str = Query(...),
  1733. tray_sub_brands: str = Query(...),
  1734. tray_color: str = Query(...),
  1735. nozzle_temp_min: int = Query(...),
  1736. nozzle_temp_max: int = Query(...),
  1737. cali_idx: int = Query(-1),
  1738. nozzle_diameter: str = Query("0.4"),
  1739. setting_id: str = Query(""),
  1740. kprofile_filament_id: str = Query(""),
  1741. kprofile_setting_id: str = Query(""),
  1742. k_value: float = Query(0.0),
  1743. db: AsyncSession = Depends(get_db),
  1744. _=RequirePermissionIfAuthEnabled(Permission.PRINTERS_CONTROL),
  1745. ):
  1746. """Configure an AMS slot with a specific filament setting and K profile.
  1747. This sends two commands to the printer:
  1748. 1. ams_filament_setting - sets filament type, color, temperature
  1749. 2. extrusion_cali_sel - sets the K profile (pressure advance value)
  1750. Args:
  1751. printer_id: Database ID of the printer
  1752. ams_id: AMS unit ID (0-3 for regular AMS, 128-135 for HT AMS)
  1753. tray_id: Tray ID within the AMS (0-3)
  1754. tray_info_idx: Filament ID short format (e.g., "GFL05") or user preset ID
  1755. tray_type: Filament type (e.g., "PLA", "PETG")
  1756. tray_sub_brands: Sub-brand/profile name (e.g., "PLA Basic", "PETG HF")
  1757. tray_color: Color in RRGGBBAA hex format (e.g., "FFFF00FF")
  1758. nozzle_temp_min: Minimum nozzle temperature
  1759. nozzle_temp_max: Maximum nozzle temperature
  1760. cali_idx: K profile calibration index (-1 for default 0.020)
  1761. nozzle_diameter: Nozzle diameter string (e.g., "0.4")
  1762. setting_id: Full setting ID with version (e.g., "GFSL05_07") - optional
  1763. kprofile_filament_id: K profile's filament_id for proper K profile linking
  1764. k_value: Direct K value to set (0.0 to skip direct K value setting)
  1765. """
  1766. logger = logging.getLogger(__name__)
  1767. logger.info("[configure_ams_slot] printer_id=%s, ams_id=%s, tray_id=%s", printer_id, ams_id, tray_id)
  1768. logger.info(
  1769. f"[configure_ams_slot] tray_info_idx={tray_info_idx!r}, tray_type={tray_type!r}, tray_sub_brands={tray_sub_brands!r}"
  1770. )
  1771. logger.info(
  1772. f"[configure_ams_slot] setting_id={setting_id!r}, kprofile_filament_id={kprofile_filament_id!r}, kprofile_setting_id={kprofile_setting_id!r}"
  1773. )
  1774. # Get MQTT client for this printer
  1775. client = printer_manager.get_client(printer_id)
  1776. if not client:
  1777. raise HTTPException(status_code=400, detail="Printer not connected")
  1778. # Resolve tray_info_idx for the MQTT command.
  1779. # Priority:
  1780. # 1. Use the provided tray_info_idx if set (including cloud-synced
  1781. # custom presets like PFUS* / P*).
  1782. # 2. Reuse the slot's existing tray_info_idx if it's a specific
  1783. # (non-generic) preset for the same material.
  1784. # 3. Fall back to a generic Bambu filament ID.
  1785. _GENERIC_FILAMENT_IDS = {
  1786. "PLA": "GFL99",
  1787. "PETG": "GFG99",
  1788. "ABS": "GFB99",
  1789. "ASA": "GFB98",
  1790. "PC": "GFC99",
  1791. "PA": "GFN99",
  1792. "NYLON": "GFN99",
  1793. "TPU": "GFU99",
  1794. "PVA": "GFS99",
  1795. "HIPS": "GFS98",
  1796. "PLA-CF": "GFL98",
  1797. "PETG-CF": "GFG98",
  1798. "PA-CF": "GFN98",
  1799. "PETG HF": "GFG96",
  1800. }
  1801. _GENERIC_ID_VALUES = set(_GENERIC_FILAMENT_IDS.values())
  1802. effective_tray_info_idx = tray_info_idx
  1803. if not tray_info_idx:
  1804. # No preset provided — try slot reuse or generic fallback
  1805. current_tray_info_idx = ""
  1806. current_tray_type = ""
  1807. state = printer_manager.get_status(printer_id)
  1808. if state and state.raw_data:
  1809. from backend.app.api.routes.inventory import _find_tray_in_ams_data
  1810. if ams_id == 255:
  1811. vt_tray = state.raw_data.get("vt_tray") or []
  1812. ext_id = tray_id + 254
  1813. for vt in vt_tray:
  1814. if isinstance(vt, dict) and int(vt.get("id", 254)) == ext_id:
  1815. current_tray_info_idx = vt.get("tray_info_idx", "")
  1816. current_tray_type = vt.get("tray_type", "")
  1817. break
  1818. else:
  1819. ams_data = state.raw_data.get("ams", {})
  1820. ams_list = (
  1821. ams_data.get("ams", [])
  1822. if isinstance(ams_data, dict)
  1823. else ams_data
  1824. if isinstance(ams_data, list)
  1825. else []
  1826. )
  1827. cur_tray = _find_tray_in_ams_data(ams_list, ams_id, tray_id)
  1828. if cur_tray:
  1829. current_tray_info_idx = cur_tray.get("tray_info_idx", "")
  1830. current_tray_type = cur_tray.get("tray_type", "")
  1831. if (
  1832. current_tray_info_idx
  1833. and current_tray_info_idx not in _GENERIC_ID_VALUES
  1834. and current_tray_type
  1835. and current_tray_type.upper() == tray_type.upper()
  1836. ):
  1837. logger.info(
  1838. "[configure_ams_slot] Reusing slot's existing tray_info_idx=%r (same material %r)",
  1839. current_tray_info_idx,
  1840. tray_type,
  1841. )
  1842. effective_tray_info_idx = current_tray_info_idx
  1843. elif tray_type:
  1844. material = tray_type.upper().strip()
  1845. generic = (
  1846. _GENERIC_FILAMENT_IDS.get(material)
  1847. or _GENERIC_FILAMENT_IDS.get(material.split("-")[0].split(" ")[0])
  1848. or ""
  1849. )
  1850. if generic:
  1851. logger.info("[configure_ams_slot] Falling back to generic %r for material %r", generic, tray_type)
  1852. effective_tray_info_idx = generic
  1853. # Send filament setting + K-profile commands
  1854. filament_id_for_kprofile = kprofile_filament_id if kprofile_filament_id else effective_tray_info_idx
  1855. # Realign the slot's filament context to the K-profile's calibration
  1856. # context. The printer's calibration table is keyed by (filament_id,
  1857. # cali_idx) — so for the cali_idx selected via extrusion_cali_sel to
  1858. # actually stick to the slot, ams_filament_setting must declare the
  1859. # slot under the SAME filament_id.
  1860. #
  1861. # Without this, configure_ams_slot would send:
  1862. # ams_filament_setting → tray_info_idx=GFL99 (generic from material)
  1863. # extrusion_cali_sel → filament_id=P4d64437 (kp's preset)
  1864. # ...and the cali_idx would silently be dropped to default because the
  1865. # slot's filament context (GFL99) doesn't match the kp's (P4d64437).
  1866. #
  1867. # This realignment fires only when the kp is targeted at a different
  1868. # preset than the user's filament selection AND the kp's preset is a
  1869. # valid tray_info_idx (GF* official, P* local — not PFUS* cloud-user
  1870. # which the slicer rejects in tray_info_idx).
  1871. effective_setting_id = setting_id
  1872. if (
  1873. kprofile_filament_id
  1874. and kprofile_filament_id != effective_tray_info_idx
  1875. and not kprofile_filament_id.startswith("PFUS")
  1876. ):
  1877. logger.info(
  1878. "[configure_ams_slot] realigning slot filament context to kp: tray_info_idx %r → %r, setting_id %r → %r",
  1879. effective_tray_info_idx,
  1880. kprofile_filament_id,
  1881. setting_id,
  1882. kprofile_setting_id or setting_id,
  1883. )
  1884. effective_tray_info_idx = kprofile_filament_id
  1885. if kprofile_setting_id:
  1886. effective_setting_id = kprofile_setting_id
  1887. # Always send ams_set_filament_setting — the user explicitly clicked
  1888. # "Configure Slot", so honor that. Previous versions skipped this for
  1889. # RFID-tagged slots to preserve the slicer eye icon, but printers cache
  1890. # stale tag_uid/tray_uuid after a BL spool is removed, causing the check
  1891. # to false-positive on non-RFID slots and silently drop the command.
  1892. success = client.ams_set_filament_setting(
  1893. ams_id=ams_id,
  1894. tray_id=tray_id,
  1895. tray_info_idx=effective_tray_info_idx,
  1896. tray_type=tray_type,
  1897. tray_sub_brands=tray_sub_brands,
  1898. tray_color=tray_color,
  1899. nozzle_temp_min=nozzle_temp_min,
  1900. nozzle_temp_max=nozzle_temp_max,
  1901. setting_id=effective_setting_id,
  1902. )
  1903. if not success:
  1904. raise HTTPException(status_code=500, detail="Failed to send filament configuration command")
  1905. # Method 1: Select existing calibration profile by cali_idx
  1906. # Do NOT include setting_id — BambuStudio never sends it in extrusion_cali_sel,
  1907. # and including it causes the firmware to mislink the profile on X1C/P1S.
  1908. client.extrusion_cali_sel(
  1909. ams_id=ams_id,
  1910. tray_id=tray_id,
  1911. cali_idx=cali_idx,
  1912. filament_id=filament_id_for_kprofile,
  1913. nozzle_diameter=nozzle_diameter,
  1914. )
  1915. # Method 2: Only send extrusion_cali_set when NO existing profile was selected
  1916. # (cali_idx == -1). When cali_idx >= 0, extrusion_cali_sel already selected the
  1917. # correct profile. Sending extrusion_cali_set with the same cali_idx would MODIFY
  1918. # the existing profile's metadata (extruder_id, nozzle_id, name, setting_id),
  1919. # corrupting it — e.g., overwriting a High Flow extruder 1 profile with
  1920. # hardcoded extruder_id=0 and nozzle_id=HS00.
  1921. if k_value > 0 and cali_idx < 0:
  1922. # Calculate global tray ID for extrusion_cali_set
  1923. if ams_id <= 3:
  1924. global_tray_id = ams_id * 4 + tray_id
  1925. elif ams_id >= 128 and ams_id <= 135:
  1926. global_tray_id = (ams_id - 128) * 4 + tray_id
  1927. else:
  1928. global_tray_id = tray_id
  1929. client.extrusion_cali_set(
  1930. tray_id=global_tray_id,
  1931. k_value=k_value,
  1932. nozzle_diameter=nozzle_diameter,
  1933. nozzle_temp=nozzle_temp_max,
  1934. filament_id=filament_id_for_kprofile,
  1935. setting_id=kprofile_setting_id or "",
  1936. name=tray_sub_brands or "",
  1937. cali_idx=cali_idx,
  1938. )
  1939. # Persist the user's K-profile choice so it survives RFID re-reads and
  1940. # session restarts. Pre-Phase-13 this was ephemeral — the MQTT command
  1941. # took effect on the printer but bambuddy never recorded it, so the next
  1942. # `_apply_pa_after_refresh` cycle had no stored profile to re-assert.
  1943. if cali_idx >= 0:
  1944. try:
  1945. from sqlalchemy.orm import selectinload
  1946. from backend.app.models.spool_assignment import SpoolAssignment
  1947. from backend.app.models.spool_k_profile import SpoolKProfile
  1948. from backend.app.models.spoolman_k_profile import SpoolmanKProfile
  1949. from backend.app.models.spoolman_slot_assignment import SpoolmanSlotAssignment
  1950. # Resolve slot's extruder index for the K-profile match key. Same
  1951. # logic as _apply_pa_after_refresh: external slots invert tray→extruder,
  1952. # AMS slots come from ams_extruder_map. Falls back to 0 (single-nozzle).
  1953. slot_state = printer_manager.get_status(printer_id)
  1954. slot_extruder: int | None = None
  1955. if slot_state and slot_state.ams_extruder_map:
  1956. if ams_id == 255:
  1957. slot_extruder = 1 - tray_id
  1958. else:
  1959. slot_extruder = slot_state.ams_extruder_map.get(str(ams_id))
  1960. kp_extruder = slot_extruder if slot_extruder is not None else 0
  1961. # Spoolman SlotAssignment first — has UniqueConstraint, idempotent.
  1962. sm_result = await db.execute(
  1963. select(SpoolmanSlotAssignment).where(
  1964. SpoolmanSlotAssignment.printer_id == printer_id,
  1965. SpoolmanSlotAssignment.ams_id == ams_id,
  1966. SpoolmanSlotAssignment.tray_id == tray_id,
  1967. )
  1968. )
  1969. sm_assignment = sm_result.scalar_one_or_none()
  1970. if sm_assignment:
  1971. existing = await db.execute(
  1972. select(SpoolmanKProfile).where(
  1973. SpoolmanKProfile.spoolman_spool_id == sm_assignment.spoolman_spool_id,
  1974. SpoolmanKProfile.printer_id == printer_id,
  1975. SpoolmanKProfile.extruder == kp_extruder,
  1976. SpoolmanKProfile.nozzle_diameter == nozzle_diameter,
  1977. )
  1978. )
  1979. kp = existing.scalar_one_or_none()
  1980. if kp:
  1981. kp.cali_idx = cali_idx
  1982. kp.k_value = k_value or 0.0
  1983. kp.setting_id = kprofile_setting_id or None
  1984. kp.name = tray_sub_brands or None
  1985. else:
  1986. db.add(
  1987. SpoolmanKProfile(
  1988. spoolman_spool_id=sm_assignment.spoolman_spool_id,
  1989. printer_id=printer_id,
  1990. extruder=kp_extruder,
  1991. nozzle_diameter=nozzle_diameter,
  1992. k_value=k_value or 0.0,
  1993. name=tray_sub_brands or None,
  1994. cali_idx=cali_idx,
  1995. setting_id=kprofile_setting_id or None,
  1996. )
  1997. )
  1998. await db.commit()
  1999. logger.info(
  2000. "[configure_ams_slot] Persisted Spoolman K-profile spool=%d printer=%d ams=%d tray=%d cali_idx=%d",
  2001. sm_assignment.spoolman_spool_id,
  2002. printer_id,
  2003. ams_id,
  2004. tray_id,
  2005. cali_idx,
  2006. )
  2007. else:
  2008. # Local SpoolAssignment + SpoolKProfile (no UNIQUE — use .first())
  2009. local_result = await db.execute(
  2010. select(SpoolAssignment)
  2011. .options(selectinload(SpoolAssignment.spool))
  2012. .where(
  2013. SpoolAssignment.printer_id == printer_id,
  2014. SpoolAssignment.ams_id == ams_id,
  2015. SpoolAssignment.tray_id == tray_id,
  2016. )
  2017. )
  2018. local_assignment = local_result.scalar_one_or_none()
  2019. if local_assignment and local_assignment.spool:
  2020. existing = await db.execute(
  2021. select(SpoolKProfile).where(
  2022. SpoolKProfile.spool_id == local_assignment.spool.id,
  2023. SpoolKProfile.printer_id == printer_id,
  2024. SpoolKProfile.extruder == kp_extruder,
  2025. SpoolKProfile.nozzle_diameter == nozzle_diameter,
  2026. )
  2027. )
  2028. # SpoolKProfile has no unique constraint on this tuple, so
  2029. # multiple rows could theoretically exist (shouldn't, but
  2030. # don't crash if they do). Update the first match, leave
  2031. # any duplicates alone.
  2032. kp = existing.scalars().first()
  2033. if kp:
  2034. kp.cali_idx = cali_idx
  2035. kp.k_value = k_value or 0.0
  2036. kp.setting_id = kprofile_setting_id or None
  2037. kp.name = tray_sub_brands or None
  2038. else:
  2039. db.add(
  2040. SpoolKProfile(
  2041. spool_id=local_assignment.spool.id,
  2042. printer_id=printer_id,
  2043. extruder=kp_extruder,
  2044. nozzle_diameter=nozzle_diameter,
  2045. k_value=k_value or 0.0,
  2046. name=tray_sub_brands or None,
  2047. cali_idx=cali_idx,
  2048. setting_id=kprofile_setting_id or None,
  2049. )
  2050. )
  2051. await db.commit()
  2052. logger.info(
  2053. "[configure_ams_slot] Persisted local K-profile spool=%d printer=%d ams=%d tray=%d cali_idx=%d",
  2054. local_assignment.spool.id,
  2055. printer_id,
  2056. ams_id,
  2057. tray_id,
  2058. cali_idx,
  2059. )
  2060. except Exception:
  2061. # MQTT command was already sent successfully — DB persist is best-effort.
  2062. logger.exception(
  2063. "[configure_ams_slot] Failed to persist K-profile (printer=%d ams=%d tray=%d cali_idx=%d)",
  2064. printer_id,
  2065. ams_id,
  2066. tray_id,
  2067. cali_idx,
  2068. )
  2069. try:
  2070. await db.rollback()
  2071. except Exception:
  2072. pass
  2073. # Request fresh status push from printer so frontend gets updated data via WebSocket
  2074. logger.info("[configure_ams_slot] Requesting status update from printer")
  2075. update_result = client.request_status_update()
  2076. logger.info("[configure_ams_slot] Status update request result: %s", update_result)
  2077. return {
  2078. "success": True,
  2079. "message": f"Configured AMS {ams_id} tray {tray_id} with {tray_sub_brands}",
  2080. }
  2081. @router.post("/{printer_id}/ams/{ams_id}/tray/{tray_id}/reset")
  2082. async def reset_ams_slot(
  2083. printer_id: int,
  2084. ams_id: int,
  2085. tray_id: int,
  2086. db: AsyncSession = Depends(get_db),
  2087. _=RequirePermissionIfAuthEnabled(Permission.PRINTERS_CONTROL),
  2088. ):
  2089. """Reset an AMS slot to empty/unconfigured state.
  2090. This clears the filament configuration from the slot.
  2091. """
  2092. # Get MQTT client for this printer
  2093. client = printer_manager.get_client(printer_id)
  2094. if not client:
  2095. raise HTTPException(status_code=400, detail="Printer not connected")
  2096. # Reset the slot
  2097. success = client.reset_ams_slot(ams_id=ams_id, tray_id=tray_id)
  2098. if not success:
  2099. raise HTTPException(status_code=500, detail="Failed to send reset command")
  2100. # Also delete any saved slot preset mapping
  2101. result = await db.execute(
  2102. select(SlotPresetMapping).where(
  2103. SlotPresetMapping.printer_id == printer_id,
  2104. SlotPresetMapping.ams_id == ams_id,
  2105. SlotPresetMapping.tray_id == tray_id,
  2106. )
  2107. )
  2108. mapping = result.scalar_one_or_none()
  2109. if mapping:
  2110. await db.delete(mapping)
  2111. await db.commit()
  2112. # Request fresh status push from printer so frontend gets updated data via WebSocket
  2113. client.request_status_update()
  2114. return {
  2115. "success": True,
  2116. "message": f"Reset AMS {ams_id} tray {tray_id}",
  2117. }
  2118. @router.get("/{printer_id}/ams-labels")
  2119. async def get_ams_labels(
  2120. printer_id: int,
  2121. _=RequirePermissionIfAuthEnabled(Permission.PRINTERS_READ),
  2122. db: AsyncSession = Depends(get_db),
  2123. ):
  2124. """Get all user-defined AMS labels for a printer, keyed by AMS unit ID.
  2125. Labels are stored by AMS serial number. This endpoint resolves the current
  2126. serial-to-ams_id mapping from the live printer state so the response is still
  2127. keyed by ams_id for UI compatibility.
  2128. """
  2129. # Build serial -> ams_id map from live printer state
  2130. serial_to_ams_id: dict[str, int] = {}
  2131. state = printer_manager.get_status(printer_id)
  2132. if state and state.raw_data:
  2133. for ams_unit in state.raw_data.get("ams", []):
  2134. sn = str(ams_unit.get("sn") or ams_unit.get("serial_number") or "")
  2135. if sn:
  2136. serial_to_ams_id[sn] = int(ams_unit.get("id", 0))
  2137. # Collect all known serials for this printer (live + synthetic fallback keys)
  2138. serials_to_query = set(serial_to_ams_id.keys())
  2139. # Fetch labels for all known serials
  2140. labels: dict[int, str] = {}
  2141. if serials_to_query:
  2142. result = await db.execute(select(AmsLabel).where(AmsLabel.ams_serial_number.in_(serials_to_query)))
  2143. for lbl in result.scalars().all():
  2144. aid = serial_to_ams_id.get(lbl.ams_serial_number)
  2145. if aid is not None:
  2146. labels[aid] = lbl.label
  2147. # Also fetch labels stored under synthetic keys for this printer (backward compat)
  2148. # Collect all synthetic keys first, then query with a single IN clause.
  2149. if state and state.raw_data:
  2150. synthetic_key_to_aid: dict[str, int] = {
  2151. f"p{printer_id}a{int(ams_unit.get('id', 0))}": int(ams_unit.get("id", 0))
  2152. for ams_unit in state.raw_data.get("ams", [])
  2153. if int(ams_unit.get("id", 0)) not in labels
  2154. }
  2155. if synthetic_key_to_aid:
  2156. result = await db.execute(
  2157. select(AmsLabel).where(AmsLabel.ams_serial_number.in_(synthetic_key_to_aid.keys()))
  2158. )
  2159. for lbl in result.scalars().all():
  2160. aid = synthetic_key_to_aid.get(lbl.ams_serial_number)
  2161. if aid is not None:
  2162. labels[aid] = lbl.label
  2163. return labels
  2164. @router.put("/{printer_id}/ams-labels/{ams_id}")
  2165. async def save_ams_label(
  2166. printer_id: int,
  2167. ams_id: int,
  2168. body: AmsLabelBody,
  2169. _=RequirePermissionIfAuthEnabled(Permission.PRINTERS_UPDATE),
  2170. db: AsyncSession = Depends(get_db),
  2171. ):
  2172. """Create or update the friendly name for a specific AMS unit.
  2173. When ``ams_serial`` is provided the label is stored under that serial number so
  2174. it survives the AMS being moved to a different printer. When it is absent (e.g.
  2175. older firmware that does not report a serial) a synthetic key based on the
  2176. printer_id and ams_id is used as a fallback.
  2177. """
  2178. # Verify printer exists
  2179. result = await db.execute(select(Printer).where(Printer.id == printer_id))
  2180. if not result.scalar_one_or_none():
  2181. raise HTTPException(404, "Printer not found")
  2182. # Determine the serial key to store under
  2183. stripped = body.ams_serial.strip() if body.ams_serial else ""
  2184. serial_key = stripped if stripped else f"p{printer_id}a{ams_id}"
  2185. result = await db.execute(select(AmsLabel).where(AmsLabel.ams_serial_number == serial_key))
  2186. existing = result.scalar_one_or_none()
  2187. if existing:
  2188. existing.label = body.label
  2189. existing.ams_id = ams_id
  2190. else:
  2191. db.add(AmsLabel(ams_serial_number=serial_key, ams_id=ams_id, label=body.label))
  2192. await db.commit()
  2193. return {"ams_id": ams_id, "label": body.label}
  2194. @router.delete("/{printer_id}/ams-labels/{ams_id}")
  2195. async def delete_ams_label(
  2196. printer_id: int,
  2197. ams_id: int,
  2198. ams_serial: str = Query(default="", max_length=50),
  2199. _=RequirePermissionIfAuthEnabled(Permission.PRINTERS_UPDATE),
  2200. db: AsyncSession = Depends(get_db),
  2201. ):
  2202. """Delete the friendly name for a specific AMS unit, reverting to the auto label."""
  2203. stripped = ams_serial.strip() if ams_serial else ""
  2204. serial_key = stripped if stripped else f"p{printer_id}a{ams_id}"
  2205. result = await db.execute(select(AmsLabel).where(AmsLabel.ams_serial_number == serial_key))
  2206. existing = result.scalar_one_or_none()
  2207. if existing:
  2208. await db.delete(existing)
  2209. await db.commit()
  2210. return {"success": True}
  2211. @router.post("/{printer_id}/debug/simulate-print-complete")
  2212. async def debug_simulate_print_complete(
  2213. printer_id: int,
  2214. db: AsyncSession = Depends(get_db),
  2215. _=RequirePermissionIfAuthEnabled(Permission.PRINTERS_CONTROL),
  2216. ):
  2217. """DEBUG: Simulate print completion to test freeze behavior.
  2218. This triggers the same code path as a real print completion,
  2219. without needing to wait for an actual print to finish.
  2220. """
  2221. from backend.app.main import _active_prints, on_print_complete
  2222. from backend.app.models.archive import PrintArchive
  2223. # Get the most recent archive for this printer
  2224. result = await db.execute(
  2225. select(PrintArchive)
  2226. .where(PrintArchive.printer_id == printer_id)
  2227. .order_by(PrintArchive.created_at.desc())
  2228. .limit(1)
  2229. )
  2230. archive = result.scalar_one_or_none()
  2231. if not archive:
  2232. raise HTTPException(status_code=404, detail="No archives found for this printer")
  2233. # Register this archive as "active" so on_print_complete can find it
  2234. filename = archive.file_path.split("/")[-1] if archive.file_path else "test.3mf"
  2235. subtask_name = archive.print_name or "Test Print"
  2236. _active_prints[(printer_id, filename)] = archive.id
  2237. _active_prints[(printer_id, subtask_name)] = archive.id
  2238. # Simulate print completion data
  2239. data = {
  2240. "status": "completed",
  2241. "filename": filename,
  2242. "subtask_name": subtask_name,
  2243. "timelapse_was_active": False,
  2244. }
  2245. logger.info("Simulating print complete for printer %s, archive %s", printer_id, archive.id)
  2246. # Call the actual on_print_complete handler
  2247. await on_print_complete(printer_id, data)
  2248. return {"success": True, "archive_id": archive.id, "message": "Print completion simulated"}
  2249. # =============================================================================
  2250. # Print Control Endpoints
  2251. # =============================================================================
  2252. @router.post("/{printer_id}/print/stop")
  2253. async def stop_print(
  2254. printer_id: int,
  2255. _=RequirePermissionIfAuthEnabled(Permission.PRINTERS_CONTROL),
  2256. db: AsyncSession = Depends(get_db),
  2257. ):
  2258. """Stop/cancel the current print job."""
  2259. result = await db.execute(select(Printer).where(Printer.id == printer_id))
  2260. printer = result.scalar_one_or_none()
  2261. if not printer:
  2262. raise HTTPException(404, "Printer not found")
  2263. client = printer_manager.get_client(printer_id)
  2264. if not client:
  2265. raise HTTPException(400, "Printer not connected")
  2266. success = client.stop_print()
  2267. if not success:
  2268. raise HTTPException(500, "Failed to stop print")
  2269. # Mark this printer as user-stopped so on_print_complete reclassifies
  2270. # the resulting "failed"/"aborted" MQTT status as "cancelled" — otherwise
  2271. # the HMS heuristic in _dispatch_archive_update mislabels user-cancels
  2272. # (e.g. the H2D's cancel-sequence module-0x0C HMS) as "Layer shift".
  2273. try:
  2274. from backend.app.main import mark_printer_stopped_by_user
  2275. mark_printer_stopped_by_user(printer_id)
  2276. except Exception as _mark_err:
  2277. logger.warning("Failed to mark printer %s as user-stopped: %s", printer_id, _mark_err)
  2278. return {"success": True, "message": "Print stop command sent"}
  2279. @router.post("/{printer_id}/clear-plate")
  2280. async def clear_plate(
  2281. printer_id: int,
  2282. _=RequirePermissionIfAuthEnabled(Permission.PRINTERS_CLEAR_PLATE),
  2283. db: AsyncSession = Depends(get_db),
  2284. ):
  2285. """Acknowledge that the build plate has been cleared after a finished/failed print.
  2286. Sets a plate-cleared flag so the scheduler can start the next queued print.
  2287. No MQTT command is sent to the printer — the scheduler's start_print command
  2288. will override the FINISH/FAILED state when it sends the next job.
  2289. """
  2290. result = await db.execute(select(Printer).where(Printer.id == printer_id))
  2291. printer = result.scalar_one_or_none()
  2292. if not printer:
  2293. raise HTTPException(404, "Printer not found")
  2294. if not printer_manager.is_connected(printer_id):
  2295. raise HTTPException(400, "Printer not connected")
  2296. # Accept the acknowledgment whenever the printer is awaiting it — not only when the
  2297. # reported state is FINISH/FAILED. After a power cycle the printer boots into IDLE
  2298. # but the awaiting flag persists, and the user still needs a way to ack it (#961).
  2299. state = printer_manager.get_status(printer_id)
  2300. awaiting = printer_manager.is_awaiting_plate_clear(printer_id)
  2301. if not awaiting and (not state or state.state not in ("FINISH", "FAILED")):
  2302. raise HTTPException(
  2303. 400,
  2304. f"Printer is not awaiting plate-clear acknowledgment (state={state.state if state else 'unknown'})",
  2305. )
  2306. printer_manager.set_awaiting_plate_clear(printer_id, False)
  2307. return {"success": True, "message": "Plate cleared, next print will start shortly"}
  2308. @router.post("/{printer_id}/print/pause")
  2309. async def pause_print(
  2310. printer_id: int,
  2311. _=RequirePermissionIfAuthEnabled(Permission.PRINTERS_CONTROL),
  2312. db: AsyncSession = Depends(get_db),
  2313. ):
  2314. """Pause the current print job."""
  2315. result = await db.execute(select(Printer).where(Printer.id == printer_id))
  2316. printer = result.scalar_one_or_none()
  2317. if not printer:
  2318. raise HTTPException(404, "Printer not found")
  2319. client = printer_manager.get_client(printer_id)
  2320. if not client:
  2321. raise HTTPException(400, "Printer not connected")
  2322. success = client.pause_print()
  2323. if not success:
  2324. raise HTTPException(500, "Failed to pause print")
  2325. return {"success": True, "message": "Print pause command sent"}
  2326. @router.post("/{printer_id}/print/resume")
  2327. async def resume_print(
  2328. printer_id: int,
  2329. _=RequirePermissionIfAuthEnabled(Permission.PRINTERS_CONTROL),
  2330. db: AsyncSession = Depends(get_db),
  2331. ):
  2332. """Resume a paused print job."""
  2333. result = await db.execute(select(Printer).where(Printer.id == printer_id))
  2334. printer = result.scalar_one_or_none()
  2335. if not printer:
  2336. raise HTTPException(404, "Printer not found")
  2337. client = printer_manager.get_client(printer_id)
  2338. if not client:
  2339. raise HTTPException(400, "Printer not connected")
  2340. success = client.resume_print()
  2341. if not success:
  2342. raise HTTPException(500, "Failed to resume print")
  2343. return {"success": True, "message": "Print resume command sent"}
  2344. @router.post("/{printer_id}/print-speed")
  2345. async def set_print_speed(
  2346. printer_id: int,
  2347. mode: int = Query(..., description="Speed mode (1=silent, 2=standard, 3=sport, 4=ludicrous)"),
  2348. _=RequirePermissionIfAuthEnabled(Permission.PRINTERS_CONTROL),
  2349. db: AsyncSession = Depends(get_db),
  2350. ):
  2351. """Set the print speed mode."""
  2352. result = await db.execute(select(Printer).where(Printer.id == printer_id))
  2353. printer = result.scalar_one_or_none()
  2354. if not printer:
  2355. raise HTTPException(404, "Printer not found")
  2356. client = printer_manager.get_client(printer_id)
  2357. if not client:
  2358. raise HTTPException(400, "Printer not connected")
  2359. success = client.set_print_speed(mode)
  2360. if not success:
  2361. raise HTTPException(500, "Failed to set print speed")
  2362. speed_names = {1: "Silent", 2: "Standard", 3: "Sport", 4: "Ludicrous"}
  2363. return {"success": True, "message": f"Print speed set to {speed_names.get(mode, 'Unknown')}"}
  2364. @router.post("/{printer_id}/airduct-mode")
  2365. async def set_airduct_mode(
  2366. printer_id: int,
  2367. mode: str = Query(..., description="Airduct mode: 'cooling' or 'heating'"),
  2368. _=RequirePermissionIfAuthEnabled(Permission.PRINTERS_CONTROL),
  2369. db: AsyncSession = Depends(get_db),
  2370. ):
  2371. """Set the airduct mode (cooling/heating) on supported printers (P2S/H2*)."""
  2372. if mode not in ("cooling", "heating"):
  2373. raise HTTPException(400, "Mode must be 'cooling' or 'heating'")
  2374. result = await db.execute(select(Printer).where(Printer.id == printer_id))
  2375. printer = result.scalar_one_or_none()
  2376. if not printer:
  2377. raise HTTPException(404, "Printer not found")
  2378. client = printer_manager.get_client(printer_id)
  2379. if not client:
  2380. raise HTTPException(400, "Printer not connected")
  2381. success = client.set_airduct_mode(mode)
  2382. if not success:
  2383. raise HTTPException(500, "Failed to set airduct mode")
  2384. return {"success": True, "message": f"Airduct mode set to {mode}"}
  2385. @router.post("/{printer_id}/chamber-light")
  2386. async def set_chamber_light(
  2387. printer_id: int,
  2388. on: bool = Query(..., description="True to turn on, False to turn off"),
  2389. _=RequirePermissionIfAuthEnabled(Permission.PRINTERS_CONTROL),
  2390. db: AsyncSession = Depends(get_db),
  2391. ):
  2392. """Turn the chamber light on or off."""
  2393. result = await db.execute(select(Printer).where(Printer.id == printer_id))
  2394. printer = result.scalar_one_or_none()
  2395. if not printer:
  2396. raise HTTPException(404, "Printer not found")
  2397. client = printer_manager.get_client(printer_id)
  2398. if not client:
  2399. raise HTTPException(400, "Printer not connected")
  2400. success = client.set_chamber_light(on)
  2401. if not success:
  2402. raise HTTPException(500, "Failed to control chamber light")
  2403. return {"success": True, "message": f"Chamber light {'on' if on else 'off'}"}
  2404. @router.post("/{printer_id}/bed-jog")
  2405. async def bed_jog(
  2406. printer_id: int,
  2407. distance: float = Query(
  2408. ...,
  2409. description=(
  2410. "Signed nozzle-bed gap adjustment in mm. Negative = decrease gap "
  2411. '("up" arrow in the UI: bed up on bed-on-Z models, toolhead down '
  2412. "on A1 bed-slingers). Positive = increase gap. The backend "
  2413. "translates this into the right G-code Z sign per printer model."
  2414. ),
  2415. ),
  2416. force: bool = Query(False, description="If true, bypass soft endstops via M211 (for use when Z is not homed)"),
  2417. _=RequirePermissionIfAuthEnabled(Permission.PRINTERS_CONTROL),
  2418. db: AsyncSession = Depends(get_db),
  2419. ):
  2420. """Adjust the nozzle-bed gap by a relative distance.
  2421. Emits a short G-code sequence via MQTT. When ``force`` is true the soft
  2422. endstops are disabled for the duration of the move, matching the
  2423. "ignore and move anyway" option Bambu Studio offers when the printer
  2424. is not homed.
  2425. Direction handling: on bed-on-Z printers (X1 / P1 / H2 family) the bed
  2426. is the Z-axis, and Bambu's home convention puts Z=0 at the top with
  2427. Z+ moving the bed down — so a frontend "Up" (decrease gap) maps
  2428. naturally to ``G1 Z-``. On bed-slingers (A1 / A1 Mini) the Z-axis is
  2429. the *toolhead*, and ``G1 Z-`` instead drives the nozzle DOWN into the
  2430. bed (#1334 reported exactly that crash). For those models we invert
  2431. the sign before emitting the G-code, so the UI semantics stay the
  2432. same regardless of which part physically moves.
  2433. """
  2434. if distance == 0 or abs(distance) > 200:
  2435. raise HTTPException(400, "Distance must be non-zero and ≤ 200 mm")
  2436. result = await db.execute(select(Printer).where(Printer.id == printer_id))
  2437. printer = result.scalar_one_or_none()
  2438. if not printer:
  2439. raise HTTPException(404, "Printer not found")
  2440. client = printer_manager.get_client(printer_id)
  2441. if not client:
  2442. raise HTTPException(400, "Printer not connected")
  2443. from backend.app.services.printer_manager import is_bed_slinger
  2444. gcode_distance = -distance if is_bed_slinger(printer.model) else distance
  2445. lines = []
  2446. if force:
  2447. lines.append("M211 S0")
  2448. lines += ["G91", f"G1 Z{gcode_distance:.2f} F600", "G90"]
  2449. if force:
  2450. lines.append("M211 S1")
  2451. if not client.send_gcode("\n".join(lines)):
  2452. raise HTTPException(500, "Failed to send bed-jog command")
  2453. return {"success": True, "message": f"Bed jog {distance:+.1f} mm sent"}
  2454. @router.post("/{printer_id}/home-axes")
  2455. async def home_axes(
  2456. printer_id: int,
  2457. axes: str = Query(
  2458. "all",
  2459. description="Legacy; accepted values are 'z' | 'xy' | 'all'. Always runs the printer's full auto-home sequence — see below.",
  2460. ),
  2461. _=RequirePermissionIfAuthEnabled(Permission.PRINTERS_CONTROL),
  2462. db: AsyncSession = Depends(get_db),
  2463. ):
  2464. """Run the printer's full auto-home sequence via bare `G28`.
  2465. Bambu printers (H2C / H2D / H2S / X1 family) home the Z axis by moving
  2466. the BED UP toward an endstop at the top of travel. If the toolhead is
  2467. not already parked out of the way, a bare `G28 Z` will crash the bed
  2468. into the toolhead — #1052 reported exactly that on H2C: the bed rose
  2469. without stopping at a safe height because `G28 Z` skipped the
  2470. toolhead-park step that a full `G28` runs first.
  2471. The endpoint therefore ignores the `axes` argument and always sends a
  2472. bare `G28`, which the firmware expands into a safe multi-step sequence
  2473. (park toolhead → home XY → home Z). The argument is kept only for
  2474. backward-compat with existing clients; sending an invalid value still
  2475. returns 400 so typos surface instead of silently proceeding.
  2476. """
  2477. axes = axes.lower()
  2478. if axes not in ("z", "xy", "all"):
  2479. raise HTTPException(400, "axes must be 'z', 'xy', or 'all'")
  2480. result = await db.execute(select(Printer).where(Printer.id == printer_id))
  2481. printer = result.scalar_one_or_none()
  2482. if not printer:
  2483. raise HTTPException(404, "Printer not found")
  2484. client = printer_manager.get_client(printer_id)
  2485. if not client:
  2486. raise HTTPException(400, "Printer not connected")
  2487. if not client.send_gcode("G28"):
  2488. raise HTTPException(500, "Failed to send home command")
  2489. return {"success": True, "message": "Full auto-home sequence sent"}
  2490. @router.post("/{printer_id}/hms/clear")
  2491. async def clear_hms_errors(
  2492. printer_id: int,
  2493. _=RequirePermissionIfAuthEnabled(Permission.PRINTERS_CONTROL),
  2494. db: AsyncSession = Depends(get_db),
  2495. ):
  2496. """Clear HMS/print errors on the printer."""
  2497. result = await db.execute(select(Printer).where(Printer.id == printer_id))
  2498. printer = result.scalar_one_or_none()
  2499. if not printer:
  2500. raise HTTPException(404, "Printer not found")
  2501. client = printer_manager.get_client(printer_id)
  2502. if not client:
  2503. raise HTTPException(400, "Printer not connected")
  2504. success = client.clear_hms_errors()
  2505. if not success:
  2506. raise HTTPException(500, "Failed to clear HMS errors")
  2507. return {"success": True, "message": "HMS errors cleared"}
  2508. @router.get("/{printer_id}/print/objects")
  2509. async def get_printable_objects(
  2510. printer_id: int,
  2511. reload: bool = False,
  2512. _=RequirePermissionIfAuthEnabled(Permission.PRINTERS_READ),
  2513. db: AsyncSession = Depends(get_db),
  2514. ):
  2515. """Get the list of printable objects for the current print.
  2516. Returns a list of objects with id, name, position (if available), and skip status.
  2517. Objects that have already been skipped are marked in the skipped_objects list.
  2518. Args:
  2519. reload: If True, reload objects from the archive file (useful after restart)
  2520. """
  2521. result = await db.execute(select(Printer).where(Printer.id == printer_id))
  2522. printer = result.scalar_one_or_none()
  2523. if not printer:
  2524. raise HTTPException(404, "Printer not found")
  2525. client = printer_manager.get_client(printer_id)
  2526. if not client:
  2527. raise HTTPException(400, "Printer not connected")
  2528. # Reload objects from 3MF if requested or no objects loaded
  2529. if reload or not client.state.printable_objects:
  2530. subtask_name = client.state.subtask_name
  2531. if subtask_name:
  2532. from backend.app.services.archive import extract_printable_objects_from_3mf
  2533. from backend.app.services.bambu_ftp import download_file_try_paths_async
  2534. # Build possible 3MF filenames (try both .gcode.3mf and .3mf)
  2535. possible_filenames = []
  2536. if subtask_name.endswith(".3mf"):
  2537. possible_filenames.append(subtask_name)
  2538. else:
  2539. possible_filenames.append(f"{subtask_name}.gcode.3mf")
  2540. possible_filenames.append(f"{subtask_name}.3mf")
  2541. # Also try with spaces converted to underscores (Bambu Studio may normalize filenames)
  2542. if " " in subtask_name:
  2543. normalized = subtask_name.replace(" ", "_")
  2544. if normalized.endswith(".3mf"):
  2545. possible_filenames.append(normalized)
  2546. else:
  2547. possible_filenames.append(f"{normalized}.gcode.3mf")
  2548. possible_filenames.append(f"{normalized}.3mf")
  2549. # Download 3MF from printer
  2550. temp_path = settings.archive_dir / "temp" / f"objects_{printer_id}_{possible_filenames[0]}"
  2551. temp_path.parent.mkdir(parents=True, exist_ok=True)
  2552. # Build list of all remote paths to try
  2553. remote_paths = []
  2554. for filename in possible_filenames:
  2555. remote_paths.extend([f"/{filename}", f"/cache/{filename}", f"/model/{filename}"])
  2556. try:
  2557. downloaded = await download_file_try_paths_async(
  2558. printer.ip_address,
  2559. printer.access_code,
  2560. remote_paths,
  2561. temp_path,
  2562. printer_model=printer.model,
  2563. )
  2564. if downloaded and temp_path.exists():
  2565. with open(temp_path, "rb") as f:
  2566. data = f.read()
  2567. objects, bbox_all = extract_printable_objects_from_3mf(data, include_positions=True)
  2568. if objects:
  2569. client.state.printable_objects = objects
  2570. client.state.printable_objects_bbox_all = bbox_all
  2571. logger.info("Reloaded %s objects for printer %s", len(objects), printer_id)
  2572. except Exception as e:
  2573. logger.debug("Failed to reload objects from printer: %s", e)
  2574. finally:
  2575. if temp_path.exists():
  2576. temp_path.unlink()
  2577. # Return objects with their skip status and position data
  2578. objects = []
  2579. for obj_id, obj_data in client.state.printable_objects.items():
  2580. # Handle both old format (string name) and new format (dict with name, x, y)
  2581. if isinstance(obj_data, dict):
  2582. obj_entry = {
  2583. "id": obj_id,
  2584. "name": obj_data.get("name", f"Object {obj_id}"),
  2585. "x": obj_data.get("x"),
  2586. "y": obj_data.get("y"),
  2587. "skipped": obj_id in client.state.skipped_objects,
  2588. }
  2589. else:
  2590. # Legacy format: obj_data is just the name string
  2591. obj_entry = {
  2592. "id": obj_id,
  2593. "name": obj_data,
  2594. "x": None,
  2595. "y": None,
  2596. "skipped": obj_id in client.state.skipped_objects,
  2597. }
  2598. objects.append(obj_entry)
  2599. return {
  2600. "objects": objects,
  2601. "total": len(objects),
  2602. "skipped_count": len(client.state.skipped_objects),
  2603. "is_printing": client.state.state in ("RUNNING", "PAUSE"),
  2604. "bbox_all": getattr(client.state, "printable_objects_bbox_all", None),
  2605. }
  2606. @router.post("/{printer_id}/print/skip-objects")
  2607. async def skip_objects(
  2608. printer_id: int,
  2609. object_ids: list[int],
  2610. _=RequirePermissionIfAuthEnabled(Permission.PRINTERS_CONTROL),
  2611. db: AsyncSession = Depends(get_db),
  2612. ):
  2613. """Skip specific objects during the current print.
  2614. Args:
  2615. object_ids: List of object identify_id values to skip
  2616. """
  2617. result = await db.execute(select(Printer).where(Printer.id == printer_id))
  2618. printer = result.scalar_one_or_none()
  2619. if not printer:
  2620. raise HTTPException(404, "Printer not found")
  2621. client = printer_manager.get_client(printer_id)
  2622. if not client:
  2623. raise HTTPException(400, "Printer not connected")
  2624. if not object_ids:
  2625. raise HTTPException(400, "No object IDs provided")
  2626. # Validate object IDs exist in printable_objects
  2627. invalid_ids = [oid for oid in object_ids if oid not in client.state.printable_objects]
  2628. if invalid_ids:
  2629. raise HTTPException(400, f"Invalid object IDs: {invalid_ids}")
  2630. success = client.skip_objects(object_ids)
  2631. if not success:
  2632. raise HTTPException(500, "Failed to skip objects")
  2633. # Get names of skipped objects for response (handle both old and new format)
  2634. skipped_names = []
  2635. for oid in object_ids:
  2636. obj_data = client.state.printable_objects.get(oid, str(oid))
  2637. if isinstance(obj_data, dict):
  2638. skipped_names.append(obj_data.get("name", str(oid)))
  2639. else:
  2640. skipped_names.append(obj_data)
  2641. return {
  2642. "success": True,
  2643. "message": f"Skipped {len(object_ids)} object(s): {', '.join(skipped_names)}",
  2644. "skipped_objects": object_ids,
  2645. }
  2646. # =============================================================================
  2647. # AMS Control Endpoints
  2648. # =============================================================================
  2649. @router.post("/{printer_id}/ams/{ams_id}/slot/{slot_id}/refresh")
  2650. async def refresh_ams_slot(
  2651. printer_id: int,
  2652. ams_id: int,
  2653. slot_id: int,
  2654. _=RequirePermissionIfAuthEnabled(Permission.PRINTERS_AMS_RFID),
  2655. db: AsyncSession = Depends(get_db),
  2656. ):
  2657. """Re-read RFID for an AMS slot (triggers filament info refresh)."""
  2658. result = await db.execute(select(Printer).where(Printer.id == printer_id))
  2659. printer = result.scalar_one_or_none()
  2660. if not printer:
  2661. raise HTTPException(404, "Printer not found")
  2662. client = printer_manager.get_client(printer_id)
  2663. if not client:
  2664. raise HTTPException(400, "Printer not connected")
  2665. success, message = client.ams_refresh_tray(ams_id, slot_id)
  2666. if not success:
  2667. raise HTTPException(400, message)
  2668. # Apply PA profile after delay (RFID re-read takes a few seconds)
  2669. asyncio.create_task(_apply_pa_after_refresh(printer_id, ams_id, slot_id))
  2670. return {"success": True, "message": message}
  2671. async def _apply_pa_after_refresh(printer_id: int, ams_id: int, slot_id: int):
  2672. """Apply PA profile after RFID re-read completes.
  2673. Waits for the printer to finish processing the RFID data, then selects
  2674. the K-profile via extrusion_cali_sel. Does NOT re-send ams_set_filament_setting
  2675. because that would overwrite the RFID-provided filament data.
  2676. """
  2677. await asyncio.sleep(5)
  2678. try:
  2679. from backend.app.api.routes.inventory import _find_tray_in_ams_data
  2680. from backend.app.core.database import async_session
  2681. from backend.app.models.spool import Spool
  2682. from backend.app.models.spool_assignment import SpoolAssignment as SA
  2683. from backend.app.models.spoolman_k_profile import SpoolmanKProfile
  2684. from backend.app.models.spoolman_slot_assignment import SpoolmanSlotAssignment
  2685. from backend.app.services.spool_tag_matcher import (
  2686. ZERO_TAG_UID,
  2687. ZERO_TRAY_UUID,
  2688. is_bambu_tag,
  2689. )
  2690. from backend.app.utils.tag_normalization import (
  2691. normalize_tag_uid,
  2692. normalize_tray_uuid,
  2693. )
  2694. client = printer_manager.get_client(printer_id)
  2695. if not client:
  2696. return
  2697. state = printer_manager.get_status(printer_id)
  2698. if not state or not state.raw_data:
  2699. return
  2700. # Find current tray data (should have RFID data by now)
  2701. ams_data = state.raw_data.get("ams", {})
  2702. ams_list = (
  2703. ams_data.get("ams", []) if isinstance(ams_data, dict) else ams_data if isinstance(ams_data, list) else []
  2704. )
  2705. tray = _find_tray_in_ams_data(ams_list, ams_id, slot_id)
  2706. if not tray or not tray.get("tray_type"):
  2707. logger.debug("PA re-apply: no tray data for AMS%d-T%d", ams_id, slot_id)
  2708. return
  2709. tag_uid = tray.get("tag_uid", "")
  2710. tray_uuid = tray.get("tray_uuid", "")
  2711. tray_info_idx = tray.get("tray_info_idx", "")
  2712. if not is_bambu_tag(tag_uid, tray_uuid, tray_info_idx):
  2713. return
  2714. # Compute nozzle/extruder once — used by both local and Spoolman lookup.
  2715. nozzle_diameter = "0.4"
  2716. if state.nozzles:
  2717. nd = state.nozzles[0].nozzle_diameter
  2718. if nd:
  2719. nozzle_diameter = nd
  2720. slot_extruder = None
  2721. if state.ams_extruder_map:
  2722. if ams_id == 255:
  2723. # External slots: ext-L (tray 0) → extruder 1, ext-R (tray 1) → extruder 0
  2724. slot_extruder = 1 - slot_id
  2725. else:
  2726. slot_extruder = state.ams_extruder_map.get(str(ams_id))
  2727. # 3-stage K-profile cascade: local SpoolKProfile → Spoolman SpoolmanKProfile
  2728. # → live tray.cali_idx fallback. Pre-Phase-13 only handled the local path
  2729. # and exited silently if no SpoolKProfile match; Spoolman-assigned slots
  2730. # were ignored entirely and live cali_idx was never re-asserted.
  2731. matching_cali_idx: int | None = None
  2732. matching_filament_id: str = tray_info_idx
  2733. async with async_session() as db:
  2734. from sqlalchemy import or_, select as sa_select
  2735. from sqlalchemy.orm import selectinload
  2736. # Stage 1: local SpoolAssignment + SpoolKProfile match
  2737. result = await db.execute(
  2738. sa_select(SA)
  2739. .options(selectinload(SA.spool).selectinload(Spool.k_profiles))
  2740. .where(SA.printer_id == printer_id, SA.ams_id == ams_id, SA.tray_id == slot_id)
  2741. )
  2742. assignment = result.scalar_one_or_none()
  2743. spool: Spool | None = assignment.spool if assignment else None
  2744. # Stage 1b: tag-based fallback. The slot may have just been reset
  2745. # (SpoolAssignment row deleted) before the user triggered a re-read.
  2746. # The live tray already carries the spool's tray_uuid/tag_uid from
  2747. # the RFID re-read, but the SA row hasn't been re-created yet.
  2748. # Without this fallback we miss the stored SpoolKProfile and Stage 3
  2749. # ends up re-asserting whatever cali_idx the firmware reset to
  2750. # (typically the default profile).
  2751. if spool is None:
  2752. norm_uuid = normalize_tray_uuid(tray_uuid) if tray_uuid else ""
  2753. norm_tag = normalize_tag_uid(tag_uid) if tag_uid else ""
  2754. tag_filters = []
  2755. if norm_uuid and norm_uuid != ZERO_TRAY_UUID:
  2756. tag_filters.append(Spool.tray_uuid == norm_uuid)
  2757. if norm_tag and norm_tag != ZERO_TAG_UID:
  2758. tag_filters.append(Spool.tag_uid == norm_tag)
  2759. if tag_filters:
  2760. tag_lookup = await db.execute(
  2761. sa_select(Spool).options(selectinload(Spool.k_profiles)).where(or_(*tag_filters)).limit(1)
  2762. )
  2763. spool = tag_lookup.scalar_one_or_none()
  2764. if spool is not None:
  2765. logger.info(
  2766. "PA re-apply AMS%d-T%d: matched spool %d via tag fallback "
  2767. "(SpoolAssignment row missing, likely after slot reset)",
  2768. ams_id,
  2769. slot_id,
  2770. spool.id,
  2771. )
  2772. if spool is not None and spool.k_profiles:
  2773. # Prefer exact extruder match, fall back to extruder-agnostic kp
  2774. # for the same printer + nozzle. Hard-skipping on extruder
  2775. # mismatch made the cascade refuse perfectly valid stored
  2776. # profiles whenever the AMS-extruder mapping had shifted since
  2777. # calibration time, falling all the way through to Stage 3 and
  2778. # re-asserting the firmware default.
  2779. exact_kp = None
  2780. fallback_kp = None
  2781. for kp in spool.k_profiles:
  2782. if kp.printer_id != printer_id or kp.nozzle_diameter != nozzle_diameter or kp.cali_idx is None:
  2783. continue
  2784. if slot_extruder is not None and kp.extruder is not None and kp.extruder == slot_extruder:
  2785. exact_kp = kp
  2786. break
  2787. if fallback_kp is None:
  2788. fallback_kp = kp
  2789. chosen_kp = exact_kp or fallback_kp
  2790. if chosen_kp is not None:
  2791. matching_cali_idx = chosen_kp.cali_idx
  2792. # The filament_id in extrusion_cali_sel must match the preset
  2793. # under which the K-profile was calibrated. Prefer the spool's
  2794. # slicer_filament setting, falling back to the tray's RFID value.
  2795. matching_filament_id = spool.slicer_filament or tray_info_idx
  2796. # Stage 2: Spoolman SpoolmanSlotAssignment + SpoolmanKProfile match
  2797. # (only when no local spool was matched — local takes priority,
  2798. # including the tag-based fallback above)
  2799. if matching_cali_idx is None and spool is None:
  2800. sm_result = await db.execute(
  2801. sa_select(SpoolmanSlotAssignment).where(
  2802. SpoolmanSlotAssignment.printer_id == printer_id,
  2803. SpoolmanSlotAssignment.ams_id == ams_id,
  2804. SpoolmanSlotAssignment.tray_id == slot_id,
  2805. )
  2806. )
  2807. sm_assignment = sm_result.scalar_one_or_none()
  2808. if sm_assignment:
  2809. kp_result = await db.execute(
  2810. sa_select(SpoolmanKProfile).where(
  2811. SpoolmanKProfile.spoolman_spool_id == sm_assignment.spoolman_spool_id,
  2812. SpoolmanKProfile.printer_id == printer_id,
  2813. )
  2814. )
  2815. for kp in kp_result.scalars().all():
  2816. if kp.nozzle_diameter == nozzle_diameter:
  2817. if slot_extruder is not None and kp.extruder is not None and kp.extruder != slot_extruder:
  2818. continue
  2819. if kp.cali_idx is not None:
  2820. matching_cali_idx = kp.cali_idx
  2821. # Spoolman has no slicer_filament — use the tray's RFID value
  2822. matching_filament_id = tray_info_idx
  2823. break
  2824. # Stage 3: live tray.cali_idx fallback. Re-asserts the printer's current
  2825. # selection so the value sticks across the RFID re-read (otherwise some
  2826. # firmwares clear cali_idx back to -1 mid-cycle).
  2827. if matching_cali_idx is None:
  2828. live_cali_idx = tray.get("cali_idx")
  2829. if live_cali_idx is not None and live_cali_idx >= 0:
  2830. matching_cali_idx = live_cali_idx
  2831. if matching_cali_idx is None:
  2832. logger.debug(
  2833. "PA re-apply AMS%d-T%d: no stored or live cali_idx — skipping MQTT",
  2834. ams_id,
  2835. slot_id,
  2836. )
  2837. return
  2838. logger.info(
  2839. "PA re-apply AMS%d-T%d: cali_idx=%d, filament_id=%s",
  2840. ams_id,
  2841. slot_id,
  2842. matching_cali_idx,
  2843. matching_filament_id,
  2844. )
  2845. # NOTE: Do NOT send ams_set_filament_setting here — it tells the firmware
  2846. # "this is a manual config" which destroys the RFID-detected spool state
  2847. # (changes eye icon to pen icon in slicer).
  2848. client.extrusion_cali_sel(
  2849. ams_id=ams_id,
  2850. tray_id=slot_id,
  2851. cali_idx=matching_cali_idx,
  2852. filament_id=matching_filament_id,
  2853. nozzle_diameter=nozzle_diameter,
  2854. )
  2855. # NOTE: Do NOT send extrusion_cali_set here. extrusion_cali_sel already
  2856. # selected the correct profile by cali_idx. Sending extrusion_cali_set with
  2857. # the same cali_idx would MODIFY the existing profile's metadata (extruder_id,
  2858. # nozzle_id, name), corrupting it.
  2859. logger.info(
  2860. "Applied PA profile cali_idx=%d to printer %d AMS%d-T%d",
  2861. matching_cali_idx,
  2862. printer_id,
  2863. ams_id,
  2864. slot_id,
  2865. )
  2866. except Exception as e:
  2867. logger.warning("Failed to apply PA profile after RFID re-read: %s", e)
  2868. @router.post("/{printer_id}/ams/load")
  2869. async def ams_load(
  2870. printer_id: int,
  2871. tray_id: int = Query(..., description="Tray ID: 0-15 for AMS slots (ams_id*4+slot_id), 254 for external spool"),
  2872. _=RequirePermissionIfAuthEnabled(Permission.PRINTERS_CONTROL),
  2873. db: AsyncSession = Depends(get_db),
  2874. ):
  2875. """Load filament from a specific AMS slot or external spool.
  2876. Tray ID encoding (matches Bambu firmware convention):
  2877. - 0..15: AMS slot, computed as ams_id * 4 + slot_id
  2878. - 254: external spool (single-external printers, or Ext-L on dual-nozzle H2D)
  2879. - 255: Ext-R on dual-nozzle H2D
  2880. """
  2881. if tray_id not in range(16) and tray_id not in (254, 255):
  2882. raise HTTPException(400, "tray_id must be 0..15 (AMS slot), 254 (external / Ext-L), or 255 (Ext-R)")
  2883. result = await db.execute(select(Printer).where(Printer.id == printer_id))
  2884. printer = result.scalar_one_or_none()
  2885. if not printer:
  2886. raise HTTPException(404, "Printer not found")
  2887. client = printer_manager.get_client(printer_id)
  2888. if not client:
  2889. raise HTTPException(400, "Printer not connected")
  2890. success = client.ams_load_filament(tray_id)
  2891. if not success:
  2892. raise HTTPException(500, "Failed to send load command")
  2893. if tray_id == 254:
  2894. target = "external spool"
  2895. elif tray_id == 255:
  2896. target = "Ext-R"
  2897. else:
  2898. target = f"AMS {tray_id // 4} slot {tray_id % 4 + 1}"
  2899. return {"success": True, "message": f"Loading filament from {target}"}
  2900. @router.post("/{printer_id}/ams/unload")
  2901. async def ams_unload(
  2902. printer_id: int,
  2903. _=RequirePermissionIfAuthEnabled(Permission.PRINTERS_CONTROL),
  2904. db: AsyncSession = Depends(get_db),
  2905. ):
  2906. """Unload the currently loaded filament."""
  2907. result = await db.execute(select(Printer).where(Printer.id == printer_id))
  2908. printer = result.scalar_one_or_none()
  2909. if not printer:
  2910. raise HTTPException(404, "Printer not found")
  2911. client = printer_manager.get_client(printer_id)
  2912. if not client:
  2913. raise HTTPException(400, "Printer not connected")
  2914. success = client.ams_unload_filament()
  2915. if not success:
  2916. raise HTTPException(500, "Failed to send unload command")
  2917. return {"success": True, "message": "Unloading filament"}
  2918. @router.get("/{printer_id}/runtime-debug")
  2919. async def get_runtime_debug(
  2920. printer_id: int,
  2921. _=RequirePermissionIfAuthEnabled(Permission.PRINTERS_READ),
  2922. db: AsyncSession = Depends(get_db),
  2923. ):
  2924. """Debug endpoint: Get runtime tracking status for a printer."""
  2925. result = await db.execute(select(Printer).where(Printer.id == printer_id))
  2926. printer = result.scalar_one_or_none()
  2927. if not printer:
  2928. raise HTTPException(404, "Printer not found")
  2929. state = printer_manager.get_status(printer_id)
  2930. return {
  2931. "printer_name": printer.name,
  2932. "runtime_seconds": printer.runtime_seconds,
  2933. "runtime_hours": printer.runtime_seconds / 3600.0 if printer.runtime_seconds else 0,
  2934. "print_hours_offset": printer.print_hours_offset,
  2935. "total_hours": (printer.runtime_seconds / 3600.0 if printer.runtime_seconds else 0)
  2936. + (printer.print_hours_offset or 0),
  2937. "last_runtime_update": printer.last_runtime_update.isoformat() if printer.last_runtime_update else None,
  2938. "mqtt_state": {
  2939. "connected": state.connected if state else False,
  2940. "state": state.state if state else None,
  2941. "progress": state.progress if state else None,
  2942. "gcode_file": state.gcode_file if state else None,
  2943. }
  2944. if state
  2945. else None,
  2946. "is_active": printer.is_active,
  2947. }