printer.ts 4.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. if (m.includes('h2s')) return '/img/printers/h2d.png';
  12. if (m.includes('p2s')) return '/img/printers/p1s.png';
  13. if (m.includes('p1s')) return '/img/printers/p1s.png';
  14. if (m.includes('p1p')) return '/img/printers/p1p.png';
  15. if (m.includes('a2l') || m === 'n9') return '/img/printers/a2l.png';
  16. if (m.includes('a1mini')) return '/img/printers/a1mini.png';
  17. if (m.includes('a1')) return '/img/printers/a1.png';
  18. return '/img/printers/default.png';
  19. }
  20. export function getWifiStrength(rssi: number): { labelKey: string; color: string; bars: number } {
  21. if (rssi >= -50) return { labelKey: 'printers.wifiSignal.excellent', color: 'text-bambu-green', bars: 4 };
  22. if (rssi >= -60) return { labelKey: 'printers.wifiSignal.good', color: 'text-bambu-green', bars: 3 };
  23. if (rssi >= -70) return { labelKey: 'printers.wifiSignal.fair', color: 'text-yellow-400', bars: 2 };
  24. if (rssi >= -80) return { labelKey: 'printers.wifiSignal.weak', color: 'text-orange-400', bars: 1 };
  25. return { labelKey: 'printers.wifiSignal.veryWeak', color: 'text-red-400', bars: 1 };
  26. }
  27. import type { PrintQueueItem } from '../api/client';
  28. /**
  29. * Filters queue items based on printer compatibility (filament types and colors).
  30. * Mirrors backend _find_idle_printer_for_model() logic.
  31. * @param items - Array of queue items to filter
  32. * @param loadedFilamentTypes - Set of loaded filament types (e.g., "PLA", "PETG")
  33. * @param loadedFilaments - Set of loaded filament type+color pairs (e.g., "PLA:ffffff", "PETG:ff0000")
  34. * @returns Array of compatible queue items
  35. */
  36. export function filterCompatibleQueueItems(
  37. items: PrintQueueItem[],
  38. loadedFilamentTypes?: Set<string>,
  39. loadedFilaments?: Set<string>
  40. ): PrintQueueItem[] {
  41. return items.filter(item => {
  42. // Type check: all required filament types must be loaded
  43. if (item.required_filament_types && item.required_filament_types.length > 0 && loadedFilamentTypes !== undefined) {
  44. if (!item.required_filament_types.every((t: string) => loadedFilamentTypes.has(t.toUpperCase()))) {
  45. return false;
  46. }
  47. }
  48. // Color check: evaluate force_color_match per slot
  49. // Only apply when loadedFilaments is provided (not undefined).
  50. // An empty Set means no filaments are loaded — force-matched slots cannot match.
  51. if (item.filament_overrides && item.filament_overrides.length > 0 && loadedFilaments !== undefined) {
  52. const forceOverrides = item.filament_overrides.filter(o => o.force_color_match === true);
  53. const prefOverrides = item.filament_overrides.filter(o => o.force_color_match !== true);
  54. // All force-matched slots must have exact type+color on this printer
  55. if (forceOverrides.length > 0) {
  56. const allForceMatch = forceOverrides.every(o => {
  57. const oType = (o.type || '').toUpperCase();
  58. const oColor = (o.color || '').replace('#', '').toLowerCase().slice(0, 6);
  59. return loadedFilaments.has(`${oType}:${oColor}`);
  60. });
  61. if (!allForceMatch) return false;
  62. }
  63. // Preference-only overrides: at least one color must match (existing behaviour)
  64. if (prefOverrides.length > 0 && forceOverrides.length === 0) {
  65. const hasColorMatch = prefOverrides.some(o => {
  66. const oType = (o.type || '').toUpperCase();
  67. const oColor = (o.color || '').replace('#', '').toLowerCase().slice(0, 6);
  68. return loadedFilaments.has(`${oType}:${oColor}`);
  69. });
  70. if (!hasColorMatch) return false;
  71. }
  72. }
  73. return true;
  74. });
  75. }