101 lines
3.1 KiB
JavaScript
101 lines
3.1 KiB
JavaScript
// Status polling and UI flag helpers
|
|
import { placeholderStatus, renderStats } from "./status.js";
|
|
import { renderServices } from "./services.js";
|
|
|
|
export function createStatusController({
|
|
heroStats,
|
|
servicesGrid,
|
|
updatesFlagTop,
|
|
updatesNoteTop,
|
|
tempFlagTop,
|
|
readyOverlay,
|
|
logUi,
|
|
showToast = () => {},
|
|
onReadyWait = null,
|
|
getStatus,
|
|
isUpdatesDirty,
|
|
releaseUIGetter = () => null,
|
|
setUpdatesUI = null,
|
|
updatesFlagEl = null,
|
|
}) {
|
|
let lastStatusData = null;
|
|
|
|
function setTempFlag(tempC) {
|
|
if (!tempFlagTop) return;
|
|
const t = typeof tempC === "number" ? tempC : null;
|
|
let label = "Temp: n/a";
|
|
tempFlagTop.classList.remove("chip-on", "chip-warm", "chip-off");
|
|
if (t !== null) {
|
|
if (t < 55) {
|
|
label = "Temp: OK";
|
|
tempFlagTop.classList.add("chip-on");
|
|
} else if (t < 70) {
|
|
label = "Temp: Warm";
|
|
tempFlagTop.classList.add("chip-warm");
|
|
} else {
|
|
label = "Temp: Hot";
|
|
tempFlagTop.classList.add("chip-off");
|
|
}
|
|
}
|
|
tempFlagTop.textContent = label;
|
|
}
|
|
|
|
function updatesFlagEl(enabled) {
|
|
if (!updatesFlagTop) return;
|
|
const labelOn = "System updates: On";
|
|
const labelOff = "System updates: Off";
|
|
updatesFlagTop.textContent =
|
|
enabled === true ? labelOn : enabled === false ? labelOff : "System updates";
|
|
updatesFlagTop.className = "status-chip quiet chip-system";
|
|
if (enabled === false) updatesFlagTop.classList.add("chip-off");
|
|
}
|
|
|
|
async function loadStatus() {
|
|
try {
|
|
const data = await getStatus();
|
|
lastStatusData = data;
|
|
renderStats(heroStats, data);
|
|
renderServices(servicesGrid, data.services, { openAddService: window.__pikitOpenAddService });
|
|
const updatesEnabled = data?.auto_updates?.enabled ?? data.auto_updates_enabled;
|
|
if (updatesEnabled !== undefined && !isUpdatesDirty()) {
|
|
setUpdatesUI?.(updatesEnabled);
|
|
}
|
|
updatesFlagEl?.(updatesEnabled === undefined ? null : updatesEnabled === true);
|
|
|
|
const cfg = data.updates_config || {};
|
|
const rebootReq = data.reboot_required;
|
|
setTempFlag(data.cpu_temp_c);
|
|
if (updatesNoteTop) {
|
|
updatesNoteTop.textContent = "";
|
|
updatesNoteTop.classList.remove("note-warn");
|
|
if (rebootReq) {
|
|
if (cfg.auto_reboot) {
|
|
updatesNoteTop.textContent = `Reboot scheduled at ${cfg.reboot_time || "02:00"}.`;
|
|
} else {
|
|
updatesNoteTop.textContent = "Reboot required. Please reboot when you can.";
|
|
updatesNoteTop.classList.add("note-warn");
|
|
}
|
|
}
|
|
}
|
|
if (readyOverlay) {
|
|
if (data.ready) {
|
|
readyOverlay.classList.add("hidden");
|
|
} else {
|
|
readyOverlay.classList.remove("hidden");
|
|
onReadyWait?.();
|
|
}
|
|
}
|
|
releaseUIGetter()?.refreshStatus();
|
|
} catch (e) {
|
|
console.error(e);
|
|
logUi?.(`Status refresh failed: ${e?.message || e}`, "error");
|
|
if (!lastStatusData) {
|
|
renderStats(heroStats, placeholderStatus);
|
|
}
|
|
setTimeout(loadStatus, 2000);
|
|
}
|
|
}
|
|
|
|
return { loadStatus, setTempFlag, updatesFlagEl };
|
|
}
|