client.ts 161 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980
  1. import type { ArchivePlatesResponse, LibraryFilePlatesResponse } from '../types/plates';
  2. const API_BASE = '/api/v1';
  3. // Auth token storage
  4. let authToken: string | null = localStorage.getItem('auth_token');
  5. export function setAuthToken(token: string | null) {
  6. authToken = token;
  7. if (token) {
  8. localStorage.setItem('auth_token', token);
  9. } else {
  10. localStorage.removeItem('auth_token');
  11. }
  12. }
  13. export function getAuthToken(): string | null {
  14. return authToken;
  15. }
  16. function parseContentDispositionFilename(header: string | null): string | null {
  17. if (!header) return null;
  18. // RFC 5987: filename*=utf-8''percent-encoded-name
  19. const rfc5987Match = header.match(/filename\*=(?:UTF-8|utf-8)''(.+?)(?:;|$)/);
  20. if (rfc5987Match) {
  21. try { return decodeURIComponent(rfc5987Match[1]); } catch { /* fall through */ }
  22. }
  23. // Standard: filename="name" or filename=name
  24. const standardMatch = header.match(/filename="?([^";\n]+)"?/);
  25. return standardMatch?.[1] || null;
  26. }
  27. async function request<T>(
  28. endpoint: string,
  29. options: RequestInit = {}
  30. ): Promise<T> {
  31. const headers: Record<string, string> = {
  32. 'Content-Type': 'application/json',
  33. ...options.headers as Record<string, string>,
  34. };
  35. // Add auth token if available
  36. if (authToken) {
  37. headers['Authorization'] = `Bearer ${authToken}`;
  38. }
  39. const response = await fetch(`${API_BASE}${endpoint}`, {
  40. ...options,
  41. cache: 'no-store', // Prevent browser caching of API responses
  42. headers,
  43. });
  44. if (!response.ok) {
  45. const error = await response.json().catch(() => ({}));
  46. const detail = error.detail;
  47. const message = typeof detail === 'string'
  48. ? detail
  49. : (detail ? JSON.stringify(detail) : `HTTP ${response.status}`);
  50. // Handle 401 Unauthorized - only clear token if it's actually invalid
  51. // Don't clear on "Authentication required" which might be a timing issue
  52. if (response.status === 401) {
  53. const invalidTokenMessages = [
  54. 'Could not validate credentials',
  55. 'Token has expired',
  56. 'User not found or inactive',
  57. 'Invalid API key',
  58. 'API key has expired',
  59. ];
  60. if (invalidTokenMessages.some(m => message.includes(m))) {
  61. setAuthToken(null);
  62. }
  63. }
  64. throw new Error(message);
  65. }
  66. // Handle empty responses (204 No Content, etc.)
  67. const contentLength = response.headers.get('content-length');
  68. if (response.status === 204 || contentLength === '0') {
  69. return undefined as T;
  70. }
  71. return await response.json();
  72. }
  73. // Printer types
  74. export interface Printer {
  75. id: number;
  76. name: string;
  77. serial_number: string;
  78. ip_address: string;
  79. access_code: string;
  80. model: string | null;
  81. location: string | null; // Group/location name
  82. nozzle_count: number; // 1 or 2, auto-detected from MQTT
  83. is_active: boolean;
  84. auto_archive: boolean;
  85. external_camera_url: string | null;
  86. external_camera_type: string | null; // "mjpeg", "rtsp", "snapshot"
  87. external_camera_enabled: boolean;
  88. plate_detection_enabled: boolean; // Check plate before print
  89. plate_detection_roi?: PlateDetectionROI; // ROI for plate detection
  90. created_at: string;
  91. updated_at: string;
  92. }
  93. export interface HMSError {
  94. code: string;
  95. attr: number; // Attribute value for constructing wiki URL
  96. module: number;
  97. severity: number; // 1=fatal, 2=serious, 3=common, 4=info
  98. }
  99. export interface AMSTray {
  100. id: number;
  101. tray_color: string | null;
  102. tray_type: string | null;
  103. tray_sub_brands: string | null; // Full name like "PLA Basic", "PETG HF"
  104. tray_id_name: string | null; // Bambu filament ID like "A00-Y2" (can decode to color)
  105. tray_info_idx: string | null; // Filament preset ID like "GFA00" - maps to cloud setting_id
  106. remain: number;
  107. k: number | null; // Pressure advance value (from tray or K-profile lookup)
  108. cali_idx: number | null; // Calibration index for K-profile lookup
  109. tag_uid: string | null; // RFID tag UID (any tag)
  110. tray_uuid: string | null; // Bambu Lab spool UUID (32-char hex, only valid for Bambu Lab spools)
  111. nozzle_temp_min: number | null; // Min nozzle temperature
  112. nozzle_temp_max: number | null; // Max nozzle temperature
  113. }
  114. export interface AMSUnit {
  115. id: number;
  116. humidity: number | null;
  117. temp: number | null;
  118. is_ams_ht: boolean; // True for AMS-HT (single spool), False for regular AMS (4 spools)
  119. tray: AMSTray[];
  120. serial_number: string; // AMS unit serial number (from MQTT sn field)
  121. sw_ver: string; // AMS firmware version (from get_version info.module ams/* entry)
  122. }
  123. export interface NozzleInfo {
  124. nozzle_type: string; // "stainless_steel" or "hardened_steel"
  125. nozzle_diameter: string; // e.g., "0.4"
  126. }
  127. export interface NozzleRackSlot {
  128. id: number;
  129. nozzle_type: string;
  130. nozzle_diameter: string;
  131. wear: number | null;
  132. stat: number | null; // Nozzle status (e.g. mounted/docked)
  133. max_temp: number;
  134. serial_number: string;
  135. filament_color: string; // RGBA hex ("00000000" = no filament)
  136. filament_id: string;
  137. filament_type: string; // Material type (e.g. "PLA", "PETG")
  138. }
  139. export interface PrintOptions {
  140. // Core AI detectors
  141. spaghetti_detector: boolean;
  142. print_halt: boolean;
  143. halt_print_sensitivity: string; // "low", "medium", "high" - spaghetti sensitivity
  144. first_layer_inspector: boolean;
  145. printing_monitor: boolean;
  146. buildplate_marker_detector: boolean;
  147. allow_skip_parts: boolean;
  148. // Additional AI detectors (decoded from cfg bitmask)
  149. nozzle_clumping_detector: boolean;
  150. nozzle_clumping_sensitivity: string; // "low", "medium", "high"
  151. pileup_detector: boolean;
  152. pileup_sensitivity: string; // "low", "medium", "high"
  153. airprint_detector: boolean;
  154. airprint_sensitivity: string; // "low", "medium", "high"
  155. auto_recovery_step_loss: boolean;
  156. filament_tangle_detect: boolean;
  157. }
  158. export interface PrinterStatus {
  159. id: number;
  160. name: string;
  161. connected: boolean;
  162. state: string | null;
  163. current_print: string | null;
  164. subtask_name: string | null;
  165. gcode_file: string | null;
  166. progress: number | null;
  167. remaining_time: number | null;
  168. layer_num: number | null;
  169. total_layers: number | null;
  170. temperatures: {
  171. bed?: number;
  172. bed_target?: number;
  173. bed_heating?: boolean; // Actual heater state from MQTT
  174. nozzle?: number;
  175. nozzle_target?: number;
  176. nozzle_heating?: boolean; // Actual heater state from MQTT
  177. nozzle_2?: number; // Second nozzle for H2 series (dual nozzle)
  178. nozzle_2_target?: number;
  179. nozzle_2_heating?: boolean; // Actual heater state from MQTT
  180. chamber?: number;
  181. chamber_target?: number;
  182. chamber_heating?: boolean; // Actual heater state from MQTT
  183. } | null;
  184. cover_url: string | null;
  185. hms_errors: HMSError[];
  186. ams: AMSUnit[];
  187. ams_exists: boolean;
  188. vt_tray: AMSTray[]; // Virtual tray / external spool(s)
  189. sdcard: boolean; // SD card inserted
  190. store_to_sdcard: boolean; // Store sent files on SD card
  191. timelapse: boolean; // Timelapse recording active
  192. ipcam: boolean; // Live view enabled
  193. wifi_signal: number | null; // WiFi signal strength in dBm
  194. wired_network: boolean; // Ethernet connection detected
  195. nozzles: NozzleInfo[]; // Nozzle hardware info (index 0=left/primary, 1=right)
  196. nozzle_rack: NozzleRackSlot[]; // H2C 6-nozzle tool-changer rack
  197. print_options: PrintOptions | null; // AI detection and print options
  198. // Calibration stage tracking
  199. stg_cur: number; // Current stage number (-1 = not calibrating)
  200. stg_cur_name: string | null; // Human-readable current stage name
  201. stg: number[]; // List of stage numbers in calibration sequence
  202. // Air conditioning mode (0=cooling, 1=heating)
  203. airduct_mode: number;
  204. // Print speed level (1=silent, 2=standard, 3=sport, 4=ludicrous)
  205. speed_level: number;
  206. // Chamber light on/off
  207. chamber_light: boolean;
  208. // Active extruder for dual nozzle (0=right, 1=left)
  209. active_extruder: number;
  210. // AMS mapping - which AMS is connected to which nozzle
  211. // Format: [ams_id_for_nozzle0, ams_id_for_nozzle1, ...] where -1 means no AMS
  212. ams_mapping: number[];
  213. // Per-AMS extruder mapping - extracted from each AMS unit's info field
  214. // Format: {ams_id: extruder_id} where extruder 0=right, 1=left
  215. // Note: JSON keys are always strings
  216. ams_extruder_map: Record<string, number>;
  217. // Currently loaded tray (global tray ID, 255 = no filament loaded, 254 = external spool)
  218. tray_now: number;
  219. // AMS status for filament change tracking (0=idle, 1=filament_change, 2=rfid_identifying, 3=assist, 4=calibration)
  220. ams_status_main: number;
  221. // AMS sub-status for filament change step (when main=1): 4=retraction, 6=load verification, 7=purge
  222. ams_status_sub: number;
  223. // mc_print_sub_stage - filament change step indicator used by OrcaSlicer/BambuStudio
  224. mc_print_sub_stage: number;
  225. // Timestamp of last AMS data update (for RFID refresh detection)
  226. last_ams_update: number;
  227. // Number of printable objects in current print (for skip objects feature)
  228. printable_objects_count: number;
  229. // Fan speeds (0-100 percentage, null if not available for this model)
  230. cooling_fan_speed: number | null; // Part cooling fan
  231. big_fan1_speed: number | null; // Auxiliary fan
  232. big_fan2_speed: number | null; // Chamber/exhaust fan
  233. heatbreak_fan_speed: number | null; // Hotend heatbreak fan
  234. firmware_version: string | null; // Firmware version from MQTT
  235. // Developer LAN mode: true = enabled, false = disabled, null = unknown
  236. developer_mode: boolean | null;
  237. // Queue: user has acknowledged plate is cleared for next queued print
  238. plate_cleared: boolean;
  239. }
  240. export interface PrinterCreate {
  241. name: string;
  242. serial_number: string;
  243. ip_address: string;
  244. access_code: string;
  245. model?: string;
  246. location?: string;
  247. auto_archive?: boolean;
  248. external_camera_url?: string | null;
  249. external_camera_type?: string | null;
  250. external_camera_enabled?: boolean;
  251. plate_detection_enabled?: boolean;
  252. plate_detection_roi?: PlateDetectionROI;
  253. }
  254. // Plate Detection
  255. export interface PlateDetectionROI {
  256. x: number; // X start % (0.0-1.0)
  257. y: number; // Y start % (0.0-1.0)
  258. w: number; // Width % (0.0-1.0)
  259. h: number; // Height % (0.0-1.0)
  260. }
  261. export interface PlateDetectionResult {
  262. is_empty: boolean;
  263. confidence: number;
  264. difference_percent: number;
  265. message: string;
  266. has_debug_image: boolean;
  267. debug_image_url?: string;
  268. needs_calibration: boolean;
  269. light_warning?: boolean;
  270. reference_count?: number;
  271. max_references?: number;
  272. roi?: PlateDetectionROI;
  273. }
  274. export interface PlateDetectionStatus {
  275. available: boolean;
  276. calibrated: boolean;
  277. reference_count: number;
  278. max_references: number;
  279. message: string;
  280. }
  281. export interface CalibrationResult {
  282. success: boolean;
  283. message: string;
  284. }
  285. export interface PlateReference {
  286. index: number;
  287. label: string;
  288. timestamp: string;
  289. has_image: boolean;
  290. thumbnail_url: string;
  291. }
  292. // Archive types
  293. export interface ArchiveDuplicate {
  294. id: number;
  295. print_name: string | null;
  296. created_at: string;
  297. match_type: 'exact' | 'similar'; // 'exact' = hash match, 'similar' = name match
  298. }
  299. export interface Archive {
  300. id: number;
  301. printer_id: number | null;
  302. project_id: number | null;
  303. project_name: string | null;
  304. filename: string;
  305. file_path: string;
  306. file_size: number;
  307. content_hash: string | null;
  308. thumbnail_path: string | null;
  309. timelapse_path: string | null;
  310. source_3mf_path: string | null;
  311. f3d_path: string | null;
  312. duplicates: ArchiveDuplicate[] | null;
  313. duplicate_count: number;
  314. object_count: number | null;
  315. print_name: string | null;
  316. print_time_seconds: number | null;
  317. actual_time_seconds: number | null; // Computed from started_at/completed_at
  318. time_accuracy: number | null; // Percentage: 100 = perfect, >100 = faster than estimated
  319. filament_used_grams: number | null;
  320. filament_type: string | null;
  321. filament_color: string | null;
  322. layer_height: number | null;
  323. total_layers: number | null;
  324. nozzle_diameter: number | null;
  325. bed_temperature: number | null;
  326. nozzle_temperature: number | null;
  327. sliced_for_model: string | null; // Printer model this file was sliced for
  328. status: string;
  329. started_at: string | null;
  330. completed_at: string | null;
  331. extra_data: Record<string, unknown> | null;
  332. makerworld_url: string | null;
  333. designer: string | null;
  334. external_url: string | null;
  335. is_favorite: boolean;
  336. tags: string | null;
  337. notes: string | null;
  338. cost: number | null;
  339. photos: string[] | null;
  340. failure_reason: string | null;
  341. quantity: number;
  342. energy_kwh: number | null;
  343. energy_cost: number | null;
  344. created_at: string;
  345. // User tracking (Issue #206)
  346. created_by_id: number | null;
  347. created_by_username: string | null;
  348. }
  349. export interface ArchiveSlim {
  350. printer_id: number | null;
  351. print_name: string | null;
  352. print_time_seconds: number | null;
  353. actual_time_seconds: number | null;
  354. filament_used_grams: number | null;
  355. filament_type: string | null;
  356. filament_color: string | null;
  357. status: string;
  358. started_at: string | null;
  359. completed_at: string | null;
  360. cost: number | null;
  361. quantity: number;
  362. created_at: string;
  363. }
  364. export interface PrintLogEntry {
  365. id: number;
  366. print_name: string | null;
  367. printer_name: string | null;
  368. printer_id: number | null;
  369. status: string;
  370. started_at: string | null;
  371. completed_at: string | null;
  372. duration_seconds: number | null;
  373. filament_type: string | null;
  374. filament_color: string | null;
  375. filament_used_grams: number | null;
  376. thumbnail_path: string | null;
  377. created_by_username: string | null;
  378. created_at: string;
  379. }
  380. export interface PrintLogResponse {
  381. items: PrintLogEntry[];
  382. total: number;
  383. }
  384. export interface ArchiveStats {
  385. total_prints: number;
  386. successful_prints: number;
  387. failed_prints: number;
  388. total_print_time_hours: number;
  389. total_filament_grams: number;
  390. total_cost: number;
  391. prints_by_filament_type: Record<string, number>;
  392. prints_by_printer: Record<string, number>;
  393. average_time_accuracy: number | null;
  394. time_accuracy_by_printer: Record<string, number> | null;
  395. total_energy_kwh: number;
  396. total_energy_cost: number;
  397. }
  398. export interface TagInfo {
  399. name: string;
  400. count: number;
  401. }
  402. export interface FailureAnalysis {
  403. period_days: number;
  404. total_prints: number;
  405. failed_prints: number;
  406. failure_rate: number;
  407. failures_by_reason: Record<string, number>;
  408. failures_by_filament: Record<string, number>;
  409. failures_by_printer: Record<string, number>;
  410. failures_by_hour: Record<number, number>;
  411. recent_failures: Array<{
  412. id: number;
  413. print_name: string;
  414. failure_reason: string | null;
  415. filament_type: string | null;
  416. printer_id: number | null;
  417. created_at: string | null;
  418. }>;
  419. trend: Array<{
  420. week_start: string;
  421. total_prints: number;
  422. failed_prints: number;
  423. failure_rate: number;
  424. }>;
  425. }
  426. export interface BulkUploadResult {
  427. uploaded: number;
  428. failed: number;
  429. results: Array<{ filename: string; id: number; status: string }>;
  430. errors: Array<{ filename: string; error: string }>;
  431. }
  432. // Archive Comparison types
  433. export interface ComparisonArchiveInfo {
  434. id: number;
  435. print_name: string;
  436. status: string;
  437. created_at: string | null;
  438. printer_id: number | null;
  439. project_name: string | null;
  440. }
  441. export interface ComparisonField {
  442. field: string;
  443. label: string;
  444. unit: string | null;
  445. values: (string | number | null)[];
  446. raw_values: (string | number | null)[];
  447. has_difference: boolean;
  448. }
  449. export interface SuccessCorrelationInsight {
  450. field: string;
  451. label: string;
  452. insight: string;
  453. success_avg?: number;
  454. failed_avg?: number;
  455. success_values?: string[];
  456. failed_values?: string[];
  457. }
  458. export interface SuccessCorrelation {
  459. has_both_outcomes: boolean;
  460. message?: string;
  461. successful_count?: number;
  462. failed_count?: number;
  463. insights?: SuccessCorrelationInsight[];
  464. }
  465. export interface ArchiveComparison {
  466. archives: ComparisonArchiveInfo[];
  467. comparison: ComparisonField[];
  468. differences: ComparisonField[];
  469. success_correlation: SuccessCorrelation;
  470. }
  471. export interface SimilarArchive {
  472. archive: {
  473. id: number;
  474. print_name: string;
  475. status: string;
  476. created_at: string | null;
  477. };
  478. match_reason: string;
  479. match_score: number;
  480. }
  481. // Project types
  482. export interface ProjectStats {
  483. total_archives: number;
  484. total_items: number; // Sum of quantities (total items printed)
  485. completed_prints: number; // Sum of quantities for completed prints (parts)
  486. failed_prints: number;
  487. queued_prints: number;
  488. in_progress_prints: number;
  489. total_print_time_hours: number;
  490. total_filament_grams: number;
  491. progress_percent: number | null; // Plates progress (total_archives / target_count)
  492. parts_progress_percent: number | null; // Parts progress (completed_prints / target_parts_count)
  493. estimated_cost: number;
  494. total_energy_kwh: number;
  495. total_energy_cost: number;
  496. remaining_prints: number | null; // Remaining plates
  497. remaining_parts: number | null; // Remaining parts
  498. bom_total_items: number;
  499. bom_completed_items: number;
  500. }
  501. export interface ProjectChildPreview {
  502. id: number;
  503. name: string;
  504. color: string | null;
  505. status: string;
  506. progress_percent: number | null;
  507. }
  508. export interface Project {
  509. id: number;
  510. name: string;
  511. description: string | null;
  512. color: string | null;
  513. status: string; // active, completed, archived
  514. target_count: number | null; // Target number of plates/print jobs
  515. target_parts_count: number | null; // Target number of parts/objects
  516. notes: string | null;
  517. attachments: ProjectAttachment[] | null;
  518. tags: string | null;
  519. due_date: string | null;
  520. priority: string; // low, normal, high, urgent
  521. budget: number | null;
  522. is_template: boolean;
  523. template_source_id: number | null;
  524. parent_id: number | null;
  525. parent_name: string | null;
  526. children: ProjectChildPreview[];
  527. created_at: string;
  528. updated_at: string;
  529. stats?: ProjectStats;
  530. }
  531. export interface ProjectAttachment {
  532. filename: string;
  533. original_name: string;
  534. size: number;
  535. uploaded_at: string;
  536. }
  537. export interface ArchivePreview {
  538. id: number;
  539. print_name: string | null;
  540. thumbnail_path: string | null;
  541. status: string;
  542. filament_type: string | null;
  543. filament_color: string | null;
  544. }
  545. export interface ProjectListItem {
  546. id: number;
  547. name: string;
  548. description: string | null;
  549. color: string | null;
  550. status: string;
  551. target_count: number | null; // Target number of plates/print jobs
  552. target_parts_count: number | null; // Target number of parts/objects
  553. created_at: string;
  554. archive_count: number; // Number of print jobs (plates)
  555. total_items: number; // Sum of quantities (total items printed, including failed)
  556. completed_count: number; // Sum of quantities for completed prints only (parts)
  557. failed_count: number; // Sum of quantities for failed prints
  558. queue_count: number;
  559. progress_percent: number | null; // Plates progress
  560. archives: ArchivePreview[];
  561. }
  562. export interface ProjectCreate {
  563. name: string;
  564. description?: string;
  565. color?: string;
  566. target_count?: number;
  567. target_parts_count?: number;
  568. notes?: string;
  569. tags?: string;
  570. due_date?: string;
  571. priority?: string;
  572. budget?: number;
  573. parent_id?: number;
  574. }
  575. export interface ProjectUpdate {
  576. name?: string;
  577. description?: string;
  578. color?: string;
  579. status?: string;
  580. target_count?: number;
  581. target_parts_count?: number;
  582. notes?: string;
  583. tags?: string;
  584. due_date?: string;
  585. priority?: string;
  586. budget?: number;
  587. parent_id?: number;
  588. }
  589. // BOM Types - Tracks sourced/purchased parts (hardware, electronics, etc.)
  590. export interface BOMItem {
  591. id: number;
  592. project_id: number;
  593. name: string;
  594. quantity_needed: number;
  595. quantity_acquired: number;
  596. unit_price: number | null;
  597. sourcing_url: string | null;
  598. archive_id: number | null;
  599. archive_name: string | null;
  600. stl_filename: string | null;
  601. remarks: string | null;
  602. sort_order: number;
  603. is_complete: boolean;
  604. created_at: string;
  605. updated_at: string;
  606. }
  607. export interface BOMItemCreate {
  608. name: string;
  609. quantity_needed?: number;
  610. unit_price?: number;
  611. sourcing_url?: string;
  612. archive_id?: number;
  613. stl_filename?: string;
  614. remarks?: string;
  615. }
  616. export interface BOMItemUpdate {
  617. name?: string;
  618. quantity_needed?: number;
  619. quantity_acquired?: number;
  620. unit_price?: number;
  621. sourcing_url?: string;
  622. archive_id?: number;
  623. stl_filename?: string;
  624. remarks?: string;
  625. }
  626. // Project Export/Import Types
  627. export interface BOMItemExport {
  628. name: string;
  629. quantity_needed: number;
  630. quantity_acquired: number;
  631. unit_price: number | null;
  632. sourcing_url: string | null;
  633. stl_filename: string | null;
  634. remarks: string | null;
  635. }
  636. export interface LinkedFolderExport {
  637. name: string;
  638. }
  639. export interface ProjectExport {
  640. name: string;
  641. description: string | null;
  642. color: string | null;
  643. status: string;
  644. target_count: number | null;
  645. target_parts_count: number | null;
  646. notes: string | null;
  647. tags: string | null;
  648. due_date: string | null;
  649. priority: string;
  650. budget: number | null;
  651. bom_items: BOMItemExport[];
  652. linked_folders: LinkedFolderExport[];
  653. }
  654. export interface ProjectImport {
  655. name: string;
  656. description?: string;
  657. color?: string;
  658. status?: string;
  659. target_count?: number;
  660. target_parts_count?: number;
  661. notes?: string;
  662. tags?: string;
  663. due_date?: string;
  664. priority?: string;
  665. budget?: number;
  666. bom_items?: BOMItemExport[];
  667. linked_folders?: LinkedFolderExport[];
  668. }
  669. // Timeline Types
  670. export interface TimelineEvent {
  671. event_type: string;
  672. timestamp: string;
  673. title: string;
  674. description: string | null;
  675. metadata: Record<string, unknown> | null;
  676. }
  677. // API Key types
  678. export interface APIKey {
  679. id: number;
  680. name: string;
  681. key_prefix: string;
  682. can_queue: boolean;
  683. can_control_printer: boolean;
  684. can_read_status: boolean;
  685. printer_ids: number[] | null;
  686. enabled: boolean;
  687. last_used: string | null;
  688. created_at: string;
  689. expires_at: string | null;
  690. }
  691. export interface APIKeyCreate {
  692. name: string;
  693. can_queue?: boolean;
  694. can_control_printer?: boolean;
  695. can_read_status?: boolean;
  696. printer_ids?: number[] | null;
  697. expires_at?: string | null;
  698. }
  699. export interface APIKeyCreateResponse extends APIKey {
  700. key: string; // Full key, only shown on creation
  701. }
  702. export interface APIKeyUpdate {
  703. name?: string;
  704. can_queue?: boolean;
  705. can_control_printer?: boolean;
  706. can_read_status?: boolean;
  707. printer_ids?: number[] | null;
  708. enabled?: boolean;
  709. expires_at?: string | null;
  710. }
  711. // Settings types
  712. export interface AppSettings {
  713. auto_archive: boolean;
  714. save_thumbnails: boolean;
  715. capture_finish_photo: boolean;
  716. default_filament_cost: number;
  717. currency: string;
  718. energy_cost_per_kwh: number;
  719. energy_tracking_mode: 'print' | 'total';
  720. check_updates: boolean;
  721. check_printer_firmware: boolean;
  722. include_beta_updates: boolean;
  723. language: string;
  724. notification_language: string;
  725. // AMS threshold settings
  726. ams_humidity_good: number; // <= this is green
  727. ams_humidity_fair: number; // <= this is orange, > is red
  728. ams_temp_good: number; // <= this is green/blue
  729. ams_temp_fair: number; // <= this is orange, > is red
  730. ams_history_retention_days: number; // days to keep AMS sensor history
  731. // Print modal settings
  732. per_printer_mapping_expanded: boolean; // Whether custom mapping is expanded by default in print modal
  733. // Date/time format settings
  734. date_format: 'system' | 'us' | 'eu' | 'iso';
  735. time_format: 'system' | '12h' | '24h';
  736. // Default printer
  737. default_printer_id: number | null;
  738. // Dark mode theme settings
  739. dark_style: 'classic' | 'glow' | 'vibrant';
  740. dark_background: 'neutral' | 'warm' | 'cool' | 'oled' | 'slate' | 'forest';
  741. dark_accent: 'green' | 'teal' | 'blue' | 'orange' | 'purple' | 'red';
  742. // Light mode theme settings
  743. light_style: 'classic' | 'glow' | 'vibrant';
  744. light_background: 'neutral' | 'warm' | 'cool';
  745. light_accent: 'green' | 'teal' | 'blue' | 'orange' | 'purple' | 'red';
  746. // FTP retry settings
  747. ftp_retry_enabled: boolean;
  748. ftp_retry_count: number;
  749. ftp_retry_delay: number;
  750. ftp_timeout: number;
  751. // MQTT relay settings
  752. mqtt_enabled: boolean;
  753. mqtt_broker: string;
  754. mqtt_port: number;
  755. mqtt_username: string;
  756. mqtt_password: string;
  757. mqtt_topic_prefix: string;
  758. mqtt_use_tls: boolean;
  759. // External URL for notifications
  760. external_url: string;
  761. // Home Assistant integration
  762. ha_enabled: boolean;
  763. ha_url: string;
  764. ha_token: string;
  765. ha_url_from_env: boolean;
  766. ha_token_from_env: boolean;
  767. ha_env_managed: boolean;
  768. // File Manager / Library settings
  769. library_archive_mode: 'always' | 'never' | 'ask';
  770. library_disk_warning_gb: number;
  771. // Camera view settings
  772. camera_view_mode: 'window' | 'embedded';
  773. // Preferred slicer
  774. preferred_slicer: 'bambu_studio' | 'orcaslicer';
  775. // Prometheus metrics
  776. prometheus_enabled: boolean;
  777. prometheus_token: string;
  778. // Bed cooled threshold
  779. bed_cooled_threshold: number;
  780. // Inventory low stock threshold
  781. low_stock_threshold: number;
  782. }
  783. export type AppSettingsUpdate = Partial<AppSettings>;
  784. // MQTT relay status
  785. export interface MQTTStatus {
  786. enabled: boolean;
  787. connected: boolean;
  788. broker: string;
  789. port: number;
  790. topic_prefix: string;
  791. }
  792. // Cloud types
  793. export interface CloudAuthStatus {
  794. is_authenticated: boolean;
  795. email: string | null;
  796. }
  797. export interface CloudLoginResponse {
  798. success: boolean;
  799. needs_verification: boolean;
  800. message: string;
  801. verification_type?: 'email' | 'totp' | null;
  802. tfa_key?: string | null;
  803. }
  804. export interface SlicerSetting {
  805. setting_id: string;
  806. name: string;
  807. type: string;
  808. version: string | null;
  809. user_id: string | null;
  810. updated_time: string | null;
  811. is_custom: boolean;
  812. }
  813. export interface SpoolCatalogEntry {
  814. id: number;
  815. name: string;
  816. weight: number;
  817. is_default: boolean;
  818. }
  819. export interface ColorCatalogEntry {
  820. id: number;
  821. manufacturer: string;
  822. color_name: string;
  823. hex_color: string;
  824. material: string | null;
  825. is_default: boolean;
  826. }
  827. export interface ColorLookupResult {
  828. found: boolean;
  829. hex_color: string | null;
  830. material: string | null;
  831. }
  832. export interface SlicerSettingsResponse {
  833. filament: SlicerSetting[];
  834. printer: SlicerSetting[];
  835. process: SlicerSetting[];
  836. }
  837. export interface SlicerSettingDetail {
  838. message?: string | null;
  839. code?: string | null;
  840. error?: string | null;
  841. public: boolean;
  842. version?: string | null;
  843. type: string;
  844. name: string;
  845. update_time?: string | null;
  846. nickname?: string | null;
  847. base_id?: string | null;
  848. setting: Record<string, unknown>;
  849. filament_id?: string | null;
  850. setting_id?: string | null;
  851. }
  852. export interface SlicerSettingCreate {
  853. type: string; // 'filament', 'print', or 'printer'
  854. name: string;
  855. base_id: string;
  856. setting: Record<string, unknown>;
  857. }
  858. export interface SlicerSettingUpdate {
  859. name?: string;
  860. setting?: Record<string, unknown>;
  861. }
  862. export interface SlicerSettingDeleteResponse {
  863. success: boolean;
  864. message: string;
  865. }
  866. // Built-in filament fallback (static table from backend)
  867. export interface BuiltinFilament {
  868. filament_id: string;
  869. name: string;
  870. }
  871. // Local preset types (OrcaSlicer imports)
  872. export interface LocalPreset {
  873. id: number;
  874. name: string;
  875. preset_type: string;
  876. source: string;
  877. filament_type: string | null;
  878. filament_vendor: string | null;
  879. nozzle_temp_min: number | null;
  880. nozzle_temp_max: number | null;
  881. pressure_advance: string | null;
  882. default_filament_colour: string | null;
  883. filament_cost: string | null;
  884. filament_density: string | null;
  885. compatible_printers: string | null;
  886. inherits: string | null;
  887. version: string | null;
  888. created_at: string;
  889. updated_at: string;
  890. }
  891. export interface LocalPresetDetail extends LocalPreset {
  892. setting: Record<string, unknown>;
  893. }
  894. export interface LocalPresetsResponse {
  895. filament: LocalPreset[];
  896. printer: LocalPreset[];
  897. process: LocalPreset[];
  898. }
  899. export interface ImportResponse {
  900. success: boolean;
  901. imported: number;
  902. skipped: number;
  903. errors: string[];
  904. }
  905. export interface FieldOption {
  906. value: string;
  907. label: string;
  908. }
  909. export interface FieldDefinition {
  910. key: string;
  911. label: string;
  912. type: 'text' | 'number' | 'boolean' | 'select';
  913. category: string;
  914. description?: string;
  915. options?: FieldOption[];
  916. unit?: string;
  917. min?: number;
  918. max?: number;
  919. step?: number;
  920. }
  921. export interface FieldDefinitionsResponse {
  922. version: string;
  923. description: string;
  924. fields: FieldDefinition[];
  925. }
  926. export interface CloudDevice {
  927. dev_id: string;
  928. name: string;
  929. dev_model_name: string | null;
  930. dev_product_name: string | null;
  931. online: boolean;
  932. }
  933. // Smart Plug types
  934. export interface SmartPlug {
  935. id: number;
  936. name: string;
  937. plug_type: 'tasmota' | 'homeassistant' | 'mqtt';
  938. ip_address: string | null; // Required for Tasmota
  939. ha_entity_id: string | null; // Required for Home Assistant (e.g., "switch.printer_plug", "script.turn_on_printer")
  940. // Home Assistant energy sensor entities (optional)
  941. ha_power_entity: string | null;
  942. ha_energy_today_entity: string | null;
  943. ha_energy_total_entity: string | null;
  944. // MQTT fields (required when plug_type="mqtt")
  945. // Legacy field - kept for backward compatibility
  946. mqtt_topic: string | null; // Deprecated, use mqtt_power_topic
  947. mqtt_multiplier: number; // Deprecated, use mqtt_power_multiplier
  948. // Power monitoring
  949. mqtt_power_topic: string | null; // Topic for power data
  950. mqtt_power_path: string | null; // e.g., "power_l1" or "data.power"
  951. mqtt_power_multiplier: number; // Unit conversion for power
  952. // Energy monitoring
  953. mqtt_energy_topic: string | null; // Topic for energy data
  954. mqtt_energy_path: string | null; // e.g., "energy_l1"
  955. mqtt_energy_multiplier: number; // Unit conversion for energy
  956. // State monitoring
  957. mqtt_state_topic: string | null; // Topic for state data
  958. mqtt_state_path: string | null; // e.g., "state_l1" for ON/OFF
  959. mqtt_state_on_value: string | null; // What value means "ON" (e.g., "ON", "true", "1")
  960. printer_id: number | null;
  961. enabled: boolean;
  962. auto_on: boolean;
  963. auto_off: boolean;
  964. off_delay_mode: 'time' | 'temperature';
  965. off_delay_minutes: number;
  966. off_temp_threshold: number;
  967. username: string | null;
  968. password: string | null;
  969. // Power alerts
  970. power_alert_enabled: boolean;
  971. power_alert_high: number | null;
  972. power_alert_low: number | null;
  973. power_alert_last_triggered: string | null;
  974. // Schedule
  975. schedule_enabled: boolean;
  976. schedule_on_time: string | null;
  977. schedule_off_time: string | null;
  978. // Visibility options
  979. show_in_switchbar: boolean;
  980. show_on_printer_card: boolean; // For scripts: show on printer card
  981. // Status
  982. last_state: string | null;
  983. last_checked: string | null;
  984. auto_off_executed: boolean; // True when auto-off was triggered after print
  985. created_at: string;
  986. updated_at: string;
  987. }
  988. export interface SmartPlugCreate {
  989. name: string;
  990. plug_type?: 'tasmota' | 'homeassistant' | 'mqtt';
  991. ip_address?: string | null; // Required for Tasmota
  992. ha_entity_id?: string | null; // Required for Home Assistant
  993. // Home Assistant energy sensor entities (optional)
  994. ha_power_entity?: string | null;
  995. ha_energy_today_entity?: string | null;
  996. ha_energy_total_entity?: string | null;
  997. // MQTT fields (required when plug_type="mqtt")
  998. // Legacy fields - kept for backward compatibility
  999. mqtt_topic?: string | null;
  1000. mqtt_multiplier?: number;
  1001. // Power monitoring
  1002. mqtt_power_topic?: string | null;
  1003. mqtt_power_path?: string | null;
  1004. mqtt_power_multiplier?: number;
  1005. // Energy monitoring
  1006. mqtt_energy_topic?: string | null;
  1007. mqtt_energy_path?: string | null;
  1008. mqtt_energy_multiplier?: number;
  1009. // State monitoring
  1010. mqtt_state_topic?: string | null;
  1011. mqtt_state_path?: string | null;
  1012. mqtt_state_on_value?: string | null;
  1013. printer_id?: number | null;
  1014. enabled?: boolean;
  1015. auto_on?: boolean;
  1016. auto_off?: boolean;
  1017. off_delay_mode?: 'time' | 'temperature';
  1018. off_delay_minutes?: number;
  1019. off_temp_threshold?: number;
  1020. username?: string | null;
  1021. password?: string | null;
  1022. // Power alerts
  1023. power_alert_enabled?: boolean;
  1024. power_alert_high?: number | null;
  1025. power_alert_low?: number | null;
  1026. // Schedule
  1027. schedule_enabled?: boolean;
  1028. schedule_on_time?: string | null;
  1029. schedule_off_time?: string | null;
  1030. // Visibility options
  1031. show_in_switchbar?: boolean;
  1032. show_on_printer_card?: boolean;
  1033. }
  1034. export interface SmartPlugUpdate {
  1035. name?: string;
  1036. plug_type?: 'tasmota' | 'homeassistant' | 'mqtt';
  1037. ip_address?: string | null;
  1038. ha_entity_id?: string | null;
  1039. // Home Assistant energy sensor entities (optional)
  1040. ha_power_entity?: string | null;
  1041. ha_energy_today_entity?: string | null;
  1042. ha_energy_total_entity?: string | null;
  1043. // MQTT fields (legacy)
  1044. mqtt_topic?: string | null;
  1045. mqtt_multiplier?: number;
  1046. // MQTT power fields
  1047. mqtt_power_topic?: string | null;
  1048. mqtt_power_path?: string | null;
  1049. mqtt_power_multiplier?: number;
  1050. // MQTT energy fields
  1051. mqtt_energy_topic?: string | null;
  1052. mqtt_energy_path?: string | null;
  1053. mqtt_energy_multiplier?: number;
  1054. // MQTT state fields
  1055. mqtt_state_topic?: string | null;
  1056. mqtt_state_path?: string | null;
  1057. mqtt_state_on_value?: string | null;
  1058. printer_id?: number | null;
  1059. enabled?: boolean;
  1060. auto_on?: boolean;
  1061. auto_off?: boolean;
  1062. off_delay_mode?: 'time' | 'temperature';
  1063. off_delay_minutes?: number;
  1064. off_temp_threshold?: number;
  1065. username?: string | null;
  1066. password?: string | null;
  1067. // Power alerts
  1068. power_alert_enabled?: boolean;
  1069. power_alert_high?: number | null;
  1070. power_alert_low?: number | null;
  1071. // Schedule
  1072. schedule_enabled?: boolean;
  1073. schedule_on_time?: string | null;
  1074. schedule_off_time?: string | null;
  1075. // Visibility options
  1076. show_in_switchbar?: boolean;
  1077. show_on_printer_card?: boolean;
  1078. }
  1079. // Home Assistant entity for smart plug selection
  1080. export interface HAEntity {
  1081. entity_id: string;
  1082. friendly_name: string;
  1083. state: string | null;
  1084. domain: string; // "switch", "light", "input_boolean", "script"
  1085. }
  1086. // Home Assistant sensor entity for energy monitoring
  1087. export interface HASensorEntity {
  1088. entity_id: string;
  1089. friendly_name: string;
  1090. state: string | null;
  1091. unit_of_measurement: string | null; // "W", "kW", "kWh", "Wh"
  1092. }
  1093. export interface HATestConnectionResult {
  1094. success: boolean;
  1095. message: string | null;
  1096. error: string | null;
  1097. }
  1098. export interface SmartPlugEnergy {
  1099. power: number | null; // Current watts
  1100. voltage: number | null; // Volts
  1101. current: number | null; // Amps
  1102. today: number | null; // kWh used today
  1103. yesterday: number | null; // kWh used yesterday
  1104. total: number | null; // Total kWh
  1105. factor: number | null; // Power factor (0-1)
  1106. apparent_power: number | null; // VA
  1107. reactive_power: number | null; // VAr
  1108. }
  1109. export interface SmartPlugStatus {
  1110. state: string | null;
  1111. reachable: boolean;
  1112. device_name: string | null;
  1113. energy: SmartPlugEnergy | null;
  1114. }
  1115. export interface SmartPlugTestResult {
  1116. success: boolean;
  1117. state: string | null;
  1118. device_name: string | null;
  1119. }
  1120. // Tasmota Discovery types
  1121. export interface TasmotaScanStatus {
  1122. running: boolean;
  1123. scanned: number;
  1124. total: number;
  1125. }
  1126. export interface DiscoveredTasmotaDevice {
  1127. ip_address: string;
  1128. name: string;
  1129. module: number | null;
  1130. state: string | null;
  1131. discovered_at: string | null;
  1132. }
  1133. // Print Queue types
  1134. export interface PrintQueueItem {
  1135. id: number;
  1136. printer_id: number | null; // null = unassigned
  1137. target_model: string | null; // Target printer model for model-based assignment
  1138. target_location: string | null; // Target location filter for model-based assignment
  1139. required_filament_types: string[] | null; // Required filament types for model-based assignment
  1140. waiting_reason: string | null; // Why a model-based job hasn't started yet
  1141. // Either archive_id OR library_file_id must be set (archive created at print start)
  1142. archive_id: number | null;
  1143. library_file_id: number | null;
  1144. position: number;
  1145. scheduled_time: string | null;
  1146. require_previous_success: boolean;
  1147. auto_off_after: boolean;
  1148. manual_start: boolean; // Requires manual trigger to start (staged)
  1149. ams_mapping: number[] | null; // AMS slot mapping for multi-color prints
  1150. filament_overrides: Array<{ slot_id: number; type: string; color: string }> | null; // Filament overrides for model-based assignment
  1151. plate_id: number | null; // Plate ID for multi-plate 3MF files
  1152. // Print options
  1153. bed_levelling: boolean;
  1154. flow_cali: boolean;
  1155. vibration_cali: boolean;
  1156. layer_inspect: boolean;
  1157. timelapse: boolean;
  1158. use_ams: boolean;
  1159. status: 'pending' | 'printing' | 'completed' | 'failed' | 'skipped' | 'cancelled';
  1160. started_at: string | null;
  1161. completed_at: string | null;
  1162. error_message: string | null;
  1163. created_at: string;
  1164. archive_name?: string | null;
  1165. archive_thumbnail?: string | null;
  1166. library_file_name?: string | null;
  1167. library_file_thumbnail?: string | null;
  1168. printer_name?: string | null;
  1169. print_time_seconds?: number | null; // Estimated print time from archive or library file
  1170. filament_used_grams?: number | null; // Estimated print weight from archive or library file
  1171. // User tracking (Issue #206)
  1172. created_by_id?: number | null;
  1173. created_by_username?: string | null;
  1174. }
  1175. export interface PrintQueueItemCreate {
  1176. printer_id?: number | null; // null = unassigned
  1177. target_model?: string | null; // Target printer model (mutually exclusive with printer_id)
  1178. target_location?: string | null; // Target location filter (only used with target_model)
  1179. filament_overrides?: Array<{ slot_id: number; type: string; color: string }> | null;
  1180. // Either archive_id OR library_file_id must be provided
  1181. archive_id?: number | null;
  1182. library_file_id?: number | null;
  1183. scheduled_time?: string | null;
  1184. require_previous_success?: boolean;
  1185. auto_off_after?: boolean;
  1186. manual_start?: boolean; // Requires manual trigger to start (staged)
  1187. ams_mapping?: number[] | null; // AMS slot mapping for multi-color prints
  1188. plate_id?: number | null; // Plate ID for multi-plate 3MF files
  1189. // Print options
  1190. bed_levelling?: boolean;
  1191. flow_cali?: boolean;
  1192. vibration_cali?: boolean;
  1193. layer_inspect?: boolean;
  1194. timelapse?: boolean;
  1195. use_ams?: boolean;
  1196. }
  1197. export interface PrintQueueItemUpdate {
  1198. printer_id?: number | null; // null = unassign
  1199. target_model?: string | null; // Target printer model (mutually exclusive with printer_id)
  1200. target_location?: string | null; // Target location filter (only used with target_model)
  1201. filament_overrides?: Array<{ slot_id: number; type: string; color: string }> | null;
  1202. position?: number;
  1203. scheduled_time?: string | null;
  1204. require_previous_success?: boolean;
  1205. auto_off_after?: boolean;
  1206. manual_start?: boolean;
  1207. ams_mapping?: number[];
  1208. plate_id?: number | null; // Plate ID for multi-plate 3MF files
  1209. // Print options
  1210. bed_levelling?: boolean;
  1211. flow_cali?: boolean;
  1212. vibration_cali?: boolean;
  1213. layer_inspect?: boolean;
  1214. timelapse?: boolean;
  1215. use_ams?: boolean;
  1216. }
  1217. export interface PrintQueueBulkUpdate {
  1218. item_ids: number[];
  1219. printer_id?: number | null;
  1220. scheduled_time?: string | null;
  1221. require_previous_success?: boolean;
  1222. auto_off_after?: boolean;
  1223. manual_start?: boolean;
  1224. // Print options
  1225. bed_levelling?: boolean;
  1226. flow_cali?: boolean;
  1227. vibration_cali?: boolean;
  1228. layer_inspect?: boolean;
  1229. timelapse?: boolean;
  1230. use_ams?: boolean;
  1231. }
  1232. export interface PrintQueueBulkUpdateResponse {
  1233. updated_count: number;
  1234. skipped_count: number;
  1235. message: string;
  1236. }
  1237. // MQTT Logging types
  1238. export interface MQTTLogEntry {
  1239. timestamp: string;
  1240. topic: string;
  1241. direction: 'in' | 'out';
  1242. payload: Record<string, unknown>;
  1243. }
  1244. export interface MQTTLogsResponse {
  1245. logging_enabled: boolean;
  1246. logs: MQTTLogEntry[];
  1247. }
  1248. // K-Profile types
  1249. export interface KProfile {
  1250. slot_id: number;
  1251. extruder_id: number;
  1252. nozzle_id: string;
  1253. nozzle_diameter: string;
  1254. filament_id: string;
  1255. name: string;
  1256. k_value: string;
  1257. n_coef: string;
  1258. ams_id: number;
  1259. tray_id: number;
  1260. setting_id: string | null;
  1261. }
  1262. export interface KProfileCreate {
  1263. slot_id?: number; // Storage slot, 0 for new profiles
  1264. extruder_id?: number;
  1265. nozzle_id: string;
  1266. nozzle_diameter: string;
  1267. filament_id: string;
  1268. name: string;
  1269. k_value: string;
  1270. n_coef?: string;
  1271. ams_id?: number;
  1272. tray_id?: number;
  1273. setting_id?: string | null;
  1274. }
  1275. export interface KProfileDelete {
  1276. slot_id: number; // cali_idx - calibration index to delete
  1277. extruder_id: number;
  1278. nozzle_id: string; // e.g., "HH00-0.4"
  1279. nozzle_diameter: string; // e.g., "0.4"
  1280. filament_id: string; // Bambu filament identifier
  1281. setting_id?: string | null; // Setting ID (for X1C series)
  1282. }
  1283. export interface KProfilesResponse {
  1284. profiles: KProfile[];
  1285. nozzle_diameter: string;
  1286. }
  1287. export interface KProfileNote {
  1288. setting_id: string;
  1289. note: string;
  1290. }
  1291. export interface KProfileNotesResponse {
  1292. notes: Record<string, string>; // setting_id -> note
  1293. }
  1294. // Slot Preset Mapping
  1295. export interface SlotPresetMapping {
  1296. ams_id: number;
  1297. tray_id: number;
  1298. preset_id: string;
  1299. preset_name: string;
  1300. }
  1301. // Filament types
  1302. export interface Filament {
  1303. id: number;
  1304. name: string;
  1305. type: string; // PLA, PETG, ABS, etc.
  1306. brand: string | null;
  1307. color: string | null;
  1308. color_hex: string | null;
  1309. cost_per_kg: number;
  1310. spool_weight_g: number;
  1311. currency: string;
  1312. density: number | null;
  1313. print_temp_min: number | null;
  1314. print_temp_max: number | null;
  1315. bed_temp_min: number | null;
  1316. bed_temp_max: number | null;
  1317. created_at: string;
  1318. updated_at: string;
  1319. }
  1320. // Notification Provider types
  1321. export type ProviderType = 'callmebot' | 'ntfy' | 'pushover' | 'telegram' | 'email' | 'discord' | 'webhook';
  1322. export interface NotificationProvider {
  1323. id: number;
  1324. name: string;
  1325. provider_type: ProviderType;
  1326. enabled: boolean;
  1327. config: Record<string, unknown>;
  1328. // Print lifecycle events
  1329. on_print_start: boolean;
  1330. on_print_complete: boolean;
  1331. on_print_failed: boolean;
  1332. on_print_stopped: boolean;
  1333. on_print_progress: boolean;
  1334. // Printer status events
  1335. on_printer_offline: boolean;
  1336. on_printer_error: boolean;
  1337. on_filament_low: boolean;
  1338. on_maintenance_due: boolean;
  1339. // AMS environmental alarms (regular AMS)
  1340. on_ams_humidity_high: boolean;
  1341. on_ams_temperature_high: boolean;
  1342. // AMS-HT environmental alarms
  1343. on_ams_ht_humidity_high: boolean;
  1344. on_ams_ht_temperature_high: boolean;
  1345. // Build plate detection
  1346. on_plate_not_empty: boolean;
  1347. // Bed cooled
  1348. on_bed_cooled: boolean;
  1349. // Print queue events
  1350. on_queue_job_added: boolean;
  1351. on_queue_job_assigned: boolean;
  1352. on_queue_job_started: boolean;
  1353. on_queue_job_waiting: boolean;
  1354. on_queue_job_skipped: boolean;
  1355. on_queue_job_failed: boolean;
  1356. on_queue_completed: boolean;
  1357. // Quiet hours
  1358. quiet_hours_enabled: boolean;
  1359. quiet_hours_start: string | null;
  1360. quiet_hours_end: string | null;
  1361. // Daily digest
  1362. daily_digest_enabled: boolean;
  1363. daily_digest_time: string | null;
  1364. // Printer filter
  1365. printer_id: number | null;
  1366. // Status tracking
  1367. last_success: string | null;
  1368. last_error: string | null;
  1369. last_error_at: string | null;
  1370. // Timestamps
  1371. created_at: string;
  1372. updated_at: string;
  1373. }
  1374. export interface NotificationProviderCreate {
  1375. name: string;
  1376. provider_type: ProviderType;
  1377. enabled?: boolean;
  1378. config: Record<string, unknown>;
  1379. // Print lifecycle events
  1380. on_print_start?: boolean;
  1381. on_print_complete?: boolean;
  1382. on_print_failed?: boolean;
  1383. on_print_stopped?: boolean;
  1384. on_print_progress?: boolean;
  1385. // Printer status events
  1386. on_printer_offline?: boolean;
  1387. on_printer_error?: boolean;
  1388. on_filament_low?: boolean;
  1389. on_maintenance_due?: boolean;
  1390. // AMS environmental alarms (regular AMS)
  1391. on_ams_humidity_high?: boolean;
  1392. on_ams_temperature_high?: boolean;
  1393. // AMS-HT environmental alarms
  1394. on_ams_ht_humidity_high?: boolean;
  1395. on_ams_ht_temperature_high?: boolean;
  1396. // Build plate detection
  1397. on_plate_not_empty?: boolean;
  1398. // Bed cooled
  1399. on_bed_cooled?: boolean;
  1400. // Print queue events
  1401. on_queue_job_added?: boolean;
  1402. on_queue_job_assigned?: boolean;
  1403. on_queue_job_started?: boolean;
  1404. on_queue_job_waiting?: boolean;
  1405. on_queue_job_skipped?: boolean;
  1406. on_queue_job_failed?: boolean;
  1407. on_queue_completed?: boolean;
  1408. // Quiet hours
  1409. quiet_hours_enabled?: boolean;
  1410. quiet_hours_start?: string | null;
  1411. quiet_hours_end?: string | null;
  1412. // Daily digest
  1413. daily_digest_enabled?: boolean;
  1414. daily_digest_time?: string | null;
  1415. // Printer filter
  1416. printer_id?: number | null;
  1417. }
  1418. export interface NotificationProviderUpdate {
  1419. name?: string;
  1420. provider_type?: ProviderType;
  1421. enabled?: boolean;
  1422. config?: Record<string, unknown>;
  1423. // Print lifecycle events
  1424. on_print_start?: boolean;
  1425. on_print_complete?: boolean;
  1426. on_print_failed?: boolean;
  1427. on_print_stopped?: boolean;
  1428. on_print_progress?: boolean;
  1429. // Printer status events
  1430. on_printer_offline?: boolean;
  1431. on_printer_error?: boolean;
  1432. on_filament_low?: boolean;
  1433. on_maintenance_due?: boolean;
  1434. // AMS environmental alarms (regular AMS)
  1435. on_ams_humidity_high?: boolean;
  1436. on_ams_temperature_high?: boolean;
  1437. // AMS-HT environmental alarms
  1438. on_ams_ht_humidity_high?: boolean;
  1439. on_ams_ht_temperature_high?: boolean;
  1440. // Build plate detection
  1441. on_plate_not_empty?: boolean;
  1442. // Bed cooled
  1443. on_bed_cooled?: boolean;
  1444. // Print queue events
  1445. on_queue_job_added?: boolean;
  1446. on_queue_job_assigned?: boolean;
  1447. on_queue_job_started?: boolean;
  1448. on_queue_job_waiting?: boolean;
  1449. on_queue_job_skipped?: boolean;
  1450. on_queue_job_failed?: boolean;
  1451. on_queue_completed?: boolean;
  1452. // Quiet hours
  1453. quiet_hours_enabled?: boolean;
  1454. quiet_hours_start?: string | null;
  1455. quiet_hours_end?: string | null;
  1456. // Daily digest
  1457. daily_digest_enabled?: boolean;
  1458. daily_digest_time?: string | null;
  1459. // Printer filter
  1460. printer_id?: number | null;
  1461. }
  1462. // GitHub Backup types
  1463. export type ScheduleType = 'hourly' | 'daily' | 'weekly';
  1464. export interface GitHubBackupConfig {
  1465. id: number;
  1466. repository_url: string;
  1467. has_token: boolean;
  1468. branch: string;
  1469. schedule_enabled: boolean;
  1470. schedule_type: ScheduleType;
  1471. backup_kprofiles: boolean;
  1472. backup_cloud_profiles: boolean;
  1473. backup_settings: boolean;
  1474. enabled: boolean;
  1475. last_backup_at: string | null;
  1476. last_backup_status: string | null;
  1477. last_backup_message: string | null;
  1478. last_backup_commit_sha: string | null;
  1479. next_scheduled_run: string | null;
  1480. created_at: string;
  1481. updated_at: string;
  1482. }
  1483. export interface GitHubBackupConfigCreate {
  1484. repository_url: string;
  1485. access_token: string;
  1486. branch?: string;
  1487. schedule_enabled?: boolean;
  1488. schedule_type?: ScheduleType;
  1489. backup_kprofiles?: boolean;
  1490. backup_cloud_profiles?: boolean;
  1491. backup_settings?: boolean;
  1492. enabled?: boolean;
  1493. }
  1494. export interface GitHubBackupLog {
  1495. id: number;
  1496. config_id: number;
  1497. started_at: string;
  1498. completed_at: string | null;
  1499. status: string;
  1500. trigger: string;
  1501. commit_sha: string | null;
  1502. files_changed: number;
  1503. error_message: string | null;
  1504. }
  1505. export interface GitHubBackupStatus {
  1506. configured: boolean;
  1507. enabled: boolean;
  1508. is_running: boolean;
  1509. progress: string | null;
  1510. last_backup_at: string | null;
  1511. last_backup_status: string | null;
  1512. next_scheduled_run: string | null;
  1513. }
  1514. export interface GitHubTestConnectionResponse {
  1515. success: boolean;
  1516. message: string;
  1517. repo_name: string | null;
  1518. permissions: Record<string, boolean> | null;
  1519. }
  1520. export interface GitHubBackupTriggerResponse {
  1521. success: boolean;
  1522. message: string;
  1523. log_id: number | null;
  1524. commit_sha: string | null;
  1525. files_changed: number;
  1526. }
  1527. export interface NotificationTestRequest {
  1528. provider_type: ProviderType;
  1529. config: Record<string, unknown>;
  1530. }
  1531. export interface NotificationTestResponse {
  1532. success: boolean;
  1533. message: string;
  1534. }
  1535. export interface BackgroundDispatchResponse {
  1536. status: 'dispatched' | string;
  1537. printer_id: number;
  1538. archive_id?: number | null;
  1539. filename: string;
  1540. dispatch_job_id: number;
  1541. dispatch_position: number;
  1542. }
  1543. // Provider-specific config types for reference
  1544. export interface CallMeBotConfig {
  1545. phone: string;
  1546. apikey: string;
  1547. }
  1548. export interface NtfyConfig {
  1549. server?: string;
  1550. topic: string;
  1551. auth_token?: string | null;
  1552. }
  1553. export interface PushoverConfig {
  1554. user_key: string;
  1555. app_token: string;
  1556. priority?: number;
  1557. }
  1558. export interface TelegramConfig {
  1559. bot_token: string;
  1560. chat_id: string;
  1561. }
  1562. export interface EmailConfig {
  1563. smtp_server: string;
  1564. smtp_port?: number;
  1565. username: string;
  1566. password: string;
  1567. from_email: string;
  1568. to_email: string;
  1569. use_tls?: boolean;
  1570. }
  1571. // Notification Template types
  1572. export interface NotificationTemplate {
  1573. id: number;
  1574. event_type: string;
  1575. name: string;
  1576. title_template: string;
  1577. body_template: string;
  1578. is_default: boolean;
  1579. created_at: string;
  1580. updated_at: string;
  1581. }
  1582. export interface NotificationTemplateUpdate {
  1583. title_template?: string;
  1584. body_template?: string;
  1585. }
  1586. export interface EventVariablesResponse {
  1587. event_type: string;
  1588. event_name: string;
  1589. variables: string[];
  1590. }
  1591. export interface TemplatePreviewRequest {
  1592. event_type: string;
  1593. title_template: string;
  1594. body_template: string;
  1595. }
  1596. export interface TemplatePreviewResponse {
  1597. title: string;
  1598. body: string;
  1599. }
  1600. // Notification Log types
  1601. export interface NotificationLogEntry {
  1602. id: number;
  1603. provider_id: number;
  1604. provider_name: string | null;
  1605. provider_type: string | null;
  1606. event_type: string;
  1607. title: string;
  1608. message: string;
  1609. success: boolean;
  1610. error_message: string | null;
  1611. printer_id: number | null;
  1612. printer_name: string | null;
  1613. created_at: string;
  1614. }
  1615. export interface NotificationLogStats {
  1616. total: number;
  1617. success_count: number;
  1618. failure_count: number;
  1619. by_event_type: Record<string, number>;
  1620. by_provider: Record<string, number>;
  1621. }
  1622. // Spoolman types
  1623. export interface SpoolmanStatus {
  1624. enabled: boolean;
  1625. connected: boolean;
  1626. url: string | null;
  1627. }
  1628. export interface SkippedSpool {
  1629. location: string;
  1630. reason: string;
  1631. filament_type: string | null;
  1632. color: string | null;
  1633. }
  1634. export interface SpoolmanSyncResult {
  1635. success: boolean;
  1636. synced_count: number;
  1637. skipped_count: number;
  1638. skipped: SkippedSpool[];
  1639. errors: string[];
  1640. }
  1641. export interface UnlinkedSpool {
  1642. id: number;
  1643. filament_name: string | null;
  1644. filament_material: string | null;
  1645. filament_color_hex: string | null;
  1646. remaining_weight: number | null;
  1647. location: string | null;
  1648. }
  1649. export interface LinkedSpoolInfo {
  1650. id: number;
  1651. remaining_weight: number | null;
  1652. filament_weight: number | null;
  1653. }
  1654. export interface LinkedSpoolsMap {
  1655. linked: Record<string, LinkedSpoolInfo>; // tag (uppercase) -> spool info
  1656. }
  1657. // Inventory types
  1658. export interface InventorySpool {
  1659. id: number;
  1660. material: string;
  1661. subtype: string | null;
  1662. color_name: string | null;
  1663. rgba: string | null;
  1664. brand: string | null;
  1665. label_weight: number;
  1666. core_weight: number;
  1667. core_weight_catalog_id: number | null;
  1668. weight_used: number;
  1669. slicer_filament: string | null;
  1670. slicer_filament_name: string | null;
  1671. nozzle_temp_min: number | null;
  1672. nozzle_temp_max: number | null;
  1673. note: string | null;
  1674. added_full: boolean | null;
  1675. last_used: string | null;
  1676. encode_time: string | null;
  1677. tag_uid: string | null;
  1678. tray_uuid: string | null;
  1679. data_origin: string | null;
  1680. tag_type: string | null;
  1681. archived_at: string | null;
  1682. created_at: string;
  1683. updated_at: string;
  1684. cost_per_kg: number | null;
  1685. last_scale_weight: number | null;
  1686. last_weighed_at: string | null;
  1687. k_profiles?: SpoolKProfile[];
  1688. }
  1689. export interface SpoolUsageRecord {
  1690. id: number;
  1691. spool_id: number;
  1692. printer_id: number | null;
  1693. print_name: string | null;
  1694. weight_used: number;
  1695. percent_used: number;
  1696. status: string;
  1697. cost: number | null;
  1698. created_at: string;
  1699. }
  1700. export interface SpoolKProfile {
  1701. id: number;
  1702. spool_id: number;
  1703. printer_id: number;
  1704. extruder: number;
  1705. nozzle_diameter: string;
  1706. nozzle_type: string | null;
  1707. k_value: number;
  1708. name: string | null;
  1709. cali_idx: number | null;
  1710. setting_id: string | null;
  1711. created_at: string;
  1712. }
  1713. export interface SpoolKProfileInput {
  1714. printer_id: number;
  1715. extruder?: number;
  1716. nozzle_diameter?: string;
  1717. nozzle_type?: string | null;
  1718. k_value: number;
  1719. name?: string | null;
  1720. cali_idx?: number | null;
  1721. setting_id?: string | null;
  1722. }
  1723. export interface SpoolAssignment {
  1724. id: number;
  1725. spool_id: number;
  1726. printer_id: number;
  1727. printer_name: string | null;
  1728. ams_id: number;
  1729. tray_id: number;
  1730. fingerprint_color: string | null;
  1731. fingerprint_type: string | null;
  1732. spool?: InventorySpool | null;
  1733. configured: boolean;
  1734. created_at: string;
  1735. ams_label?: string | null; // User-defined friendly name for the AMS unit
  1736. }
  1737. // Update types
  1738. export interface VersionInfo {
  1739. version: string;
  1740. repo: string;
  1741. }
  1742. export interface UpdateCheckResult {
  1743. update_available: boolean;
  1744. current_version: string;
  1745. latest_version: string | null;
  1746. release_name?: string;
  1747. release_notes?: string;
  1748. release_url?: string;
  1749. published_at?: string;
  1750. error?: string;
  1751. message?: string;
  1752. is_docker?: boolean;
  1753. update_method?: 'docker' | 'git';
  1754. }
  1755. export interface UpdateStatus {
  1756. status: 'idle' | 'checking' | 'downloading' | 'installing' | 'complete' | 'error';
  1757. progress: number;
  1758. message: string;
  1759. error: string | null;
  1760. }
  1761. // Maintenance types
  1762. export interface MaintenanceType {
  1763. id: number;
  1764. name: string;
  1765. description: string | null;
  1766. default_interval_hours: number;
  1767. interval_type: 'hours' | 'days'; // "hours" = print hours, "days" = calendar days
  1768. icon: string | null;
  1769. wiki_url: string | null; // Documentation link
  1770. is_system: boolean;
  1771. created_at: string;
  1772. }
  1773. export interface MaintenanceTypeCreate {
  1774. name: string;
  1775. description?: string | null;
  1776. default_interval_hours?: number;
  1777. interval_type?: 'hours' | 'days';
  1778. icon?: string | null;
  1779. wiki_url?: string | null;
  1780. }
  1781. export interface MaintenanceStatus {
  1782. id: number;
  1783. printer_id: number;
  1784. printer_name: string;
  1785. printer_model: string | null;
  1786. maintenance_type_id: number;
  1787. maintenance_type_name: string;
  1788. maintenance_type_icon: string | null;
  1789. maintenance_type_wiki_url: string | null; // Custom wiki URL from type
  1790. enabled: boolean;
  1791. interval_hours: number; // For hours type: print hours; for days type: number of days
  1792. interval_type: 'hours' | 'days';
  1793. current_hours: number;
  1794. hours_since_maintenance: number;
  1795. hours_until_due: number;
  1796. days_since_maintenance: number | null; // For days type
  1797. days_until_due: number | null; // For days type
  1798. is_due: boolean;
  1799. is_warning: boolean;
  1800. last_performed_at: string | null;
  1801. }
  1802. export interface PrinterMaintenanceOverview {
  1803. printer_id: number;
  1804. printer_name: string;
  1805. printer_model: string | null;
  1806. total_print_hours: number;
  1807. maintenance_items: MaintenanceStatus[];
  1808. due_count: number;
  1809. warning_count: number;
  1810. }
  1811. export interface MaintenanceHistory {
  1812. id: number;
  1813. printer_maintenance_id: number;
  1814. performed_at: string;
  1815. hours_at_maintenance: number;
  1816. notes: string | null;
  1817. }
  1818. export interface MaintenanceSummary {
  1819. total_due: number;
  1820. total_warning: number;
  1821. printers_with_issues: Array<{
  1822. printer_id: number;
  1823. printer_name: string;
  1824. due_count: number;
  1825. warning_count: number;
  1826. }>;
  1827. }
  1828. // External Links (sidebar)
  1829. export interface ExternalLink {
  1830. id: number;
  1831. name: string;
  1832. url: string;
  1833. icon: string;
  1834. open_in_new_tab: boolean;
  1835. custom_icon: string | null;
  1836. sort_order: number;
  1837. created_at: string;
  1838. updated_at: string;
  1839. }
  1840. export interface ExternalLinkCreate {
  1841. name: string;
  1842. url: string;
  1843. icon: string;
  1844. open_in_new_tab?: boolean;
  1845. }
  1846. export interface ExternalLinkUpdate {
  1847. name?: string;
  1848. url?: string;
  1849. icon?: string;
  1850. open_in_new_tab?: boolean;
  1851. }
  1852. // Permission type - all available permissions
  1853. export type Permission =
  1854. | 'printers:read' | 'printers:create' | 'printers:update' | 'printers:delete' | 'printers:control' | 'printers:files' | 'printers:ams_rfid' | 'printers:clear_plate'
  1855. | 'archives:read' | 'archives:create'
  1856. | 'archives:update_own' | 'archives:update_all' | 'archives:delete_own' | 'archives:delete_all'
  1857. | 'archives:reprint_own' | 'archives:reprint_all'
  1858. | 'queue:read' | 'queue:create'
  1859. | 'queue:update_own' | 'queue:update_all' | 'queue:delete_own' | 'queue:delete_all'
  1860. | 'queue:reorder'
  1861. | 'library:read' | 'library:upload'
  1862. | 'library:update_own' | 'library:update_all' | 'library:delete_own' | 'library:delete_all'
  1863. | 'projects:read' | 'projects:create' | 'projects:update' | 'projects:delete'
  1864. | 'filaments:read' | 'filaments:create' | 'filaments:update' | 'filaments:delete'
  1865. | 'inventory:read' | 'inventory:create' | 'inventory:update' | 'inventory:delete'
  1866. | 'smart_plugs:read' | 'smart_plugs:create' | 'smart_plugs:update' | 'smart_plugs:delete' | 'smart_plugs:control'
  1867. | 'camera:view'
  1868. | 'maintenance:read' | 'maintenance:create' | 'maintenance:update' | 'maintenance:delete'
  1869. | 'kprofiles:read' | 'kprofiles:create' | 'kprofiles:update' | 'kprofiles:delete'
  1870. | 'notifications:read' | 'notifications:create' | 'notifications:update' | 'notifications:delete'
  1871. | 'notification_templates:read' | 'notification_templates:update'
  1872. | 'external_links:read' | 'external_links:create' | 'external_links:update' | 'external_links:delete'
  1873. | 'discovery:scan'
  1874. | 'firmware:read' | 'firmware:update'
  1875. | 'ams_history:read'
  1876. | 'stats:read'
  1877. | 'system:read'
  1878. | 'settings:read' | 'settings:update' | 'settings:backup' | 'settings:restore'
  1879. | 'github:backup' | 'github:restore'
  1880. | 'cloud:auth'
  1881. | 'api_keys:read' | 'api_keys:create' | 'api_keys:update' | 'api_keys:delete'
  1882. | 'users:read' | 'users:create' | 'users:update' | 'users:delete'
  1883. | 'groups:read' | 'groups:create' | 'groups:update' | 'groups:delete'
  1884. | 'websocket:connect';
  1885. // Group types
  1886. export interface GroupBrief {
  1887. id: number;
  1888. name: string;
  1889. }
  1890. export interface Group {
  1891. id: number;
  1892. name: string;
  1893. description: string | null;
  1894. permissions: Permission[];
  1895. is_system: boolean;
  1896. user_count: number;
  1897. created_at: string;
  1898. updated_at: string;
  1899. }
  1900. export interface GroupDetail extends Group {
  1901. users: Array<{ id: number; username: string; is_active: boolean }>;
  1902. }
  1903. export interface GroupCreate {
  1904. name: string;
  1905. description?: string;
  1906. permissions: Permission[];
  1907. }
  1908. export interface GroupUpdate {
  1909. name?: string;
  1910. description?: string;
  1911. permissions?: Permission[];
  1912. }
  1913. export interface PermissionInfo {
  1914. value: Permission;
  1915. label: string;
  1916. }
  1917. export interface PermissionCategory {
  1918. name: string;
  1919. permissions: PermissionInfo[];
  1920. }
  1921. export interface PermissionsListResponse {
  1922. categories: PermissionCategory[];
  1923. all_permissions: Permission[];
  1924. }
  1925. // Auth types
  1926. export interface LoginRequest {
  1927. username: string;
  1928. password: string;
  1929. }
  1930. export interface LoginResponse {
  1931. access_token: string;
  1932. token_type: string;
  1933. user: UserResponse;
  1934. }
  1935. export interface UserResponse {
  1936. id: number;
  1937. username: string;
  1938. email?: string;
  1939. role: string; // Deprecated, kept for backward compatibility
  1940. is_active: boolean;
  1941. is_admin: boolean; // Computed from role and group membership
  1942. groups: GroupBrief[];
  1943. permissions: Permission[]; // All permissions from groups
  1944. created_at: string;
  1945. }
  1946. export interface UserCreate {
  1947. username: string;
  1948. password?: string; // Optional when advanced auth is enabled
  1949. email?: string;
  1950. role: string;
  1951. group_ids?: number[];
  1952. }
  1953. export interface UserUpdate {
  1954. username?: string;
  1955. password?: string;
  1956. email?: string;
  1957. role?: string;
  1958. is_active?: boolean;
  1959. group_ids?: number[];
  1960. }
  1961. export interface SetupRequest {
  1962. auth_enabled: boolean;
  1963. admin_username?: string;
  1964. admin_password?: string;
  1965. }
  1966. export interface ForgotPasswordRequest {
  1967. email: string;
  1968. }
  1969. export interface ForgotPasswordResponse {
  1970. message: string;
  1971. }
  1972. export interface ResetPasswordRequest {
  1973. user_id: number;
  1974. }
  1975. export interface ResetPasswordResponse {
  1976. message: string;
  1977. }
  1978. export interface SMTPSettings {
  1979. smtp_host: string;
  1980. smtp_port: number;
  1981. smtp_username?: string;
  1982. smtp_password?: string;
  1983. smtp_security: 'starttls' | 'ssl' | 'none';
  1984. smtp_auth_enabled: boolean;
  1985. smtp_from_email: string;
  1986. smtp_from_name: string;
  1987. }
  1988. export interface TestSMTPRequest {
  1989. smtp_host: string;
  1990. smtp_port: number;
  1991. smtp_username?: string;
  1992. smtp_password?: string;
  1993. smtp_security: 'starttls' | 'ssl' | 'none';
  1994. smtp_auth_enabled: boolean;
  1995. smtp_from_email: string;
  1996. test_recipient: string;
  1997. }
  1998. export interface TestSMTPResponse {
  1999. success: boolean;
  2000. message: string;
  2001. }
  2002. export interface AdvancedAuthStatus {
  2003. advanced_auth_enabled: boolean;
  2004. smtp_configured: boolean;
  2005. }
  2006. export interface SetupResponse {
  2007. auth_enabled: boolean;
  2008. admin_created?: boolean;
  2009. }
  2010. export interface AuthStatus {
  2011. auth_enabled: boolean;
  2012. requires_setup: boolean;
  2013. }
  2014. // API functions
  2015. export const api = {
  2016. // Authentication
  2017. getAuthStatus: () => request<AuthStatus>('/auth/status'),
  2018. setupAuth: (data: SetupRequest) =>
  2019. request<SetupResponse>('/auth/setup', {
  2020. method: 'POST',
  2021. body: JSON.stringify(data),
  2022. }),
  2023. login: (data: LoginRequest) =>
  2024. request<LoginResponse>('/auth/login', {
  2025. method: 'POST',
  2026. body: JSON.stringify(data),
  2027. }),
  2028. logout: () =>
  2029. request<{ message: string }>('/auth/logout', {
  2030. method: 'POST',
  2031. }),
  2032. getCurrentUser: () => request<UserResponse>('/auth/me'),
  2033. disableAuth: () =>
  2034. request<{ message: string; auth_enabled: boolean }>('/auth/disable', {
  2035. method: 'POST',
  2036. }),
  2037. // Advanced Authentication
  2038. testSMTP: (data: TestSMTPRequest) =>
  2039. request<TestSMTPResponse>('/auth/smtp/test', {
  2040. method: 'POST',
  2041. body: JSON.stringify(data),
  2042. }),
  2043. getSMTPSettings: () => request<SMTPSettings | null>('/auth/smtp'),
  2044. saveSMTPSettings: (data: SMTPSettings) =>
  2045. request<{ message: string }>('/auth/smtp', {
  2046. method: 'POST',
  2047. body: JSON.stringify(data),
  2048. }),
  2049. enableAdvancedAuth: () =>
  2050. request<{ message: string; advanced_auth_enabled: boolean }>('/auth/advanced-auth/enable', {
  2051. method: 'POST',
  2052. }),
  2053. disableAdvancedAuth: () =>
  2054. request<{ message: string; advanced_auth_enabled: boolean }>('/auth/advanced-auth/disable', {
  2055. method: 'POST',
  2056. }),
  2057. getAdvancedAuthStatus: () => request<AdvancedAuthStatus>('/auth/advanced-auth/status'),
  2058. forgotPassword: (data: ForgotPasswordRequest) =>
  2059. request<ForgotPasswordResponse>('/auth/forgot-password', {
  2060. method: 'POST',
  2061. body: JSON.stringify(data),
  2062. }),
  2063. resetUserPassword: (data: ResetPasswordRequest) =>
  2064. request<ResetPasswordResponse>('/auth/reset-password', {
  2065. method: 'POST',
  2066. body: JSON.stringify(data),
  2067. }),
  2068. // Users
  2069. getUsers: () => request<UserResponse[]>('/users/'),
  2070. getUser: (id: number) => request<UserResponse>(`/users/${id}`),
  2071. createUser: (data: UserCreate) =>
  2072. request<UserResponse>('/users/', {
  2073. method: 'POST',
  2074. body: JSON.stringify(data),
  2075. }),
  2076. updateUser: (id: number, data: UserUpdate) =>
  2077. request<UserResponse>(`/users/${id}`, {
  2078. method: 'PATCH',
  2079. body: JSON.stringify(data),
  2080. }),
  2081. deleteUser: (id: number, deleteItems: boolean = false) =>
  2082. request<void>(`/users/${id}?delete_items=${deleteItems}`, {
  2083. method: 'DELETE',
  2084. }),
  2085. getUserItemsCount: (id: number) =>
  2086. request<{ archives: number; queue_items: number; library_files: number }>(`/users/${id}/items-count`),
  2087. changePassword: (currentPassword: string, newPassword: string) =>
  2088. request<{ message: string }>('/users/me/change-password', {
  2089. method: 'POST',
  2090. body: JSON.stringify({ current_password: currentPassword, new_password: newPassword }),
  2091. }),
  2092. // Groups
  2093. getPermissions: () => request<PermissionsListResponse>('/groups/permissions'),
  2094. getGroups: () => request<Group[]>('/groups/'),
  2095. getGroup: (id: number) => request<GroupDetail>(`/groups/${id}`),
  2096. createGroup: (data: GroupCreate) =>
  2097. request<Group>('/groups/', {
  2098. method: 'POST',
  2099. body: JSON.stringify(data),
  2100. }),
  2101. updateGroup: (id: number, data: GroupUpdate) =>
  2102. request<Group>(`/groups/${id}`, {
  2103. method: 'PATCH',
  2104. body: JSON.stringify(data),
  2105. }),
  2106. deleteGroup: (id: number) =>
  2107. request<void>(`/groups/${id}`, {
  2108. method: 'DELETE',
  2109. }),
  2110. addUserToGroup: (groupId: number, userId: number) =>
  2111. request<void>(`/groups/${groupId}/users/${userId}`, {
  2112. method: 'POST',
  2113. }),
  2114. removeUserFromGroup: (groupId: number, userId: number) =>
  2115. request<void>(`/groups/${groupId}/users/${userId}`, {
  2116. method: 'DELETE',
  2117. }),
  2118. // Printers
  2119. getPrinters: () => request<Printer[]>('/printers/'),
  2120. getPrinter: (id: number) => request<Printer>(`/printers/${id}`),
  2121. createPrinter: (data: PrinterCreate) =>
  2122. request<Printer>('/printers/', {
  2123. method: 'POST',
  2124. body: JSON.stringify(data),
  2125. }),
  2126. updatePrinter: (id: number, data: Partial<PrinterCreate>) =>
  2127. request<Printer>(`/printers/${id}`, {
  2128. method: 'PATCH',
  2129. body: JSON.stringify(data),
  2130. }),
  2131. deletePrinter: (id: number, deleteArchives: boolean = true) =>
  2132. request<{ status: string; archives_deleted: boolean }>(
  2133. `/printers/${id}?delete_archives=${deleteArchives}`,
  2134. { method: 'DELETE' }
  2135. ),
  2136. getDeveloperModeWarnings: () =>
  2137. request<{ printer_id: number; name: string }[]>('/printers/developer-mode-warnings'),
  2138. getAvailableFilaments: (model: string, location?: string) => {
  2139. const params = new URLSearchParams({ model });
  2140. if (location) params.set('location', location);
  2141. return request<Array<{ type: string; color: string; tray_info_idx: string; tray_sub_brands: string; extruder_id: number | null }>>(`/printers/available-filaments?${params}`);
  2142. },
  2143. getPrinterStatus: (id: number) =>
  2144. request<PrinterStatus>(`/printers/${id}/status`),
  2145. refreshPrinterStatus: (id: number) =>
  2146. request<{ status: string }>(`/printers/${id}/refresh-status`, {
  2147. method: 'POST',
  2148. }),
  2149. connectPrinter: (id: number) =>
  2150. request<{ connected: boolean }>(`/printers/${id}/connect`, {
  2151. method: 'POST',
  2152. }),
  2153. disconnectPrinter: (id: number) =>
  2154. request<{ connected: boolean }>(`/printers/${id}/disconnect`, {
  2155. method: 'POST',
  2156. }),
  2157. testExternalCamera: (printerId: number, url: string, cameraType: string) =>
  2158. request<{ success: boolean; error?: string; resolution?: string }>(
  2159. `/printers/${printerId}/camera/external/test?url=${encodeURIComponent(url)}&camera_type=${encodeURIComponent(cameraType)}`,
  2160. { method: 'POST' }
  2161. ),
  2162. // Print Control
  2163. stopPrint: (printerId: number) =>
  2164. request<{ success: boolean; message: string }>(`/printers/${printerId}/print/stop`, {
  2165. method: 'POST',
  2166. }),
  2167. pausePrint: (printerId: number) =>
  2168. request<{ success: boolean; message: string }>(`/printers/${printerId}/print/pause`, {
  2169. method: 'POST',
  2170. }),
  2171. resumePrint: (printerId: number) =>
  2172. request<{ success: boolean; message: string }>(`/printers/${printerId}/print/resume`, {
  2173. method: 'POST',
  2174. }),
  2175. clearPlate: (printerId: number) =>
  2176. request<{ success: boolean; message: string }>(`/printers/${printerId}/clear-plate`, {
  2177. method: 'POST',
  2178. }),
  2179. // Get current print user (for reprint tracking - Issue #206)
  2180. getCurrentPrintUser: (printerId: number) =>
  2181. request<{ user_id?: number; username?: string }>(`/printers/${printerId}/current-print-user`),
  2182. // Chamber Light Control
  2183. setChamberLight: (printerId: number, on: boolean) =>
  2184. request<{ success: boolean; message: string }>(`/printers/${printerId}/chamber-light?on=${on}`, {
  2185. method: 'POST',
  2186. }),
  2187. // Skip Objects
  2188. getPrintableObjects: (printerId: number) =>
  2189. request<{
  2190. objects: Array<{ id: number; name: string; x: number | null; y: number | null; skipped: boolean }>;
  2191. total: number;
  2192. skipped_count: number;
  2193. is_printing: boolean;
  2194. bbox_all: [number, number, number, number] | null;
  2195. }>(`/printers/${printerId}/print/objects`),
  2196. skipObjects: (printerId: number, objectIds: number[]) =>
  2197. request<{ success: boolean; message: string; skipped_objects: number[] }>(
  2198. `/printers/${printerId}/print/skip-objects`,
  2199. {
  2200. method: 'POST',
  2201. body: JSON.stringify(objectIds),
  2202. }
  2203. ),
  2204. // HMS Errors
  2205. clearHMSErrors: (printerId: number) =>
  2206. request<{ success: boolean; message: string }>(`/printers/${printerId}/hms/clear`, { method: 'POST' }),
  2207. // AMS Control
  2208. refreshAmsSlot: (printerId: number, amsId: number, slotId: number) =>
  2209. request<{ success: boolean; message: string }>(
  2210. `/printers/${printerId}/ams/${amsId}/slot/${slotId}/refresh`,
  2211. { method: 'POST' }
  2212. ),
  2213. // MQTT Debug Logging
  2214. enableMQTTLogging: (printerId: number) =>
  2215. request<{ logging_enabled: boolean }>(`/printers/${printerId}/logging/enable`, {
  2216. method: 'POST',
  2217. }),
  2218. disableMQTTLogging: (printerId: number) =>
  2219. request<{ logging_enabled: boolean }>(`/printers/${printerId}/logging/disable`, {
  2220. method: 'POST',
  2221. }),
  2222. getMQTTLogs: (printerId: number) =>
  2223. request<MQTTLogsResponse>(`/printers/${printerId}/logging`),
  2224. clearMQTTLogs: (printerId: number) =>
  2225. request<{ status: string }>(`/printers/${printerId}/logging`, {
  2226. method: 'DELETE',
  2227. }),
  2228. // Printer File Manager
  2229. getPrinterFiles: (printerId: number, path = '/') =>
  2230. request<{
  2231. path: string;
  2232. files: Array<{
  2233. name: string;
  2234. is_directory: boolean;
  2235. size: number;
  2236. path: string;
  2237. mtime?: string;
  2238. }>;
  2239. }>(`/printers/${printerId}/files?path=${encodeURIComponent(path)}`),
  2240. getPrinterFileDownloadUrl: (printerId: number, path: string) =>
  2241. `${API_BASE}/printers/${printerId}/files/download?path=${encodeURIComponent(path)}`,
  2242. getPrinterFileGcodeUrl: (printerId: number, path: string) =>
  2243. `${API_BASE}/printers/${printerId}/files/gcode?path=${encodeURIComponent(path)}`,
  2244. getPrinterFilePlates: (printerId: number, path: string) =>
  2245. request<{
  2246. printer_id: number;
  2247. path: string;
  2248. filename: string;
  2249. plates: Array<{
  2250. index: number;
  2251. name: string | null;
  2252. objects: string[];
  2253. has_thumbnail: boolean;
  2254. thumbnail_url: string | null;
  2255. print_time_seconds: number | null;
  2256. filament_used_grams: number | null;
  2257. filaments: Array<{
  2258. slot_id: number;
  2259. type: string;
  2260. color: string;
  2261. used_grams: number;
  2262. used_meters: number;
  2263. }>;
  2264. }>;
  2265. is_multi_plate: boolean;
  2266. }>(`/printers/${printerId}/files/plates?path=${encodeURIComponent(path)}`),
  2267. getPrinterFilePlateThumbnail: (printerId: number, plateIndex: number, path: string) =>
  2268. `${API_BASE}/printers/${printerId}/files/plate-thumbnail/${plateIndex}?path=${encodeURIComponent(path)}`,
  2269. downloadPrinterFile: async (printerId: number, path: string): Promise<void> => {
  2270. const headers: Record<string, string> = {};
  2271. if (authToken) {
  2272. headers['Authorization'] = `Bearer ${authToken}`;
  2273. }
  2274. const response = await fetch(
  2275. `${API_BASE}/printers/${printerId}/files/download?path=${encodeURIComponent(path)}`,
  2276. { headers }
  2277. );
  2278. if (!response.ok) {
  2279. const error = await response.json().catch(() => ({}));
  2280. throw new Error(error.detail || `HTTP ${response.status}`);
  2281. }
  2282. const disposition = response.headers.get('Content-Disposition');
  2283. const filename = parseContentDispositionFilename(disposition) || path.split('/').pop() || 'download';
  2284. const blob = await response.blob();
  2285. const url = window.URL.createObjectURL(blob);
  2286. const a = document.createElement('a');
  2287. a.href = url;
  2288. a.download = filename;
  2289. document.body.appendChild(a);
  2290. a.click();
  2291. document.body.removeChild(a);
  2292. window.URL.revokeObjectURL(url);
  2293. },
  2294. downloadPrinterFilesAsZip: async (printerId: number, paths: string[]): Promise<Blob> => {
  2295. const headers: Record<string, string> = { 'Content-Type': 'application/json' };
  2296. if (authToken) {
  2297. headers['Authorization'] = `Bearer ${authToken}`;
  2298. }
  2299. const response = await fetch(`${API_BASE}/printers/${printerId}/files/download-zip`, {
  2300. method: 'POST',
  2301. headers,
  2302. body: JSON.stringify({ paths }),
  2303. });
  2304. if (!response.ok) {
  2305. const error = await response.json().catch(() => ({}));
  2306. throw new Error(error.detail || `HTTP ${response.status}`);
  2307. }
  2308. return response.blob();
  2309. },
  2310. deletePrinterFile: (printerId: number, path: string) =>
  2311. request<{ status: string; path: string }>(`/printers/${printerId}/files?path=${encodeURIComponent(path)}`, {
  2312. method: 'DELETE',
  2313. }),
  2314. getPrinterStorage: (printerId: number) =>
  2315. request<{ used_bytes: number | null; free_bytes: number | null }>(`/printers/${printerId}/storage`),
  2316. // Archives
  2317. getArchives: (printerId?: number, projectId?: number, limit = 50, offset = 0, dateFrom?: string, dateTo?: string) => {
  2318. const params = new URLSearchParams();
  2319. if (printerId) params.set('printer_id', String(printerId));
  2320. if (projectId) params.set('project_id', String(projectId));
  2321. params.set('limit', String(limit));
  2322. params.set('offset', String(offset));
  2323. if (dateFrom) params.set('date_from', dateFrom);
  2324. if (dateTo) params.set('date_to', dateTo);
  2325. return request<Archive[]>(`/archives/?${params}`);
  2326. },
  2327. getArchivesSlim: (dateFrom?: string, dateTo?: string) => {
  2328. const params = new URLSearchParams();
  2329. if (dateFrom) params.set('date_from', dateFrom);
  2330. if (dateTo) params.set('date_to', dateTo);
  2331. const qs = params.toString();
  2332. return request<ArchiveSlim[]>(`/archives/slim${qs ? `?${qs}` : ''}`);
  2333. },
  2334. getArchive: (id: number) => request<Archive>(`/archives/${id}`),
  2335. searchArchives: (query: string, options?: {
  2336. printerId?: number;
  2337. projectId?: number;
  2338. status?: string;
  2339. limit?: number;
  2340. offset?: number;
  2341. }) => {
  2342. const params = new URLSearchParams();
  2343. params.set('q', query);
  2344. if (options?.printerId) params.set('printer_id', String(options.printerId));
  2345. if (options?.projectId) params.set('project_id', String(options.projectId));
  2346. if (options?.status) params.set('status', options.status);
  2347. if (options?.limit) params.set('limit', String(options.limit));
  2348. if (options?.offset) params.set('offset', String(options.offset));
  2349. return request<Archive[]>(`/archives/search?${params}`);
  2350. },
  2351. rebuildSearchIndex: () => request<{ message: string }>('/archives/search/rebuild-index', { method: 'POST' }),
  2352. updateArchive: (id: number, data: {
  2353. printer_id?: number | null;
  2354. project_id?: number | null;
  2355. print_name?: string;
  2356. is_favorite?: boolean;
  2357. tags?: string;
  2358. notes?: string;
  2359. cost?: number;
  2360. failure_reason?: string | null;
  2361. status?: string;
  2362. quantity?: number;
  2363. external_url?: string | null;
  2364. }) =>
  2365. request<Archive>(`/archives/${id}`, {
  2366. method: 'PATCH',
  2367. body: JSON.stringify(data),
  2368. }),
  2369. toggleFavorite: (id: number) =>
  2370. request<Archive>(`/archives/${id}/favorite`, { method: 'POST' }),
  2371. deleteArchive: (id: number) =>
  2372. request<void>(`/archives/${id}`, { method: 'DELETE' }),
  2373. getArchiveStats: (options?: { dateFrom?: string; dateTo?: string }) => {
  2374. const params = new URLSearchParams();
  2375. if (options?.dateFrom) params.set('date_from', options.dateFrom);
  2376. if (options?.dateTo) params.set('date_to', options.dateTo);
  2377. const qs = params.toString();
  2378. return request<ArchiveStats>(`/archives/stats${qs ? `?${qs}` : ''}`);
  2379. },
  2380. // Tag management
  2381. getTags: () => request<TagInfo[]>('/archives/tags'),
  2382. renameTag: (oldName: string, newName: string) =>
  2383. request<{ affected: number }>(`/archives/tags/${encodeURIComponent(oldName)}`, {
  2384. method: 'PUT',
  2385. body: JSON.stringify({ new_name: newName }),
  2386. }),
  2387. deleteTag: (name: string) =>
  2388. request<{ affected: number }>(`/archives/tags/${encodeURIComponent(name)}`, {
  2389. method: 'DELETE',
  2390. }),
  2391. recalculateCosts: () =>
  2392. request<{ message: string; updated: number }>('/archives/recalculate-costs', { method: 'POST' }),
  2393. getFailureAnalysis: (options?: { days?: number; dateFrom?: string; dateTo?: string; printerId?: number; projectId?: number }) => {
  2394. const params = new URLSearchParams();
  2395. if (options?.days) params.set('days', String(options.days));
  2396. if (options?.dateFrom) params.set('date_from', options.dateFrom);
  2397. if (options?.dateTo) params.set('date_to', options.dateTo);
  2398. if (options?.printerId) params.set('printer_id', String(options.printerId));
  2399. if (options?.projectId) params.set('project_id', String(options.projectId));
  2400. const qs = params.toString();
  2401. return request<FailureAnalysis>(`/archives/analysis/failures${qs ? `?${qs}` : ''}`);
  2402. },
  2403. compareArchives: (archiveIds: number[]) =>
  2404. request<ArchiveComparison>(`/archives/compare?archive_ids=${archiveIds.join(',')}`),
  2405. findSimilarArchives: (archiveId: number, limit = 10) =>
  2406. request<SimilarArchive[]>(`/archives/${archiveId}/similar?limit=${limit}`),
  2407. exportArchives: async (options?: {
  2408. format?: 'csv' | 'xlsx';
  2409. fields?: string[];
  2410. printerId?: number;
  2411. projectId?: number;
  2412. status?: string;
  2413. dateFrom?: string;
  2414. dateTo?: string;
  2415. search?: string;
  2416. }): Promise<{ blob: Blob; filename: string }> => {
  2417. const params = new URLSearchParams();
  2418. if (options?.format) params.set('format', options.format);
  2419. if (options?.fields) params.set('fields', options.fields.join(','));
  2420. if (options?.printerId) params.set('printer_id', String(options.printerId));
  2421. if (options?.projectId) params.set('project_id', String(options.projectId));
  2422. if (options?.status) params.set('status', options.status);
  2423. if (options?.dateFrom) params.set('date_from', options.dateFrom);
  2424. if (options?.dateTo) params.set('date_to', options.dateTo);
  2425. if (options?.search) params.set('search', options.search);
  2426. const headers: Record<string, string> = {};
  2427. if (authToken) {
  2428. headers['Authorization'] = `Bearer ${authToken}`;
  2429. }
  2430. const response = await fetch(`${API_BASE}/archives/export?${params}`, { headers });
  2431. if (!response.ok) {
  2432. const error = await response.json().catch(() => ({}));
  2433. throw new Error(error.detail || `HTTP ${response.status}`);
  2434. }
  2435. const contentDisposition = response.headers.get('Content-Disposition');
  2436. let filename = options?.format === 'xlsx' ? 'archives_export.xlsx' : 'archives_export.csv';
  2437. if (contentDisposition) {
  2438. const match = contentDisposition.match(/filename="?([^"]+)"?/);
  2439. if (match) filename = match[1];
  2440. }
  2441. const blob = await response.blob();
  2442. return { blob, filename };
  2443. },
  2444. exportStats: async (options?: {
  2445. format?: 'csv' | 'xlsx';
  2446. days?: number;
  2447. printerId?: number;
  2448. projectId?: number;
  2449. }): Promise<{ blob: Blob; filename: string }> => {
  2450. const params = new URLSearchParams();
  2451. if (options?.format) params.set('format', options.format);
  2452. if (options?.days) params.set('days', String(options.days));
  2453. if (options?.printerId) params.set('printer_id', String(options.printerId));
  2454. if (options?.projectId) params.set('project_id', String(options.projectId));
  2455. const headers: Record<string, string> = {};
  2456. if (authToken) {
  2457. headers['Authorization'] = `Bearer ${authToken}`;
  2458. }
  2459. const response = await fetch(`${API_BASE}/archives/stats/export?${params}`, { headers });
  2460. if (!response.ok) {
  2461. const error = await response.json().catch(() => ({}));
  2462. throw new Error(error.detail || `HTTP ${response.status}`);
  2463. }
  2464. const contentDisposition = response.headers.get('Content-Disposition');
  2465. let filename = options?.format === 'xlsx' ? 'stats_export.xlsx' : 'stats_export.csv';
  2466. if (contentDisposition) {
  2467. const match = contentDisposition.match(/filename="?([^"]+)"?/);
  2468. if (match) filename = match[1];
  2469. }
  2470. const blob = await response.blob();
  2471. return { blob, filename };
  2472. },
  2473. getArchiveDuplicates: (id: number) =>
  2474. request<{ duplicates: ArchiveDuplicate[]; count: number }>(`/archives/${id}/duplicates`),
  2475. backfillContentHashes: () =>
  2476. request<{ updated: number; errors: Array<{ id: number; error: string }> }>('/archives/backfill-hashes', {
  2477. method: 'POST',
  2478. }),
  2479. getArchiveThumbnail: (id: number) => `${API_BASE}/archives/${id}/thumbnail?v=${Date.now()}`,
  2480. getArchivePlateThumbnail: (id: number, plateIndex: number) =>
  2481. `${API_BASE}/archives/${id}/plate-thumbnail/${plateIndex}`,
  2482. getArchiveDownload: (id: number) => `${API_BASE}/archives/${id}/download`,
  2483. downloadArchive: async (id: number, filename?: string): Promise<void> => {
  2484. const headers: Record<string, string> = {};
  2485. if (authToken) {
  2486. headers['Authorization'] = `Bearer ${authToken}`;
  2487. }
  2488. const response = await fetch(`${API_BASE}/archives/${id}/download`, { headers });
  2489. if (!response.ok) {
  2490. const error = await response.json().catch(() => ({}));
  2491. throw new Error(error.detail || `HTTP ${response.status}`);
  2492. }
  2493. const disposition = response.headers.get('Content-Disposition');
  2494. const downloadFilename = parseContentDispositionFilename(disposition) || filename || `archive_${id}.3mf`;
  2495. const blob = await response.blob();
  2496. const url = window.URL.createObjectURL(blob);
  2497. const a = document.createElement('a');
  2498. a.href = url;
  2499. a.download = downloadFilename;
  2500. document.body.appendChild(a);
  2501. a.click();
  2502. document.body.removeChild(a);
  2503. window.URL.revokeObjectURL(url);
  2504. },
  2505. getArchiveGcode: (id: number) => `${API_BASE}/archives/${id}/gcode`,
  2506. getArchivePlatePreview: (id: number) => `${API_BASE}/archives/${id}/plate-preview`,
  2507. getArchiveTimelapse: (id: number) => `${API_BASE}/archives/${id}/timelapse?v=${Date.now()}`,
  2508. scanArchiveTimelapse: (id: number) =>
  2509. request<{
  2510. status: string;
  2511. message: string;
  2512. filename?: string;
  2513. available_files?: Array<{ name: string; path: string; size: number; mtime: string | null }>;
  2514. }>(`/archives/${id}/timelapse/scan`, {
  2515. method: 'POST',
  2516. }),
  2517. selectArchiveTimelapse: (id: number, filename: string) =>
  2518. request<{ status: string; message: string; filename: string }>(
  2519. `/archives/${id}/timelapse/select?filename=${encodeURIComponent(filename)}`,
  2520. { method: 'POST' }
  2521. ),
  2522. deleteArchiveTimelapse: (id: number) =>
  2523. request<{ status: string }>(`/archives/${id}/timelapse`, {
  2524. method: 'DELETE',
  2525. }),
  2526. uploadArchiveTimelapse: async (archiveId: number, file: File): Promise<{ status: string; filename: string }> => {
  2527. const formData = new FormData();
  2528. formData.append('file', file);
  2529. const headers: Record<string, string> = {};
  2530. if (authToken) {
  2531. headers['Authorization'] = `Bearer ${authToken}`;
  2532. }
  2533. const response = await fetch(`${API_BASE}/archives/${archiveId}/timelapse/upload`, {
  2534. method: 'POST',
  2535. headers,
  2536. body: formData,
  2537. });
  2538. if (!response.ok) {
  2539. const error = await response.json().catch(() => ({}));
  2540. throw new Error(error.detail || `HTTP ${response.status}`);
  2541. }
  2542. return response.json();
  2543. },
  2544. // Timelapse Editor
  2545. getTimelapseInfo: (archiveId: number) =>
  2546. request<{
  2547. duration: number;
  2548. width: number;
  2549. height: number;
  2550. fps: number;
  2551. codec: string;
  2552. file_size: number;
  2553. has_audio: boolean;
  2554. }>(`/archives/${archiveId}/timelapse/info`),
  2555. getTimelapseThumbnails: (archiveId: number, count: number = 10) =>
  2556. request<{
  2557. thumbnails: string[];
  2558. timestamps: number[];
  2559. }>(`/archives/${archiveId}/timelapse/thumbnails?count=${count}`),
  2560. processTimelapse: async (
  2561. archiveId: number,
  2562. params: {
  2563. trimStart?: number;
  2564. trimEnd?: number;
  2565. speed?: number;
  2566. saveMode: 'replace' | 'new';
  2567. outputFilename?: string;
  2568. },
  2569. audioFile?: File
  2570. ): Promise<{ status: string; output_path: string | null; message: string }> => {
  2571. const formData = new FormData();
  2572. formData.append('trim_start', String(params.trimStart ?? 0));
  2573. if (params.trimEnd !== undefined) {
  2574. formData.append('trim_end', String(params.trimEnd));
  2575. }
  2576. formData.append('speed', String(params.speed ?? 1));
  2577. formData.append('save_mode', params.saveMode);
  2578. if (params.outputFilename) {
  2579. formData.append('output_filename', params.outputFilename);
  2580. }
  2581. if (audioFile) {
  2582. formData.append('audio', audioFile);
  2583. }
  2584. const headers: Record<string, string> = {};
  2585. if (authToken) {
  2586. headers['Authorization'] = `Bearer ${authToken}`;
  2587. }
  2588. const response = await fetch(`${API_BASE}/archives/${archiveId}/timelapse/process`, {
  2589. method: 'POST',
  2590. headers,
  2591. body: formData,
  2592. });
  2593. if (!response.ok) {
  2594. const error = await response.json().catch(() => ({}));
  2595. throw new Error(error.detail || `HTTP ${response.status}`);
  2596. }
  2597. return response.json();
  2598. },
  2599. // Photos
  2600. getArchivePhotoUrl: (archiveId: number, filename: string) =>
  2601. `${API_BASE}/archives/${archiveId}/photos/${encodeURIComponent(filename)}`,
  2602. uploadArchivePhoto: async (archiveId: number, file: File): Promise<{ status: string; filename: string; photos: string[] }> => {
  2603. const formData = new FormData();
  2604. formData.append('file', file);
  2605. const headers: Record<string, string> = {};
  2606. if (authToken) {
  2607. headers['Authorization'] = `Bearer ${authToken}`;
  2608. }
  2609. const response = await fetch(`${API_BASE}/archives/${archiveId}/photos`, {
  2610. headers,
  2611. method: 'POST',
  2612. body: formData,
  2613. });
  2614. if (!response.ok) {
  2615. const error = await response.json().catch(() => ({}));
  2616. throw new Error(error.detail || `HTTP ${response.status}`);
  2617. }
  2618. return response.json();
  2619. },
  2620. deleteArchivePhoto: (archiveId: number, filename: string) =>
  2621. request<{ status: string; photos: string[] | null }>(`/archives/${archiveId}/photos/${encodeURIComponent(filename)}`, {
  2622. method: 'DELETE',
  2623. }),
  2624. // Source 3MF (original slicer project file)
  2625. getSource3mfDownloadUrl: (archiveId: number) =>
  2626. `${API_BASE}/archives/${archiveId}/source`,
  2627. downloadSource3mf: async (archiveId: number): Promise<void> => {
  2628. const headers: Record<string, string> = {};
  2629. if (authToken) {
  2630. headers['Authorization'] = `Bearer ${authToken}`;
  2631. }
  2632. const response = await fetch(`${API_BASE}/archives/${archiveId}/source`, { headers });
  2633. if (!response.ok) {
  2634. const error = await response.json().catch(() => ({}));
  2635. throw new Error(error.detail || `HTTP ${response.status}`);
  2636. }
  2637. const disposition = response.headers.get('Content-Disposition');
  2638. const filename = parseContentDispositionFilename(disposition) || `source_${archiveId}.3mf`;
  2639. const blob = await response.blob();
  2640. const url = window.URL.createObjectURL(blob);
  2641. const a = document.createElement('a');
  2642. a.href = url;
  2643. a.download = filename;
  2644. document.body.appendChild(a);
  2645. a.click();
  2646. document.body.removeChild(a);
  2647. window.URL.revokeObjectURL(url);
  2648. },
  2649. getSource3mfForSlicer: (archiveId: number, filename: string) => {
  2650. // Sanitize: slicers url_decode() the entire URL, so / \ ? # in filenames break path routing
  2651. const safe = filename.replace(/[/\\?#]/g, '_');
  2652. return `${API_BASE}/archives/${archiveId}/source/${encodeURIComponent(safe.endsWith('.3mf') ? safe : safe + '.3mf')}`;
  2653. },
  2654. createSourceSlicerToken: (archiveId: number) =>
  2655. request<{ token: string }>(`/archives/${archiveId}/source-slicer-token`, { method: 'POST' }),
  2656. getSourceSlicerDownloadUrl: (archiveId: number, token: string, filename: string) => {
  2657. const safe = filename.replace(/[/\\?#]/g, '_');
  2658. return `${API_BASE}/archives/${archiveId}/source-dl/${token}/${encodeURIComponent(safe.endsWith('.3mf') ? safe : safe + '.3mf')}`;
  2659. },
  2660. uploadSource3mf: async (archiveId: number, file: File): Promise<{ status: string; filename: string }> => {
  2661. const formData = new FormData();
  2662. formData.append('file', file);
  2663. const headers: Record<string, string> = {};
  2664. if (authToken) {
  2665. headers['Authorization'] = `Bearer ${authToken}`;
  2666. }
  2667. const response = await fetch(`${API_BASE}/archives/${archiveId}/source`, {
  2668. method: 'POST',
  2669. headers,
  2670. body: formData,
  2671. });
  2672. if (!response.ok) {
  2673. const error = await response.json().catch(() => ({}));
  2674. throw new Error(error.detail || `HTTP ${response.status}`);
  2675. }
  2676. return response.json();
  2677. },
  2678. deleteSource3mf: (archiveId: number) =>
  2679. request<{ status: string }>(`/archives/${archiveId}/source`, {
  2680. method: 'DELETE',
  2681. }),
  2682. // F3D (Fusion 360 design file)
  2683. getF3dDownloadUrl: (archiveId: number) =>
  2684. `${API_BASE}/archives/${archiveId}/f3d`,
  2685. downloadF3d: async (archiveId: number): Promise<void> => {
  2686. const headers: Record<string, string> = {};
  2687. if (authToken) {
  2688. headers['Authorization'] = `Bearer ${authToken}`;
  2689. }
  2690. const response = await fetch(`${API_BASE}/archives/${archiveId}/f3d`, { headers });
  2691. if (!response.ok) {
  2692. const error = await response.json().catch(() => ({}));
  2693. throw new Error(error.detail || `HTTP ${response.status}`);
  2694. }
  2695. const disposition = response.headers.get('Content-Disposition');
  2696. const filename = parseContentDispositionFilename(disposition) || `archive_${archiveId}.f3d`;
  2697. const blob = await response.blob();
  2698. const url = window.URL.createObjectURL(blob);
  2699. const a = document.createElement('a');
  2700. a.href = url;
  2701. a.download = filename;
  2702. document.body.appendChild(a);
  2703. a.click();
  2704. document.body.removeChild(a);
  2705. window.URL.revokeObjectURL(url);
  2706. },
  2707. uploadF3d: async (archiveId: number, file: File): Promise<{ status: string; filename: string }> => {
  2708. const formData = new FormData();
  2709. formData.append('file', file);
  2710. const headers: Record<string, string> = {};
  2711. if (authToken) {
  2712. headers['Authorization'] = `Bearer ${authToken}`;
  2713. }
  2714. const response = await fetch(`${API_BASE}/archives/${archiveId}/f3d`, {
  2715. method: 'POST',
  2716. headers,
  2717. body: formData,
  2718. });
  2719. if (!response.ok) {
  2720. const error = await response.json().catch(() => ({}));
  2721. throw new Error(error.detail || `HTTP ${response.status}`);
  2722. }
  2723. return response.json();
  2724. },
  2725. deleteF3d: (archiveId: number) =>
  2726. request<{ status: string }>(`/archives/${archiveId}/f3d`, {
  2727. method: 'DELETE',
  2728. }),
  2729. // QR Code
  2730. getArchiveQRCodeUrl: (archiveId: number, size = 200) =>
  2731. `${API_BASE}/archives/${archiveId}/qrcode?size=${size}`,
  2732. getArchiveCapabilities: (id: number) =>
  2733. request<{
  2734. has_model: boolean;
  2735. has_gcode: boolean;
  2736. has_source: boolean;
  2737. build_volume: { x: number; y: number; z: number };
  2738. filament_colors: string[];
  2739. }>(`/archives/${id}/capabilities`),
  2740. // Project Page
  2741. getArchiveProjectPage: (id: number) =>
  2742. request<{
  2743. title: string | null;
  2744. description: string | null;
  2745. designer: string | null;
  2746. designer_user_id: string | null;
  2747. license: string | null;
  2748. copyright: string | null;
  2749. creation_date: string | null;
  2750. modification_date: string | null;
  2751. origin: string | null;
  2752. profile_title: string | null;
  2753. profile_description: string | null;
  2754. profile_cover: string | null;
  2755. profile_user_id: string | null;
  2756. profile_user_name: string | null;
  2757. design_model_id: string | null;
  2758. design_profile_id: string | null;
  2759. design_region: string | null;
  2760. model_pictures: Array<{ name: string; path: string; url: string }>;
  2761. profile_pictures: Array<{ name: string; path: string; url: string }>;
  2762. thumbnails: Array<{ name: string; path: string; url: string }>;
  2763. }>(`/archives/${id}/project-page`),
  2764. updateArchiveProjectPage: (id: number, data: {
  2765. title?: string;
  2766. description?: string;
  2767. designer?: string;
  2768. license?: string;
  2769. copyright?: string;
  2770. profile_title?: string;
  2771. profile_description?: string;
  2772. }) =>
  2773. request(`/archives/${id}/project-page`, {
  2774. method: 'PATCH',
  2775. body: JSON.stringify(data),
  2776. }),
  2777. getArchiveProjectImageUrl: (archiveId: number, imagePath: string) =>
  2778. `${API_BASE}/archives/${archiveId}/project-image/${encodeURIComponent(imagePath)}`,
  2779. getArchiveForSlicer: (id: number, filename: string) => {
  2780. const safe = filename.replace(/[/\\?#]/g, '_');
  2781. return `${API_BASE}/archives/${id}/file/${encodeURIComponent(safe.endsWith('.3mf') ? safe : safe + '.3mf')}`;
  2782. },
  2783. createArchiveSlicerToken: (archiveId: number) =>
  2784. request<{ token: string }>(`/archives/${archiveId}/slicer-token`, { method: 'POST' }),
  2785. getArchiveSlicerDownloadUrl: (archiveId: number, token: string, filename: string) => {
  2786. const safe = filename.replace(/[/\\?#]/g, '_');
  2787. return `${API_BASE}/archives/${archiveId}/dl/${token}/${encodeURIComponent(safe.endsWith('.3mf') ? safe : safe + '.3mf')}`;
  2788. },
  2789. getArchivePlates: (archiveId: number) =>
  2790. request<ArchivePlatesResponse>(`/archives/${archiveId}/plates`),
  2791. getArchiveFilamentRequirements: (archiveId: number, plateId?: number) =>
  2792. request<{
  2793. archive_id: number;
  2794. filename: string;
  2795. plate_id: number | null;
  2796. filaments: Array<{
  2797. slot_id: number;
  2798. type: string;
  2799. color: string;
  2800. used_grams: number;
  2801. used_meters: number;
  2802. }>;
  2803. }>(`/archives/${archiveId}/filament-requirements${plateId !== undefined ? `?plate_id=${plateId}` : ''}`),
  2804. reprintArchive: (
  2805. archiveId: number,
  2806. printerId: number,
  2807. options?: {
  2808. plate_id?: number;
  2809. plate_name?: string;
  2810. ams_mapping?: number[];
  2811. timelapse?: boolean;
  2812. bed_levelling?: boolean;
  2813. flow_cali?: boolean;
  2814. vibration_cali?: boolean;
  2815. layer_inspect?: boolean;
  2816. use_ams?: boolean;
  2817. }
  2818. ) =>
  2819. request<BackgroundDispatchResponse>(
  2820. `/archives/${archiveId}/reprint?printer_id=${printerId}`,
  2821. {
  2822. method: 'POST',
  2823. headers: options ? { 'Content-Type': 'application/json' } : undefined,
  2824. body: options ? JSON.stringify(options) : undefined,
  2825. }
  2826. ),
  2827. uploadArchive: async (file: File, printerId?: number): Promise<Archive> => {
  2828. const formData = new FormData();
  2829. formData.append('file', file);
  2830. const url = printerId
  2831. ? `${API_BASE}/archives/upload?printer_id=${printerId}`
  2832. : `${API_BASE}/archives/upload`;
  2833. const headers: Record<string, string> = {};
  2834. if (authToken) {
  2835. headers['Authorization'] = `Bearer ${authToken}`;
  2836. }
  2837. const response = await fetch(url, {
  2838. method: 'POST',
  2839. headers,
  2840. body: formData,
  2841. });
  2842. if (!response.ok) {
  2843. const error = await response.json().catch(() => ({}));
  2844. throw new Error(error.detail || `HTTP ${response.status}`);
  2845. }
  2846. return response.json();
  2847. },
  2848. uploadArchivesBulk: async (files: File[], printerId?: number): Promise<BulkUploadResult> => {
  2849. const formData = new FormData();
  2850. files.forEach((file) => formData.append('files', file));
  2851. const url = printerId
  2852. ? `${API_BASE}/archives/upload-bulk?printer_id=${printerId}`
  2853. : `${API_BASE}/archives/upload-bulk`;
  2854. const headers: Record<string, string> = {};
  2855. if (authToken) {
  2856. headers['Authorization'] = `Bearer ${authToken}`;
  2857. }
  2858. const response = await fetch(url, {
  2859. method: 'POST',
  2860. headers,
  2861. body: formData,
  2862. });
  2863. if (!response.ok) {
  2864. const error = await response.json().catch(() => ({}));
  2865. throw new Error(error.detail || `HTTP ${response.status}`);
  2866. }
  2867. return response.json();
  2868. },
  2869. // Print Log
  2870. getPrintLog: (params?: {
  2871. search?: string;
  2872. printerId?: number;
  2873. username?: string;
  2874. status?: string;
  2875. dateFrom?: string;
  2876. dateTo?: string;
  2877. limit?: number;
  2878. offset?: number;
  2879. }) => {
  2880. const searchParams = new URLSearchParams();
  2881. if (params?.search) searchParams.set('search', params.search);
  2882. if (params?.printerId) searchParams.set('printer_id', String(params.printerId));
  2883. if (params?.username) searchParams.set('created_by_username', params.username);
  2884. if (params?.status) searchParams.set('status', params.status);
  2885. if (params?.dateFrom) searchParams.set('date_from', params.dateFrom);
  2886. if (params?.dateTo) searchParams.set('date_to', params.dateTo);
  2887. if (params?.limit) searchParams.set('limit', String(params.limit));
  2888. if (params?.offset !== undefined) searchParams.set('offset', String(params.offset));
  2889. return request<PrintLogResponse>(`/print-log/?${searchParams}`);
  2890. },
  2891. getPrintLogThumbnail: (id: number) => `${API_BASE}/print-log/${id}/thumbnail`,
  2892. clearPrintLog: () =>
  2893. request<{ deleted: number }>('/print-log/', { method: 'DELETE' }),
  2894. // Settings
  2895. getSettings: () => request<AppSettings>('/settings/'),
  2896. updateSettings: (data: AppSettingsUpdate) =>
  2897. request<AppSettings>('/settings/', {
  2898. method: 'PUT',
  2899. body: JSON.stringify(data),
  2900. }),
  2901. getMQTTStatus: () => request<MQTTStatus>('/settings/mqtt/status'),
  2902. resetSettings: () =>
  2903. request<AppSettings>('/settings/reset', { method: 'POST' }),
  2904. exportBackup: async (): Promise<{ blob: Blob; filename: string }> => {
  2905. // New simplified backup - complete database + all files
  2906. const url = `${API_BASE}/settings/backup`;
  2907. const headers: Record<string, string> = {};
  2908. if (authToken) {
  2909. headers['Authorization'] = `Bearer ${authToken}`;
  2910. }
  2911. const response = await fetch(url, { headers });
  2912. // Check for errors
  2913. if (!response.ok) {
  2914. const errorText = await response.text();
  2915. throw new Error(errorText || `Backup failed with status ${response.status}`);
  2916. }
  2917. // Get filename from Content-Disposition header
  2918. const contentDisposition = response.headers.get('Content-Disposition');
  2919. let filename = 'bambuddy-backup.zip';
  2920. if (contentDisposition) {
  2921. const match = contentDisposition.match(/filename=([^;]+)/);
  2922. if (match) filename = match[1].trim();
  2923. }
  2924. const blob = await response.blob();
  2925. return { blob, filename };
  2926. },
  2927. importBackup: async (file: File) => {
  2928. // New simplified restore - replaces database + all directories
  2929. const formData = new FormData();
  2930. formData.append('file', file);
  2931. const url = `${API_BASE}/settings/restore`;
  2932. const headers: Record<string, string> = {};
  2933. if (authToken) {
  2934. headers['Authorization'] = `Bearer ${authToken}`;
  2935. }
  2936. const response = await fetch(url, {
  2937. method: 'POST',
  2938. headers,
  2939. body: formData,
  2940. });
  2941. return response.json() as Promise<{
  2942. success: boolean;
  2943. message: string;
  2944. }>;
  2945. },
  2946. checkFfmpeg: () =>
  2947. request<{ installed: boolean; path: string | null }>('/settings/check-ffmpeg'),
  2948. getNetworkInterfaces: () =>
  2949. request<{ interfaces: NetworkInterface[] }>('/settings/network-interfaces'),
  2950. // Cloud
  2951. getCloudStatus: () => request<CloudAuthStatus>('/cloud/status'),
  2952. cloudLogin: (email: string, password: string, region = 'global') =>
  2953. request<CloudLoginResponse>('/cloud/login', {
  2954. method: 'POST',
  2955. body: JSON.stringify({ email, password, region }),
  2956. }),
  2957. cloudVerify: (email: string, code: string, tfaKey?: string) =>
  2958. request<CloudLoginResponse>('/cloud/verify', {
  2959. method: 'POST',
  2960. body: JSON.stringify({ email, code, tfa_key: tfaKey }),
  2961. }),
  2962. cloudSetToken: (access_token: string) =>
  2963. request<CloudAuthStatus>('/cloud/token', {
  2964. method: 'POST',
  2965. body: JSON.stringify({ access_token }),
  2966. }),
  2967. cloudLogout: () =>
  2968. request<{ success: boolean }>('/cloud/logout', { method: 'POST' }),
  2969. getCloudSettings: (version = '02.04.00.70') =>
  2970. request<SlicerSettingsResponse>(`/cloud/settings?version=${version}`),
  2971. getBuiltinFilaments: () =>
  2972. request<BuiltinFilament[]>('/cloud/builtin-filaments'),
  2973. getFilamentIdMap: () =>
  2974. request<Record<string, string>>('/cloud/filament-id-map'),
  2975. getCloudSettingDetail: (settingId: string) =>
  2976. request<SlicerSettingDetail>(`/cloud/settings/${settingId}`),
  2977. createCloudSetting: (data: SlicerSettingCreate) =>
  2978. request<SlicerSettingDetail>('/cloud/settings', {
  2979. method: 'POST',
  2980. body: JSON.stringify(data),
  2981. }),
  2982. updateCloudSetting: (settingId: string, data: SlicerSettingUpdate) =>
  2983. request<SlicerSettingDetail>(`/cloud/settings/${settingId}`, {
  2984. method: 'PUT',
  2985. body: JSON.stringify(data),
  2986. }),
  2987. deleteCloudSetting: (settingId: string) =>
  2988. request<SlicerSettingDeleteResponse>(`/cloud/settings/${settingId}`, {
  2989. method: 'DELETE',
  2990. }),
  2991. getCloudDevices: () => request<CloudDevice[]>('/cloud/devices'),
  2992. getCloudFields: (presetType: 'filament' | 'print' | 'process' | 'printer') =>
  2993. request<FieldDefinitionsResponse>(`/cloud/fields/${presetType}`),
  2994. getAllCloudFields: () =>
  2995. request<Record<string, FieldDefinitionsResponse>>('/cloud/fields'),
  2996. getFilamentInfo: (settingIds: string[]) =>
  2997. request<Record<string, { name: string; k: number | null }>>('/cloud/filament-info', {
  2998. method: 'POST',
  2999. body: JSON.stringify(settingIds),
  3000. }),
  3001. // Smart Plugs
  3002. getSmartPlugs: () => request<SmartPlug[]>('/smart-plugs/'),
  3003. getSmartPlug: (id: number) => request<SmartPlug>(`/smart-plugs/${id}`),
  3004. getSmartPlugByPrinter: (printerId: number) => request<SmartPlug | null>(`/smart-plugs/by-printer/${printerId}`),
  3005. getScriptPlugsByPrinter: (printerId: number) => request<SmartPlug[]>(`/smart-plugs/by-printer/${printerId}/scripts`),
  3006. createSmartPlug: (data: SmartPlugCreate) =>
  3007. request<SmartPlug>('/smart-plugs/', {
  3008. method: 'POST',
  3009. body: JSON.stringify(data),
  3010. }),
  3011. updateSmartPlug: (id: number, data: SmartPlugUpdate) =>
  3012. request<SmartPlug>(`/smart-plugs/${id}`, {
  3013. method: 'PATCH',
  3014. body: JSON.stringify(data),
  3015. }),
  3016. deleteSmartPlug: (id: number) =>
  3017. request<void>(`/smart-plugs/${id}`, { method: 'DELETE' }),
  3018. controlSmartPlug: (id: number, action: 'on' | 'off' | 'toggle') =>
  3019. request<{ success: boolean; action: string }>(`/smart-plugs/${id}/control`, {
  3020. method: 'POST',
  3021. body: JSON.stringify({ action }),
  3022. }),
  3023. getSmartPlugStatus: (id: number) =>
  3024. request<SmartPlugStatus>(`/smart-plugs/${id}/status`),
  3025. testSmartPlugConnection: (ip_address: string, username?: string | null, password?: string | null) =>
  3026. request<SmartPlugTestResult>('/smart-plugs/test-connection', {
  3027. method: 'POST',
  3028. body: JSON.stringify({ ip_address, username, password }),
  3029. }),
  3030. // Tasmota Discovery (auto-detects network)
  3031. startTasmotaScan: () =>
  3032. request<TasmotaScanStatus>('/smart-plugs/discover/scan', { method: 'POST' }),
  3033. getTasmotaScanStatus: () =>
  3034. request<TasmotaScanStatus>('/smart-plugs/discover/status'),
  3035. stopTasmotaScan: () =>
  3036. request<TasmotaScanStatus>('/smart-plugs/discover/stop', { method: 'POST' }),
  3037. getDiscoveredTasmotaDevices: () =>
  3038. request<DiscoveredTasmotaDevice[]>('/smart-plugs/discover/devices'),
  3039. // Home Assistant Integration
  3040. testHAConnection: (url: string, token: string) =>
  3041. request<HATestConnectionResult>('/smart-plugs/ha/test-connection', {
  3042. method: 'POST',
  3043. body: JSON.stringify({ url, token }),
  3044. }),
  3045. getHAEntities: (search?: string) => {
  3046. const params = search ? `?search=${encodeURIComponent(search)}` : '';
  3047. return request<HAEntity[]>(`/smart-plugs/ha/entities${params}`);
  3048. },
  3049. getHASensorEntities: () =>
  3050. request<HASensorEntity[]>('/smart-plugs/ha/sensors'),
  3051. // Print Queue
  3052. getQueue: (printerId?: number, status?: string, targetModel?: string) => {
  3053. const params = new URLSearchParams();
  3054. if (printerId) params.set('printer_id', String(printerId));
  3055. if (status) params.set('status', status);
  3056. if (targetModel) params.set('target_model', targetModel);
  3057. return request<PrintQueueItem[]>(`/queue/?${params}`);
  3058. },
  3059. getQueueItem: (id: number) => request<PrintQueueItem>(`/queue/${id}`),
  3060. addToQueue: (data: PrintQueueItemCreate) =>
  3061. request<PrintQueueItem>('/queue/', {
  3062. method: 'POST',
  3063. body: JSON.stringify(data),
  3064. }),
  3065. updateQueueItem: (id: number, data: PrintQueueItemUpdate) =>
  3066. request<PrintQueueItem>(`/queue/${id}`, {
  3067. method: 'PATCH',
  3068. body: JSON.stringify(data),
  3069. }),
  3070. removeFromQueue: (id: number) =>
  3071. request<{ message: string }>(`/queue/${id}`, { method: 'DELETE' }),
  3072. reorderQueue: (items: { id: number; position: number }[]) =>
  3073. request<{ message: string }>('/queue/reorder', {
  3074. method: 'POST',
  3075. body: JSON.stringify({ items }),
  3076. }),
  3077. cancelQueueItem: (id: number) =>
  3078. request<{ message: string }>(`/queue/${id}/cancel`, { method: 'POST' }),
  3079. stopQueueItem: (id: number) =>
  3080. request<{ message: string }>(`/queue/${id}/stop`, { method: 'POST' }),
  3081. startQueueItem: (id: number) =>
  3082. request<PrintQueueItem>(`/queue/${id}/start`, { method: 'POST' }),
  3083. bulkUpdateQueue: (data: PrintQueueBulkUpdate) =>
  3084. request<PrintQueueBulkUpdateResponse>('/queue/bulk', {
  3085. method: 'PATCH',
  3086. body: JSON.stringify(data),
  3087. }),
  3088. // K-Profiles
  3089. getKProfiles: (printerId: number, nozzleDiameter = '0.4') =>
  3090. request<KProfilesResponse>(`/printers/${printerId}/kprofiles/?nozzle_diameter=${nozzleDiameter}`),
  3091. setKProfile: (printerId: number, profile: KProfileCreate) =>
  3092. request<{ success: boolean; message: string }>(`/printers/${printerId}/kprofiles/`, {
  3093. method: 'POST',
  3094. body: JSON.stringify(profile),
  3095. }),
  3096. deleteKProfile: (printerId: number, profile: KProfileDelete) =>
  3097. request<{ success: boolean; message: string }>(`/printers/${printerId}/kprofiles/`, {
  3098. method: 'DELETE',
  3099. body: JSON.stringify(profile),
  3100. }),
  3101. setKProfilesBatch: (printerId: number, profiles: KProfileCreate[]) =>
  3102. request<{ success: boolean; message: string }>(`/printers/${printerId}/kprofiles/batch`, {
  3103. method: 'POST',
  3104. body: JSON.stringify(profiles),
  3105. }),
  3106. // K-Profile Notes (stored locally, not on printer)
  3107. getKProfileNotes: (printerId: number) =>
  3108. request<KProfileNotesResponse>(`/printers/${printerId}/kprofiles/notes`),
  3109. setKProfileNote: (printerId: number, settingId: string, note: string) =>
  3110. request<{ success: boolean; message: string }>(`/printers/${printerId}/kprofiles/notes`, {
  3111. method: 'PUT',
  3112. body: JSON.stringify({ setting_id: settingId, note }),
  3113. }),
  3114. deleteKProfileNote: (printerId: number, settingId: string) =>
  3115. request<{ success: boolean; message: string }>(`/printers/${printerId}/kprofiles/notes/${encodeURIComponent(settingId)}`, {
  3116. method: 'DELETE',
  3117. }),
  3118. // Slot Preset Mappings
  3119. getSlotPresets: (printerId: number) =>
  3120. request<Record<number, SlotPresetMapping>>(`/printers/${printerId}/slot-presets`),
  3121. getSlotPreset: (printerId: number, amsId: number, trayId: number) =>
  3122. request<SlotPresetMapping | null>(`/printers/${printerId}/slot-presets/${amsId}/${trayId}`),
  3123. saveSlotPreset: (printerId: number, amsId: number, trayId: number, presetId: string, presetName: string, presetSource = 'cloud') =>
  3124. request<SlotPresetMapping>(`/printers/${printerId}/slot-presets/${amsId}/${trayId}?preset_id=${encodeURIComponent(presetId)}&preset_name=${encodeURIComponent(presetName)}&preset_source=${encodeURIComponent(presetSource)}`, {
  3125. method: 'PUT',
  3126. }),
  3127. deleteSlotPreset: (printerId: number, amsId: number, trayId: number) =>
  3128. request<{ success: boolean }>(`/printers/${printerId}/slot-presets/${amsId}/${trayId}`, {
  3129. method: 'DELETE',
  3130. }),
  3131. // AMS Labels (user-defined friendly names)
  3132. getAmsLabels: (printerId: number) =>
  3133. request<Record<number, string>>(`/printers/${printerId}/ams-labels`),
  3134. saveAmsLabel: (printerId: number, amsId: number, label: string, amsSerial = '') =>
  3135. request<{ ams_id: number; label: string }>(
  3136. `/printers/${printerId}/ams-labels/${amsId}`,
  3137. {
  3138. method: 'PUT',
  3139. body: JSON.stringify({ label, ams_serial: amsSerial }),
  3140. }
  3141. ),
  3142. deleteAmsLabel: (printerId: number, amsId: number, amsSerial = '') =>
  3143. request<{ success: boolean }>(`/printers/${printerId}/ams-labels/${amsId}?ams_serial=${encodeURIComponent(amsSerial)}`, {
  3144. method: 'DELETE',
  3145. }),
  3146. configureAmsSlot: (
  3147. printerId: number,
  3148. amsId: number,
  3149. trayId: number,
  3150. config: {
  3151. tray_info_idx: string;
  3152. tray_type: string;
  3153. tray_sub_brands: string;
  3154. tray_color: string;
  3155. nozzle_temp_min: number;
  3156. nozzle_temp_max: number;
  3157. cali_idx: number;
  3158. nozzle_diameter: string;
  3159. setting_id?: string;
  3160. kprofile_filament_id?: string;
  3161. kprofile_setting_id?: string;
  3162. k_value?: number;
  3163. }
  3164. ) => {
  3165. const params = new URLSearchParams({
  3166. tray_info_idx: config.tray_info_idx,
  3167. tray_type: config.tray_type,
  3168. tray_sub_brands: config.tray_sub_brands,
  3169. tray_color: config.tray_color,
  3170. nozzle_temp_min: config.nozzle_temp_min.toString(),
  3171. nozzle_temp_max: config.nozzle_temp_max.toString(),
  3172. cali_idx: config.cali_idx.toString(),
  3173. nozzle_diameter: config.nozzle_diameter,
  3174. });
  3175. if (config.setting_id) {
  3176. params.set('setting_id', config.setting_id);
  3177. }
  3178. if (config.kprofile_filament_id) {
  3179. params.set('kprofile_filament_id', config.kprofile_filament_id);
  3180. }
  3181. if (config.kprofile_setting_id) {
  3182. params.set('kprofile_setting_id', config.kprofile_setting_id);
  3183. }
  3184. if (config.k_value !== undefined && config.k_value > 0) {
  3185. params.set('k_value', config.k_value.toString());
  3186. }
  3187. return request<{ success: boolean; message: string }>(
  3188. `/printers/${printerId}/slots/${amsId}/${trayId}/configure?${params}`,
  3189. { method: 'POST' }
  3190. );
  3191. },
  3192. resetAmsSlot: (printerId: number, amsId: number, trayId: number) =>
  3193. request<{ success: boolean; message: string }>(
  3194. `/printers/${printerId}/ams/${amsId}/tray/${trayId}/reset`,
  3195. { method: 'POST' }
  3196. ),
  3197. // Filament Catalog (material types with cost/temp data)
  3198. listFilaments: () => request<Filament[]>('/filament-catalog/'),
  3199. getFilament: (id: number) => request<Filament>(`/filament-catalog/${id}`),
  3200. getFilamentsByType: (type: string) => request<Filament[]>(`/filament-catalog/by-type/${type}`),
  3201. // Notification Providers
  3202. getNotificationProviders: () => request<NotificationProvider[]>('/notifications/'),
  3203. getNotificationProvider: (id: number) => request<NotificationProvider>(`/notifications/${id}`),
  3204. createNotificationProvider: (data: NotificationProviderCreate) =>
  3205. request<NotificationProvider>('/notifications/', {
  3206. method: 'POST',
  3207. body: JSON.stringify(data),
  3208. }),
  3209. updateNotificationProvider: (id: number, data: NotificationProviderUpdate) =>
  3210. request<NotificationProvider>(`/notifications/${id}`, {
  3211. method: 'PATCH',
  3212. body: JSON.stringify(data),
  3213. }),
  3214. deleteNotificationProvider: (id: number) =>
  3215. request<{ message: string }>(`/notifications/${id}`, { method: 'DELETE' }),
  3216. testNotificationProvider: (id: number) =>
  3217. request<NotificationTestResponse>(`/notifications/${id}/test`, { method: 'POST' }),
  3218. testNotificationConfig: (data: NotificationTestRequest) =>
  3219. request<NotificationTestResponse>('/notifications/test-config', {
  3220. method: 'POST',
  3221. body: JSON.stringify(data),
  3222. }),
  3223. testAllNotificationProviders: () =>
  3224. request<{
  3225. tested: number;
  3226. success: number;
  3227. failed: number;
  3228. results: Array<{
  3229. provider_id: number;
  3230. provider_name: string;
  3231. provider_type: string;
  3232. success: boolean;
  3233. message: string;
  3234. }>;
  3235. }>('/notifications/test-all', { method: 'POST' }),
  3236. // Notification Templates
  3237. getNotificationTemplates: () => request<NotificationTemplate[]>('/notification-templates'),
  3238. getNotificationTemplate: (id: number) => request<NotificationTemplate>(`/notification-templates/${id}`),
  3239. updateNotificationTemplate: (id: number, data: NotificationTemplateUpdate) =>
  3240. request<NotificationTemplate>(`/notification-templates/${id}`, {
  3241. method: 'PUT',
  3242. body: JSON.stringify(data),
  3243. }),
  3244. resetNotificationTemplate: (id: number) =>
  3245. request<NotificationTemplate>(`/notification-templates/${id}/reset`, {
  3246. method: 'POST',
  3247. }),
  3248. getTemplateVariables: () => request<EventVariablesResponse[]>('/notification-templates/variables'),
  3249. previewTemplate: (data: TemplatePreviewRequest) =>
  3250. request<TemplatePreviewResponse>('/notification-templates/preview', {
  3251. method: 'POST',
  3252. body: JSON.stringify(data),
  3253. }),
  3254. // Notification Logs
  3255. getNotificationLogs: (params?: {
  3256. limit?: number;
  3257. offset?: number;
  3258. provider_id?: number;
  3259. event_type?: string;
  3260. success?: boolean;
  3261. days?: number;
  3262. }) => {
  3263. const searchParams = new URLSearchParams();
  3264. if (params?.limit) searchParams.set('limit', String(params.limit));
  3265. if (params?.offset) searchParams.set('offset', String(params.offset));
  3266. if (params?.provider_id) searchParams.set('provider_id', String(params.provider_id));
  3267. if (params?.event_type) searchParams.set('event_type', params.event_type);
  3268. if (params?.success !== undefined) searchParams.set('success', String(params.success));
  3269. if (params?.days) searchParams.set('days', String(params.days));
  3270. return request<NotificationLogEntry[]>(`/notifications/logs?${searchParams}`);
  3271. },
  3272. getNotificationLogStats: (days = 7) =>
  3273. request<NotificationLogStats>(`/notifications/logs/stats?days=${days}`),
  3274. clearNotificationLogs: (olderThanDays = 30) =>
  3275. request<{ deleted: number; message: string }>(
  3276. `/notifications/logs?older_than_days=${olderThanDays}`,
  3277. { method: 'DELETE' }
  3278. ),
  3279. // Spoolman Integration
  3280. getSpoolmanStatus: () => request<SpoolmanStatus>('/spoolman/status'),
  3281. connectSpoolman: () =>
  3282. request<{ success: boolean; message: string }>('/spoolman/connect', {
  3283. method: 'POST',
  3284. }),
  3285. disconnectSpoolman: () =>
  3286. request<{ success: boolean; message: string }>('/spoolman/disconnect', {
  3287. method: 'POST',
  3288. }),
  3289. syncPrinterAms: (printerId: number) =>
  3290. request<SpoolmanSyncResult>(`/spoolman/sync/${printerId}`, {
  3291. method: 'POST',
  3292. }),
  3293. syncAllPrintersAms: () =>
  3294. request<SpoolmanSyncResult>('/spoolman/sync-all', {
  3295. method: 'POST',
  3296. }),
  3297. getSpoolmanSpools: () =>
  3298. request<{ spools: unknown[] }>('/spoolman/spools'),
  3299. getSpoolmanFilaments: () =>
  3300. request<{ filaments: unknown[] }>('/spoolman/filaments'),
  3301. getUnlinkedSpools: () =>
  3302. request<UnlinkedSpool[]>('/spoolman/spools/unlinked'),
  3303. getLinkedSpools: () =>
  3304. request<LinkedSpoolsMap>('/spoolman/spools/linked'),
  3305. linkSpool: (spoolId: number, trayUuid: string) =>
  3306. request<{ success: boolean; message: string }>(`/spoolman/spools/${spoolId}/link`, {
  3307. method: 'POST',
  3308. body: JSON.stringify({ tray_uuid: trayUuid }),
  3309. }),
  3310. getSpoolmanSettings: () =>
  3311. request<{ spoolman_enabled: string; spoolman_url: string; spoolman_sync_mode: string; spoolman_disable_weight_sync: string; spoolman_report_partial_usage: string; }>('/settings/spoolman'),
  3312. updateSpoolmanSettings: (data: { spoolman_enabled?: string; spoolman_url?: string; spoolman_sync_mode?: string; spoolman_disable_weight_sync?: string; spoolman_report_partial_usage?: string; }) =>
  3313. request<{ spoolman_enabled: string; spoolman_url: string; spoolman_sync_mode: string; spoolman_disable_weight_sync: string; spoolman_report_partial_usage: string; }>('/settings/spoolman', {
  3314. method: 'PUT',
  3315. body: JSON.stringify(data),
  3316. }),
  3317. // Inventory
  3318. getSpools: (includeArchived = false) =>
  3319. request<InventorySpool[]>(`/inventory/spools?include_archived=${includeArchived}`),
  3320. getSpool: (id: number) => request<InventorySpool>(`/inventory/spools/${id}`),
  3321. createSpool: (data: Omit<InventorySpool, 'id' | 'archived_at' | 'created_at' | 'updated_at' | 'k_profiles'>) =>
  3322. request<InventorySpool>('/inventory/spools', {
  3323. method: 'POST',
  3324. body: JSON.stringify(data),
  3325. }),
  3326. bulkCreateSpools: (data: Omit<InventorySpool, 'id' | 'archived_at' | 'created_at' | 'updated_at' | 'k_profiles'>, quantity: number) =>
  3327. request<InventorySpool[]>('/inventory/spools/bulk', {
  3328. method: 'POST',
  3329. body: JSON.stringify({ spool: data, quantity }),
  3330. }),
  3331. updateSpool: (id: number, data: Partial<Omit<InventorySpool, 'id' | 'archived_at' | 'created_at' | 'updated_at' | 'k_profiles'>>) =>
  3332. request<InventorySpool>(`/inventory/spools/${id}`, {
  3333. method: 'PATCH',
  3334. body: JSON.stringify(data),
  3335. }),
  3336. deleteSpool: (id: number) =>
  3337. request<{ status: string }>(`/inventory/spools/${id}`, { method: 'DELETE' }),
  3338. archiveSpool: (id: number) =>
  3339. request<InventorySpool>(`/inventory/spools/${id}/archive`, { method: 'POST' }),
  3340. restoreSpool: (id: number) =>
  3341. request<InventorySpool>(`/inventory/spools/${id}/restore`, { method: 'POST' }),
  3342. getSpoolKProfiles: (spoolId: number) =>
  3343. request<SpoolKProfile[]>(`/inventory/spools/${spoolId}/k-profiles`),
  3344. saveSpoolKProfiles: (spoolId: number, profiles: SpoolKProfileInput[]) =>
  3345. request<SpoolKProfile[]>(`/inventory/spools/${spoolId}/k-profiles`, {
  3346. method: 'PUT',
  3347. body: JSON.stringify(profiles),
  3348. }),
  3349. getAssignments: (printerId?: number) =>
  3350. request<SpoolAssignment[]>(`/inventory/assignments${printerId ? `?printer_id=${printerId}` : ''}`),
  3351. assignSpool: (data: { spool_id: number; printer_id: number; ams_id: number; tray_id: number }) =>
  3352. request<SpoolAssignment>('/inventory/assignments', {
  3353. method: 'POST',
  3354. body: JSON.stringify(data),
  3355. }),
  3356. unassignSpool: (printerId: number, amsId: number, trayId: number) =>
  3357. request<{ status: string }>(`/inventory/assignments/${printerId}/${amsId}/${trayId}`, { method: 'DELETE' }),
  3358. getSpoolCatalog: () =>
  3359. request<SpoolCatalogEntry[]>('/inventory/catalog'),
  3360. addCatalogEntry: (data: { name: string; weight: number }) =>
  3361. request<SpoolCatalogEntry>('/inventory/catalog', { method: 'POST', body: JSON.stringify(data) }),
  3362. updateCatalogEntry: (id: number, data: { name: string; weight: number }) =>
  3363. request<SpoolCatalogEntry>(`/inventory/catalog/${id}`, { method: 'PUT', body: JSON.stringify(data) }),
  3364. deleteCatalogEntry: (id: number) =>
  3365. request<{ status: string }>(`/inventory/catalog/${id}`, { method: 'DELETE' }),
  3366. resetSpoolCatalog: () =>
  3367. request<{ status: string }>('/inventory/catalog/reset', { method: 'POST' }),
  3368. getColorCatalog: () =>
  3369. request<ColorCatalogEntry[]>('/inventory/colors'),
  3370. addColorEntry: (data: { manufacturer: string; color_name: string; hex_color: string; material: string | null }) =>
  3371. request<ColorCatalogEntry>('/inventory/colors', { method: 'POST', body: JSON.stringify(data) }),
  3372. updateColorEntry: (id: number, data: { manufacturer: string; color_name: string; hex_color: string; material: string | null }) =>
  3373. request<ColorCatalogEntry>(`/inventory/colors/${id}`, { method: 'PUT', body: JSON.stringify(data) }),
  3374. deleteColorEntry: (id: number) =>
  3375. request<{ status: string }>(`/inventory/colors/${id}`, { method: 'DELETE' }),
  3376. resetColorCatalog: () =>
  3377. request<{ status: string }>('/inventory/colors/reset', { method: 'POST' }),
  3378. lookupColor: (manufacturer: string, colorName: string, material?: string) =>
  3379. request<ColorLookupResult>(`/inventory/colors/lookup?manufacturer=${encodeURIComponent(manufacturer)}&color_name=${encodeURIComponent(colorName)}${material ? `&material=${encodeURIComponent(material)}` : ''}`),
  3380. searchColors: (manufacturer?: string, material?: string) =>
  3381. request<ColorCatalogEntry[]>(`/inventory/colors/search?${manufacturer ? `manufacturer=${encodeURIComponent(manufacturer)}` : ''}${manufacturer && material ? '&' : ''}${material ? `material=${encodeURIComponent(material)}` : ''}`),
  3382. linkTagToSpool: (spoolId: number, data: { tag_uid?: string; tray_uuid?: string; tag_type?: string; data_origin?: string }) =>
  3383. request<InventorySpool>(`/inventory/spools/${spoolId}/link-tag`, {
  3384. method: 'PATCH',
  3385. body: JSON.stringify(data),
  3386. }),
  3387. getSpoolUsageHistory: (spoolId: number, limit = 50) =>
  3388. request<SpoolUsageRecord[]>(`/inventory/spools/${spoolId}/usage?limit=${limit}`),
  3389. getAllUsageHistory: (limit = 100, printerId?: number) =>
  3390. request<SpoolUsageRecord[]>(`/inventory/usage?limit=${limit}${printerId ? `&printer_id=${printerId}` : ''}`),
  3391. clearSpoolUsageHistory: (spoolId: number) =>
  3392. request<{ status: string }>(`/inventory/spools/${spoolId}/usage`, { method: 'DELETE' }),
  3393. syncWeightsFromAms: () =>
  3394. request<{ synced: number; skipped: number }>('/inventory/sync-ams-weights', { method: 'POST' }),
  3395. getFilamentPresets: () =>
  3396. request<SlicerSetting[]>('/cloud/filaments'),
  3397. // Updates
  3398. getVersion: () => request<VersionInfo>('/updates/version'),
  3399. checkForUpdates: () => request<UpdateCheckResult>('/updates/check'),
  3400. applyUpdate: () =>
  3401. request<{ success: boolean; message: string; status?: UpdateStatus; is_docker?: boolean }>('/updates/apply', {
  3402. method: 'POST',
  3403. }),
  3404. getUpdateStatus: () => request<UpdateStatus>('/updates/status'),
  3405. // Maintenance
  3406. getMaintenanceTypes: () => request<MaintenanceType[]>('/maintenance/types'),
  3407. createMaintenanceType: (data: MaintenanceTypeCreate) =>
  3408. request<MaintenanceType>('/maintenance/types', {
  3409. method: 'POST',
  3410. body: JSON.stringify(data),
  3411. }),
  3412. updateMaintenanceType: (id: number, data: Partial<MaintenanceTypeCreate>) =>
  3413. request<MaintenanceType>(`/maintenance/types/${id}`, {
  3414. method: 'PATCH',
  3415. body: JSON.stringify(data),
  3416. }),
  3417. deleteMaintenanceType: (id: number) =>
  3418. request<{ status: string }>(`/maintenance/types/${id}`, { method: 'DELETE' }),
  3419. restoreDefaultMaintenanceTypes: () =>
  3420. request<{ restored: number }>(`/maintenance/types/restore-defaults`, { method: 'POST' }),
  3421. getMaintenanceOverview: () => request<PrinterMaintenanceOverview[]>('/maintenance/overview'),
  3422. getPrinterMaintenance: (printerId: number) =>
  3423. request<PrinterMaintenanceOverview>(`/maintenance/printers/${printerId}`),
  3424. updateMaintenanceItem: (itemId: number, data: { custom_interval_hours?: number | null; custom_interval_type?: 'hours' | 'days' | null; enabled?: boolean }) =>
  3425. request<MaintenanceStatus>(`/maintenance/items/${itemId}`, {
  3426. method: 'PATCH',
  3427. body: JSON.stringify(data),
  3428. }),
  3429. performMaintenance: (itemId: number, notes?: string) =>
  3430. request<MaintenanceStatus>(`/maintenance/items/${itemId}/perform`, {
  3431. method: 'POST',
  3432. body: JSON.stringify({ notes }),
  3433. }),
  3434. getMaintenanceHistory: (itemId: number) =>
  3435. request<MaintenanceHistory[]>(`/maintenance/items/${itemId}/history`),
  3436. getMaintenanceSummary: () => request<MaintenanceSummary>('/maintenance/summary'),
  3437. setPrinterHours: (printerId: number, totalHours: number) =>
  3438. request<{ printer_id: number; total_hours: number; archive_hours: number; offset_hours: number }>(
  3439. `/maintenance/printers/${printerId}/hours?total_hours=${totalHours}`,
  3440. { method: 'PATCH' }
  3441. ),
  3442. assignMaintenanceType: (printerId: number, typeId: number) =>
  3443. request<MaintenanceStatus>(`/maintenance/printers/${printerId}/assign/${typeId}`, {
  3444. method: 'POST',
  3445. }),
  3446. removeMaintenanceItem: (itemId: number) =>
  3447. request<{ status: string }>(`/maintenance/items/${itemId}`, {
  3448. method: 'DELETE',
  3449. }),
  3450. // Camera
  3451. getCameraStreamUrl: (printerId: number, fps = 10) =>
  3452. `${API_BASE}/printers/${printerId}/camera/stream?fps=${fps}`,
  3453. getCameraSnapshotUrl: (printerId: number) =>
  3454. `${API_BASE}/printers/${printerId}/camera/snapshot`,
  3455. testCameraConnection: (printerId: number) =>
  3456. request<{ success: boolean; message?: string; error?: string }>(`/printers/${printerId}/camera/test`),
  3457. getCameraStatus: (printerId: number) =>
  3458. request<{ active: boolean; stalled: boolean }>(`/printers/${printerId}/camera/status`),
  3459. // Plate Detection - Multi-reference calibration (stores up to 5 references per printer)
  3460. checkPlateEmpty: (printerId: number, options?: { useExternal?: boolean; includeDebugImage?: boolean }) => {
  3461. const params = new URLSearchParams();
  3462. params.set('use_external', String(options?.useExternal ?? false));
  3463. params.set('include_debug_image', String(options?.includeDebugImage ?? false));
  3464. return request<PlateDetectionResult>(
  3465. `/printers/${printerId}/camera/check-plate?${params.toString()}`
  3466. );
  3467. },
  3468. getPlateDetectionStatus: (printerId: number) => {
  3469. return request<PlateDetectionStatus & { chamber_light?: boolean }>(
  3470. `/printers/${printerId}/camera/plate-detection/status`
  3471. );
  3472. },
  3473. calibratePlateDetection: (printerId: number, options?: { label?: string; useExternal?: boolean }) => {
  3474. const params = new URLSearchParams();
  3475. if (options?.label) params.set('label', options.label);
  3476. params.set('use_external', String(options?.useExternal ?? false));
  3477. return request<CalibrationResult & { index: number }>(
  3478. `/printers/${printerId}/camera/plate-detection/calibrate?${params.toString()}`,
  3479. { method: 'POST' }
  3480. );
  3481. },
  3482. deletePlateCalibration: (printerId: number) => {
  3483. return request<CalibrationResult>(
  3484. `/printers/${printerId}/camera/plate-detection/calibrate`,
  3485. { method: 'DELETE' }
  3486. );
  3487. },
  3488. getPlateReferences: (printerId: number) => {
  3489. return request<{
  3490. references: PlateReference[];
  3491. max_references: number;
  3492. }>(`/printers/${printerId}/camera/plate-detection/references`);
  3493. },
  3494. getPlateReferenceThumbnailUrl: (printerId: number, index: number) => {
  3495. return `${API_BASE}/printers/${printerId}/camera/plate-detection/references/${index}/thumbnail`;
  3496. },
  3497. updatePlateReferenceLabel: (printerId: number, index: number, label: string) => {
  3498. const params = new URLSearchParams();
  3499. params.set('label', label);
  3500. return request<{ success: boolean; index: number; label: string }>(
  3501. `/printers/${printerId}/camera/plate-detection/references/${index}?${params.toString()}`,
  3502. { method: 'PUT' }
  3503. );
  3504. },
  3505. deletePlateReference: (printerId: number, index: number) => {
  3506. return request<{ success: boolean; message: string }>(
  3507. `/printers/${printerId}/camera/plate-detection/references/${index}`,
  3508. { method: 'DELETE' }
  3509. );
  3510. },
  3511. // External Links
  3512. getExternalLinks: () => request<ExternalLink[]>('/external-links/'),
  3513. getExternalLink: (id: number) => request<ExternalLink>(`/external-links/${id}`),
  3514. createExternalLink: (data: ExternalLinkCreate) =>
  3515. request<ExternalLink>('/external-links/', {
  3516. method: 'POST',
  3517. body: JSON.stringify(data),
  3518. }),
  3519. updateExternalLink: (id: number, data: ExternalLinkUpdate) =>
  3520. request<ExternalLink>(`/external-links/${id}`, {
  3521. method: 'PATCH',
  3522. body: JSON.stringify(data),
  3523. }),
  3524. deleteExternalLink: (id: number) =>
  3525. request<{ message: string }>(`/external-links/${id}`, { method: 'DELETE' }),
  3526. reorderExternalLinks: (ids: number[]) =>
  3527. request<ExternalLink[]>('/external-links/reorder', {
  3528. method: 'PUT',
  3529. body: JSON.stringify({ ids }),
  3530. }),
  3531. uploadExternalLinkIcon: async (id: number, file: File): Promise<ExternalLink> => {
  3532. const formData = new FormData();
  3533. formData.append('file', file);
  3534. const headers: Record<string, string> = {};
  3535. if (authToken) {
  3536. headers['Authorization'] = `Bearer ${authToken}`;
  3537. }
  3538. const response = await fetch(`${API_BASE}/external-links/${id}/icon`, {
  3539. method: 'POST',
  3540. headers,
  3541. body: formData,
  3542. });
  3543. if (!response.ok) {
  3544. const error = await response.json().catch(() => ({}));
  3545. throw new Error(error.detail || `HTTP ${response.status}`);
  3546. }
  3547. return response.json();
  3548. },
  3549. deleteExternalLinkIcon: (id: number) =>
  3550. request<ExternalLink>(`/external-links/${id}/icon`, { method: 'DELETE' }),
  3551. getExternalLinkIconUrl: (id: number) => `${API_BASE}/external-links/${id}/icon`,
  3552. // Projects
  3553. getProjects: (status?: string) => {
  3554. const params = new URLSearchParams();
  3555. if (status) params.set('status', status);
  3556. return request<ProjectListItem[]>(`/projects/?${params}`);
  3557. },
  3558. getProject: (id: number) => request<Project>(`/projects/${id}`),
  3559. createProject: (data: ProjectCreate) =>
  3560. request<Project>('/projects/', {
  3561. method: 'POST',
  3562. body: JSON.stringify(data),
  3563. }),
  3564. updateProject: (id: number, data: ProjectUpdate) =>
  3565. request<Project>(`/projects/${id}`, {
  3566. method: 'PATCH',
  3567. body: JSON.stringify(data),
  3568. }),
  3569. deleteProject: (id: number) =>
  3570. request<{ message: string }>(`/projects/${id}`, { method: 'DELETE' }),
  3571. getProjectArchives: (id: number, limit = 100, offset = 0) =>
  3572. request<Archive[]>(`/projects/${id}/archives?limit=${limit}&offset=${offset}`),
  3573. addArchivesToProject: (projectId: number, archiveIds: number[]) =>
  3574. request<{ message: string }>(`/projects/${projectId}/add-archives`, {
  3575. method: 'POST',
  3576. body: JSON.stringify({ archive_ids: archiveIds }),
  3577. }),
  3578. removeArchivesFromProject: (projectId: number, archiveIds: number[]) =>
  3579. request<{ message: string }>(`/projects/${projectId}/remove-archives`, {
  3580. method: 'POST',
  3581. body: JSON.stringify({ archive_ids: archiveIds }),
  3582. }),
  3583. addQueueItemsToProject: (projectId: number, queueItemIds: number[]) =>
  3584. request<{ message: string }>(`/projects/${projectId}/add-queue`, {
  3585. method: 'POST',
  3586. body: JSON.stringify({ queue_item_ids: queueItemIds }),
  3587. }),
  3588. // Project Attachments
  3589. uploadProjectAttachment: async (projectId: number, file: File): Promise<{
  3590. status: string;
  3591. filename: string;
  3592. original_name: string;
  3593. attachments: ProjectAttachment[];
  3594. }> => {
  3595. const formData = new FormData();
  3596. formData.append('file', file);
  3597. const headers: Record<string, string> = {};
  3598. if (authToken) {
  3599. headers['Authorization'] = `Bearer ${authToken}`;
  3600. }
  3601. const response = await fetch(`${API_BASE}/projects/${projectId}/attachments`, {
  3602. method: 'POST',
  3603. headers,
  3604. body: formData,
  3605. });
  3606. if (!response.ok) {
  3607. const error = await response.json().catch(() => ({}));
  3608. throw new Error(error.detail || `HTTP ${response.status}`);
  3609. }
  3610. return response.json();
  3611. },
  3612. getProjectAttachmentUrl: (projectId: number, filename: string) =>
  3613. `${API_BASE}/projects/${projectId}/attachments/${encodeURIComponent(filename)}`,
  3614. deleteProjectAttachment: (projectId: number, filename: string) =>
  3615. request<{ status: string; message: string; attachments: ProjectAttachment[] | null }>(
  3616. `/projects/${projectId}/attachments/${encodeURIComponent(filename)}`,
  3617. { method: 'DELETE' }
  3618. ),
  3619. // BOM (Bill of Materials)
  3620. getProjectBOM: (projectId: number) =>
  3621. request<BOMItem[]>(`/projects/${projectId}/bom`),
  3622. createBOMItem: (projectId: number, data: BOMItemCreate) =>
  3623. request<BOMItem>(`/projects/${projectId}/bom`, {
  3624. method: 'POST',
  3625. body: JSON.stringify(data),
  3626. }),
  3627. updateBOMItem: (projectId: number, itemId: number, data: BOMItemUpdate) =>
  3628. request<BOMItem>(`/projects/${projectId}/bom/${itemId}`, {
  3629. method: 'PATCH',
  3630. body: JSON.stringify(data),
  3631. }),
  3632. deleteBOMItem: (projectId: number, itemId: number) =>
  3633. request<{ status: string; message: string }>(`/projects/${projectId}/bom/${itemId}`, {
  3634. method: 'DELETE',
  3635. }),
  3636. // Templates
  3637. getTemplates: () => request<ProjectListItem[]>('/projects/templates/'),
  3638. createTemplateFromProject: (projectId: number) =>
  3639. request<Project>(`/projects/${projectId}/create-template`, { method: 'POST' }),
  3640. createProjectFromTemplate: (templateId: number, name?: string) =>
  3641. request<Project>(`/projects/from-template/${templateId}${name ? `?name=${encodeURIComponent(name)}` : ''}`, {
  3642. method: 'POST',
  3643. }),
  3644. // Timeline
  3645. getProjectTimeline: (projectId: number, limit = 50) =>
  3646. request<TimelineEvent[]>(`/projects/${projectId}/timeline?limit=${limit}`),
  3647. // Project Export/Import
  3648. exportProjectJson: (projectId: number) =>
  3649. request<ProjectExport>(`/projects/${projectId}/export?format=json`),
  3650. importProject: (data: ProjectImport) =>
  3651. request<Project>('/projects/import', {
  3652. method: 'POST',
  3653. body: JSON.stringify(data),
  3654. }),
  3655. importProjectFile: async (file: File): Promise<Project> => {
  3656. const formData = new FormData();
  3657. formData.append('file', file);
  3658. const headers: Record<string, string> = {};
  3659. if (authToken) {
  3660. headers['Authorization'] = `Bearer ${authToken}`;
  3661. }
  3662. const response = await fetch(`${API_BASE}/projects/import/file`, {
  3663. method: 'POST',
  3664. headers,
  3665. body: formData,
  3666. });
  3667. if (!response.ok) {
  3668. const error = await response.json().catch(() => ({}));
  3669. throw new Error(error.detail || `HTTP ${response.status}`);
  3670. }
  3671. return response.json();
  3672. },
  3673. exportProjectZip: async (projectId: number): Promise<{ blob: Blob; filename: string }> => {
  3674. const headers: Record<string, string> = {};
  3675. if (authToken) {
  3676. headers['Authorization'] = `Bearer ${authToken}`;
  3677. }
  3678. const response = await fetch(`${API_BASE}/projects/${projectId}/export`, {
  3679. headers,
  3680. });
  3681. if (!response.ok) {
  3682. const error = await response.json().catch(() => ({}));
  3683. throw new Error(error.detail || `HTTP ${response.status}`);
  3684. }
  3685. const contentDisposition = response.headers.get('Content-Disposition');
  3686. const filename = parseContentDispositionFilename(contentDisposition) || `project_${projectId}.zip`;
  3687. const blob = await response.blob();
  3688. return { blob, filename };
  3689. },
  3690. // API Keys
  3691. getAPIKeys: () => request<APIKey[]>('/api-keys/'),
  3692. createAPIKey: (data: APIKeyCreate) =>
  3693. request<APIKeyCreateResponse>('/api-keys/', {
  3694. method: 'POST',
  3695. body: JSON.stringify(data),
  3696. }),
  3697. updateAPIKey: (id: number, data: APIKeyUpdate) =>
  3698. request<APIKey>(`/api-keys/${id}`, {
  3699. method: 'PATCH',
  3700. body: JSON.stringify(data),
  3701. }),
  3702. deleteAPIKey: (id: number) =>
  3703. request<{ message: string }>(`/api-keys/${id}`, { method: 'DELETE' }),
  3704. // AMS History
  3705. getAMSHistory: (printerId: number, amsId: number, hours = 24) =>
  3706. request<AMSHistoryResponse>(`/ams-history/${printerId}/${amsId}?hours=${hours}`),
  3707. // System Info
  3708. getSystemInfo: () => request<SystemInfo>('/system/info'),
  3709. getStorageUsage: (options?: { refresh?: boolean }) => {
  3710. const params = new URLSearchParams();
  3711. if (options?.refresh) {
  3712. params.set('refresh', 'true');
  3713. }
  3714. const query = params.toString();
  3715. return request<StorageUsageResponse>(`/system/storage-usage${query ? `?${query}` : ''}`);
  3716. },
  3717. // Library (File Manager)
  3718. getLibraryFolders: () => request<LibraryFolderTree[]>('/library/folders'),
  3719. createLibraryFolder: (data: LibraryFolderCreate) =>
  3720. request<LibraryFolder>('/library/folders', {
  3721. method: 'POST',
  3722. body: JSON.stringify(data),
  3723. }),
  3724. updateLibraryFolder: (id: number, data: LibraryFolderUpdate) =>
  3725. request<LibraryFolder>(`/library/folders/${id}`, {
  3726. method: 'PUT',
  3727. body: JSON.stringify(data),
  3728. }),
  3729. deleteLibraryFolder: (id: number) =>
  3730. request<{ status: string; message: string }>(`/library/folders/${id}`, { method: 'DELETE' }),
  3731. getLibraryFoldersByProject: (projectId: number) =>
  3732. request<LibraryFolder[]>(`/library/folders/by-project/${projectId}`),
  3733. getLibraryFoldersByArchive: (archiveId: number) =>
  3734. request<LibraryFolder[]>(`/library/folders/by-archive/${archiveId}`),
  3735. getLibraryFiles: (folderId?: number | null, includeRoot = true) => {
  3736. const params = new URLSearchParams();
  3737. if (folderId !== undefined && folderId !== null) {
  3738. params.set('folder_id', String(folderId));
  3739. }
  3740. params.set('include_root', String(includeRoot));
  3741. return request<LibraryFileListItem[]>(`/library/files?${params}`);
  3742. },
  3743. getLibraryFile: (id: number) => request<LibraryFile>(`/library/files/${id}`),
  3744. uploadLibraryFile: async (
  3745. file: File,
  3746. folderId?: number | null,
  3747. generateStlThumbnails: boolean = true
  3748. ): Promise<LibraryFileUploadResponse> => {
  3749. const formData = new FormData();
  3750. formData.append('file', file);
  3751. const params = new URLSearchParams();
  3752. if (folderId) params.set('folder_id', String(folderId));
  3753. params.set('generate_stl_thumbnails', String(generateStlThumbnails));
  3754. const headers: Record<string, string> = {};
  3755. if (authToken) {
  3756. headers['Authorization'] = `Bearer ${authToken}`;
  3757. }
  3758. const response = await fetch(`${API_BASE}/library/files?${params}`, {
  3759. method: 'POST',
  3760. headers,
  3761. body: formData,
  3762. });
  3763. if (!response.ok) {
  3764. const error = await response.json().catch(() => ({}));
  3765. throw new Error(error.detail || `HTTP ${response.status}`);
  3766. }
  3767. return response.json();
  3768. },
  3769. extractZipFile: async (
  3770. file: File,
  3771. folderId?: number | null,
  3772. preserveStructure: boolean = true,
  3773. createFolderFromZip: boolean = false,
  3774. generateStlThumbnails: boolean = true
  3775. ): Promise<ZipExtractResponse> => {
  3776. const formData = new FormData();
  3777. formData.append('file', file);
  3778. const params = new URLSearchParams();
  3779. if (folderId) params.set('folder_id', String(folderId));
  3780. params.set('preserve_structure', String(preserveStructure));
  3781. params.set('create_folder_from_zip', String(createFolderFromZip));
  3782. params.set('generate_stl_thumbnails', String(generateStlThumbnails));
  3783. const headers: Record<string, string> = {};
  3784. if (authToken) {
  3785. headers['Authorization'] = `Bearer ${authToken}`;
  3786. }
  3787. const response = await fetch(`${API_BASE}/library/files/extract-zip?${params}`, {
  3788. method: 'POST',
  3789. headers,
  3790. body: formData,
  3791. });
  3792. if (!response.ok) {
  3793. const error = await response.json().catch(() => ({}));
  3794. throw new Error(error.detail || `HTTP ${response.status}`);
  3795. }
  3796. return response.json();
  3797. },
  3798. updateLibraryFile: (id: number, data: LibraryFileUpdate) =>
  3799. request<LibraryFile>(`/library/files/${id}`, {
  3800. method: 'PUT',
  3801. body: JSON.stringify(data),
  3802. }),
  3803. deleteLibraryFile: (id: number) =>
  3804. request<{ status: string; message: string }>(`/library/files/${id}`, { method: 'DELETE' }),
  3805. getLibraryFileDownloadUrl: (id: number) => `${API_BASE}/library/files/${id}/download`,
  3806. createLibrarySlicerToken: (fileId: number) =>
  3807. request<{ token: string }>(`/library/files/${fileId}/slicer-token`, { method: 'POST' }),
  3808. getLibrarySlicerDownloadUrl: (fileId: number, token: string, filename: string) =>
  3809. `${API_BASE}/library/files/${fileId}/dl/${token}/${encodeURIComponent(filename)}`,
  3810. downloadLibraryFile: async (id: number, filename?: string): Promise<void> => {
  3811. const headers: Record<string, string> = {};
  3812. if (authToken) {
  3813. headers['Authorization'] = `Bearer ${authToken}`;
  3814. }
  3815. const response = await fetch(`${API_BASE}/library/files/${id}/download`, { headers });
  3816. if (!response.ok) {
  3817. const error = await response.json().catch(() => ({}));
  3818. throw new Error(error.detail || `HTTP ${response.status}`);
  3819. }
  3820. const disposition = response.headers.get('Content-Disposition');
  3821. const downloadFilename = parseContentDispositionFilename(disposition) || filename || `file_${id}`;
  3822. const blob = await response.blob();
  3823. const url = window.URL.createObjectURL(blob);
  3824. const a = document.createElement('a');
  3825. a.href = url;
  3826. a.download = downloadFilename;
  3827. document.body.appendChild(a);
  3828. a.click();
  3829. document.body.removeChild(a);
  3830. window.URL.revokeObjectURL(url);
  3831. },
  3832. getLibraryFileThumbnailUrl: (id: number) => `${API_BASE}/library/files/${id}/thumbnail`,
  3833. getLibraryFilePlateThumbnail: (id: number, plateIndex: number) =>
  3834. `${API_BASE}/library/files/${id}/plate-thumbnail/${plateIndex}`,
  3835. getLibraryFileGcodeUrl: (id: number) => `${API_BASE}/library/files/${id}/gcode`,
  3836. moveLibraryFiles: (fileIds: number[], folderId: number | null) =>
  3837. request<{ status: string; moved: number }>('/library/files/move', {
  3838. method: 'POST',
  3839. body: JSON.stringify({ file_ids: fileIds, folder_id: folderId }),
  3840. }),
  3841. bulkDeleteLibrary: (fileIds: number[], folderIds: number[]) =>
  3842. request<{ deleted_files: number; deleted_folders: number }>('/library/bulk-delete', {
  3843. method: 'POST',
  3844. body: JSON.stringify({ file_ids: fileIds, folder_ids: folderIds }),
  3845. }),
  3846. getLibraryStats: () => request<LibraryStats>('/library/stats'),
  3847. batchGenerateStlThumbnails: (options: {
  3848. file_ids?: number[];
  3849. folder_id?: number;
  3850. all_missing?: boolean;
  3851. }) =>
  3852. request<BatchThumbnailResponse>('/library/generate-stl-thumbnails', {
  3853. method: 'POST',
  3854. body: JSON.stringify(options),
  3855. }),
  3856. addLibraryFilesToQueue: (fileIds: number[]) =>
  3857. request<AddToQueueResponse>('/library/files/add-to-queue', {
  3858. method: 'POST',
  3859. body: JSON.stringify({ file_ids: fileIds }),
  3860. }),
  3861. printLibraryFile: (
  3862. fileId: number,
  3863. printerId: number,
  3864. options?: {
  3865. plate_id?: number;
  3866. plate_name?: string;
  3867. ams_mapping?: number[];
  3868. bed_levelling?: boolean;
  3869. flow_cali?: boolean;
  3870. vibration_cali?: boolean;
  3871. layer_inspect?: boolean;
  3872. timelapse?: boolean;
  3873. use_ams?: boolean;
  3874. }
  3875. ) =>
  3876. request<BackgroundDispatchResponse>(
  3877. `/library/files/${fileId}/print?printer_id=${printerId}`,
  3878. {
  3879. method: 'POST',
  3880. body: options ? JSON.stringify(options) : undefined,
  3881. }
  3882. ),
  3883. cancelBackgroundDispatchJob: (jobId: number) =>
  3884. request<{
  3885. status: 'cancelled' | 'cancelling';
  3886. job_id: number;
  3887. source_name: string;
  3888. printer_id: number;
  3889. printer_name: string;
  3890. }>(`/background-dispatch/${jobId}`, {
  3891. method: 'DELETE',
  3892. }),
  3893. getLibraryFilePlates: (fileId: number) =>
  3894. request<LibraryFilePlatesResponse>(`/library/files/${fileId}/plates`),
  3895. getLibraryFileFilamentRequirements: (fileId: number, plateId?: number) =>
  3896. request<{
  3897. file_id: number;
  3898. filename: string;
  3899. filaments: Array<{
  3900. slot_id: number;
  3901. type: string;
  3902. color: string;
  3903. used_grams: number;
  3904. used_meters: number;
  3905. }>;
  3906. }>(`/library/files/${fileId}/filament-requirements${plateId !== undefined ? `?plate_id=${plateId}` : ''}`),
  3907. // GitHub Backup
  3908. getGitHubBackupConfig: () =>
  3909. request<GitHubBackupConfig | null>('/github-backup/config'),
  3910. saveGitHubBackupConfig: (config: GitHubBackupConfigCreate) =>
  3911. request<GitHubBackupConfig>('/github-backup/config', {
  3912. method: 'POST',
  3913. body: JSON.stringify(config),
  3914. }),
  3915. updateGitHubBackupConfig: (config: Partial<GitHubBackupConfigCreate>) =>
  3916. request<GitHubBackupConfig>('/github-backup/config', {
  3917. method: 'PATCH',
  3918. body: JSON.stringify(config),
  3919. }),
  3920. deleteGitHubBackupConfig: () =>
  3921. request<{ message: string }>('/github-backup/config', { method: 'DELETE' }),
  3922. testGitHubConnection: (repoUrl: string, token: string) =>
  3923. request<GitHubTestConnectionResponse>(
  3924. `/github-backup/test?repo_url=${encodeURIComponent(repoUrl)}&token=${encodeURIComponent(token)}`,
  3925. { method: 'POST' }
  3926. ),
  3927. testGitHubStoredConnection: () =>
  3928. request<GitHubTestConnectionResponse>('/github-backup/test-stored', { method: 'POST' }),
  3929. triggerGitHubBackup: () =>
  3930. request<GitHubBackupTriggerResponse>('/github-backup/run', { method: 'POST' }),
  3931. getGitHubBackupStatus: () =>
  3932. request<GitHubBackupStatus>('/github-backup/status'),
  3933. getGitHubBackupLogs: (limit: number = 50) =>
  3934. request<GitHubBackupLog[]>(`/github-backup/logs?limit=${limit}`),
  3935. clearGitHubBackupLogs: (keepLast: number = 10) =>
  3936. request<{ deleted: number; message: string }>(`/github-backup/logs?keep_last=${keepLast}`, { method: 'DELETE' }),
  3937. // Local Presets (OrcaSlicer imports)
  3938. getLocalPresets: () =>
  3939. request<LocalPresetsResponse>('/local-presets/'),
  3940. getLocalPresetDetail: (id: number) =>
  3941. request<LocalPresetDetail>(`/local-presets/${id}`),
  3942. importLocalPresets: (formData: FormData) =>
  3943. fetch(`${API_BASE}/local-presets/import`, {
  3944. method: 'POST',
  3945. headers: authToken ? { 'Authorization': `Bearer ${authToken}` } : {},
  3946. body: formData,
  3947. }).then(async (res) => {
  3948. if (!res.ok) {
  3949. const err = await res.json().catch(() => ({}));
  3950. throw new Error(err.detail || `HTTP ${res.status}`);
  3951. }
  3952. return res.json() as Promise<ImportResponse>;
  3953. }),
  3954. createLocalPreset: (data: { name: string; preset_type: string; setting: Record<string, unknown> }) =>
  3955. request<LocalPreset>('/local-presets/', {
  3956. method: 'POST',
  3957. body: JSON.stringify(data),
  3958. }),
  3959. updateLocalPreset: (id: number, data: { name?: string; setting?: Record<string, unknown> }) =>
  3960. request<LocalPreset>(`/local-presets/${id}`, {
  3961. method: 'PUT',
  3962. body: JSON.stringify(data),
  3963. }),
  3964. deleteLocalPreset: (id: number) =>
  3965. request<{ success: boolean }>(`/local-presets/${id}`, { method: 'DELETE' }),
  3966. refreshBaseProfileCache: () =>
  3967. request<{ refreshed: number; failed: number; total: number }>('/local-presets/base-cache/refresh', { method: 'POST' }),
  3968. };
  3969. // AMS History types
  3970. export interface AMSHistoryPoint {
  3971. recorded_at: string;
  3972. humidity: number | null;
  3973. humidity_raw: number | null;
  3974. temperature: number | null;
  3975. }
  3976. export interface AMSHistoryResponse {
  3977. printer_id: number;
  3978. ams_id: number;
  3979. data: AMSHistoryPoint[];
  3980. min_humidity: number | null;
  3981. max_humidity: number | null;
  3982. avg_humidity: number | null;
  3983. min_temperature: number | null;
  3984. max_temperature: number | null;
  3985. avg_temperature: number | null;
  3986. }
  3987. // System Info types
  3988. export interface SystemInfo {
  3989. app: {
  3990. version: string;
  3991. base_dir: string;
  3992. archive_dir: string;
  3993. };
  3994. database: {
  3995. archives: number;
  3996. archives_completed: number;
  3997. archives_failed: number;
  3998. archives_printing: number;
  3999. printers: number;
  4000. filaments: number;
  4001. projects: number;
  4002. smart_plugs: number;
  4003. total_print_time_seconds: number;
  4004. total_print_time_formatted: string;
  4005. total_filament_grams: number;
  4006. total_filament_kg: number;
  4007. };
  4008. printers: {
  4009. total: number;
  4010. connected: number;
  4011. connected_list: Array<{
  4012. id: number;
  4013. name: string;
  4014. state: string;
  4015. model: string;
  4016. }>;
  4017. };
  4018. storage: {
  4019. archive_size_bytes: number;
  4020. archive_size_formatted: string;
  4021. database_size_bytes: number;
  4022. database_size_formatted: string;
  4023. disk_total_bytes: number;
  4024. disk_total_formatted: string;
  4025. disk_used_bytes: number;
  4026. disk_used_formatted: string;
  4027. disk_free_bytes: number;
  4028. disk_free_formatted: string;
  4029. disk_percent_used: number;
  4030. };
  4031. system: {
  4032. platform: string;
  4033. platform_release: string;
  4034. platform_version: string;
  4035. architecture: string;
  4036. hostname: string;
  4037. python_version: string;
  4038. uptime_seconds: number;
  4039. uptime_formatted: string;
  4040. boot_time: string;
  4041. };
  4042. memory: {
  4043. total_bytes: number;
  4044. total_formatted: string;
  4045. available_bytes: number;
  4046. available_formatted: string;
  4047. used_bytes: number;
  4048. used_formatted: string;
  4049. percent_used: number;
  4050. };
  4051. cpu: {
  4052. count: number;
  4053. count_logical: number;
  4054. percent: number;
  4055. };
  4056. }
  4057. export interface StorageUsageCategory {
  4058. key: string;
  4059. label: string;
  4060. bytes: number;
  4061. formatted: string;
  4062. percent_of_total: number;
  4063. }
  4064. export interface StorageUsageOtherItem {
  4065. bucket: string;
  4066. label: string;
  4067. kind: 'system' | 'data';
  4068. deletable: boolean;
  4069. bytes: number;
  4070. formatted: string;
  4071. percent_of_total: number;
  4072. }
  4073. export interface StorageUsageResponse {
  4074. roots: string[];
  4075. total_bytes: number;
  4076. total_formatted: string;
  4077. categories: StorageUsageCategory[];
  4078. other_breakdown: StorageUsageOtherItem[];
  4079. scan_errors: number;
  4080. generated_at: string;
  4081. cache: {
  4082. hit: boolean;
  4083. age_seconds: number;
  4084. max_age_seconds: number;
  4085. };
  4086. }
  4087. // Library (File Manager) types
  4088. export interface LibraryFolderTree {
  4089. id: number;
  4090. name: string;
  4091. parent_id: number | null;
  4092. project_id: number | null;
  4093. archive_id: number | null;
  4094. project_name: string | null;
  4095. archive_name: string | null;
  4096. file_count: number;
  4097. children: LibraryFolderTree[];
  4098. }
  4099. export interface LibraryFolder {
  4100. id: number;
  4101. name: string;
  4102. parent_id: number | null;
  4103. project_id: number | null;
  4104. archive_id: number | null;
  4105. project_name: string | null;
  4106. archive_name: string | null;
  4107. file_count: number;
  4108. created_at: string;
  4109. updated_at: string;
  4110. }
  4111. export interface LibraryFolderCreate {
  4112. name: string;
  4113. parent_id?: number | null;
  4114. project_id?: number | null;
  4115. archive_id?: number | null;
  4116. }
  4117. export interface LibraryFolderUpdate {
  4118. name?: string;
  4119. parent_id?: number | null;
  4120. project_id?: number | null; // 0 to unlink
  4121. archive_id?: number | null; // 0 to unlink
  4122. }
  4123. export interface LibraryFileDuplicate {
  4124. id: number;
  4125. filename: string;
  4126. folder_id: number | null;
  4127. folder_name: string | null;
  4128. created_at: string;
  4129. }
  4130. export interface LibraryFile {
  4131. id: number;
  4132. folder_id: number | null;
  4133. folder_name: string | null;
  4134. project_id: number | null;
  4135. project_name: string | null;
  4136. filename: string;
  4137. file_path: string;
  4138. file_type: string;
  4139. file_size: number;
  4140. file_hash: string | null;
  4141. thumbnail_path: string | null;
  4142. metadata: Record<string, unknown> | null;
  4143. print_count: number;
  4144. last_printed_at: string | null;
  4145. notes: string | null;
  4146. duplicates: LibraryFileDuplicate[] | null;
  4147. duplicate_count: number;
  4148. // User tracking (Issue #206)
  4149. created_by_id: number | null;
  4150. created_by_username: string | null;
  4151. created_at: string;
  4152. updated_at: string;
  4153. // Metadata fields
  4154. print_name: string | null;
  4155. print_time_seconds: number | null;
  4156. filament_used_grams: number | null;
  4157. sliced_for_model: string | null;
  4158. }
  4159. export interface LibraryFileListItem {
  4160. id: number;
  4161. folder_id: number | null;
  4162. filename: string;
  4163. file_type: string;
  4164. file_size: number;
  4165. thumbnail_path: string | null;
  4166. print_count: number;
  4167. duplicate_count: number;
  4168. // User tracking (Issue #206)
  4169. created_by_id: number | null;
  4170. created_by_username: string | null;
  4171. created_at: string;
  4172. print_name: string | null;
  4173. print_time_seconds: number | null;
  4174. filament_used_grams: number | null;
  4175. sliced_for_model: string | null;
  4176. }
  4177. export interface LibraryFileUpdate {
  4178. filename?: string;
  4179. folder_id?: number | null;
  4180. project_id?: number | null;
  4181. notes?: string | null;
  4182. }
  4183. export interface LibraryFileUploadResponse {
  4184. id: number;
  4185. filename: string;
  4186. file_type: string;
  4187. file_size: number;
  4188. thumbnail_path: string | null;
  4189. duplicate_of: number | null;
  4190. metadata: Record<string, unknown> | null;
  4191. }
  4192. export interface LibraryStats {
  4193. total_files: number;
  4194. total_folders: number;
  4195. total_size_bytes: number;
  4196. files_by_type: Record<string, number>;
  4197. total_prints: number;
  4198. disk_free_bytes: number;
  4199. disk_total_bytes: number;
  4200. disk_used_bytes: number;
  4201. }
  4202. export interface ZipExtractResult {
  4203. filename: string;
  4204. file_id: number;
  4205. folder_id: number | null;
  4206. }
  4207. export interface ZipExtractError {
  4208. filename: string;
  4209. error: string;
  4210. }
  4211. export interface ZipExtractResponse {
  4212. extracted: number;
  4213. folders_created: number;
  4214. files: ZipExtractResult[];
  4215. errors: ZipExtractError[];
  4216. }
  4217. // STL Thumbnail Generation types
  4218. export interface BatchThumbnailResult {
  4219. file_id: number;
  4220. filename: string;
  4221. success: boolean;
  4222. error?: string | null;
  4223. }
  4224. export interface BatchThumbnailResponse {
  4225. processed: number;
  4226. succeeded: number;
  4227. failed: number;
  4228. results: BatchThumbnailResult[];
  4229. }
  4230. // Library Queue types
  4231. export interface AddToQueueResult {
  4232. file_id: number;
  4233. filename: string;
  4234. queue_item_id: number;
  4235. archive_id: number;
  4236. }
  4237. export interface AddToQueueError {
  4238. file_id: number;
  4239. filename: string;
  4240. error: string;
  4241. }
  4242. export interface AddToQueueResponse {
  4243. added: AddToQueueResult[];
  4244. errors: AddToQueueError[];
  4245. }
  4246. // Discovery types
  4247. export interface DiscoveredPrinter {
  4248. serial: string;
  4249. name: string;
  4250. ip_address: string;
  4251. model: string | null;
  4252. discovered_at: string | null;
  4253. }
  4254. export interface DiscoveryStatus {
  4255. running: boolean;
  4256. }
  4257. export interface DiscoveryInfo {
  4258. is_docker: boolean;
  4259. ssdp_running: boolean;
  4260. scan_running: boolean;
  4261. subnets: string[];
  4262. }
  4263. export interface SubnetScanStatus {
  4264. running: boolean;
  4265. scanned: number;
  4266. total: number;
  4267. }
  4268. // Discovery API
  4269. export const discoveryApi = {
  4270. getInfo: () => request<DiscoveryInfo>('/discovery/info'),
  4271. getStatus: () => request<DiscoveryStatus>('/discovery/status'),
  4272. startDiscovery: (duration: number = 10) =>
  4273. request<DiscoveryStatus>(`/discovery/start?duration=${duration}`, { method: 'POST' }),
  4274. stopDiscovery: () =>
  4275. request<DiscoveryStatus>('/discovery/stop', { method: 'POST' }),
  4276. getDiscoveredPrinters: () =>
  4277. request<DiscoveredPrinter[]>('/discovery/printers'),
  4278. // Subnet scanning (for Docker environments)
  4279. startSubnetScan: (subnet: string, timeout: number = 1.0) =>
  4280. request<SubnetScanStatus>('/discovery/scan', {
  4281. method: 'POST',
  4282. body: JSON.stringify({ subnet, timeout }),
  4283. }),
  4284. getScanStatus: () => request<SubnetScanStatus>('/discovery/scan/status'),
  4285. stopSubnetScan: () =>
  4286. request<SubnetScanStatus>('/discovery/scan/stop', { method: 'POST' }),
  4287. };
  4288. // Virtual Printer types
  4289. export type VirtualPrinterMode = 'immediate' | 'queue' | 'review' | 'print_queue' | 'proxy'; // 'queue' is legacy, normalized to 'review'
  4290. export interface VirtualPrinterProxyStatus {
  4291. running: boolean;
  4292. target_host: string;
  4293. ftp_port: number;
  4294. mqtt_port: number;
  4295. ftp_connections: number;
  4296. mqtt_connections: number;
  4297. }
  4298. export interface VirtualPrinterStatus {
  4299. enabled: boolean;
  4300. running: boolean;
  4301. mode: VirtualPrinterMode;
  4302. name: string;
  4303. serial: string;
  4304. model: string;
  4305. model_name: string;
  4306. pending_files: number;
  4307. target_printer_ip?: string; // For proxy mode
  4308. proxy?: VirtualPrinterProxyStatus; // For proxy mode
  4309. }
  4310. export interface VirtualPrinterSettings {
  4311. enabled: boolean;
  4312. access_code_set: boolean;
  4313. mode: VirtualPrinterMode;
  4314. model: string;
  4315. target_printer_id: number | null; // For proxy mode
  4316. remote_interface_ip: string | null; // For SSDP proxy across networks
  4317. status: VirtualPrinterStatus;
  4318. }
  4319. export interface NetworkInterface {
  4320. name: string;
  4321. ip: string;
  4322. netmask: string;
  4323. subnet: string;
  4324. is_alias?: boolean;
  4325. label?: string;
  4326. }
  4327. export interface VirtualPrinterModels {
  4328. models: Record<string, string>; // SSDP code -> display name
  4329. default: string;
  4330. }
  4331. export interface PendingUpload {
  4332. id: number;
  4333. filename: string;
  4334. file_size: number;
  4335. source_ip: string | null;
  4336. status: string;
  4337. tags: string | null;
  4338. notes: string | null;
  4339. project_id: number | null;
  4340. uploaded_at: string;
  4341. }
  4342. // Virtual Printer API
  4343. export const virtualPrinterApi = {
  4344. getSettings: () => request<VirtualPrinterSettings>('/settings/virtual-printer'),
  4345. getModels: () => request<VirtualPrinterModels>('/settings/virtual-printer/models'),
  4346. updateSettings: (data: {
  4347. enabled?: boolean;
  4348. access_code?: string;
  4349. mode?: 'immediate' | 'review' | 'print_queue' | 'proxy';
  4350. model?: string;
  4351. target_printer_id?: number;
  4352. remote_interface_ip?: string;
  4353. }) => {
  4354. const params = new URLSearchParams();
  4355. if (data.enabled !== undefined) params.set('enabled', String(data.enabled));
  4356. if (data.access_code !== undefined) params.set('access_code', data.access_code);
  4357. if (data.mode !== undefined) params.set('mode', data.mode);
  4358. if (data.model !== undefined) params.set('model', data.model);
  4359. if (data.target_printer_id !== undefined) params.set('target_printer_id', String(data.target_printer_id));
  4360. if (data.remote_interface_ip !== undefined) params.set('remote_interface_ip', data.remote_interface_ip);
  4361. return request<VirtualPrinterSettings>(`/settings/virtual-printer?${params.toString()}`, {
  4362. method: 'PUT',
  4363. });
  4364. },
  4365. };
  4366. // Multi Virtual Printer API
  4367. export interface VirtualPrinterConfig {
  4368. id: number;
  4369. name: string;
  4370. enabled: boolean;
  4371. mode: VirtualPrinterMode;
  4372. model: string | null;
  4373. model_name: string | null;
  4374. access_code_set: boolean;
  4375. serial: string;
  4376. target_printer_id: number | null;
  4377. bind_ip: string | null;
  4378. remote_interface_ip: string | null;
  4379. position: number;
  4380. status: { running: boolean; pending_files: number; proxy?: VirtualPrinterProxyStatus };
  4381. }
  4382. export interface VirtualPrinterListResponse {
  4383. printers: VirtualPrinterConfig[];
  4384. models: Record<string, string>;
  4385. }
  4386. export const multiVirtualPrinterApi = {
  4387. list: () => request<VirtualPrinterListResponse>('/virtual-printers'),
  4388. get: (id: number) => request<VirtualPrinterConfig>(`/virtual-printers/${id}`),
  4389. create: (data: {
  4390. name?: string;
  4391. enabled?: boolean;
  4392. mode?: string;
  4393. model?: string;
  4394. access_code?: string;
  4395. target_printer_id?: number;
  4396. bind_ip?: string;
  4397. remote_interface_ip?: string;
  4398. }) =>
  4399. request<VirtualPrinterConfig>('/virtual-printers', {
  4400. method: 'POST',
  4401. body: JSON.stringify(data),
  4402. }),
  4403. update: (id: number, data: {
  4404. name?: string;
  4405. enabled?: boolean;
  4406. mode?: string;
  4407. model?: string;
  4408. access_code?: string;
  4409. target_printer_id?: number;
  4410. bind_ip?: string;
  4411. remote_interface_ip?: string;
  4412. }) =>
  4413. request<VirtualPrinterConfig>(`/virtual-printers/${id}`, {
  4414. method: 'PUT',
  4415. body: JSON.stringify(data),
  4416. }),
  4417. remove: (id: number) =>
  4418. request<{ detail: string; id: number }>(`/virtual-printers/${id}`, {
  4419. method: 'DELETE',
  4420. }),
  4421. };
  4422. // Pending Uploads API
  4423. export const pendingUploadsApi = {
  4424. list: () => request<PendingUpload[]>('/pending-uploads/'),
  4425. getCount: () => request<{ count: number }>('/pending-uploads/count'),
  4426. get: (id: number) => request<PendingUpload>(`/pending-uploads/${id}`),
  4427. archive: (id: number, data?: { tags?: string; notes?: string; project_id?: number }) =>
  4428. request<{ id: number; print_name: string; filename: string }>(`/pending-uploads/${id}/archive`, {
  4429. method: 'POST',
  4430. body: JSON.stringify(data || {}),
  4431. }),
  4432. discard: (id: number) =>
  4433. request<{ success: boolean }>(`/pending-uploads/${id}`, { method: 'DELETE' }),
  4434. archiveAll: () =>
  4435. request<{ archived: number; failed: number }>('/pending-uploads/archive-all', { method: 'POST' }),
  4436. discardAll: () =>
  4437. request<{ discarded: number }>('/pending-uploads/discard-all', { method: 'DELETE' }),
  4438. };
  4439. // Firmware API Types
  4440. export interface FirmwareUpdateInfo {
  4441. printer_id: number;
  4442. printer_name: string;
  4443. model: string | null;
  4444. current_version: string | null;
  4445. latest_version: string | null;
  4446. update_available: boolean;
  4447. download_url: string | null;
  4448. release_notes: string | null;
  4449. }
  4450. export interface FirmwareUploadPrepare {
  4451. can_proceed: boolean;
  4452. sd_card_present: boolean;
  4453. sd_card_free_space: number;
  4454. firmware_size: number;
  4455. space_sufficient: boolean;
  4456. update_available: boolean;
  4457. current_version: string | null;
  4458. latest_version: string | null;
  4459. firmware_filename: string | null;
  4460. errors: string[];
  4461. }
  4462. export interface FirmwareUploadStatus {
  4463. status: 'idle' | 'preparing' | 'downloading' | 'uploading' | 'complete' | 'error';
  4464. progress: number;
  4465. message: string;
  4466. error: string | null;
  4467. firmware_filename: string | null;
  4468. firmware_version: string | null;
  4469. }
  4470. // Firmware API
  4471. export const firmwareApi = {
  4472. checkUpdates: () =>
  4473. request<{ updates: FirmwareUpdateInfo[]; updates_available: number }>('/firmware/updates'),
  4474. checkPrinterUpdate: (printerId: number) =>
  4475. request<FirmwareUpdateInfo>(`/firmware/updates/${printerId}`),
  4476. prepareUpload: (printerId: number) =>
  4477. request<FirmwareUploadPrepare>(`/firmware/updates/${printerId}/prepare`),
  4478. startUpload: (printerId: number) =>
  4479. request<{ started: boolean; message: string }>(`/firmware/updates/${printerId}/upload`, {
  4480. method: 'POST',
  4481. }),
  4482. getUploadStatus: (printerId: number) =>
  4483. request<FirmwareUploadStatus>(`/firmware/updates/${printerId}/upload/status`),
  4484. };
  4485. // Support types
  4486. export interface DebugLoggingState {
  4487. enabled: boolean;
  4488. enabled_at: string | null;
  4489. duration_seconds: number | null;
  4490. }
  4491. export interface LogEntry {
  4492. timestamp: string;
  4493. level: string;
  4494. logger_name: string;
  4495. message: string;
  4496. }
  4497. export interface LogsResponse {
  4498. entries: LogEntry[];
  4499. total_in_file: number;
  4500. filtered_count: number;
  4501. }
  4502. // Support API
  4503. export const supportApi = {
  4504. getDebugLoggingState: () =>
  4505. request<DebugLoggingState>('/support/debug-logging'),
  4506. setDebugLogging: (enabled: boolean) =>
  4507. request<DebugLoggingState>('/support/debug-logging', {
  4508. method: 'POST',
  4509. body: JSON.stringify({ enabled }),
  4510. }),
  4511. downloadSupportBundle: async () => {
  4512. const headers: Record<string, string> = {};
  4513. if (authToken) {
  4514. headers['Authorization'] = `Bearer ${authToken}`;
  4515. }
  4516. const response = await fetch(`${API_BASE}/support/bundle`, { headers });
  4517. if (!response.ok) {
  4518. const error = await response.json().catch(() => ({}));
  4519. throw new Error(error.detail || `HTTP ${response.status}`);
  4520. }
  4521. // Get filename from Content-Disposition header or use default
  4522. const disposition = response.headers.get('Content-Disposition');
  4523. const filename = parseContentDispositionFilename(disposition) || 'bambuddy-support.zip';
  4524. // Download the blob
  4525. const blob = await response.blob();
  4526. const url = window.URL.createObjectURL(blob);
  4527. const a = document.createElement('a');
  4528. a.href = url;
  4529. a.download = filename;
  4530. document.body.appendChild(a);
  4531. a.click();
  4532. document.body.removeChild(a);
  4533. window.URL.revokeObjectURL(url);
  4534. },
  4535. getLogs: (params?: { limit?: number; level?: string; search?: string }) => {
  4536. const searchParams = new URLSearchParams();
  4537. if (params?.limit) searchParams.set('limit', params.limit.toString());
  4538. if (params?.level) searchParams.set('level', params.level);
  4539. if (params?.search) searchParams.set('search', params.search);
  4540. const query = searchParams.toString();
  4541. return request<LogsResponse>(`/support/logs${query ? `?${query}` : ''}`);
  4542. },
  4543. clearLogs: () =>
  4544. request<{ message: string }>('/support/logs', { method: 'DELETE' }),
  4545. };
  4546. // SpoolBuddy types
  4547. export interface SpoolBuddyDevice {
  4548. id: number;
  4549. device_id: string;
  4550. hostname: string;
  4551. ip_address: string;
  4552. firmware_version: string | null;
  4553. has_nfc: boolean;
  4554. has_scale: boolean;
  4555. tare_offset: number;
  4556. calibration_factor: number;
  4557. nfc_reader_type: string | null;
  4558. nfc_connection: string | null;
  4559. display_brightness: number;
  4560. display_blank_timeout: number;
  4561. has_backlight: boolean;
  4562. last_calibrated_at: string | null;
  4563. last_seen: string | null;
  4564. pending_command: string | null;
  4565. nfc_ok: boolean;
  4566. scale_ok: boolean;
  4567. uptime_s: number;
  4568. online: boolean;
  4569. }
  4570. export interface DaemonUpdateCheck {
  4571. current_version: string;
  4572. latest_version: string | null;
  4573. update_available: boolean;
  4574. release_url: string | null;
  4575. }
  4576. // SpoolBuddy API
  4577. export const spoolbuddyApi = {
  4578. getDevices: () =>
  4579. request<SpoolBuddyDevice[]>('/spoolbuddy/devices'),
  4580. tare: (deviceId: string) =>
  4581. request<{ status: string }>(`/spoolbuddy/devices/${deviceId}/calibration/tare`, {
  4582. method: 'POST',
  4583. body: '{}',
  4584. }),
  4585. getCalibration: (deviceId: string) =>
  4586. request<{ tare_offset: number; calibration_factor: number }>(`/spoolbuddy/devices/${deviceId}/calibration`),
  4587. setCalibrationFactor: (deviceId: string, knownWeightGrams: number, rawAdc: number, tareRawAdc?: number) =>
  4588. request<{ tare_offset: number; calibration_factor: number }>(`/spoolbuddy/devices/${deviceId}/calibration/set-factor`, {
  4589. method: 'POST',
  4590. body: JSON.stringify({ known_weight_grams: knownWeightGrams, raw_adc: rawAdc, tare_raw_adc: tareRawAdc }),
  4591. }),
  4592. updateSpoolWeight: (spoolId: number, weightGrams: number) =>
  4593. request<{ status: string; weight_used: number }>('/spoolbuddy/scale/update-spool-weight', {
  4594. method: 'POST',
  4595. body: JSON.stringify({ spool_id: spoolId, weight_grams: weightGrams }),
  4596. }),
  4597. updateDisplay: (deviceId: string, brightness: number, blankTimeout: number) =>
  4598. request<{ status: string }>(`/spoolbuddy/devices/${deviceId}/display`, {
  4599. method: 'PUT',
  4600. body: JSON.stringify({ brightness, blank_timeout: blankTimeout }),
  4601. }),
  4602. checkDaemonUpdate: (deviceId: string, includeBeta?: boolean) =>
  4603. request<DaemonUpdateCheck>(`/spoolbuddy/devices/${deviceId}/update-check?include_beta=${includeBeta ?? false}`),
  4604. writeTag: (deviceId: string, spoolId: number) =>
  4605. request<{ status: string }>('/spoolbuddy/nfc/write-tag', {
  4606. method: 'POST',
  4607. body: JSON.stringify({ device_id: deviceId, spool_id: spoolId }),
  4608. }),
  4609. cancelWrite: (deviceId: string) =>
  4610. request<{ status: string }>(`/spoolbuddy/devices/${deviceId}/cancel-write`, {
  4611. method: 'POST',
  4612. body: '{}',
  4613. }),
  4614. };
  4615. export interface BugReportRequest {
  4616. description: string;
  4617. email?: string;
  4618. screenshot_base64?: string;
  4619. include_support_info?: boolean;
  4620. }
  4621. export interface BugReportResponse {
  4622. success: boolean;
  4623. message: string;
  4624. issue_url?: string;
  4625. issue_number?: number;
  4626. }
  4627. export const bugReportApi = {
  4628. submit: (data: BugReportRequest) =>
  4629. request<BugReportResponse>('/bug-report/submit', {
  4630. method: 'POST',
  4631. body: JSON.stringify(data),
  4632. }),
  4633. };