client.ts 139 KB

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