Commit fc277cf1580c1e4b540ba8663c9d80475e337bc6
1 parent
f304c4a3
Exists in
dev
AgentDevice.py cleaned pour mode DEBUG off
Showing
2 changed files
with
44 additions
and
41 deletions
Show diff stats
src/core/pyros_django/agent/Agent.py
... | ... | @@ -597,7 +597,7 @@ class Agent: |
597 | 597 | |
598 | 598 | # ROUTINE process |
599 | 599 | # To be overriden to be specified by subclass |
600 | - self.routine_process() | |
600 | + self._routine_process() | |
601 | 601 | #self.printd("I am IDLE, so I bypass the routine_process (do not send any new command)") |
602 | 602 | |
603 | 603 | # Processing the next pending command if exists |
... | ... | @@ -750,8 +750,7 @@ class Agent: |
750 | 750 | Command.purge_old_commands_sent_to_agent(self.name) |
751 | 751 | |
752 | 752 | |
753 | - # To be overriden to be specified by subclass | |
754 | - def routine_process(self): | |
753 | + def _routine_process(self): | |
755 | 754 | """ |
756 | 755 | Routine processing. |
757 | 756 | This is a command or set of commands that this agent sends regularly, |
... | ... | @@ -760,9 +759,13 @@ class Agent: |
760 | 759 | """ |
761 | 760 | self.print("\n\n--------- START ROUTINE PROCESSING ---------") |
762 | 761 | self._set_status(self.STATUS_ROUTINE_PROCESS) |
763 | - if self.TEST_MODE: self._test_routine_process() | |
762 | + self.routine_process_body() | |
764 | 763 | self.print("\n--------- END ROUTINE PROCESSING ---------\n") |
765 | 764 | |
765 | + # To be overridden by subclasses | |
766 | + def routine_process_body(self): | |
767 | + if self.TEST_MODE: self._test_routine_process() | |
768 | + | |
766 | 769 | |
767 | 770 | """ |
768 | 771 | def purge_commands(self): | ... | ... |
src/core/pyros_django/agent/AgentDevice.py
... | ... | @@ -103,10 +103,10 @@ class AgentDevice(Agent): |
103 | 103 | self._agent_device_status = AgentDeviceStatus.getStatusForAgent(self.name) |
104 | 104 | """ |
105 | 105 | if not AgentDeviceTelescopeStatus.objects.exists(): |
106 | - print("CREATE first row") | |
106 | + self.printd("CREATE first row") | |
107 | 107 | self._agent_device_status = AgentDeviceTelescopeStatus.objects.create(id=1) |
108 | 108 | # Get 1st row (will be updated at each iteration by routine_process() with current device status) |
109 | - print("GET first row") | |
109 | + self.printd("GET first row") | |
110 | 110 | self._agent_device_status = AgentDeviceTelescopeStatus.objects.get(id=1) |
111 | 111 | """ |
112 | 112 | |
... | ... | @@ -117,7 +117,8 @@ class AgentDevice(Agent): |
117 | 117 | #self._device_ctrl = TelescopeControllerGEMINI(host, port, True) |
118 | 118 | ##self._device_ctrl = device_controller(HOST, PORT, True) |
119 | 119 | ##self._device_ctrl = device_controller(host, port, True) |
120 | - self._log.print("init done") | |
120 | + #self._log.printd("init done") | |
121 | + self.printd("init done") | |
121 | 122 | |
122 | 123 | |
123 | 124 | # @override |
... | ... | @@ -157,7 +158,7 @@ class AgentDevice(Agent): |
157 | 158 | def device_simulator_run(self): |
158 | 159 | #HOST, PORT = "localhost", 11110 |
159 | 160 | #with get_SocketServer_UDP_TCP(HOST, PORT, "UDP") as myserver: |
160 | - print("Starting device simulator on (host, port): ", self.HOST, self.PORT) | |
161 | + self.printd("Starting device simulator on (host, port): ", self.HOST, self.PORT) | |
161 | 162 | self._device_sim.serve_forever(self.PORT) |
162 | 163 | #with get_SocketServer_UDP_TCP(self.HOST, self.PORT, "UDP") as myserver: myserver.serve_forever() |
163 | 164 | #'' |
... | ... | @@ -190,18 +191,17 @@ class AgentDevice(Agent): |
190 | 191 | |
191 | 192 | |
192 | 193 | # @override parent class (Agent) |
193 | - def routine_process(self): | |
194 | - self.print("ROUTINE PROCESS START: reading my dedicated device status information and storing it in DB)...") | |
194 | + def routine_process_body(self): | |
195 | + self.printd("Reading my dedicated device status information and storing it in DB)...") | |
195 | 196 | |
196 | 197 | # Save current device status to DB |
197 | 198 | #AgentDeviceTelescopeStatus.objects.create(radec=myradec) |
198 | 199 | #if not self.is_running_specific_cmd(): |
199 | 200 | self._save_device_status() |
200 | 201 | |
201 | - self.print("Status saved in DB") | |
202 | + self.printd("Status saved in DB") | |
202 | 203 | |
203 | 204 | #time.sleep(3) |
204 | - self.print("ROUTINE PROCESS END") | |
205 | 205 | |
206 | 206 | |
207 | 207 | |
... | ... | @@ -227,17 +227,17 @@ class AgentDevice(Agent): |
227 | 227 | def exec_device_cmd_if_possible(self, cmd:Command): |
228 | 228 | |
229 | 229 | self._set_status(self.STATUS_SPECIFIC_PROCESS) |
230 | - self.printd(f"Starting execution of a DEVICE cmd {cmd}") | |
230 | + self.print(f"Starting execution of a DEVICE cmd {cmd}") | |
231 | 231 | |
232 | 232 | if self._is_idle(): |
233 | - self.printd("I am IDLE ==> I mark the cmd SKIPPED and ignore it") | |
233 | + self.print("I am IDLE ==> I mark the cmd SKIPPED and ignore it") | |
234 | 234 | cmd.set_result("Skipped because AgentDevice idle") |
235 | 235 | cmd.set_as_skipped() |
236 | 236 | return |
237 | 237 | |
238 | 238 | # TODO: changer รงa car on devrait pouvoir executer N cmds en parallรจle... |
239 | 239 | if self._is_running_device_cmd(): |
240 | - self.printd("There is still a thread executing a command ==> I cannot execute this new one now (I will try again to execute it at next iteration)") | |
240 | + self.print("There is still a thread executing a command ==> I cannot execute this new one now (I will try again to execute it at next iteration)") | |
241 | 241 | return |
242 | 242 | |
243 | 243 | # DC (DEVICE CONTROLLER) level command: |
... | ... | @@ -250,7 +250,7 @@ class AgentDevice(Agent): |
250 | 250 | temps (max 20 min par exemple) mais dans cette mรฉthode |
251 | 251 | il y a une boucle sur la prise des images. |
252 | 252 | """ |
253 | - self.printd("Starting specific process...") | |
253 | + self.printd("Starting DEVICE cmd processing...") | |
254 | 254 | self._current_device_cmd = cmd |
255 | 255 | # Update read time to say that the command has been READ |
256 | 256 | cmd.set_read_time() |
... | ... | @@ -258,7 +258,7 @@ class AgentDevice(Agent): |
258 | 258 | |
259 | 259 | # Run in a thread |
260 | 260 | if self.RUN_IN_THREAD: |
261 | - self.printd("(run cmd in a thread)") | |
261 | + self.print("(run device cmd in a thread)") | |
262 | 262 | self._current_device_cmd_thread = StoppableThreadEvenWhenSleeping(target=self._thread_exec_device_cmd) |
263 | 263 | #self._current_device_cmd_thread = StoppableThreadEvenWhenSleeping(target=self.exec_specific_cmd, args=(cmd,)) |
264 | 264 | #self._current_thread = threading.Thread(target=self.exec_command) |
... | ... | @@ -268,7 +268,7 @@ class AgentDevice(Agent): |
268 | 268 | |
269 | 269 | # Run in a process |
270 | 270 | else: |
271 | - self.printd("(run cmd in a process)") | |
271 | + self.print("(run cmd in a process)") | |
272 | 272 | # close the database connection first, it will be re-opened in each process |
273 | 273 | db.connections.close_all() |
274 | 274 | self._current_device_cmd_thread = multiprocessing.Process(target=self._thread_exec_device_cmd) |
... | ... | @@ -316,15 +316,15 @@ class AgentDevice(Agent): |
316 | 316 | self._current_device_cmd = None |
317 | 317 | |
318 | 318 | # DEVICE specific |
319 | - print("Close device socket") | |
319 | + self.print("Close device socket") | |
320 | 320 | try: |
321 | 321 | self._device_ctrl.close() |
322 | 322 | except AttributeError as e: |
323 | - print("Erreur sur fermeture socket (TBC):", e) | |
323 | + self.print("Error on closing the socket (TBC):", e) | |
324 | 324 | ''' |
325 | 325 | # Stop device simulator (only if used) |
326 | 326 | if self.is_using_simulator(): |
327 | - print("Stopping device simulator") | |
327 | + self.printd("Stopping device simulator") | |
328 | 328 | self._device_sim.stop() |
329 | 329 | ''' |
330 | 330 | |
... | ... | @@ -336,8 +336,8 @@ class AgentDevice(Agent): |
336 | 336 | cmd.set_as_running() |
337 | 337 | res = self._device_ctrl.execute_cmd(cmd.name) |
338 | 338 | cmd.set_result(str(res)) |
339 | - print("result is", str(res)) | |
340 | - if res.ok: print("OK") | |
339 | + self.printd("result is", str(res)) | |
340 | + if res.ok: self.printd("OK") | |
341 | 341 | cmd.set_as_processed() |
342 | 342 | time.sleep(1) |
343 | 343 | """ |
... | ... | @@ -351,7 +351,7 @@ class AgentDevice(Agent): |
351 | 351 | ================================================================= |
352 | 352 | """ |
353 | 353 | |
354 | - def tprint(self, *args, **kwargs): | |
354 | + def tprintd(self, *args, **kwargs): | |
355 | 355 | self.printd('(THREAD):', *args, *kwargs) |
356 | 356 | |
357 | 357 | def _thread_exec_device_cmd(self): |
... | ... | @@ -363,7 +363,7 @@ class AgentDevice(Agent): |
363 | 363 | # Exit if I was asked to stop |
364 | 364 | cmd = self._current_device_cmd |
365 | 365 | if self.RUN_IN_THREAD and threading.current_thread().stopped(): |
366 | - self.tprint(f">>>>> Thread (cmd {cmd.name}): I received the stop signal, so I stop (in error)") | |
366 | + self.tprintd(f">>>>> Thread (cmd {cmd.name}): I received the stop signal, so I stop (in error)") | |
367 | 367 | exit(1) |
368 | 368 | |
369 | 369 | if cmd.name == "do_eval": |
... | ... | @@ -373,12 +373,12 @@ class AgentDevice(Agent): |
373 | 373 | try: |
374 | 374 | res = self.exec_device_cmd(cmd) |
375 | 375 | except (DCCNotFoundException, UnimplementedGenericCmdException, UnknownNativeCmdException) as e: |
376 | - self.tprint(f"EXCEPTION caught by {type(self).__name__} (from thread)", e) | |
376 | + self.tprintd(f"EXCEPTION caught by {type(self).__name__} (from thread)", e) | |
377 | 377 | #raise |
378 | 378 | cmd.set_result(e) |
379 | 379 | cmd.set_as_killed_by(type(self).__name__) |
380 | 380 | #time.sleep(2) |
381 | - self.tprint(">>>>> Thread: execution of command", cmd.name, "is now aborted") | |
381 | + self.tprintd(">>>>> Thread: execution of command", cmd.name, "is now aborted") | |
382 | 382 | self._current_device_cmd = None |
383 | 383 | self._current_device_cmd_thread.terminate() |
384 | 384 | # (EP) ...NOT SURE THAT BELOW THIS LINE, SOMETHING WILL BE EXECUTED, because THIS current thread is being killed... |
... | ... | @@ -418,8 +418,8 @@ class AgentDevice(Agent): |
418 | 418 | cmd = self._current_device_cmd |
419 | 419 | """ specific command execution setting up """ |
420 | 420 | #cmd = self.get_current_device_cmd() |
421 | - self.printd(">>>>> Thread: starting execution of command", cmd.name) | |
422 | - self.printd(">>>>> Thread: PID: %s, Process Name: %s, Thread Name: %s" % ( | |
421 | + self.tprintd(">>>>> Thread: starting execution of command", cmd.name) | |
422 | + self.tprintd(">>>>> Thread: PID: %s, Process Name: %s, Thread Name: %s" % ( | |
423 | 423 | os.getpid(), |
424 | 424 | multiprocessing.current_process().name, |
425 | 425 | threading.current_thread().name) |
... | ... | @@ -497,7 +497,7 @@ class AgentDevice(Agent): |
497 | 497 | with transaction.atomic(): |
498 | 498 | cmd.set_as_processed() |
499 | 499 | """ |
500 | - self.printd(">>>>> Thread: ended execution of command", cmd.name) | |
500 | + self.tprintd(">>>>> Thread: ended execution of command", cmd.name) | |
501 | 501 | cmd = None |
502 | 502 | # No more current thread |
503 | 503 | #self._current_device_cmd_thread = None |
... | ... | @@ -508,9 +508,9 @@ class AgentDevice(Agent): |
508 | 508 | # Exit if I was asked to stop |
509 | 509 | cmd = self._current_device_cmd |
510 | 510 | if self.RUN_IN_THREAD and threading.current_thread().stopped(): |
511 | - self.printd(f">>>>> Thread (cmd {cmd.name}): I received the stop signal, so I stop (in error)") | |
511 | + self.tprintd(f">>>>> Thread (cmd {cmd.name}): I received the stop signal, so I stop (in error)") | |
512 | 512 | exit(1) |
513 | - self.printd(f">>>>> Thread (cmd {cmd.name}): step #{step}/{self._thread_total_steps_number}") | |
513 | + self.tprintd(f">>>>> Thread (cmd {cmd.name}): step #{step}/{self._thread_total_steps_number}") | |
514 | 514 | # call a specific function to be defined by subclass |
515 | 515 | cmd_step_function(step) |
516 | 516 | # Wait for a specific time (interruptible) |
... | ... | @@ -541,27 +541,27 @@ class AgentDevice(Agent): |
541 | 541 | # @override parent class (Agent) |
542 | 542 | def exec_device_cmd(self, cmd:Command): |
543 | 543 | #cmd = self._current_device_cmd |
544 | - self.tprint("*** DEVICE cmd name is", cmd.name) | |
545 | - self.tprint("*** PASS IT TO DEVICE TYPE", cmd.device_type) | |
544 | + self.tprintd("*** DEVICE cmd name is", cmd.name) | |
545 | + self.tprintd("*** PASS IT TO DEVICE TYPE", cmd.device_type) | |
546 | 546 | try: |
547 | 547 | res = self._device_ctrl.exec_cmd(cmd.full_name) |
548 | 548 | except (DCCNotFoundException, UnimplementedGenericCmdException) as e: |
549 | - self.tprint(f"EXCEPTION caught by {type(self).__name__} (from AD)", e) | |
549 | + self.tprintd(f"EXCEPTION caught by {type(self).__name__} (from AD)", e) | |
550 | 550 | raise |
551 | - self.tprint("result is", str(res)) | |
552 | - if res.ok: self.tprint("OK") | |
551 | + self.tprintd("result is", str(res)) | |
552 | + if res.ok: self.tprintd("OK") | |
553 | 553 | time.sleep(1) |
554 | 554 | return res |
555 | 555 | |
556 | 556 | # @override superclass (Agent) method |
557 | 557 | def OLD_cmd_step_OLD(self, step:int): |
558 | 558 | cmd = self._current_device_cmd |
559 | - print("cmd name is", cmd.name) | |
559 | + self.printd("cmd name is", cmd.name) | |
560 | 560 | #res = self._device_ctrl.execute_cmd(cmd.name_and_args) |
561 | 561 | res = self._device_ctrl.exec_cmd(cmd.full_name) |
562 | 562 | ##cmd.set_result(str(res)) |
563 | - print("result is", str(res)) | |
564 | - if res.ok: print("OK") | |
563 | + self.printd("result is", str(res)) | |
564 | + if res.ok: self.printd("OK") | |
565 | 565 | #cmd.set_as_processed() |
566 | 566 | time.sleep(1) |
567 | 567 | #cmd.set_result(f"in step #{step}/{self._thread_total_steps_number}") |
... | ... | @@ -610,6 +610,6 @@ if __name__ == "__main__": |
610 | 610 | #agent.setSimulatorMode(TEST_MODE) |
611 | 611 | agent.setTestMode(TEST_MODE) |
612 | 612 | agent.setWithSimulator(WITH_SIM) |
613 | - print(agent) | |
613 | + self.printd(agent) | |
614 | 614 | ''' |
615 | 615 | agent.run() | ... | ... |