mgbam commited on
Commit
eada831
Β·
verified Β·
1 Parent(s): cf35a6b

Update deployer/simulator_interface.py

Browse files
Files changed (1) hide show
  1. 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 error handling."""
15
 
16
- VALID_STATES = {"IDLE", "WAVING", "SPEAKING", "ERROR"}
17
- VALID_ACTIONS = {"wave", "say"}
18
-
19
  def __init__(self):
 
20
  self.state = "IDLE"
21
- self.last_action_time = time.time()
22
- logging.info("[πŸ€–] Virtual Robot initialized in state: %s", self.state)
23
-
24
- def _validate_state(self) -> bool:
25
- """Ensure robot is in a valid state."""
26
- if self.state not in self.VALID_STATES:
27
- self.state = "ERROR"
28
- logging.error("Invalid robot state detected!")
29
- return False
30
- return True
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
- """Main action dispatcher with comprehensive validation."""
 
 
 
 
 
 
 
 
75
  if not action or not isinstance(action, str):
76
  logging.warning("Invalid action command received")
77
- return "❌ Invalid action command"
78
-
79
- action = action.strip().lower()
80
 
81
  try:
82
  if action == "wave":
83
- return self.wave()
84
  elif action.startswith("say"):
85
- phrase = action[3:].strip()
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
- self.state = "ERROR"
94
- logging.error("Action processing failed: %s", str(e))
95
- return f"❌ System error: {str(e)}"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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()}"