|
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) |
|
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)) |
|
self.state = "IDLE" |
|
return f"π£οΈ {message.capitalize()}" |