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) => {
|
app.get('/api/turtle/:id/commands', (req, res) => {
|
||||||
try {
|
try {
|
||||||
const turtleID = parseInt(req.params.id);
|
const turtleID = parseInt(req.params.id);
|
||||||
|
const turtle = turtles.get(turtleID);
|
||||||
|
|
||||||
if (turtleData.has(turtleID)) {
|
if (turtle) {
|
||||||
const turtle = turtleData.get(turtleID);
|
// Combine eval commands + legacy commands
|
||||||
const commands = turtle.pendingCommands || [];
|
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();
|
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) {
|
if (turtle.pendingLegacyCommands.length > 0) {
|
||||||
console.log(`📤 Sending ${turtle.pendingCommands.length} command(s) to turtle ${turtleID}`);
|
console.log(`📤 Sending ${turtle.pendingLegacyCommands.length} command(s) to turtle ${turtleID}`);
|
||||||
turtle.pendingCommands.forEach(cmd => console.log(` - ${cmd.command}`, cmd.param ? `(${cmd.param})` : ''));
|
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 {
|
} else {
|
||||||
res.json({ commands: [] });
|
res.json({ commands: [] });
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user