feat: Improve command handling for turtles; clean up old commands and enhance logging
This commit is contained in:
@@ -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: [] });
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user