mirror of
https://github.com/legop3/MultiRoombaRover.git
synced 2026-09-15 17:12:59 -04:00
glogar
This commit is contained in:
@@ -94,20 +94,24 @@ io.on('connection', (socket) => {
|
|||||||
throw new Error('Not your turn or no control');
|
throw new Error('Not your turn or no control');
|
||||||
}
|
}
|
||||||
const driveDirect = payload?.driveDirect;
|
const driveDirect = payload?.driveDirect;
|
||||||
if (type === 'drive' && driveDirect && !isAdminSocket) {
|
if (type === 'drive' && driveDirect) {
|
||||||
const safeDrive = roverManager.applyPrivateDriveSafety(roverId, socket, driveDirect);
|
if (!isAdminSocket) {
|
||||||
if (safeDrive) {
|
const safeDrive = roverManager.applyPrivateDriveSafety(roverId, socket, driveDirect);
|
||||||
payload.driveDirect = safeDrive;
|
if (safeDrive) {
|
||||||
|
payload.driveDirect = safeDrive;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
const left = Number(payload?.driveDirect?.left);
|
const left = Number(payload?.driveDirect?.left);
|
||||||
const right = Number(payload?.driveDirect?.right);
|
const right = Number(payload?.driveDirect?.right);
|
||||||
const speed = Math.max(Math.abs(left), Math.abs(right));
|
const speed = Math.max(Math.abs(left), Math.abs(right));
|
||||||
const blockedUntil = driveCooldowns.get(roverId);
|
if (!isAdminSocket) {
|
||||||
if (blockedUntil && Date.now() < blockedUntil && speed > 0) {
|
const blockedUntil = driveCooldowns.get(roverId);
|
||||||
const reason = isLockdownAdmin(socket)
|
if (blockedUntil && Date.now() < blockedUntil && speed > 0) {
|
||||||
? 'Drive blocked: cooldown'
|
const reason = isLockdownAdmin(socket)
|
||||||
: 'Drive blocked: safety cooldown';
|
? 'Drive blocked: cooldown'
|
||||||
throw new Error(reason);
|
: 'Drive blocked: safety cooldown';
|
||||||
|
throw new Error(reason);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if (speed > 0) {
|
if (speed > 0) {
|
||||||
let direction = 'turn';
|
let direction = 'turn';
|
||||||
|
|||||||
@@ -4,19 +4,32 @@
|
|||||||
const logger = require('../../globals/logger').child('idleService');
|
const logger = require('../../globals/logger').child('idleService');
|
||||||
const { getActiveDrivers, turnEvents } = require('../turnService');
|
const { getActiveDrivers, turnEvents } = require('../turnService');
|
||||||
const roverManager = require('../roverManager');
|
const roverManager = require('../roverManager');
|
||||||
|
const { getRecentDriveActivity } = require('../commandService');
|
||||||
const { IDLE_TIMEOUT_MS } = require('./constants');
|
const { IDLE_TIMEOUT_MS } = require('./constants');
|
||||||
const { runtime } = require('./state');
|
const { runtime } = require('./state');
|
||||||
const { runIdleActions } = require('./actions');
|
const { runIdleActions } = require('./actions');
|
||||||
|
|
||||||
function getActiveDriverCount() {
|
function getActivitySnapshot() {
|
||||||
const active = getActiveDrivers();
|
const active = getActiveDrivers();
|
||||||
const turnCount = active && typeof active === 'object' ? Object.keys(active).length : 0;
|
const turnCount = active && typeof active === 'object' ? Object.keys(active).length : 0;
|
||||||
if (turnCount > 0) return turnCount;
|
const activeByTurn = turnCount;
|
||||||
|
|
||||||
let liveCount = 0;
|
let liveCount = 0;
|
||||||
roverManager.rovers.forEach((record) => {
|
roverManager.rovers.forEach((record) => {
|
||||||
if (record?.drivers?.size > 0) liveCount += 1;
|
if (record?.drivers?.size > 0) liveCount += 1;
|
||||||
});
|
});
|
||||||
return liveCount;
|
const activeByRoverDrivers = liveCount;
|
||||||
|
|
||||||
|
const recentDriveEvents = getRecentDriveActivity(IDLE_TIMEOUT_MS, { excludeAdmins: false });
|
||||||
|
const activeByRecentDrive = recentDriveEvents.length;
|
||||||
|
|
||||||
|
const totalActive = Math.max(activeByTurn, activeByRoverDrivers, activeByRecentDrive);
|
||||||
|
return {
|
||||||
|
activeByTurn,
|
||||||
|
activeByRoverDrivers,
|
||||||
|
activeByRecentDrive,
|
||||||
|
totalActive,
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
function clearIdleTimer() {
|
function clearIdleTimer() {
|
||||||
@@ -33,7 +46,9 @@ function scheduleIdleTimer() {
|
|||||||
runtime.timer = setTimeout(async () => {
|
runtime.timer = setTimeout(async () => {
|
||||||
runtime.timer = null;
|
runtime.timer = null;
|
||||||
runtime.deadlineAt = null;
|
runtime.deadlineAt = null;
|
||||||
if (getActiveDriverCount() > 0) {
|
const activity = getActivitySnapshot();
|
||||||
|
if (activity.totalActive > 0) {
|
||||||
|
logger.info('Idle automation skipped; active control detected', activity);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
runtime.lastTriggeredAt = Date.now();
|
runtime.lastTriggeredAt = Date.now();
|
||||||
@@ -48,7 +63,8 @@ function scheduleIdleTimer() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function refreshIdleState() {
|
function refreshIdleState() {
|
||||||
if (getActiveDriverCount() > 0) {
|
const activity = getActivitySnapshot();
|
||||||
|
if (activity.totalActive > 0) {
|
||||||
clearIdleTimer();
|
clearIdleTimer();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user