feat: Update command polling endpoint to support combined eval and legacy commands for turtles
This commit is contained in:
@@ -325,25 +325,32 @@ app.post('/api/turtle/update', (req, res) => {
|
||||
}
|
||||
});
|
||||
|
||||
// Endpoint for turtles to poll for commands
|
||||
// Endpoint for webbridge to poll for commands (includes eval + legacy)
|
||||
app.get('/api/turtle/:id/commands', (req, res) => {
|
||||
try {
|
||||
const turtleID = parseInt(req.params.id);
|
||||
const turtle = turtles.get(turtleID);
|
||||
|
||||
if (turtleData.has(turtleID)) {
|
||||
const turtle = turtleData.get(turtleID);
|
||||
const commands = turtle.pendingCommands || [];
|
||||
if (turtle) {
|
||||
// Combine eval commands + legacy commands
|
||||
const commands = turtle.pendingLegacyCommands || [];
|
||||
|
||||
// Clean up old commands (older than 30 seconds - they're probably lost)
|
||||
// Clean up old commands (older than 30 seconds)
|
||||
const now = Date.now();
|
||||
turtle.pendingCommands = commands.filter(cmd => (now - cmd.timestamp) < 30000);
|
||||
turtle.pendingLegacyCommands = 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})` : ''));
|
||||
if (turtle.pendingLegacyCommands.length > 0) {
|
||||
console.log(`📤 Sending ${turtle.pendingLegacyCommands.length} command(s) to turtle ${turtleID}`);
|
||||
turtle.pendingLegacyCommands.forEach(cmd => {
|
||||
if (cmd.type === 'eval') {
|
||||
console.log(` - EVAL ${(cmd.uuid || '').substring(0, 8)}`);
|
||||
} else {
|
||||
console.log(` - ${cmd.command}`, cmd.param ? `(${JSON.stringify(cmd.param)})` : '');
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
res.json({ commands: turtle.pendingCommands });
|
||||
res.json({ commands: turtle.pendingLegacyCommands });
|
||||
} else {
|
||||
res.json({ commands: [] });
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user