useMultiPrinterFilamentMapping.ts 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. import { useMemo } from 'react';
  2. import { useQueries } from '@tanstack/react-query';
  3. import { api } from '../api/client';
  4. import type { PrinterStatus, Printer } from '../api/client';
  5. import {
  6. buildLoadedFilaments,
  7. computeAmsMapping,
  8. type LoadedFilament,
  9. type FilamentRequirement,
  10. } from './useFilamentMapping';
  11. import {
  12. normalizeColorForCompare,
  13. colorsAreSimilar,
  14. } from '../utils/amsHelpers';
  15. /**
  16. * Match status for a single printer's filament configuration.
  17. */
  18. export type PrinterMatchStatus = 'full' | 'partial' | 'missing';
  19. /**
  20. * Per-printer configuration for AMS mapping.
  21. */
  22. export interface PerPrinterConfig {
  23. /** Whether this printer uses the default mapping or has custom config */
  24. useDefault: boolean;
  25. /** Manual slot overrides for this printer (slot_id -> globalTrayId) */
  26. manualMappings: Record<number, number>;
  27. /** Whether this mapping was auto-configured */
  28. autoConfigured: boolean;
  29. }
  30. /**
  31. * Result of filament mapping for a single printer.
  32. */
  33. export interface PrinterMappingResult {
  34. printerId: number;
  35. printerName: string;
  36. /** Printer status data */
  37. status: PrinterStatus | undefined;
  38. /** Whether status is still loading */
  39. isLoading: boolean;
  40. /** List of loaded filaments in this printer */
  41. loadedFilaments: LoadedFilament[];
  42. /** Auto-computed AMS mapping for this printer */
  43. autoMapping: number[] | undefined;
  44. /** Final AMS mapping (considering manual overrides) */
  45. finalMapping: number[] | undefined;
  46. /** Match status: full (all exact), partial (some mismatches), missing (type not found) */
  47. matchStatus: PrinterMatchStatus;
  48. /** Number of slots with exact match (type + color) */
  49. exactMatches: number;
  50. /** Number of slots with type-only match */
  51. typeOnlyMatches: number;
  52. /** Number of slots with missing type */
  53. missingTypes: number;
  54. /** Total required slots */
  55. totalSlots: number;
  56. /** Per-printer config */
  57. config: PerPrinterConfig;
  58. }
  59. /**
  60. * Result of the useMultiPrinterFilamentMapping hook.
  61. */
  62. export interface UseMultiPrinterFilamentMappingResult {
  63. /** Results for each selected printer */
  64. printerResults: PrinterMappingResult[];
  65. /** Whether any printer data is still loading */
  66. isLoading: boolean;
  67. /** Per-printer configurations */
  68. perPrinterConfigs: Record<number, PerPrinterConfig>;
  69. /** Update config for a specific printer */
  70. updatePrinterConfig: (printerId: number, config: Partial<PerPrinterConfig>) => void;
  71. /** Auto-configure all printers based on their loaded filaments */
  72. autoConfigureAll: () => void;
  73. /** Auto-configure a specific printer */
  74. autoConfigurePrinter: (printerId: number) => void;
  75. /** Get final mapping for a specific printer (for submission) */
  76. getFinalMapping: (printerId: number) => number[] | undefined;
  77. /** Check if all printers have acceptable mappings */
  78. allPrintersReady: boolean;
  79. }
  80. /**
  81. * Compute match details for a printer given filament requirements and loaded filaments.
  82. */
  83. function computeMatchDetails(
  84. filamentReqs: FilamentRequirement[] | undefined,
  85. loadedFilaments: LoadedFilament[],
  86. manualMappings: Record<number, number>
  87. ): { exactMatches: number; typeOnlyMatches: number; missingTypes: number; totalSlots: number; status: PrinterMatchStatus } {
  88. if (!filamentReqs || filamentReqs.length === 0) {
  89. return { exactMatches: 0, typeOnlyMatches: 0, missingTypes: 0, totalSlots: 0, status: 'full' };
  90. }
  91. let exactMatches = 0;
  92. let typeOnlyMatches = 0;
  93. let missingTypes = 0;
  94. const usedTrayIds = new Set<number>(Object.values(manualMappings));
  95. for (const req of filamentReqs) {
  96. const slotId = req.slot_id || 0;
  97. // Check manual override first
  98. if (slotId > 0 && manualMappings[slotId] !== undefined) {
  99. const manualTrayId = manualMappings[slotId];
  100. const manualLoaded = loadedFilaments.find((f) => f.globalTrayId === manualTrayId);
  101. if (manualLoaded) {
  102. const typeMatch = manualLoaded.type?.toUpperCase() === req.type?.toUpperCase();
  103. const colorMatch =
  104. normalizeColorForCompare(manualLoaded.color) === normalizeColorForCompare(req.color) ||
  105. colorsAreSimilar(manualLoaded.color, req.color);
  106. if (typeMatch && colorMatch) {
  107. exactMatches++;
  108. } else if (typeMatch) {
  109. typeOnlyMatches++;
  110. } else {
  111. missingTypes++;
  112. }
  113. continue;
  114. }
  115. }
  116. // Auto-match
  117. const exactMatch = loadedFilaments.find(
  118. (f) =>
  119. !usedTrayIds.has(f.globalTrayId) &&
  120. f.type?.toUpperCase() === req.type?.toUpperCase() &&
  121. normalizeColorForCompare(f.color) === normalizeColorForCompare(req.color)
  122. );
  123. const similarMatch = exactMatch
  124. ? undefined
  125. : loadedFilaments.find(
  126. (f) =>
  127. !usedTrayIds.has(f.globalTrayId) &&
  128. f.type?.toUpperCase() === req.type?.toUpperCase() &&
  129. colorsAreSimilar(f.color, req.color)
  130. );
  131. const typeOnlyMatch =
  132. exactMatch || similarMatch
  133. ? undefined
  134. : loadedFilaments.find(
  135. (f) => !usedTrayIds.has(f.globalTrayId) && f.type?.toUpperCase() === req.type?.toUpperCase()
  136. );
  137. const loaded = exactMatch ?? similarMatch ?? typeOnlyMatch;
  138. if (loaded) {
  139. usedTrayIds.add(loaded.globalTrayId);
  140. }
  141. if (exactMatch || similarMatch) {
  142. exactMatches++;
  143. } else if (typeOnlyMatch) {
  144. typeOnlyMatches++;
  145. } else {
  146. missingTypes++;
  147. }
  148. }
  149. const totalSlots = filamentReqs.length;
  150. let status: PrinterMatchStatus = 'full';
  151. if (missingTypes > 0) {
  152. status = 'missing';
  153. } else if (typeOnlyMatches > 0) {
  154. status = 'partial';
  155. }
  156. return { exactMatches, typeOnlyMatches, missingTypes, totalSlots, status };
  157. }
  158. /**
  159. * Compute AMS mapping with manual overrides applied.
  160. */
  161. function computeMappingWithOverrides(
  162. filamentReqs: { filaments: FilamentRequirement[] } | undefined,
  163. printerStatus: PrinterStatus | undefined,
  164. manualMappings: Record<number, number>
  165. ): number[] | undefined {
  166. if (!filamentReqs?.filaments || filamentReqs.filaments.length === 0) return undefined;
  167. const loadedFilaments = buildLoadedFilaments(printerStatus);
  168. if (loadedFilaments.length === 0) return undefined;
  169. const usedTrayIds = new Set<number>(Object.values(manualMappings));
  170. const comparisons: { slot_id: number; globalTrayId: number }[] = [];
  171. for (const req of filamentReqs.filaments) {
  172. const slotId = req.slot_id || 0;
  173. // Check manual override first
  174. if (slotId > 0 && manualMappings[slotId] !== undefined) {
  175. comparisons.push({ slot_id: slotId, globalTrayId: manualMappings[slotId] });
  176. continue;
  177. }
  178. // Auto-match
  179. const exactMatch = loadedFilaments.find(
  180. (f) =>
  181. !usedTrayIds.has(f.globalTrayId) &&
  182. f.type?.toUpperCase() === req.type?.toUpperCase() &&
  183. normalizeColorForCompare(f.color) === normalizeColorForCompare(req.color)
  184. );
  185. const similarMatch = exactMatch
  186. ? undefined
  187. : loadedFilaments.find(
  188. (f) =>
  189. !usedTrayIds.has(f.globalTrayId) &&
  190. f.type?.toUpperCase() === req.type?.toUpperCase() &&
  191. colorsAreSimilar(f.color, req.color)
  192. );
  193. const typeOnlyMatch =
  194. exactMatch || similarMatch
  195. ? undefined
  196. : loadedFilaments.find(
  197. (f) => !usedTrayIds.has(f.globalTrayId) && f.type?.toUpperCase() === req.type?.toUpperCase()
  198. );
  199. const loaded = exactMatch ?? similarMatch ?? typeOnlyMatch;
  200. if (loaded) {
  201. usedTrayIds.add(loaded.globalTrayId);
  202. }
  203. comparisons.push({ slot_id: slotId, globalTrayId: loaded?.globalTrayId ?? -1 });
  204. }
  205. const maxSlotId = Math.max(...comparisons.map((f) => f.slot_id || 0));
  206. if (maxSlotId <= 0) return undefined;
  207. const mapping = new Array(maxSlotId).fill(-1);
  208. comparisons.forEach((f) => {
  209. if (f.slot_id && f.slot_id > 0) {
  210. mapping[f.slot_id - 1] = f.globalTrayId;
  211. }
  212. });
  213. return mapping;
  214. }
  215. /**
  216. * Default per-printer config (use default mapping).
  217. */
  218. const DEFAULT_PRINTER_CONFIG: PerPrinterConfig = {
  219. useDefault: true,
  220. manualMappings: {},
  221. autoConfigured: false,
  222. };
  223. /**
  224. * Hook to manage filament mapping for multiple printers.
  225. * Fetches printer status for all selected printers and computes per-printer mappings.
  226. */
  227. export function useMultiPrinterFilamentMapping(
  228. selectedPrinterIds: number[],
  229. printers: Printer[] | undefined,
  230. filamentReqs: { filaments: FilamentRequirement[] } | undefined,
  231. defaultMappings: Record<number, number>,
  232. perPrinterConfigs: Record<number, PerPrinterConfig>,
  233. setPerPrinterConfigs: React.Dispatch<React.SetStateAction<Record<number, PerPrinterConfig>>>
  234. ): UseMultiPrinterFilamentMappingResult {
  235. // Fetch printer status for all selected printers in parallel
  236. const statusQueries = useQueries({
  237. queries: selectedPrinterIds.map((printerId) => ({
  238. queryKey: ['printer-status', printerId],
  239. queryFn: () => api.getPrinterStatus(printerId),
  240. enabled: selectedPrinterIds.length > 0,
  241. staleTime: 5000, // Consider data fresh for 5 seconds
  242. })),
  243. });
  244. // Build results for each printer
  245. const printerResults = useMemo((): PrinterMappingResult[] => {
  246. return selectedPrinterIds.map((printerId, index) => {
  247. const query = statusQueries[index];
  248. const printerStatus = query?.data;
  249. const printer = printers?.find((p) => p.id === printerId);
  250. const printerName = printer?.name || `Printer ${printerId}`;
  251. const loadedFilaments = buildLoadedFilaments(printerStatus);
  252. const config = perPrinterConfigs[printerId] || DEFAULT_PRINTER_CONFIG;
  253. // Compute auto mapping for this printer
  254. const autoMapping = computeAmsMapping(filamentReqs, printerStatus);
  255. // Determine which mappings to use:
  256. // If printer has override (useDefault=false), use its custom mappings
  257. // Otherwise use the default mappings
  258. const effectiveMappings = !config.useDefault
  259. ? config.manualMappings
  260. : defaultMappings;
  261. // Compute final mapping with overrides
  262. const finalMapping = computeMappingWithOverrides(filamentReqs, printerStatus, effectiveMappings);
  263. // Compute match details
  264. const matchDetails = computeMatchDetails(
  265. filamentReqs?.filaments,
  266. loadedFilaments,
  267. effectiveMappings
  268. );
  269. return {
  270. printerId,
  271. printerName,
  272. status: printerStatus,
  273. isLoading: query?.isLoading ?? false,
  274. loadedFilaments,
  275. autoMapping,
  276. finalMapping,
  277. matchStatus: matchDetails.status,
  278. exactMatches: matchDetails.exactMatches,
  279. typeOnlyMatches: matchDetails.typeOnlyMatches,
  280. missingTypes: matchDetails.missingTypes,
  281. totalSlots: matchDetails.totalSlots,
  282. config,
  283. };
  284. });
  285. }, [selectedPrinterIds, statusQueries, printers, filamentReqs, perPrinterConfigs, defaultMappings]);
  286. const isLoading = statusQueries.some((q) => q.isLoading);
  287. // Update config for a specific printer
  288. const updatePrinterConfig = (printerId: number, updates: Partial<PerPrinterConfig>) => {
  289. setPerPrinterConfigs((prev) => ({
  290. ...prev,
  291. [printerId]: {
  292. ...(prev[printerId] || DEFAULT_PRINTER_CONFIG),
  293. ...updates,
  294. },
  295. }));
  296. };
  297. // Auto-configure a specific printer based on its loaded filaments
  298. const autoConfigurePrinter = (printerId: number) => {
  299. const result = printerResults.find((r) => r.printerId === printerId);
  300. if (!result || !result.status || !filamentReqs?.filaments) return;
  301. // Compute optimal mapping for this printer
  302. const autoMapping = computeAmsMapping(filamentReqs, result.status);
  303. if (!autoMapping) return;
  304. // Convert autoMapping array to manualMappings record
  305. const manualMappings: Record<number, number> = {};
  306. autoMapping.forEach((globalTrayId, index) => {
  307. if (globalTrayId !== -1) {
  308. manualMappings[index + 1] = globalTrayId;
  309. }
  310. });
  311. updatePrinterConfig(printerId, {
  312. useDefault: false,
  313. manualMappings,
  314. autoConfigured: true,
  315. });
  316. };
  317. // Auto-configure all printers
  318. const autoConfigureAll = () => {
  319. for (const printerId of selectedPrinterIds) {
  320. autoConfigurePrinter(printerId);
  321. }
  322. };
  323. // Get final mapping for a specific printer (for submission)
  324. const getFinalMapping = (printerId: number): number[] | undefined => {
  325. const result = printerResults.find((r) => r.printerId === printerId);
  326. return result?.finalMapping;
  327. };
  328. // Check if all printers have acceptable mappings (no missing types)
  329. const allPrintersReady = printerResults.every((r) => r.matchStatus !== 'missing');
  330. return {
  331. printerResults,
  332. isLoading,
  333. perPrinterConfigs,
  334. updatePrinterConfig,
  335. autoConfigureAll,
  336. autoConfigurePrinter,
  337. getFinalMapping,
  338. allPrintersReady,
  339. };
  340. }