Update deployer/simulator_interface.py
Browse files- deployer/simulator_interface.py +48 -76
deployer/simulator_interface.py
CHANGED
|
@@ -1,95 +1,67 @@
|
|
| 1 |
-
# simulator_interface.py - Enhanced Virtual Robot Implementation
|
| 2 |
import time
|
| 3 |
import logging
|
| 4 |
from typing import Optional
|
| 5 |
|
| 6 |
-
# Configure logging
|
| 7 |
-
logging.basicConfig(
|
| 8 |
-
level=logging.INFO,
|
| 9 |
-
format='%(asctime)s - %(levelname)s - %(message)s',
|
| 10 |
-
handlers=[logging.StreamHandler()]
|
| 11 |
-
)
|
| 12 |
-
|
| 13 |
class VirtualRobot:
|
| 14 |
-
"""Enhanced virtual robot with state management and
|
| 15 |
|
| 16 |
-
VALID_STATES = {"IDLE", "WAVING", "SPEAKING", "ERROR"}
|
| 17 |
-
VALID_ACTIONS = {"wave", "say"}
|
| 18 |
-
|
| 19 |
def __init__(self):
|
|
|
|
| 20 |
self.state = "IDLE"
|
| 21 |
-
self.
|
| 22 |
-
logging.info("
|
| 23 |
-
|
| 24 |
-
def
|
| 25 |
-
"""
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
def wave(self) -> str:
|
| 33 |
-
"""Perform waving action with state management."""
|
| 34 |
-
if not self._validate_state():
|
| 35 |
-
return "β System error - invalid state"
|
| 36 |
-
|
| 37 |
-
try:
|
| 38 |
-
self.state = "WAVING"
|
| 39 |
-
self.last_action_time = time.time()
|
| 40 |
-
logging.info("[ποΈ] Performing wave action")
|
| 41 |
-
time.sleep(0.5) # Simulate action duration
|
| 42 |
-
response = "π€ *waves hello!*"
|
| 43 |
-
return response
|
| 44 |
-
except Exception as e:
|
| 45 |
-
self.state = "ERROR"
|
| 46 |
-
logging.error("Wave action failed: %s", str(e))
|
| 47 |
-
return f"β Wave failed: {str(e)}"
|
| 48 |
-
finally:
|
| 49 |
-
self.state = "IDLE"
|
| 50 |
-
|
| 51 |
-
def speak(self, phrase: str) -> str:
|
| 52 |
-
"""Perform speaking action with validation."""
|
| 53 |
-
if not phrase or not isinstance(phrase, str):
|
| 54 |
-
logging.warning("Invalid phrase received")
|
| 55 |
-
return "β Invalid phrase"
|
| 56 |
-
|
| 57 |
-
if not self._validate_state():
|
| 58 |
-
return "β System error - invalid state"
|
| 59 |
-
|
| 60 |
-
try:
|
| 61 |
-
self.state = "SPEAKING"
|
| 62 |
-
self.last_action_time = time.time()
|
| 63 |
-
logging.info("[π¬] Speaking: '%s'", phrase)
|
| 64 |
-
time.sleep(len(phrase) * 0.05) # Dynamic delay based on phrase length
|
| 65 |
-
return f"π£οΈ {phrase}"
|
| 66 |
-
except Exception as e:
|
| 67 |
-
self.state = "ERROR"
|
| 68 |
-
logging.error("Speak action failed: %s", str(e))
|
| 69 |
-
return f"β Speak failed: {str(e)}"
|
| 70 |
-
finally:
|
| 71 |
-
self.state = "IDLE"
|
| 72 |
-
|
| 73 |
def perform_action(self, action: str) -> str:
|
| 74 |
-
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
if not action or not isinstance(action, str):
|
| 76 |
logging.warning("Invalid action command received")
|
| 77 |
-
return "β Invalid
|
| 78 |
-
|
| 79 |
-
action = action.
|
| 80 |
|
| 81 |
try:
|
| 82 |
if action == "wave":
|
| 83 |
-
return self.
|
| 84 |
elif action.startswith("say"):
|
| 85 |
-
|
| 86 |
-
if not phrase:
|
| 87 |
-
return "β Please provide something to say"
|
| 88 |
-
return self.speak(phrase)
|
| 89 |
else:
|
| 90 |
logging.warning("Unknown action: %s", action)
|
| 91 |
return "β Unknown action - try 'wave' or 'say [message]'"
|
| 92 |
except Exception as e:
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import time
|
| 2 |
import logging
|
| 3 |
from typing import Optional
|
| 4 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
class VirtualRobot:
|
| 6 |
+
"""Enhanced virtual robot with state management and logging."""
|
| 7 |
|
|
|
|
|
|
|
|
|
|
| 8 |
def __init__(self):
|
| 9 |
+
"""Initialize the virtual robot with default state."""
|
| 10 |
self.state = "IDLE"
|
| 11 |
+
self._setup_logging()
|
| 12 |
+
logging.info("π€ Virtual Robot initialized in %s state", self.state)
|
| 13 |
+
|
| 14 |
+
def _setup_logging(self):
|
| 15 |
+
"""Configure logging settings."""
|
| 16 |
+
logging.basicConfig(
|
| 17 |
+
level=logging.INFO,
|
| 18 |
+
format='%(asctime)s - %(levelname)s - %(message)s',
|
| 19 |
+
handlers=[logging.StreamHandler()]
|
| 20 |
+
)
|
| 21 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
def perform_action(self, action: str) -> str:
|
| 23 |
+
"""
|
| 24 |
+
Process robot actions with validation and error handling.
|
| 25 |
+
|
| 26 |
+
Args:
|
| 27 |
+
action: The action command (e.g., "wave" or "say hello")
|
| 28 |
+
|
| 29 |
+
Returns:
|
| 30 |
+
Response string from the robot
|
| 31 |
+
"""
|
| 32 |
if not action or not isinstance(action, str):
|
| 33 |
logging.warning("Invalid action command received")
|
| 34 |
+
return "β Invalid command"
|
| 35 |
+
|
| 36 |
+
action = action.lower().strip()
|
| 37 |
|
| 38 |
try:
|
| 39 |
if action == "wave":
|
| 40 |
+
return self._wave()
|
| 41 |
elif action.startswith("say"):
|
| 42 |
+
return self._speak(action[3:].strip())
|
|
|
|
|
|
|
|
|
|
| 43 |
else:
|
| 44 |
logging.warning("Unknown action: %s", action)
|
| 45 |
return "β Unknown action - try 'wave' or 'say [message]'"
|
| 46 |
except Exception as e:
|
| 47 |
+
logging.error("Action failed: %s", str(e))
|
| 48 |
+
return f"β Error: {str(e)}"
|
| 49 |
+
|
| 50 |
+
def _wave(self) -> str:
|
| 51 |
+
"""Perform waving animation."""
|
| 52 |
+
self.state = "WAVING"
|
| 53 |
+
logging.info("Performing wave action")
|
| 54 |
+
time.sleep(0.8) # Simulate animation time
|
| 55 |
+
self.state = "IDLE"
|
| 56 |
+
return "π *waves enthusiastically*"
|
| 57 |
+
|
| 58 |
+
def _speak(self, message: str) -> str:
|
| 59 |
+
"""Generate speech response."""
|
| 60 |
+
if not message:
|
| 61 |
+
return "β Please provide something to say"
|
| 62 |
+
|
| 63 |
+
self.state = "SPEAKING"
|
| 64 |
+
logging.info("Speaking: '%s'", message)
|
| 65 |
+
time.sleep(max(0.5, len(message)*0.07)) # Dynamic delay
|
| 66 |
+
self.state = "IDLE"
|
| 67 |
+
return f"π£οΈ {message.capitalize()}"
|