robosage / deployer /simulator_interface.py
mgbam's picture
Update deployer/simulator_interface.py
eada831 verified
raw
history blame
2.31 kB
import time
import logging
from typing import Optional
class VirtualRobot:
"""Enhanced virtual robot with state management and logging."""
def __init__(self):
"""Initialize the virtual robot with default state."""
self.state = "IDLE"
self._setup_logging()
logging.info("πŸ€– Virtual Robot initialized in %s state", self.state)
def _setup_logging(self):
"""Configure logging settings."""
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(levelname)s - %(message)s',
handlers=[logging.StreamHandler()]
)
def perform_action(self, action: str) -> str:
"""
Process robot actions with validation and error handling.
Args:
action: The action command (e.g., "wave" or "say hello")
Returns:
Response string from the robot
"""
if not action or not isinstance(action, str):
logging.warning("Invalid action command received")
return "❌ Invalid command"
action = action.lower().strip()
try:
if action == "wave":
return self._wave()
elif action.startswith("say"):
return self._speak(action[3:].strip())
else:
logging.warning("Unknown action: %s", action)
return "❓ Unknown action - try 'wave' or 'say [message]'"
except Exception as e:
logging.error("Action failed: %s", str(e))
return f"❌ Error: {str(e)}"
def _wave(self) -> str:
"""Perform waving animation."""
self.state = "WAVING"
logging.info("Performing wave action")
time.sleep(0.8) # Simulate animation time
self.state = "IDLE"
return "πŸ‘‹ *waves enthusiastically*"
def _speak(self, message: str) -> str:
"""Generate speech response."""
if not message:
return "❌ Please provide something to say"
self.state = "SPEAKING"
logging.info("Speaking: '%s'", message)
time.sleep(max(0.5, len(message)*0.07)) # Dynamic delay
self.state = "IDLE"
return f"πŸ—£οΈ {message.capitalize()}"