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