Spaces:
Sleeping
Sleeping
| /** | |
| * Basic Consumer Example - LeRobot Arena | |
| * | |
| * This example demonstrates: | |
| * - Connecting to an existing room as a consumer | |
| * - Receiving joint updates and state sync | |
| * - Setting up callbacks | |
| * - Getting current state | |
| */ | |
| import { RoboticsConsumer } from '../dist/robotics/index.js'; | |
| import { createInterface } from 'readline'; | |
| async function main() { | |
| console.log('π€ LeRobot Arena Basic Consumer Example π€'); | |
| // Get room ID from user | |
| const rl = createInterface({ | |
| input: process.stdin, | |
| output: process.stdout | |
| }); | |
| const roomId = await new Promise((resolve) => { | |
| rl.question('Enter room ID to connect to: ', (answer) => { | |
| rl.close(); | |
| resolve(answer.trim()); | |
| }); | |
| }); | |
| if (!roomId) { | |
| console.error('β Room ID is required!'); | |
| return; | |
| } | |
| // Create consumer client | |
| const consumer = new RoboticsConsumer('http://localhost:8000'); | |
| // Track received updates | |
| let updateCount = 0; | |
| let stateCount = 0; | |
| const receivedUpdates = []; | |
| const receivedStates = []; | |
| // Set up callbacks | |
| consumer.onJointUpdate((joints) => { | |
| updateCount++; | |
| console.log(`π₯ [${updateCount}] Joint update: ${joints.length} joints`); | |
| // Show joint details | |
| joints.forEach(joint => { | |
| console.log(` ${joint.name}: ${joint.value}${joint.speed ? ` (speed: ${joint.speed})` : ''}`); | |
| }); | |
| receivedUpdates.push(joints); | |
| }); | |
| consumer.onStateSync((state) => { | |
| stateCount++; | |
| console.log(`π [${stateCount}] State sync: ${Object.keys(state).length} joints`); | |
| // Show state details | |
| Object.entries(state).forEach(([name, value]) => { | |
| console.log(` ${name}: ${value}`); | |
| }); | |
| receivedStates.push(state); | |
| }); | |
| consumer.onError((errorMsg) => { | |
| console.error('β Consumer error:', errorMsg); | |
| }); | |
| consumer.onConnected(() => { | |
| console.log('β Consumer connected!'); | |
| }); | |
| consumer.onDisconnected(() => { | |
| console.log('π Consumer disconnected!'); | |
| }); | |
| try { | |
| // Connect to the room | |
| console.log(`\nπ Connecting to room ${roomId}...`); | |
| const success = await consumer.connect(roomId, 'demo-consumer'); | |
| if (!success) { | |
| console.error('β Failed to connect to room!'); | |
| console.log('π‘ Make sure the room exists and the server is running'); | |
| return; | |
| } | |
| console.log(`β Connected to room ${roomId} as consumer`); | |
| // Show connection info | |
| const info = consumer.getConnectionInfo(); | |
| console.log('\nπ Connection Info:'); | |
| console.log(` Room ID: ${info.room_id}`); | |
| console.log(` Role: ${info.role}`); | |
| console.log(` Participant ID: ${info.participant_id}`); | |
| // Get initial state | |
| console.log('\nπ Getting initial state...'); | |
| const initialState = await consumer.getStateSyncAsync(); | |
| console.log('Initial state:', Object.keys(initialState).length, 'joints'); | |
| if (Object.keys(initialState).length > 0) { | |
| Object.entries(initialState).forEach(([name, value]) => { | |
| console.log(` ${name}: ${value}`); | |
| }); | |
| } else { | |
| console.log(' (Empty - no joints set yet)'); | |
| } | |
| // Listen for updates | |
| console.log('\nπ Listening for updates for 60 seconds...'); | |
| console.log(' (Producer can send commands during this time)'); | |
| const startTime = Date.now(); | |
| const duration = 60000; // 60 seconds | |
| // Show periodic status | |
| const statusInterval = setInterval(() => { | |
| const elapsed = Date.now() - startTime; | |
| const remaining = Math.max(0, duration - elapsed); | |
| if (remaining > 0) { | |
| console.log(`\nπ Status (${Math.floor(remaining / 1000)}s remaining):`); | |
| console.log(` Updates received: ${updateCount}`); | |
| console.log(` State syncs received: ${stateCount}`); | |
| } | |
| }, 10000); // Every 10 seconds | |
| await new Promise(resolve => setTimeout(resolve, duration)); | |
| clearInterval(statusInterval); | |
| // Show final summary | |
| console.log('\nπ Final Summary:'); | |
| console.log(` Total updates received: ${updateCount}`); | |
| console.log(` Total state syncs received: ${stateCount}`); | |
| // Get final state | |
| const finalState = await consumer.getStateSyncAsync(); | |
| console.log('\nπ Final state:'); | |
| if (Object.keys(finalState).length > 0) { | |
| Object.entries(finalState).forEach(([name, value]) => { | |
| console.log(` ${name}: ${value}`); | |
| }); | |
| } else { | |
| console.log(' (Empty)'); | |
| } | |
| console.log('\nβ Basic consumer example completed!'); | |
| } catch (error) { | |
| console.error('β Error:', error.message); | |
| } finally { | |
| // Always disconnect | |
| if (consumer.isConnected()) { | |
| console.log('\nπ§Ή Disconnecting...'); | |
| await consumer.disconnect(); | |
| } | |
| console.log('π Goodbye!'); | |
| } | |
| } | |
| // Handle Ctrl+C gracefully | |
| process.on('SIGINT', () => { | |
| console.log('\n\nπ Received SIGINT, shutting down gracefully...'); | |
| process.exit(0); | |
| }); | |
| main().catch(console.error); |