refactor: replace legacy command handling with unified pending command queue

This commit is contained in:
MayaTheShy
2026-02-20 03:44:44 -05:00
parent bad3b5bf13
commit 2549adc49d

View File

@@ -114,7 +114,7 @@ function getOrCreateTurtle(turtleID) {
// Forward eval commands to webbridge via pending command queue // Forward eval commands to webbridge via pending command queue
turtle.on('sendCommand', (command) => { turtle.on('sendCommand', (command) => {
// Queue eval command for webbridge to poll // Queue eval command for webbridge to poll
turtle.pendingLegacyCommands.push({ turtle.pendingCommands.push({
...command, ...command,
timestamp: Date.now(), timestamp: Date.now(),
}); });
@@ -341,15 +341,15 @@ app.get('/api/turtle/:id/commands', (req, res) => {
if (turtle) { if (turtle) {
// Combine eval commands + legacy commands // Combine eval commands + legacy commands
const commands = turtle.pendingLegacyCommands || []; const commands = turtle.pendingCommands || [];
// Clean up old commands (older than 30 seconds) // Clean up old commands (older than 30 seconds)
const now = Date.now(); const now = Date.now();
turtle.pendingLegacyCommands = commands.filter(cmd => (now - cmd.timestamp) < 30000); turtle.pendingCommands = commands.filter(cmd => (now - cmd.timestamp) < 30000);
if (turtle.pendingLegacyCommands.length > 0) { if (turtle.pendingCommands.length > 0) {
console.log(`📤 Sending ${turtle.pendingLegacyCommands.length} command(s) to turtle ${turtleID}`); console.log(`📤 Sending ${turtle.pendingCommands.length} command(s) to turtle ${turtleID}`);
turtle.pendingLegacyCommands.forEach(cmd => { turtle.pendingCommands.forEach(cmd => {
if (cmd.type === 'eval') { if (cmd.type === 'eval') {
console.log(` - EVAL ${(cmd.uuid || '').substring(0, 8)}`); console.log(` - EVAL ${(cmd.uuid || '').substring(0, 8)}`);
} else { } else {
@@ -358,7 +358,7 @@ app.get('/api/turtle/:id/commands', (req, res) => {
}); });
} }
res.json({ commands: turtle.pendingLegacyCommands }); res.json({ commands: turtle.pendingCommands });
} else { } else {
res.json({ commands: [] }); res.json({ commands: [] });
} }
@@ -375,11 +375,11 @@ app.post('/api/turtle/:id/commands/ack', (req, res) => {
const turtle = turtles.get(turtleID); const turtle = turtles.get(turtleID);
if (turtle) { if (turtle) {
const clearedCount = (turtle.pendingLegacyCommands || []).length; const clearedCount = (turtle.pendingCommands || []).length;
if (clearedCount > 0) { if (clearedCount > 0) {
console.log(`✅ Turtle ${turtleID} acknowledged ${clearedCount} command(s)`); console.log(`✅ Turtle ${turtleID} acknowledged ${clearedCount} command(s)`);
turtle.pendingLegacyCommands = []; turtle.pendingCommands = [];
} }
res.json({ success: true, cleared: clearedCount }); res.json({ success: true, cleared: clearedCount });