assignMismatch.ts 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /**
  2. * Does a spool fit the AMS slot it is being assigned to? The material check
  3. * the Assign Spool dialog and the QR assignment both ask before assigning.
  4. */
  5. import type { TFunction } from 'i18next';
  6. export const normalizeValue = (value: string | undefined | null) => (value ?? '').trim().toUpperCase();
  7. // Bambu Studio / OrcaSlicer profile names carry a printer/nozzle/variant qualifier after
  8. // `@` (e.g. "Devil Design PLA Basic @Bambu Lab H2D 0.4 nozzle (Custom)"), while the tray's
  9. // profile is typically the bare base name. Strip the qualifier before comparing so identical
  10. // base profiles don't trigger a mismatch warning (#1047).
  11. export const stripProfileQualifier = (value: string) => value.split('@')[0].trim();
  12. function checkMaterialMatch(
  13. spoolMaterial: string | undefined | null,
  14. trayMaterial: string | undefined | null,
  15. ): 'exact' | 'partial' | 'none' {
  16. const normalizedSpool = normalizeValue(spoolMaterial);
  17. const normalizedTray = normalizeValue(trayMaterial);
  18. if (!normalizedSpool || !normalizedTray) return 'none';
  19. if (normalizedSpool === normalizedTray) return 'exact';
  20. if (normalizedTray.includes(normalizedSpool) || normalizedSpool.includes(normalizedTray)) {
  21. return 'partial';
  22. }
  23. return 'none';
  24. }
  25. function checkProfileMatch(spoolProfile: string | undefined | null, trayProfile: string | undefined | null): boolean {
  26. const normalizedSpoolProfile = stripProfileQualifier(normalizeValue(spoolProfile));
  27. const normalizedTrayProfile = stripProfileQualifier(normalizeValue(trayProfile));
  28. if (!normalizedSpoolProfile || !normalizedTrayProfile) return false;
  29. return normalizedSpoolProfile === normalizedTrayProfile;
  30. }
  31. export interface SlotFilament {
  32. type: string;
  33. material?: string;
  34. profile?: string;
  35. }
  36. export interface MaterialMismatch {
  37. type: 'material' | 'partial' | 'material_profile' | 'partial_profile';
  38. spoolMaterial: string;
  39. trayMaterial: string;
  40. spoolProfile?: string;
  41. trayProfile?: string;
  42. }
  43. /**
  44. * The material mismatch to confirm before assigning `spool` to the slot, or
  45. * null when there is none to ask about.
  46. *
  47. * Profile-only mismatch no longer triggers the popup — the backend's
  48. * `apply_spool_to_slot_via_mqtt` pushes the spool's slicer profile to the AMS
  49. * slot on every assign anyway, so warning the user about a profile delta then
  50. * "fixing" it during the same action was friction without benefit (#1552).
  51. * Material mismatch still warns because the firmware can refuse a print when
  52. * type doesn't match; combined material+profile mismatches keep the profile
  53. * detail in the same popup as the material warning.
  54. *
  55. * A slot reporting no material — empty, or a spool the printer can't identify
  56. * — has nothing to mismatch: the question would read 'does not match the tray
  57. * material ""'.
  58. */
  59. export function findMaterialMismatch(
  60. spool: { material?: string | null; slicer_filament_name?: string | null; slicer_filament?: string | null },
  61. slot: SlotFilament,
  62. ): MaterialMismatch | null {
  63. const trayMaterial = slot.material || slot.type;
  64. if (!normalizeValue(trayMaterial)) return null;
  65. const materialMatchResult = checkMaterialMatch(spool.material, trayMaterial);
  66. if (materialMatchResult === 'exact') return null;
  67. const spoolProfile = spool.slicer_filament_name || spool.slicer_filament;
  68. const trayProfile = slot.profile || slot.type;
  69. const profileMatches = checkProfileMatch(spoolProfile, trayProfile);
  70. let type: MaterialMismatch['type'];
  71. if (materialMatchResult === 'none' && !profileMatches) {
  72. type = 'material_profile';
  73. } else if (materialMatchResult === 'partial' && !profileMatches) {
  74. type = 'partial_profile';
  75. } else if (materialMatchResult === 'none') {
  76. type = 'material';
  77. } else {
  78. type = 'partial';
  79. }
  80. return {
  81. type,
  82. spoolMaterial: spool.material || '',
  83. trayMaterial: trayMaterial || '',
  84. spoolProfile: spoolProfile || undefined,
  85. trayProfile: trayProfile || undefined,
  86. };
  87. }
  88. /** The confirm's text for a mismatch at `location`. */
  89. export function materialMismatchMessage(t: TFunction, mismatch: MaterialMismatch, location: string): string {
  90. const materials = { spoolMaterial: mismatch.spoolMaterial, trayMaterial: mismatch.trayMaterial, location };
  91. const profiles = () =>
  92. t('inventory.assignProfileMismatchMessage', {
  93. spoolProfile: mismatch.spoolProfile || t('common.unknown'),
  94. trayProfile: mismatch.trayProfile || t('common.unknown'),
  95. location,
  96. });
  97. let message =
  98. mismatch.type === 'material' || mismatch.type === 'material_profile'
  99. ? t('inventory.assignMismatchMessage', materials)
  100. : t('inventory.assignPartialMismatchMessage', materials);
  101. if (mismatch.type === 'material_profile' || mismatch.type === 'partial_profile') {
  102. message = `${message}\n\n${profiles()}`;
  103. }
  104. // Always tell the user the AMS slot is going to be reconfigured — the
  105. // existing wording made "Assign Anyway" sound like the popup was a no-op
  106. // confirmation, when the backend in fact pushes the spool's profile to the
  107. // slot on every assign (#1552).
  108. return `${message}\n\n${t('inventory.assignReconfigureNote')}`;
  109. }