fix: Improve logging for command sending and REST API fallback in turtle store

This commit is contained in:
MayaTheShy
2026-02-16 01:27:54 -05:00
parent 1a415beac0
commit 9a02a8d27f

View File

@@ -75,6 +75,8 @@ export const useTurtleStore = create((set, get) => ({
sendCommand: async (turtleId, command, param = null) => {
const { ws } = get();
console.log(`🎮 Sending command to turtle ${turtleId}: ${command}`, param ? `(param: ${param})` : '');
if (ws && ws.readyState === WebSocket.OPEN) {
ws.send(JSON.stringify({
type: 'command',
@@ -82,16 +84,19 @@ export const useTurtleStore = create((set, get) => ({
command,
param
}));
console.log(' ✅ Sent via WebSocket');
} else {
// Fallback to REST API
console.log(' ⚠️ WebSocket not connected, using REST API fallback');
try {
await fetch(`${API_URL}/api/turtle/${turtleId}/command`, {
await fetch(`${API_URL}/turtle/${turtleId}/command`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ command, param })
});
console.log(' ✅ Sent via REST API');
} catch (error) {
console.error('Error sending command:', error);
console.error('Error sending command:', error);
}
}
},