client.ts 137 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294
  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. role: string; // Deprecated, kept for backward compatibility
  1752. is_active: boolean;
  1753. is_admin: boolean; // Computed from role and group membership
  1754. groups: GroupBrief[];
  1755. permissions: Permission[]; // All permissions from groups
  1756. created_at: string;
  1757. }
  1758. export interface UserCreate {
  1759. username: string;
  1760. password: string;
  1761. role: string;
  1762. group_ids?: number[];
  1763. }
  1764. export interface UserUpdate {
  1765. username?: string;
  1766. password?: string;
  1767. role?: string;
  1768. is_active?: boolean;
  1769. group_ids?: number[];
  1770. }
  1771. export interface SetupRequest {
  1772. auth_enabled: boolean;
  1773. admin_username?: string;
  1774. admin_password?: string;
  1775. }
  1776. export interface SetupResponse {
  1777. auth_enabled: boolean;
  1778. admin_created?: boolean;
  1779. }
  1780. export interface AuthStatus {
  1781. auth_enabled: boolean;
  1782. requires_setup: boolean;
  1783. }
  1784. // API functions
  1785. export const api = {
  1786. // Authentication
  1787. getAuthStatus: () => request<AuthStatus>('/auth/status'),
  1788. setupAuth: (data: SetupRequest) =>
  1789. request<SetupResponse>('/auth/setup', {
  1790. method: 'POST',
  1791. body: JSON.stringify(data),
  1792. }),
  1793. login: (data: LoginRequest) =>
  1794. request<LoginResponse>('/auth/login', {
  1795. method: 'POST',
  1796. body: JSON.stringify(data),
  1797. }),
  1798. logout: () =>
  1799. request<{ message: string }>('/auth/logout', {
  1800. method: 'POST',
  1801. }),
  1802. getCurrentUser: () => request<UserResponse>('/auth/me'),
  1803. disableAuth: () =>
  1804. request<{ message: string; auth_enabled: boolean }>('/auth/disable', {
  1805. method: 'POST',
  1806. }),
  1807. // Users
  1808. getUsers: () => request<UserResponse[]>('/users/'),
  1809. getUser: (id: number) => request<UserResponse>(`/users/${id}`),
  1810. createUser: (data: UserCreate) =>
  1811. request<UserResponse>('/users/', {
  1812. method: 'POST',
  1813. body: JSON.stringify(data),
  1814. }),
  1815. updateUser: (id: number, data: UserUpdate) =>
  1816. request<UserResponse>(`/users/${id}`, {
  1817. method: 'PATCH',
  1818. body: JSON.stringify(data),
  1819. }),
  1820. deleteUser: (id: number, deleteItems: boolean = false) =>
  1821. request<void>(`/users/${id}?delete_items=${deleteItems}`, {
  1822. method: 'DELETE',
  1823. }),
  1824. getUserItemsCount: (id: number) =>
  1825. request<{ archives: number; queue_items: number; library_files: number }>(`/users/${id}/items-count`),
  1826. changePassword: (currentPassword: string, newPassword: string) =>
  1827. request<{ message: string }>('/users/me/change-password', {
  1828. method: 'POST',
  1829. body: JSON.stringify({ current_password: currentPassword, new_password: newPassword }),
  1830. }),
  1831. // Groups
  1832. getPermissions: () => request<PermissionsListResponse>('/groups/permissions'),
  1833. getGroups: () => request<Group[]>('/groups/'),
  1834. getGroup: (id: number) => request<GroupDetail>(`/groups/${id}`),
  1835. createGroup: (data: GroupCreate) =>
  1836. request<Group>('/groups/', {
  1837. method: 'POST',
  1838. body: JSON.stringify(data),
  1839. }),
  1840. updateGroup: (id: number, data: GroupUpdate) =>
  1841. request<Group>(`/groups/${id}`, {
  1842. method: 'PATCH',
  1843. body: JSON.stringify(data),
  1844. }),
  1845. deleteGroup: (id: number) =>
  1846. request<void>(`/groups/${id}`, {
  1847. method: 'DELETE',
  1848. }),
  1849. addUserToGroup: (groupId: number, userId: number) =>
  1850. request<void>(`/groups/${groupId}/users/${userId}`, {
  1851. method: 'POST',
  1852. }),
  1853. removeUserFromGroup: (groupId: number, userId: number) =>
  1854. request<void>(`/groups/${groupId}/users/${userId}`, {
  1855. method: 'DELETE',
  1856. }),
  1857. // Printers
  1858. getPrinters: () => request<Printer[]>('/printers/'),
  1859. getPrinter: (id: number) => request<Printer>(`/printers/${id}`),
  1860. createPrinter: (data: PrinterCreate) =>
  1861. request<Printer>('/printers/', {
  1862. method: 'POST',
  1863. body: JSON.stringify(data),
  1864. }),
  1865. updatePrinter: (id: number, data: Partial<PrinterCreate>) =>
  1866. request<Printer>(`/printers/${id}`, {
  1867. method: 'PATCH',
  1868. body: JSON.stringify(data),
  1869. }),
  1870. deletePrinter: (id: number, deleteArchives: boolean = true) =>
  1871. request<{ status: string; archives_deleted: boolean }>(
  1872. `/printers/${id}?delete_archives=${deleteArchives}`,
  1873. { method: 'DELETE' }
  1874. ),
  1875. getPrinterStatus: (id: number) =>
  1876. request<PrinterStatus>(`/printers/${id}/status`),
  1877. refreshPrinterStatus: (id: number) =>
  1878. request<{ status: string }>(`/printers/${id}/refresh-status`, {
  1879. method: 'POST',
  1880. }),
  1881. connectPrinter: (id: number) =>
  1882. request<{ connected: boolean }>(`/printers/${id}/connect`, {
  1883. method: 'POST',
  1884. }),
  1885. disconnectPrinter: (id: number) =>
  1886. request<{ connected: boolean }>(`/printers/${id}/disconnect`, {
  1887. method: 'POST',
  1888. }),
  1889. testExternalCamera: (printerId: number, url: string, cameraType: string) =>
  1890. request<{ success: boolean; error?: string; resolution?: string }>(
  1891. `/printers/${printerId}/camera/external/test?url=${encodeURIComponent(url)}&camera_type=${encodeURIComponent(cameraType)}`,
  1892. { method: 'POST' }
  1893. ),
  1894. // Print Control
  1895. stopPrint: (printerId: number) =>
  1896. request<{ success: boolean; message: string }>(`/printers/${printerId}/print/stop`, {
  1897. method: 'POST',
  1898. }),
  1899. pausePrint: (printerId: number) =>
  1900. request<{ success: boolean; message: string }>(`/printers/${printerId}/print/pause`, {
  1901. method: 'POST',
  1902. }),
  1903. resumePrint: (printerId: number) =>
  1904. request<{ success: boolean; message: string }>(`/printers/${printerId}/print/resume`, {
  1905. method: 'POST',
  1906. }),
  1907. // Get current print user (for reprint tracking - Issue #206)
  1908. getCurrentPrintUser: (printerId: number) =>
  1909. request<{ user_id?: number; username?: string }>(`/printers/${printerId}/current-print-user`),
  1910. // Chamber Light Control
  1911. setChamberLight: (printerId: number, on: boolean) =>
  1912. request<{ success: boolean; message: string }>(`/printers/${printerId}/chamber-light?on=${on}`, {
  1913. method: 'POST',
  1914. }),
  1915. // Skip Objects
  1916. getPrintableObjects: (printerId: number) =>
  1917. request<{
  1918. objects: Array<{ id: number; name: string; x: number | null; y: number | null; skipped: boolean }>;
  1919. total: number;
  1920. skipped_count: number;
  1921. is_printing: boolean;
  1922. bbox_all: [number, number, number, number] | null;
  1923. }>(`/printers/${printerId}/print/objects`),
  1924. skipObjects: (printerId: number, objectIds: number[]) =>
  1925. request<{ success: boolean; message: string; skipped_objects: number[] }>(
  1926. `/printers/${printerId}/print/skip-objects`,
  1927. {
  1928. method: 'POST',
  1929. body: JSON.stringify(objectIds),
  1930. }
  1931. ),
  1932. // AMS Control
  1933. refreshAmsSlot: (printerId: number, amsId: number, slotId: number) =>
  1934. request<{ success: boolean; message: string }>(
  1935. `/printers/${printerId}/ams/${amsId}/slot/${slotId}/refresh`,
  1936. { method: 'POST' }
  1937. ),
  1938. // MQTT Debug Logging
  1939. enableMQTTLogging: (printerId: number) =>
  1940. request<{ logging_enabled: boolean }>(`/printers/${printerId}/logging/enable`, {
  1941. method: 'POST',
  1942. }),
  1943. disableMQTTLogging: (printerId: number) =>
  1944. request<{ logging_enabled: boolean }>(`/printers/${printerId}/logging/disable`, {
  1945. method: 'POST',
  1946. }),
  1947. getMQTTLogs: (printerId: number) =>
  1948. request<MQTTLogsResponse>(`/printers/${printerId}/logging`),
  1949. clearMQTTLogs: (printerId: number) =>
  1950. request<{ status: string }>(`/printers/${printerId}/logging`, {
  1951. method: 'DELETE',
  1952. }),
  1953. // Printer File Manager
  1954. getPrinterFiles: (printerId: number, path = '/') =>
  1955. request<{
  1956. path: string;
  1957. files: Array<{
  1958. name: string;
  1959. is_directory: boolean;
  1960. size: number;
  1961. path: string;
  1962. mtime?: string;
  1963. }>;
  1964. }>(`/printers/${printerId}/files?path=${encodeURIComponent(path)}`),
  1965. getPrinterFileDownloadUrl: (printerId: number, path: string) =>
  1966. `${API_BASE}/printers/${printerId}/files/download?path=${encodeURIComponent(path)}`,
  1967. getPrinterFileGcodeUrl: (printerId: number, path: string) =>
  1968. `${API_BASE}/printers/${printerId}/files/gcode?path=${encodeURIComponent(path)}`,
  1969. getPrinterFilePlates: (printerId: number, path: string) =>
  1970. request<{
  1971. printer_id: number;
  1972. path: string;
  1973. filename: string;
  1974. plates: Array<{
  1975. index: number;
  1976. name: string | null;
  1977. objects: string[];
  1978. has_thumbnail: boolean;
  1979. thumbnail_url: string | null;
  1980. print_time_seconds: number | null;
  1981. filament_used_grams: number | null;
  1982. filaments: Array<{
  1983. slot_id: number;
  1984. type: string;
  1985. color: string;
  1986. used_grams: number;
  1987. used_meters: number;
  1988. }>;
  1989. }>;
  1990. is_multi_plate: boolean;
  1991. }>(`/printers/${printerId}/files/plates?path=${encodeURIComponent(path)}`),
  1992. getPrinterFilePlateThumbnail: (printerId: number, plateIndex: number, path: string) =>
  1993. `${API_BASE}/printers/${printerId}/files/plate-thumbnail/${plateIndex}?path=${encodeURIComponent(path)}`,
  1994. downloadPrinterFile: async (printerId: number, path: string): Promise<void> => {
  1995. const headers: Record<string, string> = {};
  1996. if (authToken) {
  1997. headers['Authorization'] = `Bearer ${authToken}`;
  1998. }
  1999. const response = await fetch(
  2000. `${API_BASE}/printers/${printerId}/files/download?path=${encodeURIComponent(path)}`,
  2001. { headers }
  2002. );
  2003. if (!response.ok) {
  2004. const error = await response.json().catch(() => ({}));
  2005. throw new Error(error.detail || `HTTP ${response.status}`);
  2006. }
  2007. const disposition = response.headers.get('Content-Disposition');
  2008. const filenameMatch = disposition?.match(/filename="?([^";\n]+)"?/);
  2009. const filename = filenameMatch?.[1] || path.split('/').pop() || 'download';
  2010. const blob = await response.blob();
  2011. const url = window.URL.createObjectURL(blob);
  2012. const a = document.createElement('a');
  2013. a.href = url;
  2014. a.download = filename;
  2015. document.body.appendChild(a);
  2016. a.click();
  2017. document.body.removeChild(a);
  2018. window.URL.revokeObjectURL(url);
  2019. },
  2020. downloadPrinterFilesAsZip: async (printerId: number, paths: string[]): Promise<Blob> => {
  2021. const headers: Record<string, string> = { 'Content-Type': 'application/json' };
  2022. if (authToken) {
  2023. headers['Authorization'] = `Bearer ${authToken}`;
  2024. }
  2025. const response = await fetch(`${API_BASE}/printers/${printerId}/files/download-zip`, {
  2026. method: 'POST',
  2027. headers,
  2028. body: JSON.stringify({ paths }),
  2029. });
  2030. if (!response.ok) {
  2031. const error = await response.json().catch(() => ({}));
  2032. throw new Error(error.detail || `HTTP ${response.status}`);
  2033. }
  2034. return response.blob();
  2035. },
  2036. deletePrinterFile: (printerId: number, path: string) =>
  2037. request<{ status: string; path: string }>(`/printers/${printerId}/files?path=${encodeURIComponent(path)}`, {
  2038. method: 'DELETE',
  2039. }),
  2040. getPrinterStorage: (printerId: number) =>
  2041. request<{ used_bytes: number | null; free_bytes: number | null }>(`/printers/${printerId}/storage`),
  2042. // Archives
  2043. getArchives: (printerId?: number, projectId?: number, limit = 50, offset = 0) => {
  2044. const params = new URLSearchParams();
  2045. if (printerId) params.set('printer_id', String(printerId));
  2046. if (projectId) params.set('project_id', String(projectId));
  2047. params.set('limit', String(limit));
  2048. params.set('offset', String(offset));
  2049. return request<Archive[]>(`/archives/?${params}`);
  2050. },
  2051. getArchive: (id: number) => request<Archive>(`/archives/${id}`),
  2052. searchArchives: (query: string, options?: {
  2053. printerId?: number;
  2054. projectId?: number;
  2055. status?: string;
  2056. limit?: number;
  2057. offset?: number;
  2058. }) => {
  2059. const params = new URLSearchParams();
  2060. params.set('q', query);
  2061. if (options?.printerId) params.set('printer_id', String(options.printerId));
  2062. if (options?.projectId) params.set('project_id', String(options.projectId));
  2063. if (options?.status) params.set('status', options.status);
  2064. if (options?.limit) params.set('limit', String(options.limit));
  2065. if (options?.offset) params.set('offset', String(options.offset));
  2066. return request<Archive[]>(`/archives/search?${params}`);
  2067. },
  2068. rebuildSearchIndex: () => request<{ message: string }>('/archives/search/rebuild-index', { method: 'POST' }),
  2069. updateArchive: (id: number, data: {
  2070. printer_id?: number | null;
  2071. project_id?: number | null;
  2072. print_name?: string;
  2073. is_favorite?: boolean;
  2074. tags?: string;
  2075. notes?: string;
  2076. cost?: number;
  2077. failure_reason?: string | null;
  2078. status?: string;
  2079. quantity?: number;
  2080. external_url?: string | null;
  2081. }) =>
  2082. request<Archive>(`/archives/${id}`, {
  2083. method: 'PATCH',
  2084. body: JSON.stringify(data),
  2085. }),
  2086. toggleFavorite: (id: number) =>
  2087. request<Archive>(`/archives/${id}/favorite`, { method: 'POST' }),
  2088. deleteArchive: (id: number) =>
  2089. request<void>(`/archives/${id}`, { method: 'DELETE' }),
  2090. getArchiveStats: () => request<ArchiveStats>('/archives/stats'),
  2091. // Tag management
  2092. getTags: () => request<TagInfo[]>('/archives/tags'),
  2093. renameTag: (oldName: string, newName: string) =>
  2094. request<{ affected: number }>(`/archives/tags/${encodeURIComponent(oldName)}`, {
  2095. method: 'PUT',
  2096. body: JSON.stringify({ new_name: newName }),
  2097. }),
  2098. deleteTag: (name: string) =>
  2099. request<{ affected: number }>(`/archives/tags/${encodeURIComponent(name)}`, {
  2100. method: 'DELETE',
  2101. }),
  2102. recalculateCosts: () =>
  2103. request<{ message: string; updated: number }>('/archives/recalculate-costs', { method: 'POST' }),
  2104. getFailureAnalysis: (options?: { days?: number; printerId?: number; projectId?: number }) => {
  2105. const params = new URLSearchParams();
  2106. if (options?.days) params.set('days', String(options.days));
  2107. if (options?.printerId) params.set('printer_id', String(options.printerId));
  2108. if (options?.projectId) params.set('project_id', String(options.projectId));
  2109. return request<FailureAnalysis>(`/archives/analysis/failures?${params}`);
  2110. },
  2111. compareArchives: (archiveIds: number[]) =>
  2112. request<ArchiveComparison>(`/archives/compare?archive_ids=${archiveIds.join(',')}`),
  2113. findSimilarArchives: (archiveId: number, limit = 10) =>
  2114. request<SimilarArchive[]>(`/archives/${archiveId}/similar?limit=${limit}`),
  2115. exportArchives: async (options?: {
  2116. format?: 'csv' | 'xlsx';
  2117. fields?: string[];
  2118. printerId?: number;
  2119. projectId?: number;
  2120. status?: string;
  2121. dateFrom?: string;
  2122. dateTo?: string;
  2123. search?: string;
  2124. }): Promise<{ blob: Blob; filename: string }> => {
  2125. const params = new URLSearchParams();
  2126. if (options?.format) params.set('format', options.format);
  2127. if (options?.fields) params.set('fields', options.fields.join(','));
  2128. if (options?.printerId) params.set('printer_id', String(options.printerId));
  2129. if (options?.projectId) params.set('project_id', String(options.projectId));
  2130. if (options?.status) params.set('status', options.status);
  2131. if (options?.dateFrom) params.set('date_from', options.dateFrom);
  2132. if (options?.dateTo) params.set('date_to', options.dateTo);
  2133. if (options?.search) params.set('search', options.search);
  2134. const headers: Record<string, string> = {};
  2135. if (authToken) {
  2136. headers['Authorization'] = `Bearer ${authToken}`;
  2137. }
  2138. const response = await fetch(`${API_BASE}/archives/export?${params}`, { headers });
  2139. if (!response.ok) {
  2140. const error = await response.json().catch(() => ({}));
  2141. throw new Error(error.detail || `HTTP ${response.status}`);
  2142. }
  2143. const contentDisposition = response.headers.get('Content-Disposition');
  2144. let filename = options?.format === 'xlsx' ? 'archives_export.xlsx' : 'archives_export.csv';
  2145. if (contentDisposition) {
  2146. const match = contentDisposition.match(/filename="?([^"]+)"?/);
  2147. if (match) filename = match[1];
  2148. }
  2149. const blob = await response.blob();
  2150. return { blob, filename };
  2151. },
  2152. exportStats: async (options?: {
  2153. format?: 'csv' | 'xlsx';
  2154. days?: number;
  2155. printerId?: number;
  2156. projectId?: number;
  2157. }): Promise<{ blob: Blob; filename: string }> => {
  2158. const params = new URLSearchParams();
  2159. if (options?.format) params.set('format', options.format);
  2160. if (options?.days) params.set('days', String(options.days));
  2161. if (options?.printerId) params.set('printer_id', String(options.printerId));
  2162. if (options?.projectId) params.set('project_id', String(options.projectId));
  2163. const headers: Record<string, string> = {};
  2164. if (authToken) {
  2165. headers['Authorization'] = `Bearer ${authToken}`;
  2166. }
  2167. const response = await fetch(`${API_BASE}/archives/stats/export?${params}`, { headers });
  2168. if (!response.ok) {
  2169. const error = await response.json().catch(() => ({}));
  2170. throw new Error(error.detail || `HTTP ${response.status}`);
  2171. }
  2172. const contentDisposition = response.headers.get('Content-Disposition');
  2173. let filename = options?.format === 'xlsx' ? 'stats_export.xlsx' : 'stats_export.csv';
  2174. if (contentDisposition) {
  2175. const match = contentDisposition.match(/filename="?([^"]+)"?/);
  2176. if (match) filename = match[1];
  2177. }
  2178. const blob = await response.blob();
  2179. return { blob, filename };
  2180. },
  2181. getArchiveDuplicates: (id: number) =>
  2182. request<{ duplicates: ArchiveDuplicate[]; count: number }>(`/archives/${id}/duplicates`),
  2183. backfillContentHashes: () =>
  2184. request<{ updated: number; errors: Array<{ id: number; error: string }> }>('/archives/backfill-hashes', {
  2185. method: 'POST',
  2186. }),
  2187. getArchiveThumbnail: (id: number) => `${API_BASE}/archives/${id}/thumbnail?v=${Date.now()}`,
  2188. getArchivePlateThumbnail: (id: number, plateIndex: number) =>
  2189. `${API_BASE}/archives/${id}/plate-thumbnail/${plateIndex}`,
  2190. getArchiveDownload: (id: number) => `${API_BASE}/archives/${id}/download`,
  2191. downloadArchive: async (id: number, filename?: string): Promise<void> => {
  2192. const headers: Record<string, string> = {};
  2193. if (authToken) {
  2194. headers['Authorization'] = `Bearer ${authToken}`;
  2195. }
  2196. const response = await fetch(`${API_BASE}/archives/${id}/download`, { headers });
  2197. if (!response.ok) {
  2198. const error = await response.json().catch(() => ({}));
  2199. throw new Error(error.detail || `HTTP ${response.status}`);
  2200. }
  2201. const disposition = response.headers.get('Content-Disposition');
  2202. const filenameMatch = disposition?.match(/filename="?([^";\n]+)"?/);
  2203. const downloadFilename = filenameMatch?.[1] || filename || `archive_${id}.3mf`;
  2204. const blob = await response.blob();
  2205. const url = window.URL.createObjectURL(blob);
  2206. const a = document.createElement('a');
  2207. a.href = url;
  2208. a.download = downloadFilename;
  2209. document.body.appendChild(a);
  2210. a.click();
  2211. document.body.removeChild(a);
  2212. window.URL.revokeObjectURL(url);
  2213. },
  2214. getArchiveGcode: (id: number) => `${API_BASE}/archives/${id}/gcode`,
  2215. getArchivePlatePreview: (id: number) => `${API_BASE}/archives/${id}/plate-preview`,
  2216. getArchiveTimelapse: (id: number) => `${API_BASE}/archives/${id}/timelapse?v=${Date.now()}`,
  2217. scanArchiveTimelapse: (id: number) =>
  2218. request<{
  2219. status: string;
  2220. message: string;
  2221. filename?: string;
  2222. available_files?: Array<{ name: string; path: string; size: number; mtime: string | null }>;
  2223. }>(`/archives/${id}/timelapse/scan`, {
  2224. method: 'POST',
  2225. }),
  2226. selectArchiveTimelapse: (id: number, filename: string) =>
  2227. request<{ status: string; message: string; filename: string }>(
  2228. `/archives/${id}/timelapse/select?filename=${encodeURIComponent(filename)}`,
  2229. { method: 'POST' }
  2230. ),
  2231. uploadArchiveTimelapse: async (archiveId: number, file: File): Promise<{ status: string; filename: string }> => {
  2232. const formData = new FormData();
  2233. formData.append('file', file);
  2234. const headers: Record<string, string> = {};
  2235. if (authToken) {
  2236. headers['Authorization'] = `Bearer ${authToken}`;
  2237. }
  2238. const response = await fetch(`${API_BASE}/archives/${archiveId}/timelapse/upload`, {
  2239. method: 'POST',
  2240. headers,
  2241. body: formData,
  2242. });
  2243. if (!response.ok) {
  2244. const error = await response.json().catch(() => ({}));
  2245. throw new Error(error.detail || `HTTP ${response.status}`);
  2246. }
  2247. return response.json();
  2248. },
  2249. // Timelapse Editor
  2250. getTimelapseInfo: (archiveId: number) =>
  2251. request<{
  2252. duration: number;
  2253. width: number;
  2254. height: number;
  2255. fps: number;
  2256. codec: string;
  2257. file_size: number;
  2258. has_audio: boolean;
  2259. }>(`/archives/${archiveId}/timelapse/info`),
  2260. getTimelapseThumbnails: (archiveId: number, count: number = 10) =>
  2261. request<{
  2262. thumbnails: string[];
  2263. timestamps: number[];
  2264. }>(`/archives/${archiveId}/timelapse/thumbnails?count=${count}`),
  2265. processTimelapse: async (
  2266. archiveId: number,
  2267. params: {
  2268. trimStart?: number;
  2269. trimEnd?: number;
  2270. speed?: number;
  2271. saveMode: 'replace' | 'new';
  2272. outputFilename?: string;
  2273. },
  2274. audioFile?: File
  2275. ): Promise<{ status: string; output_path: string | null; message: string }> => {
  2276. const formData = new FormData();
  2277. formData.append('trim_start', String(params.trimStart ?? 0));
  2278. if (params.trimEnd !== undefined) {
  2279. formData.append('trim_end', String(params.trimEnd));
  2280. }
  2281. formData.append('speed', String(params.speed ?? 1));
  2282. formData.append('save_mode', params.saveMode);
  2283. if (params.outputFilename) {
  2284. formData.append('output_filename', params.outputFilename);
  2285. }
  2286. if (audioFile) {
  2287. formData.append('audio', audioFile);
  2288. }
  2289. const headers: Record<string, string> = {};
  2290. if (authToken) {
  2291. headers['Authorization'] = `Bearer ${authToken}`;
  2292. }
  2293. const response = await fetch(`${API_BASE}/archives/${archiveId}/timelapse/process`, {
  2294. method: 'POST',
  2295. headers,
  2296. body: formData,
  2297. });
  2298. if (!response.ok) {
  2299. const error = await response.json().catch(() => ({}));
  2300. throw new Error(error.detail || `HTTP ${response.status}`);
  2301. }
  2302. return response.json();
  2303. },
  2304. // Photos
  2305. getArchivePhotoUrl: (archiveId: number, filename: string) =>
  2306. `${API_BASE}/archives/${archiveId}/photos/${encodeURIComponent(filename)}`,
  2307. uploadArchivePhoto: async (archiveId: number, file: File): Promise<{ status: string; filename: string; photos: string[] }> => {
  2308. const formData = new FormData();
  2309. formData.append('file', file);
  2310. const headers: Record<string, string> = {};
  2311. if (authToken) {
  2312. headers['Authorization'] = `Bearer ${authToken}`;
  2313. }
  2314. const response = await fetch(`${API_BASE}/archives/${archiveId}/photos`, {
  2315. headers,
  2316. method: 'POST',
  2317. body: formData,
  2318. });
  2319. if (!response.ok) {
  2320. const error = await response.json().catch(() => ({}));
  2321. throw new Error(error.detail || `HTTP ${response.status}`);
  2322. }
  2323. return response.json();
  2324. },
  2325. deleteArchivePhoto: (archiveId: number, filename: string) =>
  2326. request<{ status: string; photos: string[] | null }>(`/archives/${archiveId}/photos/${encodeURIComponent(filename)}`, {
  2327. method: 'DELETE',
  2328. }),
  2329. // Source 3MF (original slicer project file)
  2330. getSource3mfDownloadUrl: (archiveId: number) =>
  2331. `${API_BASE}/archives/${archiveId}/source`,
  2332. downloadSource3mf: async (archiveId: number): Promise<void> => {
  2333. const headers: Record<string, string> = {};
  2334. if (authToken) {
  2335. headers['Authorization'] = `Bearer ${authToken}`;
  2336. }
  2337. const response = await fetch(`${API_BASE}/archives/${archiveId}/source`, { headers });
  2338. if (!response.ok) {
  2339. const error = await response.json().catch(() => ({}));
  2340. throw new Error(error.detail || `HTTP ${response.status}`);
  2341. }
  2342. const disposition = response.headers.get('Content-Disposition');
  2343. const filenameMatch = disposition?.match(/filename="?([^";\n]+)"?/);
  2344. const filename = filenameMatch?.[1] || `source_${archiveId}.3mf`;
  2345. const blob = await response.blob();
  2346. const url = window.URL.createObjectURL(blob);
  2347. const a = document.createElement('a');
  2348. a.href = url;
  2349. a.download = filename;
  2350. document.body.appendChild(a);
  2351. a.click();
  2352. document.body.removeChild(a);
  2353. window.URL.revokeObjectURL(url);
  2354. },
  2355. getSource3mfForSlicer: (archiveId: number, filename: string) =>
  2356. `${API_BASE}/archives/${archiveId}/source/${encodeURIComponent(filename.endsWith('.3mf') ? filename : filename + '.3mf')}`,
  2357. uploadSource3mf: async (archiveId: number, file: File): Promise<{ status: string; filename: string }> => {
  2358. const formData = new FormData();
  2359. formData.append('file', file);
  2360. const headers: Record<string, string> = {};
  2361. if (authToken) {
  2362. headers['Authorization'] = `Bearer ${authToken}`;
  2363. }
  2364. const response = await fetch(`${API_BASE}/archives/${archiveId}/source`, {
  2365. method: 'POST',
  2366. headers,
  2367. body: formData,
  2368. });
  2369. if (!response.ok) {
  2370. const error = await response.json().catch(() => ({}));
  2371. throw new Error(error.detail || `HTTP ${response.status}`);
  2372. }
  2373. return response.json();
  2374. },
  2375. deleteSource3mf: (archiveId: number) =>
  2376. request<{ status: string }>(`/archives/${archiveId}/source`, {
  2377. method: 'DELETE',
  2378. }),
  2379. // F3D (Fusion 360 design file)
  2380. getF3dDownloadUrl: (archiveId: number) =>
  2381. `${API_BASE}/archives/${archiveId}/f3d`,
  2382. downloadF3d: async (archiveId: number): Promise<void> => {
  2383. const headers: Record<string, string> = {};
  2384. if (authToken) {
  2385. headers['Authorization'] = `Bearer ${authToken}`;
  2386. }
  2387. const response = await fetch(`${API_BASE}/archives/${archiveId}/f3d`, { headers });
  2388. if (!response.ok) {
  2389. const error = await response.json().catch(() => ({}));
  2390. throw new Error(error.detail || `HTTP ${response.status}`);
  2391. }
  2392. const disposition = response.headers.get('Content-Disposition');
  2393. const filenameMatch = disposition?.match(/filename="?([^";\n]+)"?/);
  2394. const filename = filenameMatch?.[1] || `archive_${archiveId}.f3d`;
  2395. const blob = await response.blob();
  2396. const url = window.URL.createObjectURL(blob);
  2397. const a = document.createElement('a');
  2398. a.href = url;
  2399. a.download = filename;
  2400. document.body.appendChild(a);
  2401. a.click();
  2402. document.body.removeChild(a);
  2403. window.URL.revokeObjectURL(url);
  2404. },
  2405. uploadF3d: async (archiveId: number, file: File): Promise<{ status: string; filename: string }> => {
  2406. const formData = new FormData();
  2407. formData.append('file', file);
  2408. const headers: Record<string, string> = {};
  2409. if (authToken) {
  2410. headers['Authorization'] = `Bearer ${authToken}`;
  2411. }
  2412. const response = await fetch(`${API_BASE}/archives/${archiveId}/f3d`, {
  2413. method: 'POST',
  2414. headers,
  2415. body: formData,
  2416. });
  2417. if (!response.ok) {
  2418. const error = await response.json().catch(() => ({}));
  2419. throw new Error(error.detail || `HTTP ${response.status}`);
  2420. }
  2421. return response.json();
  2422. },
  2423. deleteF3d: (archiveId: number) =>
  2424. request<{ status: string }>(`/archives/${archiveId}/f3d`, {
  2425. method: 'DELETE',
  2426. }),
  2427. // QR Code
  2428. getArchiveQRCodeUrl: (archiveId: number, size = 200) =>
  2429. `${API_BASE}/archives/${archiveId}/qrcode?size=${size}`,
  2430. getArchiveCapabilities: (id: number) =>
  2431. request<{
  2432. has_model: boolean;
  2433. has_gcode: boolean;
  2434. has_source: boolean;
  2435. build_volume: { x: number; y: number; z: number };
  2436. filament_colors: string[];
  2437. }>(`/archives/${id}/capabilities`),
  2438. // Project Page
  2439. getArchiveProjectPage: (id: number) =>
  2440. request<{
  2441. title: string | null;
  2442. description: string | null;
  2443. designer: string | null;
  2444. designer_user_id: string | null;
  2445. license: string | null;
  2446. copyright: string | null;
  2447. creation_date: string | null;
  2448. modification_date: string | null;
  2449. origin: string | null;
  2450. profile_title: string | null;
  2451. profile_description: string | null;
  2452. profile_cover: string | null;
  2453. profile_user_id: string | null;
  2454. profile_user_name: string | null;
  2455. design_model_id: string | null;
  2456. design_profile_id: string | null;
  2457. design_region: string | null;
  2458. model_pictures: Array<{ name: string; path: string; url: string }>;
  2459. profile_pictures: Array<{ name: string; path: string; url: string }>;
  2460. thumbnails: Array<{ name: string; path: string; url: string }>;
  2461. }>(`/archives/${id}/project-page`),
  2462. updateArchiveProjectPage: (id: number, data: {
  2463. title?: string;
  2464. description?: string;
  2465. designer?: string;
  2466. license?: string;
  2467. copyright?: string;
  2468. profile_title?: string;
  2469. profile_description?: string;
  2470. }) =>
  2471. request(`/archives/${id}/project-page`, {
  2472. method: 'PATCH',
  2473. body: JSON.stringify(data),
  2474. }),
  2475. getArchiveProjectImageUrl: (archiveId: number, imagePath: string) =>
  2476. `${API_BASE}/archives/${archiveId}/project-image/${encodeURIComponent(imagePath)}`,
  2477. getArchiveForSlicer: (id: number, filename: string) =>
  2478. `${API_BASE}/archives/${id}/file/${encodeURIComponent(filename.endsWith('.3mf') ? filename : filename + '.3mf')}`,
  2479. getArchivePlates: (archiveId: number) =>
  2480. request<ArchivePlatesResponse>(`/archives/${archiveId}/plates`),
  2481. getArchiveFilamentRequirements: (archiveId: number, plateId?: number) =>
  2482. request<{
  2483. archive_id: number;
  2484. filename: string;
  2485. plate_id: number | null;
  2486. filaments: Array<{
  2487. slot_id: number;
  2488. type: string;
  2489. color: string;
  2490. used_grams: number;
  2491. used_meters: number;
  2492. }>;
  2493. }>(`/archives/${archiveId}/filament-requirements${plateId !== undefined ? `?plate_id=${plateId}` : ''}`),
  2494. reprintArchive: (
  2495. archiveId: number,
  2496. printerId: number,
  2497. options?: {
  2498. plate_id?: number;
  2499. ams_mapping?: number[];
  2500. timelapse?: boolean;
  2501. bed_levelling?: boolean;
  2502. flow_cali?: boolean;
  2503. vibration_cali?: boolean;
  2504. layer_inspect?: boolean;
  2505. use_ams?: boolean;
  2506. }
  2507. ) =>
  2508. request<{ status: string; printer_id: number; archive_id: number; filename: string }>(
  2509. `/archives/${archiveId}/reprint?printer_id=${printerId}`,
  2510. {
  2511. method: 'POST',
  2512. headers: options ? { 'Content-Type': 'application/json' } : undefined,
  2513. body: options ? JSON.stringify(options) : undefined,
  2514. }
  2515. ),
  2516. uploadArchive: async (file: File, printerId?: number): Promise<Archive> => {
  2517. const formData = new FormData();
  2518. formData.append('file', file);
  2519. const url = printerId
  2520. ? `${API_BASE}/archives/upload?printer_id=${printerId}`
  2521. : `${API_BASE}/archives/upload`;
  2522. const headers: Record<string, string> = {};
  2523. if (authToken) {
  2524. headers['Authorization'] = `Bearer ${authToken}`;
  2525. }
  2526. const response = await fetch(url, {
  2527. method: 'POST',
  2528. headers,
  2529. body: formData,
  2530. });
  2531. if (!response.ok) {
  2532. const error = await response.json().catch(() => ({}));
  2533. throw new Error(error.detail || `HTTP ${response.status}`);
  2534. }
  2535. return response.json();
  2536. },
  2537. uploadArchivesBulk: async (files: File[], printerId?: number): Promise<BulkUploadResult> => {
  2538. const formData = new FormData();
  2539. files.forEach((file) => formData.append('files', file));
  2540. const url = printerId
  2541. ? `${API_BASE}/archives/upload-bulk?printer_id=${printerId}`
  2542. : `${API_BASE}/archives/upload-bulk`;
  2543. const headers: Record<string, string> = {};
  2544. if (authToken) {
  2545. headers['Authorization'] = `Bearer ${authToken}`;
  2546. }
  2547. const response = await fetch(url, {
  2548. method: 'POST',
  2549. headers,
  2550. body: formData,
  2551. });
  2552. if (!response.ok) {
  2553. const error = await response.json().catch(() => ({}));
  2554. throw new Error(error.detail || `HTTP ${response.status}`);
  2555. }
  2556. return response.json();
  2557. },
  2558. // Settings
  2559. getSettings: () => request<AppSettings>('/settings/'),
  2560. updateSettings: (data: AppSettingsUpdate) =>
  2561. request<AppSettings>('/settings/', {
  2562. method: 'PUT',
  2563. body: JSON.stringify(data),
  2564. }),
  2565. getMQTTStatus: () => request<MQTTStatus>('/settings/mqtt/status'),
  2566. resetSettings: () =>
  2567. request<AppSettings>('/settings/reset', { method: 'POST' }),
  2568. exportBackup: async (): Promise<{ blob: Blob; filename: string }> => {
  2569. // New simplified backup - complete database + all files
  2570. const url = `${API_BASE}/settings/backup`;
  2571. const headers: Record<string, string> = {};
  2572. if (authToken) {
  2573. headers['Authorization'] = `Bearer ${authToken}`;
  2574. }
  2575. const response = await fetch(url, { headers });
  2576. // Check for errors
  2577. if (!response.ok) {
  2578. const errorText = await response.text();
  2579. throw new Error(errorText || `Backup failed with status ${response.status}`);
  2580. }
  2581. // Get filename from Content-Disposition header
  2582. const contentDisposition = response.headers.get('Content-Disposition');
  2583. let filename = 'bambuddy-backup.zip';
  2584. if (contentDisposition) {
  2585. const match = contentDisposition.match(/filename=([^;]+)/);
  2586. if (match) filename = match[1].trim();
  2587. }
  2588. const blob = await response.blob();
  2589. return { blob, filename };
  2590. },
  2591. importBackup: async (file: File) => {
  2592. // New simplified restore - replaces database + all directories
  2593. const formData = new FormData();
  2594. formData.append('file', file);
  2595. const url = `${API_BASE}/settings/restore`;
  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. return response.json() as Promise<{
  2606. success: boolean;
  2607. message: string;
  2608. }>;
  2609. },
  2610. checkFfmpeg: () =>
  2611. request<{ installed: boolean; path: string | null }>('/settings/check-ffmpeg'),
  2612. getNetworkInterfaces: () =>
  2613. request<{ interfaces: NetworkInterface[] }>('/settings/network-interfaces'),
  2614. // Cloud
  2615. getCloudStatus: () => request<CloudAuthStatus>('/cloud/status'),
  2616. cloudLogin: (email: string, password: string, region = 'global') =>
  2617. request<CloudLoginResponse>('/cloud/login', {
  2618. method: 'POST',
  2619. body: JSON.stringify({ email, password, region }),
  2620. }),
  2621. cloudVerify: (email: string, code: string, tfaKey?: string) =>
  2622. request<CloudLoginResponse>('/cloud/verify', {
  2623. method: 'POST',
  2624. body: JSON.stringify({ email, code, tfa_key: tfaKey }),
  2625. }),
  2626. cloudSetToken: (access_token: string) =>
  2627. request<CloudAuthStatus>('/cloud/token', {
  2628. method: 'POST',
  2629. body: JSON.stringify({ access_token }),
  2630. }),
  2631. cloudLogout: () =>
  2632. request<{ success: boolean }>('/cloud/logout', { method: 'POST' }),
  2633. getCloudSettings: (version = '02.04.00.70') =>
  2634. request<SlicerSettingsResponse>(`/cloud/settings?version=${version}`),
  2635. getCloudSettingDetail: (settingId: string) =>
  2636. request<SlicerSettingDetail>(`/cloud/settings/${settingId}`),
  2637. createCloudSetting: (data: SlicerSettingCreate) =>
  2638. request<SlicerSettingDetail>('/cloud/settings', {
  2639. method: 'POST',
  2640. body: JSON.stringify(data),
  2641. }),
  2642. updateCloudSetting: (settingId: string, data: SlicerSettingUpdate) =>
  2643. request<SlicerSettingDetail>(`/cloud/settings/${settingId}`, {
  2644. method: 'PUT',
  2645. body: JSON.stringify(data),
  2646. }),
  2647. deleteCloudSetting: (settingId: string) =>
  2648. request<SlicerSettingDeleteResponse>(`/cloud/settings/${settingId}`, {
  2649. method: 'DELETE',
  2650. }),
  2651. getCloudDevices: () => request<CloudDevice[]>('/cloud/devices'),
  2652. getCloudFields: (presetType: 'filament' | 'print' | 'process' | 'printer') =>
  2653. request<FieldDefinitionsResponse>(`/cloud/fields/${presetType}`),
  2654. getAllCloudFields: () =>
  2655. request<Record<string, FieldDefinitionsResponse>>('/cloud/fields'),
  2656. getFilamentInfo: (settingIds: string[]) =>
  2657. request<Record<string, { name: string; k: number | null }>>('/cloud/filament-info', {
  2658. method: 'POST',
  2659. body: JSON.stringify(settingIds),
  2660. }),
  2661. // Smart Plugs
  2662. getSmartPlugs: () => request<SmartPlug[]>('/smart-plugs/'),
  2663. getSmartPlug: (id: number) => request<SmartPlug>(`/smart-plugs/${id}`),
  2664. getSmartPlugByPrinter: (printerId: number) => request<SmartPlug | null>(`/smart-plugs/by-printer/${printerId}`),
  2665. getScriptPlugsByPrinter: (printerId: number) => request<SmartPlug[]>(`/smart-plugs/by-printer/${printerId}/scripts`),
  2666. createSmartPlug: (data: SmartPlugCreate) =>
  2667. request<SmartPlug>('/smart-plugs/', {
  2668. method: 'POST',
  2669. body: JSON.stringify(data),
  2670. }),
  2671. updateSmartPlug: (id: number, data: SmartPlugUpdate) =>
  2672. request<SmartPlug>(`/smart-plugs/${id}`, {
  2673. method: 'PATCH',
  2674. body: JSON.stringify(data),
  2675. }),
  2676. deleteSmartPlug: (id: number) =>
  2677. request<void>(`/smart-plugs/${id}`, { method: 'DELETE' }),
  2678. controlSmartPlug: (id: number, action: 'on' | 'off' | 'toggle') =>
  2679. request<{ success: boolean; action: string }>(`/smart-plugs/${id}/control`, {
  2680. method: 'POST',
  2681. body: JSON.stringify({ action }),
  2682. }),
  2683. getSmartPlugStatus: (id: number) =>
  2684. request<SmartPlugStatus>(`/smart-plugs/${id}/status`),
  2685. testSmartPlugConnection: (ip_address: string, username?: string | null, password?: string | null) =>
  2686. request<SmartPlugTestResult>('/smart-plugs/test-connection', {
  2687. method: 'POST',
  2688. body: JSON.stringify({ ip_address, username, password }),
  2689. }),
  2690. // Tasmota Discovery (auto-detects network)
  2691. startTasmotaScan: () =>
  2692. request<TasmotaScanStatus>('/smart-plugs/discover/scan', { method: 'POST' }),
  2693. getTasmotaScanStatus: () =>
  2694. request<TasmotaScanStatus>('/smart-plugs/discover/status'),
  2695. stopTasmotaScan: () =>
  2696. request<TasmotaScanStatus>('/smart-plugs/discover/stop', { method: 'POST' }),
  2697. getDiscoveredTasmotaDevices: () =>
  2698. request<DiscoveredTasmotaDevice[]>('/smart-plugs/discover/devices'),
  2699. // Home Assistant Integration
  2700. testHAConnection: (url: string, token: string) =>
  2701. request<HATestConnectionResult>('/smart-plugs/ha/test-connection', {
  2702. method: 'POST',
  2703. body: JSON.stringify({ url, token }),
  2704. }),
  2705. getHAEntities: (search?: string) => {
  2706. const params = search ? `?search=${encodeURIComponent(search)}` : '';
  2707. return request<HAEntity[]>(`/smart-plugs/ha/entities${params}`);
  2708. },
  2709. getHASensorEntities: () =>
  2710. request<HASensorEntity[]>('/smart-plugs/ha/sensors'),
  2711. // Print Queue
  2712. getQueue: (printerId?: number, status?: string) => {
  2713. const params = new URLSearchParams();
  2714. if (printerId) params.set('printer_id', String(printerId));
  2715. if (status) params.set('status', status);
  2716. return request<PrintQueueItem[]>(`/queue/?${params}`);
  2717. },
  2718. getQueueItem: (id: number) => request<PrintQueueItem>(`/queue/${id}`),
  2719. addToQueue: (data: PrintQueueItemCreate) =>
  2720. request<PrintQueueItem>('/queue/', {
  2721. method: 'POST',
  2722. body: JSON.stringify(data),
  2723. }),
  2724. updateQueueItem: (id: number, data: PrintQueueItemUpdate) =>
  2725. request<PrintQueueItem>(`/queue/${id}`, {
  2726. method: 'PATCH',
  2727. body: JSON.stringify(data),
  2728. }),
  2729. removeFromQueue: (id: number) =>
  2730. request<{ message: string }>(`/queue/${id}`, { method: 'DELETE' }),
  2731. reorderQueue: (items: { id: number; position: number }[]) =>
  2732. request<{ message: string }>('/queue/reorder', {
  2733. method: 'POST',
  2734. body: JSON.stringify({ items }),
  2735. }),
  2736. cancelQueueItem: (id: number) =>
  2737. request<{ message: string }>(`/queue/${id}/cancel`, { method: 'POST' }),
  2738. stopQueueItem: (id: number) =>
  2739. request<{ message: string }>(`/queue/${id}/stop`, { method: 'POST' }),
  2740. startQueueItem: (id: number) =>
  2741. request<PrintQueueItem>(`/queue/${id}/start`, { method: 'POST' }),
  2742. bulkUpdateQueue: (data: PrintQueueBulkUpdate) =>
  2743. request<PrintQueueBulkUpdateResponse>('/queue/bulk', {
  2744. method: 'PATCH',
  2745. body: JSON.stringify(data),
  2746. }),
  2747. // K-Profiles
  2748. getKProfiles: (printerId: number, nozzleDiameter = '0.4') =>
  2749. request<KProfilesResponse>(`/printers/${printerId}/kprofiles/?nozzle_diameter=${nozzleDiameter}`),
  2750. setKProfile: (printerId: number, profile: KProfileCreate) =>
  2751. request<{ success: boolean; message: string }>(`/printers/${printerId}/kprofiles/`, {
  2752. method: 'POST',
  2753. body: JSON.stringify(profile),
  2754. }),
  2755. deleteKProfile: (printerId: number, profile: KProfileDelete) =>
  2756. request<{ success: boolean; message: string }>(`/printers/${printerId}/kprofiles/`, {
  2757. method: 'DELETE',
  2758. body: JSON.stringify(profile),
  2759. }),
  2760. setKProfilesBatch: (printerId: number, profiles: KProfileCreate[]) =>
  2761. request<{ success: boolean; message: string }>(`/printers/${printerId}/kprofiles/batch`, {
  2762. method: 'POST',
  2763. body: JSON.stringify(profiles),
  2764. }),
  2765. // K-Profile Notes (stored locally, not on printer)
  2766. getKProfileNotes: (printerId: number) =>
  2767. request<KProfileNotesResponse>(`/printers/${printerId}/kprofiles/notes`),
  2768. setKProfileNote: (printerId: number, settingId: string, note: string) =>
  2769. request<{ success: boolean; message: string }>(`/printers/${printerId}/kprofiles/notes`, {
  2770. method: 'PUT',
  2771. body: JSON.stringify({ setting_id: settingId, note }),
  2772. }),
  2773. deleteKProfileNote: (printerId: number, settingId: string) =>
  2774. request<{ success: boolean; message: string }>(`/printers/${printerId}/kprofiles/notes/${encodeURIComponent(settingId)}`, {
  2775. method: 'DELETE',
  2776. }),
  2777. // Slot Preset Mappings
  2778. getSlotPresets: (printerId: number) =>
  2779. request<Record<number, SlotPresetMapping>>(`/printers/${printerId}/slot-presets`),
  2780. getSlotPreset: (printerId: number, amsId: number, trayId: number) =>
  2781. request<SlotPresetMapping | null>(`/printers/${printerId}/slot-presets/${amsId}/${trayId}`),
  2782. saveSlotPreset: (printerId: number, amsId: number, trayId: number, presetId: string, presetName: string, presetSource = 'cloud') =>
  2783. request<SlotPresetMapping>(`/printers/${printerId}/slot-presets/${amsId}/${trayId}?preset_id=${encodeURIComponent(presetId)}&preset_name=${encodeURIComponent(presetName)}&preset_source=${encodeURIComponent(presetSource)}`, {
  2784. method: 'PUT',
  2785. }),
  2786. deleteSlotPreset: (printerId: number, amsId: number, trayId: number) =>
  2787. request<{ success: boolean }>(`/printers/${printerId}/slot-presets/${amsId}/${trayId}`, {
  2788. method: 'DELETE',
  2789. }),
  2790. configureAmsSlot: (
  2791. printerId: number,
  2792. amsId: number,
  2793. trayId: number,
  2794. config: {
  2795. tray_info_idx: string;
  2796. tray_type: string;
  2797. tray_sub_brands: string;
  2798. tray_color: string;
  2799. nozzle_temp_min: number;
  2800. nozzle_temp_max: number;
  2801. cali_idx: number;
  2802. nozzle_diameter: string;
  2803. setting_id?: string;
  2804. kprofile_filament_id?: string;
  2805. kprofile_setting_id?: string;
  2806. k_value?: number;
  2807. }
  2808. ) => {
  2809. const params = new URLSearchParams({
  2810. tray_info_idx: config.tray_info_idx,
  2811. tray_type: config.tray_type,
  2812. tray_sub_brands: config.tray_sub_brands,
  2813. tray_color: config.tray_color,
  2814. nozzle_temp_min: config.nozzle_temp_min.toString(),
  2815. nozzle_temp_max: config.nozzle_temp_max.toString(),
  2816. cali_idx: config.cali_idx.toString(),
  2817. nozzle_diameter: config.nozzle_diameter,
  2818. });
  2819. if (config.setting_id) {
  2820. params.set('setting_id', config.setting_id);
  2821. }
  2822. if (config.kprofile_filament_id) {
  2823. params.set('kprofile_filament_id', config.kprofile_filament_id);
  2824. }
  2825. if (config.kprofile_setting_id) {
  2826. params.set('kprofile_setting_id', config.kprofile_setting_id);
  2827. }
  2828. if (config.k_value !== undefined && config.k_value > 0) {
  2829. params.set('k_value', config.k_value.toString());
  2830. }
  2831. return request<{ success: boolean; message: string }>(
  2832. `/printers/${printerId}/slots/${amsId}/${trayId}/configure?${params}`,
  2833. { method: 'POST' }
  2834. );
  2835. },
  2836. resetAmsSlot: (printerId: number, amsId: number, trayId: number) =>
  2837. request<{ success: boolean; message: string }>(
  2838. `/printers/${printerId}/ams/${amsId}/tray/${trayId}/reset`,
  2839. { method: 'POST' }
  2840. ),
  2841. // Filaments
  2842. listFilaments: () => request<Filament[]>('/filaments/'),
  2843. getFilament: (id: number) => request<Filament>(`/filaments/${id}`),
  2844. getFilamentsByType: (type: string) => request<Filament[]>(`/filaments/by-type/${type}`),
  2845. // Notification Providers
  2846. getNotificationProviders: () => request<NotificationProvider[]>('/notifications/'),
  2847. getNotificationProvider: (id: number) => request<NotificationProvider>(`/notifications/${id}`),
  2848. createNotificationProvider: (data: NotificationProviderCreate) =>
  2849. request<NotificationProvider>('/notifications/', {
  2850. method: 'POST',
  2851. body: JSON.stringify(data),
  2852. }),
  2853. updateNotificationProvider: (id: number, data: NotificationProviderUpdate) =>
  2854. request<NotificationProvider>(`/notifications/${id}`, {
  2855. method: 'PATCH',
  2856. body: JSON.stringify(data),
  2857. }),
  2858. deleteNotificationProvider: (id: number) =>
  2859. request<{ message: string }>(`/notifications/${id}`, { method: 'DELETE' }),
  2860. testNotificationProvider: (id: number) =>
  2861. request<NotificationTestResponse>(`/notifications/${id}/test`, { method: 'POST' }),
  2862. testNotificationConfig: (data: NotificationTestRequest) =>
  2863. request<NotificationTestResponse>('/notifications/test-config', {
  2864. method: 'POST',
  2865. body: JSON.stringify(data),
  2866. }),
  2867. testAllNotificationProviders: () =>
  2868. request<{
  2869. tested: number;
  2870. success: number;
  2871. failed: number;
  2872. results: Array<{
  2873. provider_id: number;
  2874. provider_name: string;
  2875. provider_type: string;
  2876. success: boolean;
  2877. message: string;
  2878. }>;
  2879. }>('/notifications/test-all', { method: 'POST' }),
  2880. // Notification Templates
  2881. getNotificationTemplates: () => request<NotificationTemplate[]>('/notification-templates'),
  2882. getNotificationTemplate: (id: number) => request<NotificationTemplate>(`/notification-templates/${id}`),
  2883. updateNotificationTemplate: (id: number, data: NotificationTemplateUpdate) =>
  2884. request<NotificationTemplate>(`/notification-templates/${id}`, {
  2885. method: 'PUT',
  2886. body: JSON.stringify(data),
  2887. }),
  2888. resetNotificationTemplate: (id: number) =>
  2889. request<NotificationTemplate>(`/notification-templates/${id}/reset`, {
  2890. method: 'POST',
  2891. }),
  2892. getTemplateVariables: () => request<EventVariablesResponse[]>('/notification-templates/variables'),
  2893. previewTemplate: (data: TemplatePreviewRequest) =>
  2894. request<TemplatePreviewResponse>('/notification-templates/preview', {
  2895. method: 'POST',
  2896. body: JSON.stringify(data),
  2897. }),
  2898. // Notification Logs
  2899. getNotificationLogs: (params?: {
  2900. limit?: number;
  2901. offset?: number;
  2902. provider_id?: number;
  2903. event_type?: string;
  2904. success?: boolean;
  2905. days?: number;
  2906. }) => {
  2907. const searchParams = new URLSearchParams();
  2908. if (params?.limit) searchParams.set('limit', String(params.limit));
  2909. if (params?.offset) searchParams.set('offset', String(params.offset));
  2910. if (params?.provider_id) searchParams.set('provider_id', String(params.provider_id));
  2911. if (params?.event_type) searchParams.set('event_type', params.event_type);
  2912. if (params?.success !== undefined) searchParams.set('success', String(params.success));
  2913. if (params?.days) searchParams.set('days', String(params.days));
  2914. return request<NotificationLogEntry[]>(`/notifications/logs?${searchParams}`);
  2915. },
  2916. getNotificationLogStats: (days = 7) =>
  2917. request<NotificationLogStats>(`/notifications/logs/stats?days=${days}`),
  2918. clearNotificationLogs: (olderThanDays = 30) =>
  2919. request<{ deleted: number; message: string }>(
  2920. `/notifications/logs?older_than_days=${olderThanDays}`,
  2921. { method: 'DELETE' }
  2922. ),
  2923. // Spoolman Integration
  2924. getSpoolmanStatus: () => request<SpoolmanStatus>('/spoolman/status'),
  2925. connectSpoolman: () =>
  2926. request<{ success: boolean; message: string }>('/spoolman/connect', {
  2927. method: 'POST',
  2928. }),
  2929. disconnectSpoolman: () =>
  2930. request<{ success: boolean; message: string }>('/spoolman/disconnect', {
  2931. method: 'POST',
  2932. }),
  2933. syncPrinterAms: (printerId: number) =>
  2934. request<SpoolmanSyncResult>(`/spoolman/sync/${printerId}`, {
  2935. method: 'POST',
  2936. }),
  2937. syncAllPrintersAms: () =>
  2938. request<SpoolmanSyncResult>('/spoolman/sync-all', {
  2939. method: 'POST',
  2940. }),
  2941. getSpoolmanSpools: () =>
  2942. request<{ spools: unknown[] }>('/spoolman/spools'),
  2943. getSpoolmanFilaments: () =>
  2944. request<{ filaments: unknown[] }>('/spoolman/filaments'),
  2945. getUnlinkedSpools: () =>
  2946. request<UnlinkedSpool[]>('/spoolman/spools/unlinked'),
  2947. getLinkedSpools: () =>
  2948. request<LinkedSpoolsMap>('/spoolman/spools/linked'),
  2949. linkSpool: (spoolId: number, trayUuid: string) =>
  2950. request<{ success: boolean; message: string }>(`/spoolman/spools/${spoolId}/link`, {
  2951. method: 'POST',
  2952. body: JSON.stringify({ tray_uuid: trayUuid }),
  2953. }),
  2954. getSpoolmanSettings: () =>
  2955. request<{ spoolman_enabled: string; spoolman_url: string; spoolman_sync_mode: string; spoolman_disable_weight_sync: string; spoolman_report_partial_usage: string; }>('/settings/spoolman'),
  2956. updateSpoolmanSettings: (data: { spoolman_enabled?: string; spoolman_url?: string; spoolman_sync_mode?: string; spoolman_disable_weight_sync?: string; spoolman_report_partial_usage?: string; }) =>
  2957. request<{ spoolman_enabled: string; spoolman_url: string; spoolman_sync_mode: string; spoolman_disable_weight_sync: string; spoolman_report_partial_usage: string; }>('/settings/spoolman', {
  2958. method: 'PUT',
  2959. body: JSON.stringify(data),
  2960. }),
  2961. // Updates
  2962. getVersion: () => request<VersionInfo>('/updates/version'),
  2963. checkForUpdates: () => request<UpdateCheckResult>('/updates/check'),
  2964. applyUpdate: () =>
  2965. request<{ success: boolean; message: string; status?: UpdateStatus; is_docker?: boolean }>('/updates/apply', {
  2966. method: 'POST',
  2967. }),
  2968. getUpdateStatus: () => request<UpdateStatus>('/updates/status'),
  2969. // Maintenance
  2970. getMaintenanceTypes: () => request<MaintenanceType[]>('/maintenance/types'),
  2971. createMaintenanceType: (data: MaintenanceTypeCreate) =>
  2972. request<MaintenanceType>('/maintenance/types', {
  2973. method: 'POST',
  2974. body: JSON.stringify(data),
  2975. }),
  2976. updateMaintenanceType: (id: number, data: Partial<MaintenanceTypeCreate>) =>
  2977. request<MaintenanceType>(`/maintenance/types/${id}`, {
  2978. method: 'PATCH',
  2979. body: JSON.stringify(data),
  2980. }),
  2981. deleteMaintenanceType: (id: number) =>
  2982. request<{ status: string }>(`/maintenance/types/${id}`, { method: 'DELETE' }),
  2983. getMaintenanceOverview: () => request<PrinterMaintenanceOverview[]>('/maintenance/overview'),
  2984. getPrinterMaintenance: (printerId: number) =>
  2985. request<PrinterMaintenanceOverview>(`/maintenance/printers/${printerId}`),
  2986. updateMaintenanceItem: (itemId: number, data: { custom_interval_hours?: number | null; custom_interval_type?: 'hours' | 'days' | null; enabled?: boolean }) =>
  2987. request<MaintenanceStatus>(`/maintenance/items/${itemId}`, {
  2988. method: 'PATCH',
  2989. body: JSON.stringify(data),
  2990. }),
  2991. performMaintenance: (itemId: number, notes?: string) =>
  2992. request<MaintenanceStatus>(`/maintenance/items/${itemId}/perform`, {
  2993. method: 'POST',
  2994. body: JSON.stringify({ notes }),
  2995. }),
  2996. getMaintenanceHistory: (itemId: number) =>
  2997. request<MaintenanceHistory[]>(`/maintenance/items/${itemId}/history`),
  2998. getMaintenanceSummary: () => request<MaintenanceSummary>('/maintenance/summary'),
  2999. setPrinterHours: (printerId: number, totalHours: number) =>
  3000. request<{ printer_id: number; total_hours: number; archive_hours: number; offset_hours: number }>(
  3001. `/maintenance/printers/${printerId}/hours?total_hours=${totalHours}`,
  3002. { method: 'PATCH' }
  3003. ),
  3004. assignMaintenanceType: (printerId: number, typeId: number) =>
  3005. request<MaintenanceStatus>(`/maintenance/printers/${printerId}/assign/${typeId}`, {
  3006. method: 'POST',
  3007. }),
  3008. removeMaintenanceItem: (itemId: number) =>
  3009. request<{ status: string }>(`/maintenance/items/${itemId}`, {
  3010. method: 'DELETE',
  3011. }),
  3012. // Camera
  3013. getCameraStreamUrl: (printerId: number, fps = 10) =>
  3014. `${API_BASE}/printers/${printerId}/camera/stream?fps=${fps}`,
  3015. getCameraSnapshotUrl: (printerId: number) =>
  3016. `${API_BASE}/printers/${printerId}/camera/snapshot`,
  3017. testCameraConnection: (printerId: number) =>
  3018. request<{ success: boolean; message?: string; error?: string }>(`/printers/${printerId}/camera/test`),
  3019. getCameraStatus: (printerId: number) =>
  3020. request<{ active: boolean; stalled: boolean }>(`/printers/${printerId}/camera/status`),
  3021. // Plate Detection - Multi-reference calibration (stores up to 5 references per printer)
  3022. checkPlateEmpty: (printerId: number, options?: { useExternal?: boolean; includeDebugImage?: boolean }) => {
  3023. const params = new URLSearchParams();
  3024. params.set('use_external', String(options?.useExternal ?? false));
  3025. params.set('include_debug_image', String(options?.includeDebugImage ?? false));
  3026. return request<PlateDetectionResult>(
  3027. `/printers/${printerId}/camera/check-plate?${params.toString()}`
  3028. );
  3029. },
  3030. getPlateDetectionStatus: (printerId: number) => {
  3031. return request<PlateDetectionStatus & { chamber_light?: boolean }>(
  3032. `/printers/${printerId}/camera/plate-detection/status`
  3033. );
  3034. },
  3035. calibratePlateDetection: (printerId: number, options?: { label?: string; useExternal?: boolean }) => {
  3036. const params = new URLSearchParams();
  3037. if (options?.label) params.set('label', options.label);
  3038. params.set('use_external', String(options?.useExternal ?? false));
  3039. return request<CalibrationResult & { index: number }>(
  3040. `/printers/${printerId}/camera/plate-detection/calibrate?${params.toString()}`,
  3041. { method: 'POST' }
  3042. );
  3043. },
  3044. deletePlateCalibration: (printerId: number) => {
  3045. return request<CalibrationResult>(
  3046. `/printers/${printerId}/camera/plate-detection/calibrate`,
  3047. { method: 'DELETE' }
  3048. );
  3049. },
  3050. getPlateReferences: (printerId: number) => {
  3051. return request<{
  3052. references: PlateReference[];
  3053. max_references: number;
  3054. }>(`/printers/${printerId}/camera/plate-detection/references`);
  3055. },
  3056. getPlateReferenceThumbnailUrl: (printerId: number, index: number) => {
  3057. return `${API_BASE}/printers/${printerId}/camera/plate-detection/references/${index}/thumbnail`;
  3058. },
  3059. updatePlateReferenceLabel: (printerId: number, index: number, label: string) => {
  3060. const params = new URLSearchParams();
  3061. params.set('label', label);
  3062. return request<{ success: boolean; index: number; label: string }>(
  3063. `/printers/${printerId}/camera/plate-detection/references/${index}?${params.toString()}`,
  3064. { method: 'PUT' }
  3065. );
  3066. },
  3067. deletePlateReference: (printerId: number, index: number) => {
  3068. return request<{ success: boolean; message: string }>(
  3069. `/printers/${printerId}/camera/plate-detection/references/${index}`,
  3070. { method: 'DELETE' }
  3071. );
  3072. },
  3073. // External Links
  3074. getExternalLinks: () => request<ExternalLink[]>('/external-links/'),
  3075. getExternalLink: (id: number) => request<ExternalLink>(`/external-links/${id}`),
  3076. createExternalLink: (data: ExternalLinkCreate) =>
  3077. request<ExternalLink>('/external-links/', {
  3078. method: 'POST',
  3079. body: JSON.stringify(data),
  3080. }),
  3081. updateExternalLink: (id: number, data: ExternalLinkUpdate) =>
  3082. request<ExternalLink>(`/external-links/${id}`, {
  3083. method: 'PATCH',
  3084. body: JSON.stringify(data),
  3085. }),
  3086. deleteExternalLink: (id: number) =>
  3087. request<{ message: string }>(`/external-links/${id}`, { method: 'DELETE' }),
  3088. reorderExternalLinks: (ids: number[]) =>
  3089. request<ExternalLink[]>('/external-links/reorder', {
  3090. method: 'PUT',
  3091. body: JSON.stringify({ ids }),
  3092. }),
  3093. uploadExternalLinkIcon: async (id: number, file: File): Promise<ExternalLink> => {
  3094. const formData = new FormData();
  3095. formData.append('file', file);
  3096. const headers: Record<string, string> = {};
  3097. if (authToken) {
  3098. headers['Authorization'] = `Bearer ${authToken}`;
  3099. }
  3100. const response = await fetch(`${API_BASE}/external-links/${id}/icon`, {
  3101. method: 'POST',
  3102. headers,
  3103. body: formData,
  3104. });
  3105. if (!response.ok) {
  3106. const error = await response.json().catch(() => ({}));
  3107. throw new Error(error.detail || `HTTP ${response.status}`);
  3108. }
  3109. return response.json();
  3110. },
  3111. deleteExternalLinkIcon: (id: number) =>
  3112. request<ExternalLink>(`/external-links/${id}/icon`, { method: 'DELETE' }),
  3113. getExternalLinkIconUrl: (id: number) => `${API_BASE}/external-links/${id}/icon`,
  3114. // Projects
  3115. getProjects: (status?: string) => {
  3116. const params = new URLSearchParams();
  3117. if (status) params.set('status', status);
  3118. return request<ProjectListItem[]>(`/projects/?${params}`);
  3119. },
  3120. getProject: (id: number) => request<Project>(`/projects/${id}`),
  3121. createProject: (data: ProjectCreate) =>
  3122. request<Project>('/projects/', {
  3123. method: 'POST',
  3124. body: JSON.stringify(data),
  3125. }),
  3126. updateProject: (id: number, data: ProjectUpdate) =>
  3127. request<Project>(`/projects/${id}`, {
  3128. method: 'PATCH',
  3129. body: JSON.stringify(data),
  3130. }),
  3131. deleteProject: (id: number) =>
  3132. request<{ message: string }>(`/projects/${id}`, { method: 'DELETE' }),
  3133. getProjectArchives: (id: number, limit = 100, offset = 0) =>
  3134. request<Archive[]>(`/projects/${id}/archives?limit=${limit}&offset=${offset}`),
  3135. addArchivesToProject: (projectId: number, archiveIds: number[]) =>
  3136. request<{ message: string }>(`/projects/${projectId}/add-archives`, {
  3137. method: 'POST',
  3138. body: JSON.stringify({ archive_ids: archiveIds }),
  3139. }),
  3140. removeArchivesFromProject: (projectId: number, archiveIds: number[]) =>
  3141. request<{ message: string }>(`/projects/${projectId}/remove-archives`, {
  3142. method: 'POST',
  3143. body: JSON.stringify({ archive_ids: archiveIds }),
  3144. }),
  3145. addQueueItemsToProject: (projectId: number, queueItemIds: number[]) =>
  3146. request<{ message: string }>(`/projects/${projectId}/add-queue`, {
  3147. method: 'POST',
  3148. body: JSON.stringify({ queue_item_ids: queueItemIds }),
  3149. }),
  3150. // Project Attachments
  3151. uploadProjectAttachment: async (projectId: number, file: File): Promise<{
  3152. status: string;
  3153. filename: string;
  3154. original_name: string;
  3155. attachments: ProjectAttachment[];
  3156. }> => {
  3157. const formData = new FormData();
  3158. formData.append('file', file);
  3159. const headers: Record<string, string> = {};
  3160. if (authToken) {
  3161. headers['Authorization'] = `Bearer ${authToken}`;
  3162. }
  3163. const response = await fetch(`${API_BASE}/projects/${projectId}/attachments`, {
  3164. method: 'POST',
  3165. headers,
  3166. body: formData,
  3167. });
  3168. if (!response.ok) {
  3169. const error = await response.json().catch(() => ({}));
  3170. throw new Error(error.detail || `HTTP ${response.status}`);
  3171. }
  3172. return response.json();
  3173. },
  3174. getProjectAttachmentUrl: (projectId: number, filename: string) =>
  3175. `${API_BASE}/projects/${projectId}/attachments/${encodeURIComponent(filename)}`,
  3176. deleteProjectAttachment: (projectId: number, filename: string) =>
  3177. request<{ status: string; message: string; attachments: ProjectAttachment[] | null }>(
  3178. `/projects/${projectId}/attachments/${encodeURIComponent(filename)}`,
  3179. { method: 'DELETE' }
  3180. ),
  3181. // BOM (Bill of Materials)
  3182. getProjectBOM: (projectId: number) =>
  3183. request<BOMItem[]>(`/projects/${projectId}/bom`),
  3184. createBOMItem: (projectId: number, data: BOMItemCreate) =>
  3185. request<BOMItem>(`/projects/${projectId}/bom`, {
  3186. method: 'POST',
  3187. body: JSON.stringify(data),
  3188. }),
  3189. updateBOMItem: (projectId: number, itemId: number, data: BOMItemUpdate) =>
  3190. request<BOMItem>(`/projects/${projectId}/bom/${itemId}`, {
  3191. method: 'PATCH',
  3192. body: JSON.stringify(data),
  3193. }),
  3194. deleteBOMItem: (projectId: number, itemId: number) =>
  3195. request<{ status: string; message: string }>(`/projects/${projectId}/bom/${itemId}`, {
  3196. method: 'DELETE',
  3197. }),
  3198. // Templates
  3199. getTemplates: () => request<ProjectListItem[]>('/projects/templates/'),
  3200. createTemplateFromProject: (projectId: number) =>
  3201. request<Project>(`/projects/${projectId}/create-template`, { method: 'POST' }),
  3202. createProjectFromTemplate: (templateId: number, name?: string) =>
  3203. request<Project>(`/projects/from-template/${templateId}${name ? `?name=${encodeURIComponent(name)}` : ''}`, {
  3204. method: 'POST',
  3205. }),
  3206. // Timeline
  3207. getProjectTimeline: (projectId: number, limit = 50) =>
  3208. request<TimelineEvent[]>(`/projects/${projectId}/timeline?limit=${limit}`),
  3209. // Project Export/Import
  3210. exportProjectJson: (projectId: number) =>
  3211. request<ProjectExport>(`/projects/${projectId}/export?format=json`),
  3212. importProject: (data: ProjectImport) =>
  3213. request<Project>('/projects/import', {
  3214. method: 'POST',
  3215. body: JSON.stringify(data),
  3216. }),
  3217. importProjectFile: async (file: File): Promise<Project> => {
  3218. const formData = new FormData();
  3219. formData.append('file', file);
  3220. const headers: Record<string, string> = {};
  3221. if (authToken) {
  3222. headers['Authorization'] = `Bearer ${authToken}`;
  3223. }
  3224. const response = await fetch(`${API_BASE}/projects/import/file`, {
  3225. method: 'POST',
  3226. headers,
  3227. body: formData,
  3228. });
  3229. if (!response.ok) {
  3230. const error = await response.json().catch(() => ({}));
  3231. throw new Error(error.detail || `HTTP ${response.status}`);
  3232. }
  3233. return response.json();
  3234. },
  3235. exportProjectZip: async (projectId: number): Promise<{ blob: Blob; filename: string }> => {
  3236. const headers: Record<string, string> = {};
  3237. if (authToken) {
  3238. headers['Authorization'] = `Bearer ${authToken}`;
  3239. }
  3240. const response = await fetch(`${API_BASE}/projects/${projectId}/export`, {
  3241. headers,
  3242. });
  3243. if (!response.ok) {
  3244. const error = await response.json().catch(() => ({}));
  3245. throw new Error(error.detail || `HTTP ${response.status}`);
  3246. }
  3247. const contentDisposition = response.headers.get('Content-Disposition');
  3248. const filenameMatch = contentDisposition?.match(/filename="(.+)"/);
  3249. const filename = filenameMatch?.[1] || `project_${projectId}.zip`;
  3250. const blob = await response.blob();
  3251. return { blob, filename };
  3252. },
  3253. // API Keys
  3254. getAPIKeys: () => request<APIKey[]>('/api-keys/'),
  3255. createAPIKey: (data: APIKeyCreate) =>
  3256. request<APIKeyCreateResponse>('/api-keys/', {
  3257. method: 'POST',
  3258. body: JSON.stringify(data),
  3259. }),
  3260. updateAPIKey: (id: number, data: APIKeyUpdate) =>
  3261. request<APIKey>(`/api-keys/${id}`, {
  3262. method: 'PATCH',
  3263. body: JSON.stringify(data),
  3264. }),
  3265. deleteAPIKey: (id: number) =>
  3266. request<{ message: string }>(`/api-keys/${id}`, { method: 'DELETE' }),
  3267. // AMS History
  3268. getAMSHistory: (printerId: number, amsId: number, hours = 24) =>
  3269. request<AMSHistoryResponse>(`/ams-history/${printerId}/${amsId}?hours=${hours}`),
  3270. // System Info
  3271. getSystemInfo: () => request<SystemInfo>('/system/info'),
  3272. // Library (File Manager)
  3273. getLibraryFolders: () => request<LibraryFolderTree[]>('/library/folders'),
  3274. createLibraryFolder: (data: LibraryFolderCreate) =>
  3275. request<LibraryFolder>('/library/folders', {
  3276. method: 'POST',
  3277. body: JSON.stringify(data),
  3278. }),
  3279. updateLibraryFolder: (id: number, data: LibraryFolderUpdate) =>
  3280. request<LibraryFolder>(`/library/folders/${id}`, {
  3281. method: 'PUT',
  3282. body: JSON.stringify(data),
  3283. }),
  3284. deleteLibraryFolder: (id: number) =>
  3285. request<{ status: string; message: string }>(`/library/folders/${id}`, { method: 'DELETE' }),
  3286. getLibraryFoldersByProject: (projectId: number) =>
  3287. request<LibraryFolder[]>(`/library/folders/by-project/${projectId}`),
  3288. getLibraryFoldersByArchive: (archiveId: number) =>
  3289. request<LibraryFolder[]>(`/library/folders/by-archive/${archiveId}`),
  3290. getLibraryFiles: (folderId?: number | null, includeRoot = true) => {
  3291. const params = new URLSearchParams();
  3292. if (folderId !== undefined && folderId !== null) {
  3293. params.set('folder_id', String(folderId));
  3294. }
  3295. params.set('include_root', String(includeRoot));
  3296. return request<LibraryFileListItem[]>(`/library/files?${params}`);
  3297. },
  3298. getLibraryFile: (id: number) => request<LibraryFile>(`/library/files/${id}`),
  3299. uploadLibraryFile: async (
  3300. file: File,
  3301. folderId?: number | null,
  3302. generateStlThumbnails: boolean = true
  3303. ): Promise<LibraryFileUploadResponse> => {
  3304. const formData = new FormData();
  3305. formData.append('file', file);
  3306. const params = new URLSearchParams();
  3307. if (folderId) params.set('folder_id', String(folderId));
  3308. params.set('generate_stl_thumbnails', String(generateStlThumbnails));
  3309. const headers: Record<string, string> = {};
  3310. if (authToken) {
  3311. headers['Authorization'] = `Bearer ${authToken}`;
  3312. }
  3313. const response = await fetch(`${API_BASE}/library/files?${params}`, {
  3314. method: 'POST',
  3315. headers,
  3316. body: formData,
  3317. });
  3318. if (!response.ok) {
  3319. const error = await response.json().catch(() => ({}));
  3320. throw new Error(error.detail || `HTTP ${response.status}`);
  3321. }
  3322. return response.json();
  3323. },
  3324. extractZipFile: async (
  3325. file: File,
  3326. folderId?: number | null,
  3327. preserveStructure: boolean = true,
  3328. createFolderFromZip: boolean = false,
  3329. generateStlThumbnails: boolean = true
  3330. ): Promise<ZipExtractResponse> => {
  3331. const formData = new FormData();
  3332. formData.append('file', file);
  3333. const params = new URLSearchParams();
  3334. if (folderId) params.set('folder_id', String(folderId));
  3335. params.set('preserve_structure', String(preserveStructure));
  3336. params.set('create_folder_from_zip', String(createFolderFromZip));
  3337. params.set('generate_stl_thumbnails', String(generateStlThumbnails));
  3338. const headers: Record<string, string> = {};
  3339. if (authToken) {
  3340. headers['Authorization'] = `Bearer ${authToken}`;
  3341. }
  3342. const response = await fetch(`${API_BASE}/library/files/extract-zip?${params}`, {
  3343. method: 'POST',
  3344. headers,
  3345. body: formData,
  3346. });
  3347. if (!response.ok) {
  3348. const error = await response.json().catch(() => ({}));
  3349. throw new Error(error.detail || `HTTP ${response.status}`);
  3350. }
  3351. return response.json();
  3352. },
  3353. updateLibraryFile: (id: number, data: LibraryFileUpdate) =>
  3354. request<LibraryFile>(`/library/files/${id}`, {
  3355. method: 'PUT',
  3356. body: JSON.stringify(data),
  3357. }),
  3358. deleteLibraryFile: (id: number) =>
  3359. request<{ status: string; message: string }>(`/library/files/${id}`, { method: 'DELETE' }),
  3360. getLibraryFileDownloadUrl: (id: number) => `${API_BASE}/library/files/${id}/download`,
  3361. downloadLibraryFile: async (id: number, filename?: string): Promise<void> => {
  3362. const headers: Record<string, string> = {};
  3363. if (authToken) {
  3364. headers['Authorization'] = `Bearer ${authToken}`;
  3365. }
  3366. const response = await fetch(`${API_BASE}/library/files/${id}/download`, { headers });
  3367. if (!response.ok) {
  3368. const error = await response.json().catch(() => ({}));
  3369. throw new Error(error.detail || `HTTP ${response.status}`);
  3370. }
  3371. const disposition = response.headers.get('Content-Disposition');
  3372. const filenameMatch = disposition?.match(/filename="?([^";\n]+)"?/);
  3373. const downloadFilename = filenameMatch?.[1] || filename || `file_${id}`;
  3374. const blob = await response.blob();
  3375. const url = window.URL.createObjectURL(blob);
  3376. const a = document.createElement('a');
  3377. a.href = url;
  3378. a.download = downloadFilename;
  3379. document.body.appendChild(a);
  3380. a.click();
  3381. document.body.removeChild(a);
  3382. window.URL.revokeObjectURL(url);
  3383. },
  3384. getLibraryFileThumbnailUrl: (id: number) => `${API_BASE}/library/files/${id}/thumbnail`,
  3385. getLibraryFilePlateThumbnail: (id: number, plateIndex: number) =>
  3386. `${API_BASE}/library/files/${id}/plate-thumbnail/${plateIndex}`,
  3387. getLibraryFileGcodeUrl: (id: number) => `${API_BASE}/library/files/${id}/gcode`,
  3388. moveLibraryFiles: (fileIds: number[], folderId: number | null) =>
  3389. request<{ status: string; moved: number }>('/library/files/move', {
  3390. method: 'POST',
  3391. body: JSON.stringify({ file_ids: fileIds, folder_id: folderId }),
  3392. }),
  3393. bulkDeleteLibrary: (fileIds: number[], folderIds: number[]) =>
  3394. request<{ deleted_files: number; deleted_folders: number }>('/library/bulk-delete', {
  3395. method: 'POST',
  3396. body: JSON.stringify({ file_ids: fileIds, folder_ids: folderIds }),
  3397. }),
  3398. getLibraryStats: () => request<LibraryStats>('/library/stats'),
  3399. batchGenerateStlThumbnails: (options: {
  3400. file_ids?: number[];
  3401. folder_id?: number;
  3402. all_missing?: boolean;
  3403. }) =>
  3404. request<BatchThumbnailResponse>('/library/generate-stl-thumbnails', {
  3405. method: 'POST',
  3406. body: JSON.stringify(options),
  3407. }),
  3408. addLibraryFilesToQueue: (fileIds: number[]) =>
  3409. request<AddToQueueResponse>('/library/files/add-to-queue', {
  3410. method: 'POST',
  3411. body: JSON.stringify({ file_ids: fileIds }),
  3412. }),
  3413. printLibraryFile: (
  3414. fileId: number,
  3415. printerId: number,
  3416. options?: {
  3417. plate_id?: number;
  3418. ams_mapping?: number[];
  3419. bed_levelling?: boolean;
  3420. flow_cali?: boolean;
  3421. vibration_cali?: boolean;
  3422. layer_inspect?: boolean;
  3423. timelapse?: boolean;
  3424. use_ams?: boolean;
  3425. }
  3426. ) =>
  3427. request<{ status: string; printer_id: number; archive_id: number; filename: string }>(
  3428. `/library/files/${fileId}/print?printer_id=${printerId}`,
  3429. {
  3430. method: 'POST',
  3431. body: options ? JSON.stringify(options) : undefined,
  3432. }
  3433. ),
  3434. getLibraryFilePlates: (fileId: number) =>
  3435. request<LibraryFilePlatesResponse>(`/library/files/${fileId}/plates`),
  3436. getLibraryFileFilamentRequirements: (fileId: number, plateId?: number) =>
  3437. request<{
  3438. file_id: number;
  3439. filename: string;
  3440. filaments: Array<{
  3441. slot_id: number;
  3442. type: string;
  3443. color: string;
  3444. used_grams: number;
  3445. used_meters: number;
  3446. }>;
  3447. }>(`/library/files/${fileId}/filament-requirements${plateId !== undefined ? `?plate_id=${plateId}` : ''}`),
  3448. // GitHub Backup
  3449. getGitHubBackupConfig: () =>
  3450. request<GitHubBackupConfig | null>('/github-backup/config'),
  3451. saveGitHubBackupConfig: (config: GitHubBackupConfigCreate) =>
  3452. request<GitHubBackupConfig>('/github-backup/config', {
  3453. method: 'POST',
  3454. body: JSON.stringify(config),
  3455. }),
  3456. updateGitHubBackupConfig: (config: Partial<GitHubBackupConfigCreate>) =>
  3457. request<GitHubBackupConfig>('/github-backup/config', {
  3458. method: 'PATCH',
  3459. body: JSON.stringify(config),
  3460. }),
  3461. deleteGitHubBackupConfig: () =>
  3462. request<{ message: string }>('/github-backup/config', { method: 'DELETE' }),
  3463. testGitHubConnection: (repoUrl: string, token: string) =>
  3464. request<GitHubTestConnectionResponse>(
  3465. `/github-backup/test?repo_url=${encodeURIComponent(repoUrl)}&token=${encodeURIComponent(token)}`,
  3466. { method: 'POST' }
  3467. ),
  3468. testGitHubStoredConnection: () =>
  3469. request<GitHubTestConnectionResponse>('/github-backup/test-stored', { method: 'POST' }),
  3470. triggerGitHubBackup: () =>
  3471. request<GitHubBackupTriggerResponse>('/github-backup/run', { method: 'POST' }),
  3472. getGitHubBackupStatus: () =>
  3473. request<GitHubBackupStatus>('/github-backup/status'),
  3474. getGitHubBackupLogs: (limit: number = 50) =>
  3475. request<GitHubBackupLog[]>(`/github-backup/logs?limit=${limit}`),
  3476. clearGitHubBackupLogs: (keepLast: number = 10) =>
  3477. request<{ deleted: number; message: string }>(`/github-backup/logs?keep_last=${keepLast}`, { method: 'DELETE' }),
  3478. // Local Presets (OrcaSlicer imports)
  3479. getLocalPresets: () =>
  3480. request<LocalPresetsResponse>('/local-presets/'),
  3481. getLocalPresetDetail: (id: number) =>
  3482. request<LocalPresetDetail>(`/local-presets/${id}`),
  3483. importLocalPresets: (formData: FormData) =>
  3484. fetch(`${API_BASE}/local-presets/import`, {
  3485. method: 'POST',
  3486. headers: authToken ? { 'Authorization': `Bearer ${authToken}` } : {},
  3487. body: formData,
  3488. }).then(async (res) => {
  3489. if (!res.ok) {
  3490. const err = await res.json().catch(() => ({}));
  3491. throw new Error(err.detail || `HTTP ${res.status}`);
  3492. }
  3493. return res.json() as Promise<ImportResponse>;
  3494. }),
  3495. createLocalPreset: (data: { name: string; preset_type: string; setting: Record<string, unknown> }) =>
  3496. request<LocalPreset>('/local-presets/', {
  3497. method: 'POST',
  3498. body: JSON.stringify(data),
  3499. }),
  3500. updateLocalPreset: (id: number, data: { name?: string; setting?: Record<string, unknown> }) =>
  3501. request<LocalPreset>(`/local-presets/${id}`, {
  3502. method: 'PUT',
  3503. body: JSON.stringify(data),
  3504. }),
  3505. deleteLocalPreset: (id: number) =>
  3506. request<{ success: boolean }>(`/local-presets/${id}`, { method: 'DELETE' }),
  3507. refreshBaseProfileCache: () =>
  3508. request<{ refreshed: number; failed: number; total: number }>('/local-presets/base-cache/refresh', { method: 'POST' }),
  3509. };
  3510. // AMS History types
  3511. export interface AMSHistoryPoint {
  3512. recorded_at: string;
  3513. humidity: number | null;
  3514. humidity_raw: number | null;
  3515. temperature: number | null;
  3516. }
  3517. export interface AMSHistoryResponse {
  3518. printer_id: number;
  3519. ams_id: number;
  3520. data: AMSHistoryPoint[];
  3521. min_humidity: number | null;
  3522. max_humidity: number | null;
  3523. avg_humidity: number | null;
  3524. min_temperature: number | null;
  3525. max_temperature: number | null;
  3526. avg_temperature: number | null;
  3527. }
  3528. // System Info types
  3529. export interface SystemInfo {
  3530. app: {
  3531. version: string;
  3532. base_dir: string;
  3533. archive_dir: string;
  3534. };
  3535. database: {
  3536. archives: number;
  3537. archives_completed: number;
  3538. archives_failed: number;
  3539. archives_printing: number;
  3540. printers: number;
  3541. filaments: number;
  3542. projects: number;
  3543. smart_plugs: number;
  3544. total_print_time_seconds: number;
  3545. total_print_time_formatted: string;
  3546. total_filament_grams: number;
  3547. total_filament_kg: number;
  3548. };
  3549. printers: {
  3550. total: number;
  3551. connected: number;
  3552. connected_list: Array<{
  3553. id: number;
  3554. name: string;
  3555. state: string;
  3556. model: string;
  3557. }>;
  3558. };
  3559. storage: {
  3560. archive_size_bytes: number;
  3561. archive_size_formatted: string;
  3562. database_size_bytes: number;
  3563. database_size_formatted: string;
  3564. disk_total_bytes: number;
  3565. disk_total_formatted: string;
  3566. disk_used_bytes: number;
  3567. disk_used_formatted: string;
  3568. disk_free_bytes: number;
  3569. disk_free_formatted: string;
  3570. disk_percent_used: number;
  3571. };
  3572. system: {
  3573. platform: string;
  3574. platform_release: string;
  3575. platform_version: string;
  3576. architecture: string;
  3577. hostname: string;
  3578. python_version: string;
  3579. uptime_seconds: number;
  3580. uptime_formatted: string;
  3581. boot_time: string;
  3582. };
  3583. memory: {
  3584. total_bytes: number;
  3585. total_formatted: string;
  3586. available_bytes: number;
  3587. available_formatted: string;
  3588. used_bytes: number;
  3589. used_formatted: string;
  3590. percent_used: number;
  3591. };
  3592. cpu: {
  3593. count: number;
  3594. count_logical: number;
  3595. percent: number;
  3596. };
  3597. }
  3598. // Library (File Manager) types
  3599. export interface LibraryFolderTree {
  3600. id: number;
  3601. name: string;
  3602. parent_id: number | null;
  3603. project_id: number | null;
  3604. archive_id: number | null;
  3605. project_name: string | null;
  3606. archive_name: string | null;
  3607. file_count: number;
  3608. children: LibraryFolderTree[];
  3609. }
  3610. export interface LibraryFolder {
  3611. id: number;
  3612. name: string;
  3613. parent_id: number | null;
  3614. project_id: number | null;
  3615. archive_id: number | null;
  3616. project_name: string | null;
  3617. archive_name: string | null;
  3618. file_count: number;
  3619. created_at: string;
  3620. updated_at: string;
  3621. }
  3622. export interface LibraryFolderCreate {
  3623. name: string;
  3624. parent_id?: number | null;
  3625. project_id?: number | null;
  3626. archive_id?: number | null;
  3627. }
  3628. export interface LibraryFolderUpdate {
  3629. name?: string;
  3630. parent_id?: number | null;
  3631. project_id?: number | null; // 0 to unlink
  3632. archive_id?: number | null; // 0 to unlink
  3633. }
  3634. export interface LibraryFileDuplicate {
  3635. id: number;
  3636. filename: string;
  3637. folder_id: number | null;
  3638. folder_name: string | null;
  3639. created_at: string;
  3640. }
  3641. export interface LibraryFile {
  3642. id: number;
  3643. folder_id: number | null;
  3644. folder_name: string | null;
  3645. project_id: number | null;
  3646. project_name: string | null;
  3647. filename: string;
  3648. file_path: string;
  3649. file_type: string;
  3650. file_size: number;
  3651. file_hash: string | null;
  3652. thumbnail_path: string | null;
  3653. metadata: Record<string, unknown> | null;
  3654. print_count: number;
  3655. last_printed_at: string | null;
  3656. notes: string | null;
  3657. duplicates: LibraryFileDuplicate[] | null;
  3658. duplicate_count: number;
  3659. // User tracking (Issue #206)
  3660. created_by_id: number | null;
  3661. created_by_username: string | null;
  3662. created_at: string;
  3663. updated_at: string;
  3664. // Metadata fields
  3665. print_name: string | null;
  3666. print_time_seconds: number | null;
  3667. filament_used_grams: number | null;
  3668. sliced_for_model: string | null;
  3669. }
  3670. export interface LibraryFileListItem {
  3671. id: number;
  3672. folder_id: number | null;
  3673. filename: string;
  3674. file_type: string;
  3675. file_size: number;
  3676. thumbnail_path: string | null;
  3677. print_count: number;
  3678. duplicate_count: number;
  3679. // User tracking (Issue #206)
  3680. created_by_id: number | null;
  3681. created_by_username: string | null;
  3682. created_at: string;
  3683. print_name: string | null;
  3684. print_time_seconds: number | null;
  3685. filament_used_grams: number | null;
  3686. sliced_for_model: string | null;
  3687. }
  3688. export interface LibraryFileUpdate {
  3689. filename?: string;
  3690. folder_id?: number | null;
  3691. project_id?: number | null;
  3692. notes?: string | null;
  3693. }
  3694. export interface LibraryFileUploadResponse {
  3695. id: number;
  3696. filename: string;
  3697. file_type: string;
  3698. file_size: number;
  3699. thumbnail_path: string | null;
  3700. duplicate_of: number | null;
  3701. metadata: Record<string, unknown> | null;
  3702. }
  3703. export interface LibraryStats {
  3704. total_files: number;
  3705. total_folders: number;
  3706. total_size_bytes: number;
  3707. files_by_type: Record<string, number>;
  3708. total_prints: number;
  3709. disk_free_bytes: number;
  3710. disk_total_bytes: number;
  3711. disk_used_bytes: number;
  3712. }
  3713. export interface ZipExtractResult {
  3714. filename: string;
  3715. file_id: number;
  3716. folder_id: number | null;
  3717. }
  3718. export interface ZipExtractError {
  3719. filename: string;
  3720. error: string;
  3721. }
  3722. export interface ZipExtractResponse {
  3723. extracted: number;
  3724. folders_created: number;
  3725. files: ZipExtractResult[];
  3726. errors: ZipExtractError[];
  3727. }
  3728. // STL Thumbnail Generation types
  3729. export interface BatchThumbnailResult {
  3730. file_id: number;
  3731. filename: string;
  3732. success: boolean;
  3733. error?: string | null;
  3734. }
  3735. export interface BatchThumbnailResponse {
  3736. processed: number;
  3737. succeeded: number;
  3738. failed: number;
  3739. results: BatchThumbnailResult[];
  3740. }
  3741. // Library Queue types
  3742. export interface AddToQueueResult {
  3743. file_id: number;
  3744. filename: string;
  3745. queue_item_id: number;
  3746. archive_id: number;
  3747. }
  3748. export interface AddToQueueError {
  3749. file_id: number;
  3750. filename: string;
  3751. error: string;
  3752. }
  3753. export interface AddToQueueResponse {
  3754. added: AddToQueueResult[];
  3755. errors: AddToQueueError[];
  3756. }
  3757. // Discovery types
  3758. export interface DiscoveredPrinter {
  3759. serial: string;
  3760. name: string;
  3761. ip_address: string;
  3762. model: string | null;
  3763. discovered_at: string | null;
  3764. }
  3765. export interface DiscoveryStatus {
  3766. running: boolean;
  3767. }
  3768. export interface DiscoveryInfo {
  3769. is_docker: boolean;
  3770. ssdp_running: boolean;
  3771. scan_running: boolean;
  3772. subnets: string[];
  3773. }
  3774. export interface SubnetScanStatus {
  3775. running: boolean;
  3776. scanned: number;
  3777. total: number;
  3778. }
  3779. // Discovery API
  3780. export const discoveryApi = {
  3781. getInfo: () => request<DiscoveryInfo>('/discovery/info'),
  3782. getStatus: () => request<DiscoveryStatus>('/discovery/status'),
  3783. startDiscovery: (duration: number = 10) =>
  3784. request<DiscoveryStatus>(`/discovery/start?duration=${duration}`, { method: 'POST' }),
  3785. stopDiscovery: () =>
  3786. request<DiscoveryStatus>('/discovery/stop', { method: 'POST' }),
  3787. getDiscoveredPrinters: () =>
  3788. request<DiscoveredPrinter[]>('/discovery/printers'),
  3789. // Subnet scanning (for Docker environments)
  3790. startSubnetScan: (subnet: string, timeout: number = 1.0) =>
  3791. request<SubnetScanStatus>('/discovery/scan', {
  3792. method: 'POST',
  3793. body: JSON.stringify({ subnet, timeout }),
  3794. }),
  3795. getScanStatus: () => request<SubnetScanStatus>('/discovery/scan/status'),
  3796. stopSubnetScan: () =>
  3797. request<SubnetScanStatus>('/discovery/scan/stop', { method: 'POST' }),
  3798. };
  3799. // Virtual Printer types
  3800. export type VirtualPrinterMode = 'immediate' | 'queue' | 'review' | 'print_queue' | 'proxy'; // 'queue' is legacy, normalized to 'review'
  3801. export interface VirtualPrinterProxyStatus {
  3802. running: boolean;
  3803. target_host: string;
  3804. ftp_port: number;
  3805. mqtt_port: number;
  3806. ftp_connections: number;
  3807. mqtt_connections: number;
  3808. }
  3809. export interface VirtualPrinterStatus {
  3810. enabled: boolean;
  3811. running: boolean;
  3812. mode: VirtualPrinterMode;
  3813. name: string;
  3814. serial: string;
  3815. model: string;
  3816. model_name: string;
  3817. pending_files: number;
  3818. target_printer_ip?: string; // For proxy mode
  3819. proxy?: VirtualPrinterProxyStatus; // For proxy mode
  3820. }
  3821. export interface VirtualPrinterSettings {
  3822. enabled: boolean;
  3823. access_code_set: boolean;
  3824. mode: VirtualPrinterMode;
  3825. model: string;
  3826. target_printer_id: number | null; // For proxy mode
  3827. remote_interface_ip: string | null; // For SSDP proxy across networks
  3828. status: VirtualPrinterStatus;
  3829. }
  3830. export interface NetworkInterface {
  3831. name: string;
  3832. ip: string;
  3833. netmask: string;
  3834. subnet: string;
  3835. }
  3836. export interface VirtualPrinterModels {
  3837. models: Record<string, string>; // SSDP code -> display name
  3838. default: string;
  3839. }
  3840. export interface PendingUpload {
  3841. id: number;
  3842. filename: string;
  3843. file_size: number;
  3844. source_ip: string | null;
  3845. status: string;
  3846. tags: string | null;
  3847. notes: string | null;
  3848. project_id: number | null;
  3849. uploaded_at: string;
  3850. }
  3851. // Virtual Printer API
  3852. export const virtualPrinterApi = {
  3853. getSettings: () => request<VirtualPrinterSettings>('/settings/virtual-printer'),
  3854. getModels: () => request<VirtualPrinterModels>('/settings/virtual-printer/models'),
  3855. updateSettings: (data: {
  3856. enabled?: boolean;
  3857. access_code?: string;
  3858. mode?: 'immediate' | 'review' | 'print_queue' | 'proxy';
  3859. model?: string;
  3860. target_printer_id?: number;
  3861. remote_interface_ip?: string;
  3862. }) => {
  3863. const params = new URLSearchParams();
  3864. if (data.enabled !== undefined) params.set('enabled', String(data.enabled));
  3865. if (data.access_code !== undefined) params.set('access_code', data.access_code);
  3866. if (data.mode !== undefined) params.set('mode', data.mode);
  3867. if (data.model !== undefined) params.set('model', data.model);
  3868. if (data.target_printer_id !== undefined) params.set('target_printer_id', String(data.target_printer_id));
  3869. if (data.remote_interface_ip !== undefined) params.set('remote_interface_ip', data.remote_interface_ip);
  3870. return request<VirtualPrinterSettings>(`/settings/virtual-printer?${params.toString()}`, {
  3871. method: 'PUT',
  3872. });
  3873. },
  3874. };
  3875. // Pending Uploads API
  3876. export const pendingUploadsApi = {
  3877. list: () => request<PendingUpload[]>('/pending-uploads/'),
  3878. getCount: () => request<{ count: number }>('/pending-uploads/count'),
  3879. get: (id: number) => request<PendingUpload>(`/pending-uploads/${id}`),
  3880. archive: (id: number, data?: { tags?: string; notes?: string; project_id?: number }) =>
  3881. request<{ id: number; print_name: string; filename: string }>(`/pending-uploads/${id}/archive`, {
  3882. method: 'POST',
  3883. body: JSON.stringify(data || {}),
  3884. }),
  3885. discard: (id: number) =>
  3886. request<{ success: boolean }>(`/pending-uploads/${id}`, { method: 'DELETE' }),
  3887. archiveAll: () =>
  3888. request<{ archived: number; failed: number }>('/pending-uploads/archive-all', { method: 'POST' }),
  3889. discardAll: () =>
  3890. request<{ discarded: number }>('/pending-uploads/discard-all', { method: 'DELETE' }),
  3891. };
  3892. // Firmware API Types
  3893. export interface FirmwareUpdateInfo {
  3894. printer_id: number;
  3895. printer_name: string;
  3896. model: string | null;
  3897. current_version: string | null;
  3898. latest_version: string | null;
  3899. update_available: boolean;
  3900. download_url: string | null;
  3901. release_notes: string | null;
  3902. }
  3903. export interface FirmwareUploadPrepare {
  3904. can_proceed: boolean;
  3905. sd_card_present: boolean;
  3906. sd_card_free_space: number;
  3907. firmware_size: number;
  3908. space_sufficient: boolean;
  3909. update_available: boolean;
  3910. current_version: string | null;
  3911. latest_version: string | null;
  3912. firmware_filename: string | null;
  3913. errors: string[];
  3914. }
  3915. export interface FirmwareUploadStatus {
  3916. status: 'idle' | 'preparing' | 'downloading' | 'uploading' | 'complete' | 'error';
  3917. progress: number;
  3918. message: string;
  3919. error: string | null;
  3920. firmware_filename: string | null;
  3921. firmware_version: string | null;
  3922. }
  3923. // Firmware API
  3924. export const firmwareApi = {
  3925. checkUpdates: () =>
  3926. request<{ updates: FirmwareUpdateInfo[]; updates_available: number }>('/firmware/updates'),
  3927. checkPrinterUpdate: (printerId: number) =>
  3928. request<FirmwareUpdateInfo>(`/firmware/updates/${printerId}`),
  3929. prepareUpload: (printerId: number) =>
  3930. request<FirmwareUploadPrepare>(`/firmware/updates/${printerId}/prepare`),
  3931. startUpload: (printerId: number) =>
  3932. request<{ started: boolean; message: string }>(`/firmware/updates/${printerId}/upload`, {
  3933. method: 'POST',
  3934. }),
  3935. getUploadStatus: (printerId: number) =>
  3936. request<FirmwareUploadStatus>(`/firmware/updates/${printerId}/upload/status`),
  3937. };
  3938. // Support types
  3939. export interface DebugLoggingState {
  3940. enabled: boolean;
  3941. enabled_at: string | null;
  3942. duration_seconds: number | null;
  3943. }
  3944. export interface LogEntry {
  3945. timestamp: string;
  3946. level: string;
  3947. logger_name: string;
  3948. message: string;
  3949. }
  3950. export interface LogsResponse {
  3951. entries: LogEntry[];
  3952. total_in_file: number;
  3953. filtered_count: number;
  3954. }
  3955. // Support API
  3956. export const supportApi = {
  3957. getDebugLoggingState: () =>
  3958. request<DebugLoggingState>('/support/debug-logging'),
  3959. setDebugLogging: (enabled: boolean) =>
  3960. request<DebugLoggingState>('/support/debug-logging', {
  3961. method: 'POST',
  3962. body: JSON.stringify({ enabled }),
  3963. }),
  3964. downloadSupportBundle: async () => {
  3965. const headers: Record<string, string> = {};
  3966. if (authToken) {
  3967. headers['Authorization'] = `Bearer ${authToken}`;
  3968. }
  3969. const response = await fetch(`${API_BASE}/support/bundle`, { headers });
  3970. if (!response.ok) {
  3971. const error = await response.json().catch(() => ({}));
  3972. throw new Error(error.detail || `HTTP ${response.status}`);
  3973. }
  3974. // Get filename from Content-Disposition header or use default
  3975. const disposition = response.headers.get('Content-Disposition');
  3976. const filenameMatch = disposition?.match(/filename=(.+)/);
  3977. const filename = filenameMatch ? filenameMatch[1] : 'bambuddy-support.zip';
  3978. // Download the blob
  3979. const blob = await response.blob();
  3980. const url = window.URL.createObjectURL(blob);
  3981. const a = document.createElement('a');
  3982. a.href = url;
  3983. a.download = filename;
  3984. document.body.appendChild(a);
  3985. a.click();
  3986. document.body.removeChild(a);
  3987. window.URL.revokeObjectURL(url);
  3988. },
  3989. getLogs: (params?: { limit?: number; level?: string; search?: string }) => {
  3990. const searchParams = new URLSearchParams();
  3991. if (params?.limit) searchParams.set('limit', params.limit.toString());
  3992. if (params?.level) searchParams.set('level', params.level);
  3993. if (params?.search) searchParams.set('search', params.search);
  3994. const query = searchParams.toString();
  3995. return request<LogsResponse>(`/support/logs${query ? `?${query}` : ''}`);
  3996. },
  3997. clearLogs: () =>
  3998. request<{ message: string }>('/support/logs', { method: 'DELETE' }),
  3999. };