Skip to content

Commit 6a29a24

Browse files
authored
Merge pull request #44 from JdeRobot/issue-43
Issue 43
2 parents 6c815f6 + 2386359 commit 6a29a24

2 files changed

Lines changed: 43 additions & 9 deletions

File tree

manager/libs/applications/compatibility/exercise_wrapper.py

Lines changed: 41 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ def __init__(self, exercise_command, gui_command, update_callback):
2323
self.running = False
2424
self.linter = Lint()
2525
self.brain_ready_event = threading.Event()
26+
self.exercise_command = exercise_command
27+
self.gui_command = gui_command
28+
self.update_callback = update_callback
29+
self.pick = None
2630
# TODO: review hardcoded values
2731
process_ready, self.exercise_server = self._run_exercise_server(f"python {exercise_command}",
2832
f'{home_dir}/ws_code.log',
@@ -50,23 +54,34 @@ def __init__(self, exercise_command, gui_command, update_callback):
5054
self.gui_server.kill()
5155
raise RuntimeError(f"Exercise GUI {gui_command} could not be run")
5256

53-
self.running = True
54-
55-
self.start_send_freq_thread()
5657

5758

5859
def send_freq(self, exercise_connection, is_alive):
5960
"""Send the frequency of the brain and gui to the exercise server"""
60-
while is_alive():
61+
while self.running:
6162
exercise_connection.send(
6263
"""#freq{"brain": 20, "gui": 10, "rtf": 100}""")
6364
time.sleep(1)
6465

66+
def save_pick(self, pick):
67+
self.pick = pick
68+
69+
def send_pick(self, pick):
70+
self.gui_connection.send("#pick" + json.dumps(pick))
71+
print("#pick" + json.dumps(pick))
72+
6573
def start_send_freq_thread(self):
6674
"""Start a thread to send the frequency of the brain and gui to the exercise server"""
67-
daemon = Thread(target=lambda: self.send_freq(self.exercise_connection,
75+
self.running = True
76+
self.send_freq_thread = Thread(target=lambda: self.send_freq(self.exercise_connection,
6877
lambda: self.is_alive), daemon=False, name='Monitor frequencies')
69-
daemon.start()
78+
self.send_freq_thread.start()
79+
80+
def stop_send_freq_thread(self):
81+
"""Stop the thread sending the frequency of the brain and gui to the exercise server"""
82+
if self.running:
83+
self.running = False
84+
self.send_freq_thread.join()
7085

7186
def _run_exercise_server(self, cmd, log_file, load_string, timeout: int = 5):
7287
process = subprocess.Popen(f"{cmd}", shell=True, stdout=sys.stdout, stderr=subprocess.STDOUT,
@@ -123,13 +138,32 @@ def pause(self):
123138
rosservice.call_service('/gazebo/pause_physics', [])
124139

125140
def restart(self):
126-
pass
141+
# Terminate current processes
142+
self.stop_send_freq_thread()
143+
home_dir = os.path.expanduser('~')
144+
stop_process_and_children(self.exercise_server)
145+
try:
146+
os.remove(f'{home_dir}/ws_code.log')
147+
except OSError as error:
148+
LogManager.logger.error(f"Error al eliminar el archivo log: {error}")
149+
150+
process_ready,self.exercise_server = self._run_exercise_server(f"python {self.exercise_command}",
151+
f'{home_dir}/ws_code.log',
152+
'websocket_code=ready')
153+
if process_ready:
154+
self.start_send_freq_thread()
155+
if self.pick:
156+
self.send_pick(self.pick)
157+
158+
127159

128160
@property
129161
def is_alive(self):
130162
return self.running
131163

132164
def load_code(self, code: str):
165+
self.restart()
166+
133167
errors = self.linter.evaluate_code(code)
134168
if errors == "":
135169
self.brain_ready_event.clear()

manager/manager/manager.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,6 @@ class Manager:
5959
# Global transitions
6060
{'trigger': 'disconnect', 'source': '*',
6161
'dest': 'idle', 'before': 'on_disconnect'},
62-
#{'trigger': 'get_state', 'source': '*', 'dest': '='},
63-
6462
]
6563

6664
def __init__(self, host: str, port: int):
@@ -281,6 +279,8 @@ def signal_handler(sign, frame):
281279
self.process_messsage(message)
282280
except Exception as e:
283281
if message is not None:
282+
if message.command == "#pick":
283+
self.application.save_pick(message.data)
284284
ex = ManagerConsumerMessageException(
285285
id=message.id, message=str(e))
286286
else:

0 commit comments

Comments
 (0)