@@ -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 ()
0 commit comments