printer.ts 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. export function getPrinterImage(model: string | null | undefined): string {
  2. if (!model) return '/img/printers/default.png';
  3. const m = model.toLowerCase().replace(/\s+/g, '');
  4. if (m.includes('x1e')) return '/img/printers/x1e.png';
  5. if (m.includes('x1c') || m.includes('x1carbon')) return '/img/printers/x1c.png';
  6. if (m.includes('x1')) return '/img/printers/x1c.png';
  7. if (m.includes('x2d') || m === 'n6') return '/img/printers/x2d.png';
  8. if (m.includes('h2dpro') || m.includes('h2d-pro')) return '/img/printers/h2dpro.png';
  9. if (m.includes('h2d')) return '/img/printers/h2d.png';
  10. if (m.includes('h2c')) return '/img/printers/h2c.png';
  11. // Bambu Studio 02.09's own H3C render (resources/images/printer_thumbnail_N10_png.png).
  12. if (m.includes('h3c') || m === 'n10') return '/img/printers/h3c.png';
  13. if (m.includes('h2s')) return '/img/printers/h2d.png';
  14. if (m.includes('p2s')) return '/img/printers/p2s.png';
  15. if (m.includes('p1s')) return '/img/printers/p1s.png';
  16. if (m.includes('p1p')) return '/img/printers/p1p.png';
  17. if (m.includes('a2l') || m === 'n9') return '/img/printers/a2l.png';
  18. if (m.includes('a1mini')) return '/img/printers/a1mini.png';
  19. if (m.includes('a1')) return '/img/printers/a1.png';
  20. return '/img/printers/default.png';
  21. }
  22. // Ceiling for every chamber-temperature target the UI accepts (manual set,
  23. // preheat filament map, per-item preheat override, chamber quick-select
  24. // presets). Mirrors backend MAX_CHAMBER_TEMP_C in
  25. // backend/app/utils/printer_models.py — keep the two in sync. The H2 series
  26. // (H2C / H2D / H2D Pro / H2S) and X2D heat the chamber to 65 °C; X1E tops out
  27. // at 60 and its firmware clamps anything higher.
  28. export const MAX_CHAMBER_TEMP_C = 65;
  29. /** Highest target a BIQU Panda Breath takes, from its firmware (#1466). */
  30. export const PANDA_BREATH_MAX_TARGET_C = 60;
  31. // G-code interchange families (#2578). Mirrors backend GCODE_COMPAT_FAMILIES
  32. // in backend/app/utils/printer_models.py — keep the two in sync. A sliced 3MF
  33. // may target a different model ONLY within its family; everything else is
  34. // exact-match only.
  35. const GCODE_COMPAT_FAMILIES: ReadonlyArray<ReadonlySet<string>> = [
  36. new Set(['X1', 'X1C', 'X1E', 'P1P', 'P1S']),
  37. ];
  38. /** True when G-code sliced for one model may be dispatched to the other.
  39. * Unknown/missing metadata on either side returns true (can't validate). */
  40. export function isGcodeCompatible(
  41. slicedForModel: string | null | undefined,
  42. targetModel: string | null | undefined,
  43. ): boolean {
  44. if (!slicedForModel || !targetModel) return true;
  45. const norm = (m: string) => m.trim().toUpperCase().replace(/[\s-]/g, '');
  46. const a = norm(slicedForModel);
  47. const b = norm(targetModel);
  48. if (a === b) return true;
  49. return GCODE_COMPAT_FAMILIES.some((family) => family.has(a) && family.has(b));
  50. }
  51. export function getWifiStrength(rssi: number): { labelKey: string; color: string; bars: number } {
  52. if (rssi >= -50) return { labelKey: 'printers.wifiSignal.excellent', color: 'text-bambu-green', bars: 4 };
  53. if (rssi >= -60) return { labelKey: 'printers.wifiSignal.good', color: 'text-bambu-green', bars: 3 };
  54. if (rssi >= -70) return { labelKey: 'printers.wifiSignal.fair', color: 'text-yellow-400', bars: 2 };
  55. if (rssi >= -80) return { labelKey: 'printers.wifiSignal.weak', color: 'text-orange-400', bars: 1 };
  56. return { labelKey: 'printers.wifiSignal.veryWeak', color: 'text-red-400', bars: 1 };
  57. }
  58. import type { PrinterStatus, PrintQueueItem } from '../api/client';
  59. /** Gcode states with no print loaded. */
  60. export const IDLE_PRINTER_STATES = ['IDLE', 'FINISH', 'FAILED'];
  61. /** Gcode states in which a print is loaded or starting (mirrors the backend's ACTIVE_PRINT_STATES). */
  62. export const ACTIVE_PRINT_STATES = ['RUNNING', 'PAUSE', 'PREPARE', 'SLICING'];
  63. /**
  64. * True when a queue item aimed at this printer would start now rather than wait.
  65. *
  66. * Every print Bambuddy sends goes through the queue, so this is not "can we
  67. * print at all" — it is "will ASAP mean now". The PrintModal uses it to promise
  68. * a later start, and the printer card uses it to say whether a dropped file
  69. * prints or queues. Both must agree, or the card promises one thing and the
  70. * modal immediately says another.
  71. */
  72. export function isPrinterCurrentlyDispatchable(status: PrinterStatus | undefined): boolean {
  73. if (!status?.connected) return false;
  74. if (status.awaiting_plate_clear) return false;
  75. // A parked drying timer (#2896) never ends, so it does not hold a print back.
  76. if (status.ams?.some((ams) => ams.dry_time > 0 && !ams.dry_countdown_stalled)) return false;
  77. return IDLE_PRINTER_STATES.includes(status.state ?? '');
  78. }
  79. /**
  80. * Filters queue items based on printer compatibility (filament types and colors).
  81. * Mirrors backend _find_idle_printer_for_model() logic.
  82. * @param items - Array of queue items to filter
  83. * @param loadedFilamentTypes - Set of loaded filament types (e.g., "PLA", "PETG")
  84. * @param loadedFilaments - Set of loaded filament type+color pairs (e.g., "PLA:ffffff", "PETG:ff0000")
  85. * @param loadedVariants - Set of loaded type+color+tray_info_idx triples
  86. * (e.g., "PLA:ffffff:GFA01"; the idx is "" for custom/third-party spools). Used to
  87. * distinguish Bambu PLA sub-variants (Basic GFA00 / Matte GFA01 / Silk GFA06) that
  88. * share a base type+colour, mirroring the backend _get_missing_force_color_slots (#2650).
  89. * When omitted, force matching falls back to type+colour so the hint is never stricter
  90. * than the data available.
  91. * @returns Array of compatible queue items
  92. */
  93. export function filterCompatibleQueueItems(
  94. items: PrintQueueItem[],
  95. loadedFilamentTypes?: Set<string>,
  96. loadedFilaments?: Set<string>,
  97. loadedVariants?: Set<string>
  98. ): PrintQueueItem[] {
  99. return items.filter(item => {
  100. // A job bound to one printer has no printer left to choose, and the backend
  101. // never gates it on filament: the scheduler maps its trays at dispatch. Such
  102. // a job can carry overrides — one moved from "Any P2S" to a specific P2S
  103. // keeps its colour (#3133) — and filtering on them would hide a job that is
  104. // going to run from the card of the printer it is going to run on.
  105. if (item.printer_id != null) return true;
  106. // Type check: all required filament types must be loaded
  107. if (item.required_filament_types && item.required_filament_types.length > 0 && loadedFilamentTypes !== undefined) {
  108. if (!item.required_filament_types.every((t: string) => loadedFilamentTypes.has(t.toUpperCase()))) {
  109. return false;
  110. }
  111. }
  112. // Color check: evaluate force_color_match per slot
  113. // Only apply when loadedFilaments is provided (not undefined).
  114. // An empty Set means no filaments are loaded — force-matched slots cannot match.
  115. if (item.filament_overrides && item.filament_overrides.length > 0 && loadedFilaments !== undefined) {
  116. const forceOverrides = item.filament_overrides.filter(o => o.force_color_match === true);
  117. const prefOverrides = item.filament_overrides.filter(o => o.force_color_match !== true);
  118. // All force-matched slots must have an exact type+color match — and, when the
  119. // override carries a tray_info_idx, the same variant too (a loaded tray with a
  120. // blank idx still satisfies it, matching the backend's type+colour fallback).
  121. if (forceOverrides.length > 0) {
  122. const allForceMatch = forceOverrides.every(o => {
  123. const oType = (o.type || '').toUpperCase();
  124. const oColor = (o.color || '').replace('#', '').toLowerCase().slice(0, 6);
  125. const oIdx = o.tray_info_idx || '';
  126. // No variant on the override, or no variant data supplied → type+colour only.
  127. if (!oIdx || loadedVariants === undefined) {
  128. return loadedFilaments.has(`${oType}:${oColor}`);
  129. }
  130. // Variant-specific: same idx, or a same-colour tray that reports no idx.
  131. return loadedVariants.has(`${oType}:${oColor}:${oIdx}`) || loadedVariants.has(`${oType}:${oColor}:`);
  132. });
  133. if (!allForceMatch) return false;
  134. }
  135. // Preference-only overrides: at least one color must match (existing behaviour)
  136. if (prefOverrides.length > 0 && forceOverrides.length === 0) {
  137. const hasColorMatch = prefOverrides.some(o => {
  138. const oType = (o.type || '').toUpperCase();
  139. const oColor = (o.color || '').replace('#', '').toLowerCase().slice(0, 6);
  140. return loadedFilaments.has(`${oType}:${oColor}`);
  141. });
  142. if (!hasColorMatch) return false;
  143. }
  144. }
  145. return true;
  146. });
  147. }