111 lines
3.0 KiB
JavaScript
111 lines
3.0 KiB
JavaScript
// app.js (оркестратор)
|
||
import { fetchHealth, fetchHistory } from "./data.js";
|
||
import { state, smooth, addSignal } from "./state.js";
|
||
import {
|
||
initDOM,
|
||
setStatus,
|
||
setMeta,
|
||
setBars,
|
||
setSignal,
|
||
DOM
|
||
} from "./ui.js";
|
||
|
||
import { initAccordion } from "./accordion.js";
|
||
import { drawChart } from "./chart.js";
|
||
|
||
let lastChart = 0;
|
||
let connectionLost = false;
|
||
|
||
// ================= INIT =================
|
||
window.addEventListener("DOMContentLoaded", () => {
|
||
initDOM();
|
||
initAccordion();
|
||
});
|
||
|
||
// ================= UPDATE =================
|
||
async function update() {
|
||
try {
|
||
const [h, hist] = await Promise.all([
|
||
fetchHealth(),
|
||
fetchHistory()
|
||
]);
|
||
|
||
// ===== STATUS =====
|
||
const isOnline = h.pipe_alive && h.has_data;
|
||
|
||
if (isOnline) {
|
||
setStatus("На связи");
|
||
connectionLost = false;
|
||
} else {
|
||
setStatus("Нет связи", true); // true = lost connection
|
||
connectionLost = true;
|
||
}
|
||
|
||
const arr = Array.isArray(hist.bytes) ? hist.bytes : [];
|
||
const raw = arr[arr.length - 1] ?? 0;
|
||
|
||
// ===== DECODE =====
|
||
const value = raw & 0x3F;
|
||
const level = (raw >> 6) & 0x3;
|
||
const percent = (value / 63) * 100;
|
||
|
||
// ===== STATE =====
|
||
addSignal(value);
|
||
|
||
// ===== UI =====
|
||
if (isOnline) {
|
||
setBars(smooth(percent));
|
||
setSignal(level);
|
||
} else {
|
||
// При потере связи - сбрасываем индикаторы
|
||
setBars(0, true); // true = no connection
|
||
setSignal(0, true); // true = no connection
|
||
}
|
||
|
||
if (DOM.lastByte) {
|
||
DOM.lastByte.textContent = isOnline ? value : "--";
|
||
}
|
||
|
||
// ===== META =====
|
||
const uptime = Math.round(h.uptime_sec || 0);
|
||
const filled = h.stats?.filled ?? 0;
|
||
const bps = Math.round(h.stats?.bits_per_sec ?? 0);
|
||
const Bps = Math.round(h.stats?.bytes_per_sec ?? 0);
|
||
const idle = Math.round((h.last_data_ms || 0) / 1000);
|
||
|
||
setMeta(
|
||
`работа: ${uptime}s | буфер: ${filled} | ${bps} bit/s (${Bps} B/s) | idle: ${idle}s`
|
||
);
|
||
|
||
// ===== HISTORY =====
|
||
if (DOM.history) {
|
||
DOM.history.innerHTML = arr
|
||
.map((b, i) =>
|
||
`[${i}] ${(b & 0xFF).toString(2).padStart(8, "0")} = ${b & 0xFF}`
|
||
)
|
||
.join("<br>");
|
||
}
|
||
|
||
// ===== CHART =====
|
||
const now = performance.now();
|
||
|
||
if (now - lastChart > 100 && DOM.canvas) {
|
||
drawChart(DOM.canvas, state.signalHistory);
|
||
lastChart = now;
|
||
}
|
||
|
||
} catch (e) {
|
||
console.error("UPDATE ERROR:", e);
|
||
setStatus("СЕРВЕР НЕДОСТУПЕН", true);
|
||
setBars(0, true);
|
||
setSignal(0, true);
|
||
|
||
if (DOM.lastByte) {
|
||
DOM.lastByte.textContent = "--";
|
||
}
|
||
}
|
||
}
|
||
|
||
// ================= LOOP =================
|
||
setInterval(update, 500);
|
||
update(); |