mirror of
https://github.com/legop3/MultiRoombaRover.git
synced 2026-09-16 09:31:20 -04:00
gahwa
This commit is contained in:
@@ -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,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -82,13 +82,13 @@ function sendStatus(socket, cameraId, status) {
|
||||
socket.emit('roomCamera:status', { id: cameraId, ...status });
|
||||
}
|
||||
|
||||
roomCameraStreamEvents.on('frame', ({ id, buffer, ts, stale }) => {
|
||||
roomCameraStreamEvents.on('frame', ({ id, buffer, ts }) => {
|
||||
const bucket = cameraSubscribers.get(id);
|
||||
if (!bucket || !buffer) return;
|
||||
bucket.forEach((socketId) => {
|
||||
const socket = io.sockets.sockets.get(socketId);
|
||||
if (!socket) return;
|
||||
sendFrame(socket, id, { ts, stale: !!stale }, buffer);
|
||||
sendFrame(socket, id, { ts }, buffer);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -123,11 +123,10 @@ io.on('connection', (socket) => {
|
||||
validIds.forEach((cameraId) => {
|
||||
const state = getRoomCameraState(cameraId);
|
||||
if (state?.frame) {
|
||||
sendFrame(socket, cameraId, { ts: state.ts, stale: !!state.stale }, state.frame);
|
||||
sendFrame(socket, cameraId, { ts: state.ts }, state.frame);
|
||||
}
|
||||
sendStatus(socket, cameraId, {
|
||||
ts: state?.ts || null,
|
||||
stale: state?.stale ?? true,
|
||||
error: state?.error || null,
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user