184 lines
5.8 KiB
JavaScript
184 lines
5.8 KiB
JavaScript
function formatMmuMaterialsHtml(printStatus) {
|
|
if (!printStatus || typeof printStatus !== "object") {
|
|
return "";
|
|
}
|
|
const state = collectMmuMaterialState(printStatus);
|
|
if (!state.visible) {
|
|
return "";
|
|
}
|
|
if (state.disabled) {
|
|
return `<span class="mmu-material-chip mmu-disabled"><span class="mmu-material-chip-swatch"></span><span class="mmu-material-chip-text">${escapeHTML(state.label)}: disabled</span></span>`;
|
|
}
|
|
if (state.rows.length === 0) {
|
|
return `<span class="mmu-material-chip"><span class="mmu-material-chip-swatch"></span><span class="mmu-material-chip-text">${escapeHTML(state.label)}: enabled</span></span>`;
|
|
}
|
|
return state.rows.map((row) => mmuMaterialRowHtml(row)).join("");
|
|
}
|
|
|
|
function collectMmuMaterialState(printStatus) {
|
|
const mmu = printStatus.moonraker_status && printStatus.moonraker_status.mmu;
|
|
if (!mmu || typeof mmu !== "object" || !Object.prototype.hasOwnProperty.call(mmu, "enabled")) {
|
|
return { visible: false, disabled: false, label: "MMU", rows: [] };
|
|
}
|
|
const machine = printStatus.moonraker_status && printStatus.moonraker_status.mmu_machine;
|
|
const enabled = isTruthyMetadataValue(mmu.enabled);
|
|
const label = machine?.unit_0?.name || "MMU";
|
|
if (!enabled) {
|
|
return { visible: true, disabled: true, label, rows: [] };
|
|
}
|
|
const names = normalizeMetadataList(mmu.gate_filament_name);
|
|
const materials = normalizeMetadataList(mmu.gate_material);
|
|
const colors = normalizeMetadataList(mmu.gate_color);
|
|
const count = Math.max(names.length, materials.length, colors.length, Number(mmu.num_gates || 0));
|
|
if (count === 0) {
|
|
return { visible: true, disabled: false, label, rows: [{ label, value: "Enabled", color: "" }] };
|
|
}
|
|
return {
|
|
visible: true,
|
|
disabled: false,
|
|
label,
|
|
rows: Array.from({ length: count }, (_, index) => {
|
|
const parts = [];
|
|
let color = "";
|
|
if (names[index] && names[index] !== "Unknown") {
|
|
parts.push(String(names[index]));
|
|
}
|
|
if (materials[index] && materials[index] !== names[index]) {
|
|
parts.push(`(${String(materials[index])})`);
|
|
}
|
|
if (colors[index]) {
|
|
color = String(colors[index]).replace(/[^0-9a-fA-F]/g, "");
|
|
}
|
|
return {
|
|
label: `Gate ${index + 1}`,
|
|
value: parts.join(" ") || "Unknown material",
|
|
color,
|
|
};
|
|
}),
|
|
};
|
|
}
|
|
|
|
function normalizeMetadataList(value) {
|
|
if (Array.isArray(value)) {
|
|
return value.filter((item) => item !== null && item !== undefined && item !== "");
|
|
}
|
|
if (value === null || value === undefined || value === "") {
|
|
return [];
|
|
}
|
|
return [value];
|
|
}
|
|
|
|
function isTruthyMetadataValue(value) {
|
|
if (Array.isArray(value)) {
|
|
return value.length > 0;
|
|
}
|
|
if (typeof value === "string") {
|
|
const normalized = value.trim().toLowerCase();
|
|
return normalized === "1" || normalized === "true" || normalized === "yes";
|
|
}
|
|
return Boolean(value);
|
|
}
|
|
|
|
function mmuMaterialRowHtml(row) {
|
|
const label = escapeHTML(row.label);
|
|
const value = row.value ? escapeHTML(row.value) : "";
|
|
const color = row.color ? mmuNormalizeHexColor(row.color) : "";
|
|
const swatchStyle = color ? ` style="--mmu-swatch:${escapeHTML(color)}"` : "";
|
|
return `<span class="mmu-material-chip"${swatchStyle}><span class="mmu-material-chip-swatch"></span><span class="mmu-material-chip-text"><strong>${label}</strong>${value ? ` ${value}` : ""}</span></span>`;
|
|
}
|
|
|
|
function mmuNormalizeHexColor(value) {
|
|
const raw = String(value || "").trim().replace(/[^0-9a-fA-F]/g, "");
|
|
if (raw.length === 8) {
|
|
return `#${raw.slice(0, 6)}`;
|
|
}
|
|
if (raw.length === 6) {
|
|
return `#${raw}`;
|
|
}
|
|
if (raw.length === 3) {
|
|
return `#${raw.split("").map((ch) => ch + ch).join("")}`;
|
|
}
|
|
return "";
|
|
}
|
|
|
|
function formatTemperatureSummary(printStatus) {
|
|
const state = collectTemperatureState(printStatus);
|
|
if (!state.visible) {
|
|
return "";
|
|
}
|
|
return state.rows.map((row) => `${row.label} ${row.value}`).join(" · ");
|
|
}
|
|
|
|
function collectTemperatureState(printStatus) {
|
|
const status = printStatus && printStatus.moonraker_status;
|
|
if (!status || typeof status !== "object") {
|
|
return { visible: false, rows: [] };
|
|
}
|
|
const rows = [];
|
|
const seen = new Set();
|
|
const add = (key, label) => {
|
|
if (!Object.prototype.hasOwnProperty.call(status, key)) {
|
|
return;
|
|
}
|
|
const sensor = status[key];
|
|
if (!sensor || typeof sensor !== "object") {
|
|
return;
|
|
}
|
|
const reading = formatTemperatureReading(sensor);
|
|
if (!reading) {
|
|
return;
|
|
}
|
|
rows.push({ label, value: reading });
|
|
seen.add(key);
|
|
};
|
|
add("extruder", "Hotend");
|
|
add("heater_bed", "Bed");
|
|
add("temperature_sensor chamber", "Chamber");
|
|
Object.keys(status).sort().forEach((key) => {
|
|
if (seen.has(key) || !isTemperatureSensorKey(key)) {
|
|
return;
|
|
}
|
|
const sensor = status[key];
|
|
if (!sensor || typeof sensor !== "object") {
|
|
return;
|
|
}
|
|
const reading = formatTemperatureReading(sensor);
|
|
if (!reading) {
|
|
return;
|
|
}
|
|
rows.push({ label: prettifyTemperatureLabel(key), value: reading });
|
|
});
|
|
return { visible: rows.length > 0, rows };
|
|
}
|
|
|
|
function isTemperatureSensorKey(key) {
|
|
return typeof key === "string" && (
|
|
key.startsWith("temperature_sensor ") ||
|
|
key.startsWith("temperature_fan ") ||
|
|
key.startsWith("temperature_probe ")
|
|
);
|
|
}
|
|
|
|
function prettifyTemperatureLabel(key) {
|
|
const raw = String(key || "")
|
|
.replace(/^temperature_(sensor|fan|probe)\s+/i, "")
|
|
.replace(/_/g, " ")
|
|
.trim();
|
|
if (!raw) {
|
|
return "Sensor";
|
|
}
|
|
return raw.replace(/\b\w/g, (ch) => ch.toUpperCase());
|
|
}
|
|
|
|
function formatTemperatureReading(sensor) {
|
|
const current = Number(sensor.temperature);
|
|
if (!Number.isFinite(current)) {
|
|
return "";
|
|
}
|
|
const target = Number(sensor.target);
|
|
if (Number.isFinite(target) && target > 0) {
|
|
return `${current.toFixed(1)} / ${target.toFixed(1)}°C`;
|
|
}
|
|
return `${current.toFixed(1)}°C`;
|
|
}
|