# simulator_interface.py - Enhanced Virtual Robot Implementation import time import logging from typing import Optional # Configure logging logging.basicConfig( level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s', handlers=[logging.StreamHandler()] ) class VirtualRobot: """Enhanced virtual robot with state management and error handling.""" VALID_STATES = {"IDLE", "WAVING", "SPEAKING", "ERROR"} VALID_ACTIONS = {"wave", "say"} def __init__(self): self.state = "IDLE" self.last_action_time = time.time() logging.info("[🤖] Virtual Robot initialized in state: %s", self.state) def _validate_state(self) -> bool: """Ensure robot is in a valid state.""" if self.state not in self.VALID_STATES: self.state = "ERROR" logging.error("Invalid robot state detected!") return False return True def wave(self) -> str: """Perform waving action with state management.""" if not self._validate_state(): return "❌ System error - invalid state" try: self.state = "WAVING" self.last_action_time = time.time() logging.info("[🖐️] Performing wave action") time.sleep(0.5) # Simulate action duration response = "🤖 *waves hello!*" return response except Exception as e: self.state = "ERROR" logging.error("Wave action failed: %s", str(e)) return f"❌ Wave failed: {str(e)}" finally: self.state = "IDLE" def speak(self, phrase: str) -> str: """Perform speaking action with validation.""" if not phrase or not isinstance(phrase, str): logging.warning("Invalid phrase received") return "❌ Invalid phrase" if not self._validate_state(): return "❌ System error - invalid state" try: self.state = "SPEAKING" self.last_action_time = time.time() logging.info("[💬] Speaking: '%s'", phrase) time.sleep(len(phrase) * 0.05) # Dynamic delay based on phrase length return f"🗣️ {phrase}" except Exception as e: self.state = "ERROR" logging.error("Speak action failed: %s", str(e)) return f"❌ Speak failed: {str(e)}" finally: self.state = "IDLE" def perform_action(self, action: str) -> str: """Main action dispatcher with comprehensive validation.""" if not action or not isinstance(action, str): logging.warning("Invalid action command received") return "❌ Invalid action command" action = action.strip().lower() try: if action == "wave": return self.wave() elif action.startswith("say"): phrase = action[3:].strip() if not phrase: return "❌ Please provide something to say" return self.speak(phrase) else: logging.warning("Unknown action: %s", action) return "❓ Unknown action - try 'wave' or 'say [message]'" except Exception as e: self.state = "ERROR" logging.error("Action processing failed: %s", str(e)) return f"❌ System error: {str(e)}"