unlock on waiting and ping on unlock

This commit is contained in:
legop3
2025-11-26 23:29:24 -05:00
parent 0e391ef26d
commit 23858b1b6a
2 changed files with 15 additions and 1 deletions
+14 -1
View File
@@ -12,6 +12,8 @@ const STATES = {
LOCKED: 'locked', LOCKED: 'locked',
}; };
const WAITING_UNLOCK_MS = 5 * 60 * 1000;
const roverState = new Map(); // roverId -> state snapshot const roverState = new Map(); // roverId -> state snapshot
function getState(roverId) { function getState(roverId) {
@@ -24,6 +26,7 @@ function getState(roverId) {
dockedCharging: false, dockedCharging: false,
charging: false, charging: false,
batteryLocked: false, batteryLocked: false,
waitingSince: null,
}); });
} }
return roverState.get(roverId); return roverState.get(roverId);
@@ -127,7 +130,15 @@ function handleSensorEvent({ roverId, sensors, batteryState }) {
} }
const waitingState = chargingState === 4; const waitingState = chargingState === 4;
const shouldUnlock = state.batteryLocked && (!dockedCharging || (isFull && waitingState)); if (waitingState && state.waitingSince == null) {
state.waitingSince = Date.now();
} else if (!waitingState && state.waitingSince != null) {
state.waitingSince = null;
}
const waitingLongEnough =
waitingState && state.waitingSince != null && Date.now() - state.waitingSince >= WAITING_UNLOCK_MS;
const shouldUnlock = state.batteryLocked && (!dockedCharging || waitingLongEnough);
if (shouldUnlock) { if (shouldUnlock) {
logger.info('Unlocking rover after charge', { logger.info('Unlocking rover after charge', {
@@ -135,11 +146,13 @@ function handleSensorEvent({ roverId, sensors, batteryState }) {
isFull, isFull,
docked: dockedCharging, docked: dockedCharging,
waitingState, waitingState,
waitedMs: state.waitingSince ? Date.now() - state.waitingSince : null,
}); });
lockRover(roverId, false, { reason: 'battery' }); lockRover(roverId, false, { reason: 'battery' });
state.batteryLocked = false; state.batteryLocked = false;
state.warned = false; state.warned = false;
state.urgent = false; state.urgent = false;
state.waitingSince = null;
publishEvent({ publishEvent({
source: 'batteryManager', source: 'batteryManager',
type: 'battery.unlocked', type: 'battery.unlocked',
+1
View File
@@ -289,6 +289,7 @@ function handleBusEvent(event) {
case 'rover.unlocked': case 'rover.unlocked':
announce({ announce({
channelId: channels.announcements, channelId: channels.announcements,
pingRoleId: roles.announcementPing || null,
color: 0x4caf50, color: 0x4caf50,
title: 'Rover Unlocked', title: 'Rover Unlocked',
description: `${payload?.roverId} unlocked.`, description: `${payload?.roverId} unlocked.`,