first lol

This commit is contained in:
legop3
2025-11-15 04:05:01 -05:00
parent 4d3bc09e2b
commit d64cfe2d46
42 changed files with 5011 additions and 880 deletions
+10 -2
View File
@@ -1,6 +1,7 @@
const { v4: uuidv4 } = require('uuid');
const io = require('../globals/io');
const roverManager = require('./roverManager');
const logger = require('../globals/logger').child('commandService');
const pendingCommands = new Map(); // id -> { roverId }
@@ -13,6 +14,7 @@ function issueCommand(roverId, payload) {
const message = { ...payload, id };
record.ws.send(JSON.stringify(message));
pendingCommands.set(id, { roverId, ts: Date.now(), type: payload.type });
logger.info('Issued command', roverId, payload.type, id);
return id;
}
@@ -20,6 +22,7 @@ function handleAck(msg) {
const pending = pendingCommands.get(msg.id);
if (!pending) return;
pendingCommands.delete(msg.id);
logger.info('Command acknowledged', pending.roverId, pending.type, msg.status);
io.emit('commandAck', {
roverId: pending.roverId,
id: msg.id,
@@ -34,7 +37,7 @@ module.exports = {
};
io.on('connection', (socket) => {
socket.on('command', ({ roverId, type, data } = {}, cb = () => {}) => {
function handleCommand({ roverId, type, data } = {}, cb = () => {}) {
try {
if (!roverId) {
throw new Error('roverId required');
@@ -44,9 +47,14 @@ io.on('connection', (socket) => {
}
const payload = data ? { ...data } : {};
const id = issueCommand(roverId, { type, ...payload });
logger.info('Queued command', socket.id, roverId, type);
cb({ id });
} catch (err) {
logger.warn('Command rejected', socket.id, err.message);
cb({ error: err.message });
}
});
}
socket.on('command', handleCommand);
socket.on('command:issue', handleCommand);
});