Spaces:
Sleeping
Sleeping
File size: 2,842 Bytes
8aedc84 51f51c3 8aedc84 ec2d8f0 8aedc84 51f51c3 8aedc84 51f51c3 8aedc84 51f51c3 8aedc84 3367d1b 8aedc84 51f51c3 8aedc84 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
#!/usr/bin/env python3
"""
Basic Consumer Example - RobotHub TransportServer
This example demonstrates:
- Connecting to an existing room as a consumer
- Receiving joint updates and state sync
- Setting up callbacks
- Getting current state
"""
import asyncio
import logging
from transport_server_client import RoboticsConsumer
# Setup logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
async def main():
"""Basic consumer example."""
# You need to provide an existing workspace and room ID
# You can get these from running basic_producer.py first
print(
"Enter connection details (you can get these from running basic_producer.py):"
)
workspace_id = input("Enter workspace ID: ").strip()
room_id = input("Enter room ID: ").strip()
if not workspace_id or not room_id:
logger.error("Both workspace ID and room ID are required!")
return
# Create consumer client
consumer = RoboticsConsumer("http://localhost:8000")
# Track received updates
received_updates = []
received_states = []
# Set up callbacks
def on_joint_update(joints):
logger.info(f"Received joint update: {joints}")
received_updates.append(joints)
def on_state_sync(state):
logger.info(f"Received state sync: {state}")
received_states.append(state)
def on_error(error_msg):
logger.error(f"Consumer error: {error_msg}")
def on_connected():
logger.info("Consumer connected!")
def on_disconnected():
logger.info("Consumer disconnected!")
# Register all callbacks
consumer.on_joint_update(on_joint_update)
consumer.on_state_sync(on_state_sync)
consumer.on_error(on_error)
consumer.on_connected(on_connected)
consumer.on_disconnected(on_disconnected)
try:
# Connect to the room
success = await consumer.connect(workspace_id, room_id)
if not success:
logger.error("Failed to connect to room!")
return
logger.info(f"Connected to room {room_id} in workspace {workspace_id}")
# Get initial state
initial_state = await consumer.get_state_sync()
logger.info(f"Initial state: {initial_state}")
# Listen for updates for 30 seconds
logger.info("Listening for updates for 30 seconds...")
await asyncio.sleep(30)
# Show summary
logger.info(f"Received {len(received_updates)} joint updates")
logger.info(f"Received {len(received_states)} state syncs")
except Exception:
logger.exception("Exception: ")
finally:
# Always disconnect
if consumer.is_connected():
await consumer.disconnect()
logger.info("Consumer disconnected")
if __name__ == "__main__":
asyncio.run(main())
|