feat: Update WebSocket handling for turtle commands and status updates, transitioning to Turtle instance methods for state management
This commit is contained in:
@@ -220,7 +220,7 @@ wss.on('connection', (ws) => {
|
|||||||
|
|
||||||
ws.send(JSON.stringify({
|
ws.send(JSON.stringify({
|
||||||
type: 'initial_state',
|
type: 'initial_state',
|
||||||
turtles: Array.from(turtleData.values()),
|
turtles: Array.from(turtles.values()).map(t => t.toJSON()),
|
||||||
blocks: blocks
|
blocks: blocks
|
||||||
}));
|
}));
|
||||||
|
|
||||||
@@ -231,21 +231,28 @@ wss.on('connection', (ws) => {
|
|||||||
|
|
||||||
// Handle commands from web client
|
// Handle commands from web client
|
||||||
if (data.type === 'command') {
|
if (data.type === 'command') {
|
||||||
// Store command for turtle to poll
|
|
||||||
const turtleID = data.turtleID;
|
const turtleID = data.turtleID;
|
||||||
if (turtleData.has(turtleID)) {
|
const turtle = turtles.get(turtleID);
|
||||||
const turtle = turtleData.get(turtleID);
|
|
||||||
turtle.pendingCommands = turtle.pendingCommands || [];
|
if (turtle) {
|
||||||
turtle.pendingCommands.push({
|
// Check for state change commands
|
||||||
command: data.command,
|
if (data.command === 'set_state' || data.command === 'setState') {
|
||||||
param: data.param,
|
const stateName = data.param?.state || data.param;
|
||||||
timestamp: Date.now()
|
const stateData = data.param?.data || {};
|
||||||
});
|
turtle.setState(stateName, stateData);
|
||||||
console.log(`✅ Command queued for turtle ${turtleID}: ${data.command}`, data.param ? `(param: ${data.param})` : '');
|
console.log(`✅ State set for turtle ${turtleID}: ${stateName}`);
|
||||||
console.log(` Pending commands: ${turtle.pendingCommands.length}`);
|
} else {
|
||||||
|
// Legacy command - queue for webbridge polling
|
||||||
|
turtle.pendingLegacyCommands.push({
|
||||||
|
command: data.command,
|
||||||
|
param: data.param,
|
||||||
|
timestamp: Date.now()
|
||||||
|
});
|
||||||
|
console.log(`✅ Command queued for turtle ${turtleID}: ${data.command}`, data.param ? `(param: ${JSON.stringify(data.param)})` : '');
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
console.log(`❌ Turtle ${turtleID} not found in turtleData`);
|
console.log(`❌ Turtle ${turtleID} not found`);
|
||||||
console.log(` Available turtles:`, Array.from(turtleData.keys()));
|
console.log(` Available turtles:`, Array.from(turtles.keys()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
@@ -271,25 +278,22 @@ app.post('/api/turtle/update', (req, res) => {
|
|||||||
|
|
||||||
console.log(`🐢 Status update from Turtle ${turtleID}`);
|
console.log(`🐢 Status update from Turtle ${turtleID}`);
|
||||||
|
|
||||||
// Update turtle data
|
// Get or create Turtle instance
|
||||||
if (!turtleData.has(turtleID)) {
|
const turtle = getOrCreateTurtle(turtleID);
|
||||||
console.log(`✨ New turtle registered: ${turtleID}`);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Merge with existing data, keeping server's home position if it exists
|
// Update turtle from status data
|
||||||
const existingData = turtleData.get(turtleID) || {};
|
turtle.updateFromStatus(turtleUpdate);
|
||||||
|
|
||||||
|
// Server's home takes precedence
|
||||||
const serverHome = turtleHomes.get(turtleID);
|
const serverHome = turtleHomes.get(turtleID);
|
||||||
|
if (serverHome) {
|
||||||
turtleData.set(turtleID, {
|
turtle.homePosition = serverHome;
|
||||||
...existingData,
|
}
|
||||||
...turtleUpdate,
|
|
||||||
homePosition: serverHome || turtleUpdate.homePosition, // Server's home takes precedence
|
|
||||||
lastUpdate: Date.now()
|
|
||||||
});
|
|
||||||
|
|
||||||
// If turtle reports a home but server doesn't have one, store it
|
// If turtle reports a home but server doesn't have one, store it
|
||||||
if (turtleUpdate.homePosition && !serverHome) {
|
if (turtleUpdate.homePosition && !serverHome) {
|
||||||
turtleHomes.set(turtleID, turtleUpdate.homePosition);
|
turtleHomes.set(turtleID, turtleUpdate.homePosition);
|
||||||
|
turtle.homePosition = turtleUpdate.homePosition;
|
||||||
console.log(` 📍 Stored home position for turtle ${turtleID}:`, turtleUpdate.homePosition);
|
console.log(` 📍 Stored home position for turtle ${turtleID}:`, turtleUpdate.homePosition);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -306,7 +310,7 @@ app.post('/api/turtle/update', (req, res) => {
|
|||||||
// Broadcast to web clients
|
// Broadcast to web clients
|
||||||
broadcastToClients({
|
broadcastToClients({
|
||||||
type: 'turtle_update',
|
type: 'turtle_update',
|
||||||
turtle: turtleData.get(turtleID)
|
turtle: turtle.toJSON()
|
||||||
});
|
});
|
||||||
|
|
||||||
// Send back server-authoritative data
|
// Send back server-authoritative data
|
||||||
|
|||||||
Reference in New Issue
Block a user