roverstate yay yay. future implements for non-roomba rovers...
Container image / image (push) Failing after 54s

This commit is contained in:
legop3
2026-11-16 18:17:22 -05:00
parent 9b6bdec116
commit b0de727d3e
17 changed files with 144 additions and 166 deletions
+1 -1
View File
@@ -120,7 +120,7 @@ module.exports = {
const prevHeadlights = {};
ctx.listOnlineRovers().forEach((rover) => {
const state = rover?.headlight?.state;
const state = rover?.roverState;
const headlightOn = Boolean(state && state.headlightOn === true);
prevHeadlights[String(rover.id)] = headlightOn;
try {
@@ -8,39 +8,8 @@ const { sendAlert } = require('../alertService');
const ALERT_COLOR = '#00bcd4';
const { handleAck } = require('../commandService');
function coerceBool(value) {
if (typeof value === 'boolean') return value;
if (typeof value === 'number') return value !== 0;
if (typeof value === 'string') {
const normalized = value.trim().toLowerCase();
if (normalized === 'true') return true;
if (normalized === 'false') return false;
if (normalized === '1') return true;
if (normalized === '0') return false;
}
return null;
}
const HEARTBEAT_INTERVAL_MS = 15000;
function handleToggleEvent(roverId, msg) {
if (msg.event === 'headlight.state') {
const headlightOn = coerceBool(msg.data?.headlightOn);
if (headlightOn != null) {
roverManager.setToggleState(roverId, 'headlight', headlightOn);
return true;
}
}
if (msg.event === 'laser.state') {
const laserOn = coerceBool(msg.data?.laserOn);
if (laserOn != null) {
roverManager.setToggleState(roverId, 'laser', laserOn);
return true;
}
}
return false;
}
roverWSS.on('connection', (ws) => {
let roverId = null;
ws.isAlive = true;
@@ -80,19 +49,19 @@ roverWSS.on('connection', (ws) => {
sendAlert({ color: ALERT_COLOR, title: 'Rover Online', message: roverId });
return;
}
if (!roverId) return;
if (!roverId || roverManager.rovers.get(roverId)?.ws !== ws) return;
if (msg.type === 'sensor') {
roverManager.handleSensorFrame(roverId, msg);
} else if (msg.type === 'ack') {
handleAck(msg);
} else if (msg.type === 'event') {
handleToggleEvent(roverId, msg);
} else if (msg.type === 'roverState') {
roverManager.setRoverState(roverId, msg.state);
}
});
ws.on('close', () => {
clearInterval(heartbeat);
if (roverId) {
if (roverId && roverManager.rovers.get(roverId)?.ws === ws) {
roverManager.removeRover(roverId);
sendAlert({ color: ALERT_COLOR, title: 'Rover Offline', message: roverId });
}
@@ -116,8 +116,7 @@ function createRuntime({ config, logger }) {
// Do not serialize the live record: it contains sockets and circular state.
return {
meta: record.meta, sensors: record.lastSensor, hostStats: record.lastHostStats,
batteryState: record.batteryState, headlightState: record.headlightState,
laserState: record.laserState, drivers: [...(record.drivers || [])],
batteryState: record.batteryState, roverState: record.roverState, drivers: [...(record.drivers || [])],
locked: record.locked, privateOpen: record.privateOpen,
};
}
+2 -2
View File
@@ -134,7 +134,7 @@ const {
getRosterForSocket,
broadcastRoster,
setNeedsHelp,
setToggleState,
setRoverState,
handleHostStats,
canSeeRover,
canRequestControl,
@@ -284,7 +284,7 @@ module.exports = {
getRosterForSocket,
broadcastRoster,
setNeedsHelp,
setToggleState,
setRoverState,
handleHostStats,
handleSensorFrame,
requestControl,
@@ -41,8 +41,7 @@ function createRosterLifecycle(deps) {
locked: false,
lockReason: null,
batteryState: null,
headlightState: null,
laserState: null,
roverState: null,
room: `rover:${id}`,
lastSeen: Date.now(),
lastMovementAt: Date.now(),
@@ -79,22 +78,7 @@ function createRosterLifecycle(deps) {
} else {
record.privateOpen = true;
}
if (!meta?.headlight?.enabled) {
record.headlightState = null;
} else if (record.headlightState == null) {
record.headlightState = {
headlightOn: Boolean(meta.headlight.initialOn),
updatedAt: Date.now(),
};
}
if (!meta?.laser?.enabled) {
record.laserState = null;
} else if (record.laserState == null) {
record.laserState = {
laserOn: Boolean(meta.laser.initialOn),
updatedAt: Date.now(),
};
}
record.roverState = null;
rovers.set(id, record);
spectatorSockets.forEach((socketId) => {
const sock = io.sockets.sockets.get(socketId);
@@ -118,6 +102,7 @@ function createRosterLifecycle(deps) {
it can clean the reverse membership index and Socket.IO room membership.
*/
require('../dockingService').disconnect(record);
record.roverState = null;
rovers.delete(id);
removeRoverDrivers(id, record);
stopDockGuard(id);
@@ -249,12 +234,9 @@ function createRosterLifecycle(deps) {
// controls. Preserve that order here instead of rebuilding the inventory,
// because ESP32 registration order is also the driver's display order.
peripherals: Array.isArray(record.meta?.peripherals) ? record.meta.peripherals : [],
headlight: record.meta?.headlight
? { ...record.meta.headlight, state: record.headlightState }
: record.meta?.headlight,
laser: record.meta?.laser
? { ...record.meta.laser, state: record.laserState }
: record.meta?.laser,
headlight: record.meta?.headlight,
laser: record.meta?.laser,
roverState: record.roverState,
locked: record.locked || (isPrivateRecord(record) && !isPrivateOpen(record)),
lockReason: record.lockReason || (isPrivateRecord(record) && !isPrivateOpen(record) ? 'private' : null),
needsHelp: Boolean(record.needsHelp),
@@ -290,19 +272,14 @@ function createRosterLifecycle(deps) {
});
}
function setToggleState(roverId, device, on) {
function setRoverState(roverId, state) {
const record = rovers.get(roverId);
if (!record) return;
if (typeof on !== 'boolean') return;
if (device !== 'headlight' && device !== 'laser') return;
const stateKey = device === 'headlight' ? 'headlightState' : 'laserState';
const onKey = `${device}On`;
record[stateKey] = {
[onKey]: on,
updatedAt: Date.now(),
};
if (!record || !state || typeof state !== 'object' || Array.isArray(state)) return;
if (!['headlightOn', 'laserOn'].every((key) => Object.hasOwn(state, key)
&& (state[key] === null || typeof state[key] === 'boolean'))) return;
record.roverState = state;
broadcastRoster();
managerEvents.emit('rover', { roverId, action: device, record });
managerEvents.emit('rover', { roverId, action: 'roverState', record });
}
function setNeedsHelp(roverId, needsHelp) {
@@ -367,7 +344,7 @@ function createRosterLifecycle(deps) {
syncSpectatorRooms,
broadcastRoster,
setNeedsHelp,
setToggleState,
setRoverState,
handleHostStats,
canSeeRover,
canRequestControl,
@@ -12,8 +12,7 @@ function createRosterManager(meta) {
id: meta.name,
meta,
batteryState: null,
headlightState: null,
laserState: null,
roverState: null,
locked: false,
lockReason: null,
lastSeen: 123,