56 lines
1.5 KiB
JavaScript
56 lines
1.5 KiB
JavaScript
import { dom } from "./dom.js";
|
|
import { formatValue } from "./points.js";
|
|
import { state } from "./state.js";
|
|
|
|
export function appendLog(line) {
|
|
const div = document.createElement("div");
|
|
div.className = "log-line";
|
|
div.textContent = line;
|
|
dom.logView.appendChild(div);
|
|
dom.logView.scrollTop = dom.logView.scrollHeight;
|
|
}
|
|
|
|
export function startLogs() {
|
|
if (state.logSource) {
|
|
state.logSource.close();
|
|
}
|
|
|
|
state.logSource = new EventSource("/api/logs/stream");
|
|
state.logSource.addEventListener("log", (event) => {
|
|
const data = JSON.parse(event.data);
|
|
(data.lines || []).forEach(appendLog);
|
|
});
|
|
}
|
|
|
|
export function startPointSocket() {
|
|
const protocol = location.protocol === "https:" ? "wss" : "ws";
|
|
const ws = new WebSocket(`${protocol}://${location.host}/ws/public`);
|
|
state.pointSocket = ws;
|
|
|
|
ws.onmessage = (event) => {
|
|
try {
|
|
const payload = JSON.parse(event.data);
|
|
if (payload.type !== "PointNewValue" && payload.type !== "point_new_value") {
|
|
return;
|
|
}
|
|
|
|
const data = payload.data;
|
|
const entry = state.pointEls.get(data.point_id);
|
|
if (!entry) {
|
|
return;
|
|
}
|
|
|
|
entry.value.textContent = formatValue(data);
|
|
entry.quality.className = `badge quality-${(data.quality || "unknown").toLowerCase()}`;
|
|
entry.quality.textContent = (data.quality || "unknown").toUpperCase();
|
|
entry.time.textContent = data.timestamp || "--";
|
|
} catch {
|
|
// ignore malformed messages
|
|
}
|
|
};
|
|
|
|
ws.onclose = () => {
|
|
window.setTimeout(startPointSocket, 2000);
|
|
};
|
|
}
|