| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481 |
- import { useMemo } from 'react';
- import { getColorName } from '../utils/colors';
- import {
- normalizeColor,
- normalizeColorForCompare,
- colorsAreSimilar,
- formatSlotLabel,
- getGlobalTrayId,
- } from '../utils/amsHelpers';
- import type { PrinterStatus } from '../api/client';
- /**
- * Build loaded filaments list from printer status (non-hook version).
- * Extracts filaments from all AMS units (regular and HT) and external spool.
- */
- export function buildLoadedFilaments(printerStatus: PrinterStatus | undefined): LoadedFilament[] {
- const filaments: LoadedFilament[] = [];
- // Add filaments from all AMS units (regular and HT)
- printerStatus?.ams?.forEach((amsUnit) => {
- const isHt = amsUnit.tray.length === 1; // AMS-HT has single tray
- amsUnit.tray.forEach((tray) => {
- if (tray.tray_type) {
- const color = normalizeColor(tray.tray_color);
- filaments.push({
- type: tray.tray_type,
- color,
- colorName: getColorName(color),
- amsId: amsUnit.id,
- trayId: tray.id,
- isHt,
- isExternal: false,
- label: formatSlotLabel(amsUnit.id, tray.id, isHt, false),
- globalTrayId: getGlobalTrayId(amsUnit.id, tray.id, false),
- trayInfoIdx: tray.tray_info_idx || '',
- });
- }
- });
- });
- // Add external spool if loaded
- if (printerStatus?.vt_tray?.tray_type) {
- const color = normalizeColor(printerStatus.vt_tray.tray_color);
- filaments.push({
- type: printerStatus.vt_tray.tray_type,
- color,
- colorName: getColorName(color),
- amsId: -1,
- trayId: 0,
- isHt: false,
- isExternal: true,
- label: 'External',
- globalTrayId: 254,
- trayInfoIdx: printerStatus.vt_tray.tray_info_idx || '',
- });
- }
- return filaments;
- }
- /**
- * Compute AMS mapping for a printer given filament requirements and printer status.
- * This is a non-hook version that can be called imperatively (e.g., in a loop for multiple printers).
- *
- * Priority: unique tray_info_idx match > exact color match > similar color match > type-only match
- *
- * The tray_info_idx is a filament type identifier stored in the 3MF file when the user
- * slices (e.g., "GFA00" for generic PLA, "P4d64437" for custom presets). If the same
- * tray_info_idx appears in only ONE available tray, we use that tray. If multiple trays
- * have the same tray_info_idx (e.g., two spools of generic PLA), we fall back to color
- * matching among those trays.
- *
- * @param filamentReqs - Required filaments from the 3MF file
- * @param printerStatus - Current printer status with AMS information
- * @returns AMS mapping array or undefined if no mapping needed
- */
- export function computeAmsMapping(
- filamentReqs: { filaments: FilamentRequirement[] } | undefined,
- printerStatus: PrinterStatus | undefined
- ): number[] | undefined {
- if (!filamentReqs?.filaments || filamentReqs.filaments.length === 0) return undefined;
- const loadedFilaments = buildLoadedFilaments(printerStatus);
- if (loadedFilaments.length === 0) return undefined;
- // Track which trays have been assigned to avoid duplicates
- const usedTrayIds = new Set<number>();
- const comparisons = filamentReqs.filaments.map((req) => {
- const reqTrayInfoIdx = req.tray_info_idx || '';
- // Get available trays (not already used)
- const available = loadedFilaments.filter((f) => !usedTrayIds.has(f.globalTrayId));
- let idxMatch: LoadedFilament | undefined;
- let exactMatch: LoadedFilament | undefined;
- let similarMatch: LoadedFilament | undefined;
- let typeOnlyMatch: LoadedFilament | undefined;
- // Check if tray_info_idx is unique among available trays
- if (reqTrayInfoIdx) {
- const idxMatches = available.filter((f) => f.trayInfoIdx === reqTrayInfoIdx);
- if (idxMatches.length === 1) {
- // Unique tray_info_idx - use it as definitive match
- idxMatch = idxMatches[0];
- } else if (idxMatches.length > 1) {
- // Multiple trays with same tray_info_idx - use color matching among them
- exactMatch = idxMatches.find(
- (f) =>
- f.type?.toUpperCase() === req.type?.toUpperCase() &&
- normalizeColorForCompare(f.color) === normalizeColorForCompare(req.color)
- );
- if (!exactMatch) {
- similarMatch = idxMatches.find(
- (f) =>
- f.type?.toUpperCase() === req.type?.toUpperCase() &&
- colorsAreSimilar(f.color, req.color)
- );
- }
- if (!exactMatch && !similarMatch) {
- typeOnlyMatch = idxMatches.find(
- (f) => f.type?.toUpperCase() === req.type?.toUpperCase()
- );
- }
- }
- }
- // If no idx match, do standard type/color matching on all available trays
- if (!idxMatch && !exactMatch && !similarMatch && !typeOnlyMatch) {
- exactMatch = available.find(
- (f) =>
- f.type?.toUpperCase() === req.type?.toUpperCase() &&
- normalizeColorForCompare(f.color) === normalizeColorForCompare(req.color)
- );
- if (!exactMatch) {
- similarMatch = available.find(
- (f) =>
- f.type?.toUpperCase() === req.type?.toUpperCase() &&
- colorsAreSimilar(f.color, req.color)
- );
- }
- if (!exactMatch && !similarMatch) {
- typeOnlyMatch = available.find(
- (f) => f.type?.toUpperCase() === req.type?.toUpperCase()
- );
- }
- }
- const loaded = idxMatch || exactMatch || similarMatch || typeOnlyMatch || undefined;
- // Mark this tray as used so it won't be assigned to another slot
- if (loaded) {
- usedTrayIds.add(loaded.globalTrayId);
- }
- return {
- slot_id: req.slot_id,
- globalTrayId: loaded?.globalTrayId ?? -1,
- };
- });
- // Find the max slot_id to determine array size
- const maxSlotId = Math.max(...comparisons.map((f) => f.slot_id || 0));
- if (maxSlotId <= 0) return undefined;
- // Create array with -1 for all positions
- const mapping = new Array(maxSlotId).fill(-1);
- // Fill in tray IDs at correct positions (slot_id - 1)
- comparisons.forEach((f) => {
- if (f.slot_id && f.slot_id > 0) {
- mapping[f.slot_id - 1] = f.globalTrayId;
- }
- });
- return mapping;
- }
- /**
- * Represents a loaded filament in the printer's AMS/HT/External spool holder.
- */
- export interface LoadedFilament {
- type: string;
- color: string;
- colorName: string;
- amsId: number;
- trayId: number;
- isHt: boolean;
- isExternal: boolean;
- label: string;
- globalTrayId: number;
- /** Unique spool identifier (e.g., "GFA00", "P4d64437") */
- trayInfoIdx?: string;
- }
- /**
- * Represents a required filament from the 3MF file.
- */
- export interface FilamentRequirement {
- slot_id: number;
- type: string;
- color: string;
- used_grams: number;
- /** Unique spool identifier from slicing (e.g., "GFA00", "P4d64437") */
- tray_info_idx?: string;
- }
- /**
- * Status of filament comparison between required and loaded.
- */
- export type FilamentStatus = 'match' | 'type_only' | 'mismatch' | 'empty';
- /**
- * Result of comparing a required filament with loaded filaments.
- */
- export interface FilamentComparison extends FilamentRequirement {
- loaded: LoadedFilament | undefined;
- hasFilament: boolean;
- typeMatch: boolean;
- colorMatch: boolean;
- status: FilamentStatus;
- isManual: boolean;
- }
- interface FilamentRequirementsResponse {
- filaments: FilamentRequirement[];
- }
- interface UseFilamentMappingResult {
- /** List of all filaments loaded in the printer */
- loadedFilaments: LoadedFilament[];
- /** Comparison results for each required filament */
- filamentComparison: FilamentComparison[];
- /** AMS mapping array for the print command */
- amsMapping: number[] | undefined;
- /** Whether any required filament type is not loaded */
- hasTypeMismatch: boolean;
- /** Whether any required filament has a color mismatch */
- hasColorMismatch: boolean;
- }
- /**
- * Hook to build loaded filaments list from printer status.
- * Extracts filaments from all AMS units (regular and HT) and external spool.
- */
- export function useLoadedFilaments(
- printerStatus: PrinterStatus | undefined
- ): LoadedFilament[] {
- return useMemo(() => {
- const filaments: LoadedFilament[] = [];
- // Add filaments from all AMS units (regular and HT)
- printerStatus?.ams?.forEach((amsUnit) => {
- const isHt = amsUnit.tray.length === 1; // AMS-HT has single tray
- amsUnit.tray.forEach((tray) => {
- if (tray.tray_type) {
- const color = normalizeColor(tray.tray_color);
- filaments.push({
- type: tray.tray_type,
- color,
- colorName: getColorName(color),
- amsId: amsUnit.id,
- trayId: tray.id,
- isHt,
- isExternal: false,
- label: formatSlotLabel(amsUnit.id, tray.id, isHt, false),
- globalTrayId: getGlobalTrayId(amsUnit.id, tray.id, false),
- trayInfoIdx: tray.tray_info_idx || '',
- });
- }
- });
- });
- // Add external spool if loaded
- if (printerStatus?.vt_tray?.tray_type) {
- const color = normalizeColor(printerStatus.vt_tray.tray_color);
- filaments.push({
- type: printerStatus.vt_tray.tray_type,
- color,
- colorName: getColorName(color),
- amsId: -1,
- trayId: 0,
- isHt: false,
- isExternal: true,
- label: 'External',
- globalTrayId: 254,
- trayInfoIdx: printerStatus.vt_tray.tray_info_idx || '',
- });
- }
- return filaments;
- }, [printerStatus]);
- }
- /**
- * Hook to compare required filaments with loaded filaments and build AMS mapping.
- * Handles both auto-matching and manual overrides.
- *
- * @param filamentReqs - Required filaments from the 3MF file
- * @param printerStatus - Current printer status with AMS information
- * @param manualMappings - Manual slot overrides (slot_id -> globalTrayId)
- */
- export function useFilamentMapping(
- filamentReqs: FilamentRequirementsResponse | undefined,
- printerStatus: PrinterStatus | undefined,
- manualMappings: Record<number, number>
- ): UseFilamentMappingResult {
- const loadedFilaments = useLoadedFilaments(printerStatus);
- const filamentComparison = useMemo(() => {
- if (!filamentReqs?.filaments || filamentReqs.filaments.length === 0) return [];
- // Track which trays have been assigned to avoid duplicates
- // First, mark all manually assigned trays as used
- const usedTrayIds = new Set<number>(Object.values(manualMappings));
- return filamentReqs.filaments.map((req) => {
- const slotId = req.slot_id || 0;
- // Check if there's a manual override for this slot
- if (slotId > 0 && manualMappings[slotId] !== undefined) {
- const manualTrayId = manualMappings[slotId];
- const manualLoaded = loadedFilaments.find((f) => f.globalTrayId === manualTrayId);
- if (manualLoaded) {
- const typeMatch = manualLoaded.type?.toUpperCase() === req.type?.toUpperCase();
- const colorMatch =
- normalizeColorForCompare(manualLoaded.color) === normalizeColorForCompare(req.color) ||
- colorsAreSimilar(manualLoaded.color, req.color);
- let status: FilamentStatus;
- if (typeMatch && colorMatch) {
- status = 'match';
- } else if (typeMatch) {
- status = 'type_only';
- } else {
- status = 'mismatch';
- }
- return {
- ...req,
- loaded: manualLoaded,
- hasFilament: true,
- typeMatch,
- colorMatch,
- status,
- isManual: true,
- };
- }
- }
- // Auto-match: Find a loaded filament
- // Priority: unique tray_info_idx match > exact color match > similar color match > type-only match
- // IMPORTANT: Exclude trays that are already assigned (manually or auto)
- const reqTrayInfoIdx = req.tray_info_idx || '';
- // Get available trays (not already used)
- const available = loadedFilaments.filter((f) => !usedTrayIds.has(f.globalTrayId));
- let idxMatch: LoadedFilament | undefined;
- let exactMatch: LoadedFilament | undefined;
- let similarMatch: LoadedFilament | undefined;
- let typeOnlyMatch: LoadedFilament | undefined;
- // Check if tray_info_idx is unique among available trays
- if (reqTrayInfoIdx) {
- const idxMatches = available.filter((f) => f.trayInfoIdx === reqTrayInfoIdx);
- if (idxMatches.length === 1) {
- // Unique tray_info_idx - use it as definitive match
- idxMatch = idxMatches[0];
- } else if (idxMatches.length > 1) {
- // Multiple trays with same tray_info_idx - use color matching among them
- exactMatch = idxMatches.find(
- (f) =>
- f.type?.toUpperCase() === req.type?.toUpperCase() &&
- normalizeColorForCompare(f.color) === normalizeColorForCompare(req.color)
- );
- if (!exactMatch) {
- similarMatch = idxMatches.find(
- (f) =>
- f.type?.toUpperCase() === req.type?.toUpperCase() &&
- colorsAreSimilar(f.color, req.color)
- );
- }
- if (!exactMatch && !similarMatch) {
- typeOnlyMatch = idxMatches.find(
- (f) => f.type?.toUpperCase() === req.type?.toUpperCase()
- );
- }
- }
- }
- // If no idx match, do standard type/color matching on all available trays
- if (!idxMatch && !exactMatch && !similarMatch && !typeOnlyMatch) {
- exactMatch = available.find(
- (f) =>
- f.type?.toUpperCase() === req.type?.toUpperCase() &&
- normalizeColorForCompare(f.color) === normalizeColorForCompare(req.color)
- );
- if (!exactMatch) {
- similarMatch = available.find(
- (f) =>
- f.type?.toUpperCase() === req.type?.toUpperCase() &&
- colorsAreSimilar(f.color, req.color)
- );
- }
- if (!exactMatch && !similarMatch) {
- typeOnlyMatch = available.find(
- (f) => f.type?.toUpperCase() === req.type?.toUpperCase()
- );
- }
- }
- const loaded = idxMatch || exactMatch || similarMatch || typeOnlyMatch || undefined;
- // Mark this tray as used so it won't be assigned to another slot
- if (loaded) {
- usedTrayIds.add(loaded.globalTrayId);
- }
- const hasFilament = !!loaded;
- const typeMatch = hasFilament;
- // idxMatch is always considered a color match (same spool = same color)
- const colorMatch = !!idxMatch || !!exactMatch || !!similarMatch;
- // Status: match (tray_info_idx, type+color, or similar color), type_only (type ok, color very different), mismatch (type not found)
- let status: FilamentStatus;
- if (idxMatch || exactMatch || similarMatch) {
- status = 'match';
- } else if (typeOnlyMatch) {
- status = 'type_only';
- } else {
- status = 'mismatch';
- }
- return {
- ...req,
- loaded,
- hasFilament,
- typeMatch,
- colorMatch,
- status,
- isManual: false,
- };
- });
- }, [filamentReqs, loadedFilaments, manualMappings]);
- // Build AMS mapping from matched filaments
- // Format: array matching 3MF filament slot structure
- // Position = slot_id - 1 (0-indexed), value = global tray ID or -1 for unused
- const amsMapping = useMemo(() => {
- if (filamentComparison.length === 0) return undefined;
- // Find the max slot_id to determine array size
- const maxSlotId = Math.max(...filamentComparison.map((f) => f.slot_id || 0));
- if (maxSlotId <= 0) return undefined;
- // Create array with -1 for all positions
- const mapping = new Array(maxSlotId).fill(-1);
- // Fill in tray IDs at correct positions (slot_id - 1)
- filamentComparison.forEach((f) => {
- if (f.slot_id && f.slot_id > 0) {
- mapping[f.slot_id - 1] = f.loaded?.globalTrayId ?? -1;
- }
- });
- return mapping;
- }, [filamentComparison]);
- const hasTypeMismatch = filamentComparison.some((f) => f.status === 'mismatch');
- const hasColorMismatch = filamentComparison.some((f) => f.status === 'type_only');
- return {
- loadedFilaments,
- filamentComparison,
- amsMapping,
- hasTypeMismatch,
- hasColorMismatch,
- };
- }
|