mirror of
https://github.com/legop3/MultiRoombaRover.git
synced 2026-09-17 10:00:46 -04:00
unlock on waiting and ping on unlock
This commit is contained in:
@@ -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',
|
||||||
|
|||||||
@@ -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.`,
|
||||||
|
|||||||
Reference in New Issue
Block a user