"use strict";
|
|
class DataCache {
|
constructor() {
|
this._map = new Map();
|
this._onUpdate = null;
|
}
|
|
setUpdateListener(fn) {
|
this._onUpdate = fn;
|
}
|
|
update(ip, data) {
|
const existing = this._map.get(ip) || {};
|
this._map.set(ip, { ...existing, ...data });
|
if (this._onUpdate) {
|
this._onUpdate(ip, this._map.get(ip));
|
}
|
}
|
|
get(ip) {
|
return this._map.get(ip) || null;
|
}
|
|
getAll() {
|
const devices = [];
|
for (const [ip, data] of this._map) {
|
devices.push({ ip, ...data });
|
}
|
return devices;
|
}
|
|
getSummary() {
|
let total = 0;
|
let connected = 0;
|
let hasData = 0;
|
let abnormal = 0;
|
|
for (const [, data] of this._map) {
|
total++;
|
if (data.status === "connected") {
|
connected++;
|
if (data.lastDataAt) hasData++;
|
} else if (data.status === "disconnected" || data.status === "error") {
|
abnormal++;
|
}
|
}
|
|
return { total, connected, hasData, abnormal };
|
}
|
}
|
|
module.exports = { DataCache };
|