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