This commit is contained in:
legop3
2025-12-03 00:48:25 -05:00
parent e94a52d7fd
commit e0312c3fbb
8 changed files with 22 additions and 25 deletions
@@ -4,20 +4,14 @@ const { getRoomCameras, roomCameraEvents } = require('./roomCameraService');
const POLL_INTERVAL_MS = 800;
const FETCH_TIMEOUT_MS = 2000;
const STALE_AFTER_MS = 8000;
const cameraState = new Map(); // id -> {frame, ts, stale, error, failures, fetching}
const cameraState = new Map(); // id -> {frame, ts, error, failures, fetching}
const events = new EventEmitter(); // frame, status
let pollTimer = null;
function markState(id, updates = {}) {
const prev = cameraState.get(id) || {};
const next = { ...prev, ...updates };
if (next.ts != null) {
next.stale = Date.now() - next.ts > STALE_AFTER_MS || !!next.stale;
} else {
next.stale = true;
}
cameraState.set(id, next);
return next;
}
@@ -37,8 +31,8 @@ async function fetchSnapshot(camera) {
const arrayBuffer = await res.arrayBuffer();
const buffer = Buffer.from(arrayBuffer);
const ts = Date.now();
const next = markState(id, { frame: buffer, ts, error: null, failures: 0 });
events.emit('frame', { id, buffer, ts, stale: !!next.stale });
markState(id, { frame: buffer, ts, error: null, failures: 0 });
events.emit('frame', { id, buffer, ts });
} catch (err) {
const failures = (state?.failures || 0) + 1;
markState(id, { error: err.message, failures });
@@ -70,11 +64,9 @@ function startAll() {
function getState(id) {
const state = cameraState.get(id);
if (!state) return null;
const stale = state.ts == null || Date.now() - state.ts > STALE_AFTER_MS;
return {
frame: state.frame || null,
ts: state.ts || null,
stale,
error: state.error || null,
};
}