From 5fb8ddf68e96fe37d4d03b8a210500d33ac8115b Mon Sep 17 00:00:00 2001 From: MayaTheShy Date: Fri, 20 Feb 2026 00:48:24 -0500 Subject: [PATCH] feat: Improve command handling for turtles; clean up old commands and enhance logging --- server/server.js | 25 ++++++++----------------- 1 file changed, 8 insertions(+), 17 deletions(-) diff --git a/server/server.js b/server/server.js index 6d24216..147724a 100644 --- a/server/server.js +++ b/server/server.js @@ -249,25 +249,16 @@ app.get('/api/turtle/:id/commands', (req, res) => { const turtle = turtleData.get(turtleID); const commands = turtle.pendingCommands || []; - if (commands.length > 0) { - console.log(`📤 Sending ${commands.length} command(s) to turtle ${turtleID}`); - commands.forEach(cmd => console.log(` - ${cmd.command}`, cmd.param ? `(${cmd.param})` : '')); + // Clean up old commands (older than 30 seconds - they're probably lost) + const now = Date.now(); + turtle.pendingCommands = commands.filter(cmd => (now - cmd.timestamp) < 30000); + + if (turtle.pendingCommands.length > 0) { + console.log(`📤 Sending ${turtle.pendingCommands.length} command(s) to turtle ${turtleID}`); + turtle.pendingCommands.forEach(cmd => console.log(` - ${cmd.command}`, cmd.param ? `(${cmd.param})` : '')); } - // Mark commands as sent but don't clear them yet - they'll be cleared on next poll if turtle is processing - // This allows the webbridge to retry if the turtle didn't receive them - if (!turtle.lastCommandPollTime || (Date.now() - turtle.lastCommandPollTime) > 5000) { - // First poll or more than 5 seconds since last poll - send commands - turtle.lastCommandPollTime = Date.now(); - res.json({ commands }); - } else { - // Recent poll - assume previous commands were received, clear them - if (commands.length > 0) { - console.log(`✅ Clearing ${commands.length} command(s) for turtle ${turtleID} (acknowledged)`); - turtle.pendingCommands = []; - } - res.json({ commands: [] }); - } + res.json({ commands: turtle.pendingCommands }); } else { res.json({ commands: [] }); }