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 turtle = turtleData.get(turtleID);
|
||||||
const commands = turtle.pendingCommands || [];
|
const commands = turtle.pendingCommands || [];
|
||||||
|
|
||||||
if (commands.length > 0) {
|
// Clean up old commands (older than 30 seconds - they're probably lost)
|
||||||
console.log(`📤 Sending ${commands.length} command(s) to turtle ${turtleID}`);
|
const now = Date.now();
|
||||||
commands.forEach(cmd => console.log(` - ${cmd.command}`, cmd.param ? `(${cmd.param})` : ''));
|
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
|
res.json({ commands: turtle.pendingCommands });
|
||||||
// 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: [] });
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
res.json({ commands: [] });
|
res.json({ commands: [] });
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user