This commit is contained in:
legop3
2026-04-29 19:31:51 -04:00
parent 9e24ed2ddf
commit c31fffec20
2 changed files with 35 additions and 15 deletions
+14 -10
View File
@@ -94,20 +94,24 @@ io.on('connection', (socket) => {
throw new Error('Not your turn or no control');
}
const driveDirect = payload?.driveDirect;
if (type === 'drive' && driveDirect && !isAdminSocket) {
const safeDrive = roverManager.applyPrivateDriveSafety(roverId, socket, driveDirect);
if (safeDrive) {
payload.driveDirect = safeDrive;
if (type === 'drive' && driveDirect) {
if (!isAdminSocket) {
const safeDrive = roverManager.applyPrivateDriveSafety(roverId, socket, driveDirect);
if (safeDrive) {
payload.driveDirect = safeDrive;
}
}
const left = Number(payload?.driveDirect?.left);
const right = Number(payload?.driveDirect?.right);
const speed = Math.max(Math.abs(left), Math.abs(right));
const blockedUntil = driveCooldowns.get(roverId);
if (blockedUntil && Date.now() < blockedUntil && speed > 0) {
const reason = isLockdownAdmin(socket)
? 'Drive blocked: cooldown'
: 'Drive blocked: safety cooldown';
throw new Error(reason);
if (!isAdminSocket) {
const blockedUntil = driveCooldowns.get(roverId);
if (blockedUntil && Date.now() < blockedUntil && speed > 0) {
const reason = isLockdownAdmin(socket)
? 'Drive blocked: cooldown'
: 'Drive blocked: safety cooldown';
throw new Error(reason);
}
}
if (speed > 0) {
let direction = 'turn';
+21 -5
View File
@@ -4,19 +4,32 @@
const logger = require('../../globals/logger').child('idleService');
const { getActiveDrivers, turnEvents } = require('../turnService');
const roverManager = require('../roverManager');
const { getRecentDriveActivity } = require('../commandService');
const { IDLE_TIMEOUT_MS } = require('./constants');
const { runtime } = require('./state');
const { runIdleActions } = require('./actions');
function getActiveDriverCount() {
function getActivitySnapshot() {
const active = getActiveDrivers();
const turnCount = active && typeof active === 'object' ? Object.keys(active).length : 0;
if (turnCount > 0) return turnCount;
const activeByTurn = turnCount;
let liveCount = 0;
roverManager.rovers.forEach((record) => {
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() {
@@ -33,7 +46,9 @@ function scheduleIdleTimer() {
runtime.timer = setTimeout(async () => {
runtime.timer = null;
runtime.deadlineAt = null;
if (getActiveDriverCount() > 0) {
const activity = getActivitySnapshot();
if (activity.totalActive > 0) {
logger.info('Idle automation skipped; active control detected', activity);
return;
}
runtime.lastTriggeredAt = Date.now();
@@ -48,7 +63,8 @@ function scheduleIdleTimer() {
}
function refreshIdleState() {
if (getActiveDriverCount() > 0) {
const activity = getActivitySnapshot();
if (activity.totalActive > 0) {
clearIdleTimer();
return;
}