import time | |
import logging | |
class VirtualRobot: | |
"""Enhanced virtual robot with state management""" | |
def __init__(self): | |
self.state = "IDLE" | |
logging.info("π€ Virtual Robot initialized") | |
def perform_action(self, command: str) -> str: | |
"""Main action processor with validation""" | |
command = (command or "").strip().lower() | |
if not command: | |
return "β Please enter a command" | |
try: | |
if command == "wave": | |
return self._wave() | |
elif command.startswith("say"): | |
return self._speak(command[3:].strip()) | |
return "β Try 'wave' or 'say [message]'" | |
except Exception as e: | |
logging.error(f"Action failed: {str(e)}") | |
return f"β οΈ Error: {str(e)}" | |
def _wave(self) -> str: | |
"""Handle wave action""" | |
self.state = "WAVING" | |
time.sleep(0.5) | |
self.state = "IDLE" | |
return "π Wave complete!" | |
def _speak(self, message: str) -> str: | |
"""Handle speak action""" | |
if not message: | |
return "β No message provided" | |
self.state = "SPEAKING" | |
time.sleep(max(0.3, len(message)*0.05)) | |
self.state = "IDLE" | |
return f"π£οΈ {message.capitalize()}" |