data timers
This commit is contained in:
+16
-1
@@ -281,6 +281,19 @@
|
||||
const machineData = {}; // id → last data payload
|
||||
const wsStatus = {}; // id → 'connected' | 'error' | 'disconnected'
|
||||
const wsMap = {}; // id → WebSocket instance
|
||||
const dataTimers = {}; // id → stale-data timeout handle
|
||||
|
||||
// Jeśli przez DATA_STALE_MS nie przyjdzie żadna wiadomość od maszyny
|
||||
// (nawet przy technicznie otwartym WS), dane uznajemy za nieaktualne.
|
||||
const DATA_STALE_MS = 10 * 60 * 1000; // 10 minut
|
||||
|
||||
function resetStaleTimer(id) {
|
||||
clearTimeout(dataTimers[id]);
|
||||
dataTimers[id] = setTimeout(() => {
|
||||
delete machineData[id];
|
||||
updateDisplay();
|
||||
}, DATA_STALE_MS);
|
||||
}
|
||||
|
||||
// ── WebSocket management ──────────────────────────────────────────────────
|
||||
|
||||
@@ -304,7 +317,8 @@
|
||||
try {
|
||||
const msg = JSON.parse(ev.data);
|
||||
if (msg.data) {
|
||||
machineData[id] = { ...msg.data, _ts: msg.timestamp };
|
||||
machineData[id] = { ...msg.data, _ts: Date.now() };
|
||||
resetStaleTimer(id);
|
||||
updateDisplay();
|
||||
}
|
||||
} catch (e) {
|
||||
@@ -322,6 +336,7 @@
|
||||
// Usuwamy dane maszyny przy rozłączeniu - nie możemy potwierdzić
|
||||
// aktualnego stanu, więc nie trzymamy przestarzałego alarmu w cache.
|
||||
// Po ponownym połączeniu serwer wyśle aktualny stan.
|
||||
clearTimeout(dataTimers[id]);
|
||||
delete machineData[id];
|
||||
updateDisplay();
|
||||
refreshStatus();
|
||||
|
||||
Reference in New Issue
Block a user