useFilamentMapping.ts 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  1. import { useMemo } from 'react';
  2. import { getColorName } from '../utils/colors';
  3. import {
  4. normalizeColor,
  5. normalizeColorForCompare,
  6. colorsAreSimilar,
  7. formatSlotLabel,
  8. getGlobalTrayId,
  9. } from '../utils/amsHelpers';
  10. import type { PrinterStatus } from '../api/client';
  11. /**
  12. * Build loaded filaments list from printer status (non-hook version).
  13. * Extracts filaments from all AMS units (regular and HT) and external spool.
  14. */
  15. export function buildLoadedFilaments(printerStatus: PrinterStatus | undefined): LoadedFilament[] {
  16. const filaments: LoadedFilament[] = [];
  17. const amsExtruderMap = printerStatus?.ams_extruder_map;
  18. const hasDualNozzle = amsExtruderMap && Object.keys(amsExtruderMap).length > 0;
  19. // Add filaments from all AMS units (regular and HT)
  20. printerStatus?.ams?.forEach((amsUnit) => {
  21. const isHt = amsUnit.tray.length === 1; // AMS-HT has single tray
  22. amsUnit.tray.forEach((tray) => {
  23. if (tray.tray_type) {
  24. const color = normalizeColor(tray.tray_color);
  25. filaments.push({
  26. type: tray.tray_type,
  27. color,
  28. colorName: getColorName(color),
  29. amsId: amsUnit.id,
  30. trayId: tray.id,
  31. isHt,
  32. isExternal: false,
  33. label: formatSlotLabel(amsUnit.id, tray.id, isHt, false),
  34. globalTrayId: getGlobalTrayId(amsUnit.id, tray.id, false),
  35. trayInfoIdx: tray.tray_info_idx || '',
  36. extruderId: amsExtruderMap?.[String(amsUnit.id)],
  37. });
  38. }
  39. });
  40. });
  41. // Add external spool(s) if loaded
  42. for (const extTray of printerStatus?.vt_tray ?? []) {
  43. if (extTray.tray_type) {
  44. const color = normalizeColor(extTray.tray_color);
  45. const trayId = extTray.id ?? 254;
  46. const hasDualExternal = (printerStatus?.vt_tray?.length ?? 0) > 1;
  47. filaments.push({
  48. type: extTray.tray_type,
  49. color,
  50. colorName: getColorName(color),
  51. amsId: -1,
  52. trayId: trayId - 254,
  53. isHt: false,
  54. isExternal: true,
  55. label: hasDualExternal ? (trayId === 254 ? 'Ext-L' : 'Ext-R') : 'External',
  56. globalTrayId: trayId,
  57. trayInfoIdx: extTray.tray_info_idx || '',
  58. extruderId: hasDualNozzle ? (255 - trayId) : undefined,
  59. });
  60. }
  61. }
  62. return filaments;
  63. }
  64. /**
  65. * Compute AMS mapping for a printer given filament requirements and printer status.
  66. * This is a non-hook version that can be called imperatively (e.g., in a loop for multiple printers).
  67. *
  68. * Priority: unique tray_info_idx match > exact color match > similar color match > type-only match
  69. *
  70. * The tray_info_idx is a filament type identifier stored in the 3MF file when the user
  71. * slices (e.g., "GFA00" for generic PLA, "P4d64437" for custom presets). If the same
  72. * tray_info_idx appears in only ONE available tray, we use that tray. If multiple trays
  73. * have the same tray_info_idx (e.g., two spools of generic PLA), we fall back to color
  74. * matching among those trays.
  75. *
  76. * @param filamentReqs - Required filaments from the 3MF file
  77. * @param printerStatus - Current printer status with AMS information
  78. * @returns AMS mapping array or undefined if no mapping needed
  79. */
  80. export function computeAmsMapping(
  81. filamentReqs: { filaments: FilamentRequirement[] } | undefined,
  82. printerStatus: PrinterStatus | undefined
  83. ): number[] | undefined {
  84. if (!filamentReqs?.filaments || filamentReqs.filaments.length === 0) return undefined;
  85. const loadedFilaments = buildLoadedFilaments(printerStatus);
  86. if (loadedFilaments.length === 0) return undefined;
  87. // Track which trays have been assigned to avoid duplicates
  88. const usedTrayIds = new Set<number>();
  89. const comparisons = filamentReqs.filaments.map((req) => {
  90. const reqTrayInfoIdx = req.tray_info_idx || '';
  91. // Get available trays (not already used)
  92. let available = loadedFilaments.filter((f) => !usedTrayIds.has(f.globalTrayId));
  93. // Nozzle-aware filtering: restrict to trays on the correct nozzle.
  94. // This is a hard filter — cross-nozzle assignment causes print failures
  95. // ("position of left hotend is abnormal"), so we never fall back to wrong-nozzle trays.
  96. if (req.nozzle_id != null) {
  97. available = available.filter((f) => f.extruderId === req.nozzle_id);
  98. }
  99. let idxMatch: LoadedFilament | undefined;
  100. let exactMatch: LoadedFilament | undefined;
  101. let similarMatch: LoadedFilament | undefined;
  102. let typeOnlyMatch: LoadedFilament | undefined;
  103. // Check if tray_info_idx is unique among available trays
  104. if (reqTrayInfoIdx) {
  105. const idxMatches = available.filter((f) => f.trayInfoIdx === reqTrayInfoIdx);
  106. if (idxMatches.length === 1) {
  107. // Unique tray_info_idx - use it as definitive match
  108. idxMatch = idxMatches[0];
  109. } else if (idxMatches.length > 1) {
  110. // Multiple trays with same tray_info_idx - use color matching among them
  111. exactMatch = idxMatches.find(
  112. (f) =>
  113. f.type?.toUpperCase() === req.type?.toUpperCase() &&
  114. normalizeColorForCompare(f.color) === normalizeColorForCompare(req.color)
  115. );
  116. if (!exactMatch) {
  117. similarMatch = idxMatches.find(
  118. (f) =>
  119. f.type?.toUpperCase() === req.type?.toUpperCase() &&
  120. colorsAreSimilar(f.color, req.color)
  121. );
  122. }
  123. if (!exactMatch && !similarMatch) {
  124. typeOnlyMatch = idxMatches.find(
  125. (f) => f.type?.toUpperCase() === req.type?.toUpperCase()
  126. );
  127. }
  128. }
  129. }
  130. // If no idx match, do standard type/color matching on all available trays
  131. if (!idxMatch && !exactMatch && !similarMatch && !typeOnlyMatch) {
  132. exactMatch = available.find(
  133. (f) =>
  134. f.type?.toUpperCase() === req.type?.toUpperCase() &&
  135. normalizeColorForCompare(f.color) === normalizeColorForCompare(req.color)
  136. );
  137. if (!exactMatch) {
  138. similarMatch = available.find(
  139. (f) =>
  140. f.type?.toUpperCase() === req.type?.toUpperCase() &&
  141. colorsAreSimilar(f.color, req.color)
  142. );
  143. }
  144. if (!exactMatch && !similarMatch) {
  145. typeOnlyMatch = available.find(
  146. (f) => f.type?.toUpperCase() === req.type?.toUpperCase()
  147. );
  148. }
  149. }
  150. const loaded = idxMatch || exactMatch || similarMatch || typeOnlyMatch || undefined;
  151. // Mark this tray as used so it won't be assigned to another slot
  152. if (loaded) {
  153. usedTrayIds.add(loaded.globalTrayId);
  154. }
  155. return {
  156. slot_id: req.slot_id,
  157. globalTrayId: loaded?.globalTrayId ?? -1,
  158. };
  159. });
  160. // Find the max slot_id to determine array size
  161. const maxSlotId = Math.max(...comparisons.map((f) => f.slot_id || 0));
  162. if (maxSlotId <= 0) return undefined;
  163. // Create array with -1 for all positions
  164. const mapping = new Array(maxSlotId).fill(-1);
  165. // Fill in tray IDs at correct positions (slot_id - 1)
  166. comparisons.forEach((f) => {
  167. if (f.slot_id && f.slot_id > 0) {
  168. mapping[f.slot_id - 1] = f.globalTrayId;
  169. }
  170. });
  171. return mapping;
  172. }
  173. /**
  174. * Represents a loaded filament in the printer's AMS/HT/External spool holder.
  175. */
  176. export interface LoadedFilament {
  177. type: string;
  178. color: string;
  179. colorName: string;
  180. amsId: number;
  181. trayId: number;
  182. isHt: boolean;
  183. isExternal: boolean;
  184. label: string;
  185. globalTrayId: number;
  186. /** Unique spool identifier (e.g., "GFA00", "P4d64437") */
  187. trayInfoIdx?: string;
  188. /** Extruder ID for dual-nozzle printers (0=right, 1=left) */
  189. extruderId?: number;
  190. }
  191. /**
  192. * Represents a required filament from the 3MF file.
  193. */
  194. export interface FilamentRequirement {
  195. slot_id: number;
  196. type: string;
  197. color: string;
  198. used_grams: number;
  199. /** Unique spool identifier from slicing (e.g., "GFA00", "P4d64437") */
  200. tray_info_idx?: string;
  201. /** Target nozzle for dual-nozzle printers (0=right, 1=left) */
  202. nozzle_id?: number;
  203. }
  204. /**
  205. * Status of filament comparison between required and loaded.
  206. */
  207. export type FilamentStatus = 'match' | 'type_only' | 'mismatch' | 'empty';
  208. /**
  209. * Result of comparing a required filament with loaded filaments.
  210. */
  211. export interface FilamentComparison extends FilamentRequirement {
  212. loaded: LoadedFilament | undefined;
  213. hasFilament: boolean;
  214. typeMatch: boolean;
  215. colorMatch: boolean;
  216. status: FilamentStatus;
  217. isManual: boolean;
  218. }
  219. interface FilamentRequirementsResponse {
  220. filaments: FilamentRequirement[];
  221. }
  222. interface UseFilamentMappingResult {
  223. /** List of all filaments loaded in the printer */
  224. loadedFilaments: LoadedFilament[];
  225. /** Comparison results for each required filament */
  226. filamentComparison: FilamentComparison[];
  227. /** AMS mapping array for the print command */
  228. amsMapping: number[] | undefined;
  229. /** Whether any required filament type is not loaded */
  230. hasTypeMismatch: boolean;
  231. /** Whether any required filament has a color mismatch */
  232. hasColorMismatch: boolean;
  233. }
  234. /**
  235. * Hook to build loaded filaments list from printer status.
  236. * Extracts filaments from all AMS units (regular and HT) and external spool.
  237. */
  238. export function useLoadedFilaments(
  239. printerStatus: PrinterStatus | undefined
  240. ): LoadedFilament[] {
  241. return useMemo(() => {
  242. return buildLoadedFilaments(printerStatus);
  243. }, [printerStatus]);
  244. }
  245. /**
  246. * Hook to compare required filaments with loaded filaments and build AMS mapping.
  247. * Handles both auto-matching and manual overrides.
  248. *
  249. * @param filamentReqs - Required filaments from the 3MF file
  250. * @param printerStatus - Current printer status with AMS information
  251. * @param manualMappings - Manual slot overrides (slot_id -> globalTrayId)
  252. */
  253. export function useFilamentMapping(
  254. filamentReqs: FilamentRequirementsResponse | undefined,
  255. printerStatus: PrinterStatus | undefined,
  256. manualMappings: Record<number, number>
  257. ): UseFilamentMappingResult {
  258. const loadedFilaments = useLoadedFilaments(printerStatus);
  259. const filamentComparison = useMemo(() => {
  260. if (!filamentReqs?.filaments || filamentReqs.filaments.length === 0) return [];
  261. // Track which trays have been assigned to avoid duplicates
  262. // First, mark all manually assigned trays as used
  263. const usedTrayIds = new Set<number>(Object.values(manualMappings));
  264. return filamentReqs.filaments.map((req) => {
  265. const slotId = req.slot_id || 0;
  266. // Check if there's a manual override for this slot
  267. if (slotId > 0 && manualMappings[slotId] !== undefined) {
  268. const manualTrayId = manualMappings[slotId];
  269. const manualLoaded = loadedFilaments.find((f) => f.globalTrayId === manualTrayId);
  270. if (manualLoaded) {
  271. const typeMatch = manualLoaded.type?.toUpperCase() === req.type?.toUpperCase();
  272. const colorMatch =
  273. normalizeColorForCompare(manualLoaded.color) === normalizeColorForCompare(req.color) ||
  274. colorsAreSimilar(manualLoaded.color, req.color);
  275. let status: FilamentStatus;
  276. if (typeMatch && colorMatch) {
  277. status = 'match';
  278. } else if (typeMatch) {
  279. status = 'type_only';
  280. } else {
  281. status = 'mismatch';
  282. }
  283. return {
  284. ...req,
  285. loaded: manualLoaded,
  286. hasFilament: true,
  287. typeMatch,
  288. colorMatch,
  289. status,
  290. isManual: true,
  291. };
  292. }
  293. }
  294. // Auto-match: Find a loaded filament
  295. // Priority: unique tray_info_idx match > exact color match > similar color match > type-only match
  296. // IMPORTANT: Exclude trays that are already assigned (manually or auto)
  297. const reqTrayInfoIdx = req.tray_info_idx || '';
  298. // Get available trays (not already used)
  299. let available = loadedFilaments.filter((f) => !usedTrayIds.has(f.globalTrayId));
  300. // Nozzle-aware filtering: restrict to trays on the correct nozzle.
  301. // This is a hard filter — cross-nozzle assignment causes print failures.
  302. if (req.nozzle_id != null) {
  303. available = available.filter((f) => f.extruderId === req.nozzle_id);
  304. }
  305. let idxMatch: LoadedFilament | undefined;
  306. let exactMatch: LoadedFilament | undefined;
  307. let similarMatch: LoadedFilament | undefined;
  308. let typeOnlyMatch: LoadedFilament | undefined;
  309. // Check if tray_info_idx is unique among available trays
  310. if (reqTrayInfoIdx) {
  311. const idxMatches = available.filter((f) => f.trayInfoIdx === reqTrayInfoIdx);
  312. if (idxMatches.length === 1) {
  313. // Unique tray_info_idx - use it as definitive match
  314. idxMatch = idxMatches[0];
  315. } else if (idxMatches.length > 1) {
  316. // Multiple trays with same tray_info_idx - use color matching among them
  317. exactMatch = idxMatches.find(
  318. (f) =>
  319. f.type?.toUpperCase() === req.type?.toUpperCase() &&
  320. normalizeColorForCompare(f.color) === normalizeColorForCompare(req.color)
  321. );
  322. if (!exactMatch) {
  323. similarMatch = idxMatches.find(
  324. (f) =>
  325. f.type?.toUpperCase() === req.type?.toUpperCase() &&
  326. colorsAreSimilar(f.color, req.color)
  327. );
  328. }
  329. if (!exactMatch && !similarMatch) {
  330. typeOnlyMatch = idxMatches.find(
  331. (f) => f.type?.toUpperCase() === req.type?.toUpperCase()
  332. );
  333. }
  334. }
  335. }
  336. // If no idx match, do standard type/color matching on all available trays
  337. if (!idxMatch && !exactMatch && !similarMatch && !typeOnlyMatch) {
  338. exactMatch = available.find(
  339. (f) =>
  340. f.type?.toUpperCase() === req.type?.toUpperCase() &&
  341. normalizeColorForCompare(f.color) === normalizeColorForCompare(req.color)
  342. );
  343. if (!exactMatch) {
  344. similarMatch = available.find(
  345. (f) =>
  346. f.type?.toUpperCase() === req.type?.toUpperCase() &&
  347. colorsAreSimilar(f.color, req.color)
  348. );
  349. }
  350. if (!exactMatch && !similarMatch) {
  351. typeOnlyMatch = available.find(
  352. (f) => f.type?.toUpperCase() === req.type?.toUpperCase()
  353. );
  354. }
  355. }
  356. const loaded = idxMatch || exactMatch || similarMatch || typeOnlyMatch || undefined;
  357. // Mark this tray as used so it won't be assigned to another slot
  358. if (loaded) {
  359. usedTrayIds.add(loaded.globalTrayId);
  360. }
  361. const hasFilament = !!loaded;
  362. const typeMatch = hasFilament;
  363. // idxMatch is always considered a color match (same spool = same color)
  364. const colorMatch = !!idxMatch || !!exactMatch || !!similarMatch;
  365. // Status: match (tray_info_idx, type+color, or similar color), type_only (type ok, color very different), mismatch (type not found)
  366. let status: FilamentStatus;
  367. if (idxMatch || exactMatch || similarMatch) {
  368. status = 'match';
  369. } else if (typeOnlyMatch) {
  370. status = 'type_only';
  371. } else {
  372. status = 'mismatch';
  373. }
  374. return {
  375. ...req,
  376. loaded,
  377. hasFilament,
  378. typeMatch,
  379. colorMatch,
  380. status,
  381. isManual: false,
  382. };
  383. });
  384. }, [filamentReqs, loadedFilaments, manualMappings]);
  385. // Build AMS mapping from matched filaments
  386. // Format: array matching 3MF filament slot structure
  387. // Position = slot_id - 1 (0-indexed), value = global tray ID or -1 for unused
  388. const amsMapping = useMemo(() => {
  389. if (filamentComparison.length === 0) return undefined;
  390. // Find the max slot_id to determine array size
  391. const maxSlotId = Math.max(...filamentComparison.map((f) => f.slot_id || 0));
  392. if (maxSlotId <= 0) return undefined;
  393. // Create array with -1 for all positions
  394. const mapping = new Array(maxSlotId).fill(-1);
  395. // Fill in tray IDs at correct positions (slot_id - 1)
  396. filamentComparison.forEach((f) => {
  397. if (f.slot_id && f.slot_id > 0) {
  398. mapping[f.slot_id - 1] = f.loaded?.globalTrayId ?? -1;
  399. }
  400. });
  401. return mapping;
  402. }, [filamentComparison]);
  403. const hasTypeMismatch = filamentComparison.some((f) => f.status === 'mismatch');
  404. const hasColorMismatch = filamentComparison.some((f) => f.status === 'type_only');
  405. return {
  406. loadedFilaments,
  407. filamentComparison,
  408. amsMapping,
  409. hasTypeMismatch,
  410. hasColorMismatch,
  411. };
  412. }