AgentDevice.py 27.3 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740
#!/usr/bin/env python3


import os
import sys
##import utils.Logger as L
import time
import threading, multiprocessing
# TODO ? c'etait dans Agent
#from threading import Thread
#import socket

##from .Agent import Agent
sys.path.append("..")
from agent.Agent import Agent, build_agent, StoppableThreadEvenWhenSleeping
from common.models import AgentDeviceStatus, AgentCmd, get_or_create_unique_row_from_model


sys.path.append("../../..")
from device_controller.abstract_component.device_controller import (
    DCCNotFoundException, UnknownGenericCmdException,
    UnimplementedGenericCmdException, UnknownNativeCmdException,
    DeviceController,
    DeviceCmd,
    DeviceTimeoutException
)

##log = L.setupLogger("AgentXTaskLogger", "AgentX")





class AgentDevice(Agent):


    """
    How to run this agent exec_specific_cmd() method ?
    - True = inside a Thread (cannot be killed, must be asked to stop, and inadequate for computation)
    - False = inside a Process
    If thread, displays :
        >>>>> Thread: starting execution of command specific1
        >>>>> Thread: PID: 2695, Process Name: MainProcess, Thread Name: Thread-1
        ...
        >>>>> Thread: starting execution of command specific2
        >>>>> Thread: PID: 2695, Process Name: MainProcess, Thread Name: Thread-2
        ...
        >>>>> Thread: starting execution of command specific3
        >>>>> Thread: PID: 2695, Process Name: MainProcess, Thread Name: Thread-3
    If process, displays :
        >>>>> Thread: starting execution of command specific1
        >>>>> Thread: PID: 2687, Process Name: Process-1, Thread Name: MainThread
        ...
        >>>>> Thread: starting execution of command specific2
        >>>>> Thread: PID: 2689, Process Name: Process-2, Thread Name: MainThread
        ...
        >>>>> Thread: starting execution of command specific3
        >>>>> Thread: PID: 2690, Process Name: Process-3, Thread Name: MainThread
    """

    _current_device_cmd = None
    _current_device_cmd_thread = None

    # Default host and port of the device controller
    _device_ctrl = None
    _device_sim = None
    HOST, PORT = None, None
    _thread_device_simulator = None

    _agent_device_status = None

    # FOR TEST ONLY
    # Run this agent in simulator mode
    TEST_MODE = False
    # Run the assertion tests at the end
    TEST_WITH_FINAL_TEST = False
    #TEST_MAX_DURATION_SEC = None
    TEST_MAX_DURATION_SEC = 100
    # Who should I send commands to ?
    #TEST_COMMANDS_DEST = "myself"
    TEST_COMMANDS_DEST = "AgentB"
    # Scenario to be executed
    TEST_COMMANDS_LIST = [
        # Ask receiver to delete all its previous commands 
        "flush_commands",

        "go_active",

        # Because of this command, the receiver agent :
        # - will no more send any new command
        # - will only execute "generic" commands (and not the "specific" ones)
        "go_idle",

        # Not executed (skipped) because receiver agent is now "idle"
        #"specific0",

        # Because of this command, the receiver agent
        # will now be able to send new commands
        "go_active",

        # Executed because recipient agent is now "active"
        "specific1",
        # should abort previous command (specific1)
        "abort",

        # Executed completely because no abort
        "specific2",

        # fully executed, result is 7
        "eval 4+3",

        "go_idle",
        "exit",
    ]

    # with thread
    RUN_IN_THREAD = True
    # with process
    #RUN_IN_THREAD = False
    _thread_total_steps_number = 1

    # New agent STATUS specific to AgentDevice
    STATUS_SPECIFIC_PROCESS = "IN_SPECIFIC_PROCESS"



    """
    =================================================================
        FUNCTIONS RUN INSIDE MAIN THREAD
    =================================================================
    """

    ##def __init__(self, config_filename, RUN_IN_THREAD, device_controller:DeviceController, host, port, device_simulator):
    #def __init__(self, config_filename, RUN_IN_THREAD, device_controller:DeviceController, host, port, DEBUG_MODE=False):
    def __init__(self, config_filename, RUN_IN_THREAD, device_controller: DeviceController, host, port):
        ##if name is None: name = self.__class__.__name__
        #super().__init__(name, config_filename, RUN_IN_THREAD)
        #super().__init__(config_filename, RUN_IN_THREAD, DEBUG_MODE)
        super().__init__(config_filename, RUN_IN_THREAD)

        # (EP) etait avant dans Agent
        self._set_agent_device_aliases_from_config(self.name)
        self.RUN_IN_THREAD = RUN_IN_THREAD

        self.HOST, self.PORT = host, port
        self._device_ctrl = device_controller
        ##self._device_sim = device_simulator

        # Initialize the device table status
        # If table is empty, create a default 1st row
        ##self._agent_device_status = get_or_create_unique_row_from_model(AgentDeviceStatus)
        self._agent_device_status = AgentDeviceStatus.getStatusForAgent(self.name)
        """
        if not AgentDeviceTelescopeStatus.objects.exists():
            self.printd("CREATE first row")
            self._agent_device_status = AgentDeviceTelescopeStatus.objects.create(id=1)
        # Get 1st row (will be updated at each iteration by routine_process() with current device status)
        self.printd("GET first row")
        self._agent_device_status = AgentDeviceTelescopeStatus.objects.get(id=1)
        """

        # Initialize the device socket
        # Port local AK 8085 = redirigé sur l’IP du tele 192.168.0.12 sur port 11110
        ##HOST, PORT = "82.64.28.71", 11110
        #HOST, PORT = "localhost", 11110
        #self._device_ctrl = TelescopeControllerGEMINI(host, port, True)
        ##self._device_ctrl = device_controller(HOST, PORT, True)
        ##self._device_ctrl = device_controller(host, port, True)
        #self._log.printd("init done")
        self.printd("init done")


    #TODO:
    def _set_agent_device_aliases_from_config(self, agent_alias):
        for a in self._my_client_agents_aliases:
            # TODO: activer
            ##self._my_client_agents[a] = self.config.get_paramvalue(a,'general','real_agent_device_name')
            pass

    # @override
    def _init(self):

        # --- Set the mode according the startmode value
        ##agent_alias = self.__class__.__name__
        ##self.set_mode_from_config(agent_alias)

        # If in test and simulator mode ==> set my DC as connected to localhost (simulator server)
        if self.is_in_test_mode() and self.WITH_SIMULATOR:
            # START device SIMULATOR (in a thread) so that we can connect to it in place of the real device
            self.HOST = "localhost"
            '''
            self._thread_device_simulator = threading.Thread(target=self.device_simulator_run)
            self._thread_device_simulator.start()
            '''

        # Create instance of a SPECIFIC device controller (device client)
        # Ex: this can be the Gemini or the SBIG (...) DC
        #self._device_ctrl = self._device_ctrl(self.HOST, self.PORT, DEBUG=self.DEBUG_MODE)
        self._device_ctrl = self._device_ctrl(self.HOST, self.PORT)

        # Device socket init
        # (optional) Only useful for TCP (does nothing for UDP)
        self._device_ctrl._connect_to_device()
        if self.DEBUG_MODE: self._device_ctrl.print_available_cmds()

        # Telescope (long) init
        # TODO:


    def is_using_simulator(self):
        return self.WITH_SIMULATOR

    '''
    def device_simulator_run(self):
        #HOST, PORT = "localhost", 11110
        #with get_SocketServer_UDP_TCP(HOST, PORT, "UDP") as myserver:
        self.printd("Starting device simulator on (host, port): ", self.HOST, self.PORT)
        self._device_sim.serve_forever(self.PORT)
        #with get_SocketServer_UDP_TCP(self.HOST, self.PORT, "UDP") as myserver: myserver.serve_forever()
        #''
        myserver = get_SocketServer_UDP_TCP(self.HOST, self.PORT, "UDP")
        myserver.serve_forever()
        #''
    '''

    '''
    # @override
    def load_config(self):
        super().load_config()
    '''

    '''
    # @override
    def update_survey(self):
        super().update_survey()
    '''

    '''
    # @override
    def get_next_command(self):
        return super().get_next_command()
    '''

    # @override
    def do_log(self):
        super().do_log()


    # @override parent class (Agent)
    def routine_process_body(self):
        self.print("Getting my device status information and storing it in DB)")

        # Save current device status to DB
        #AgentDeviceTelescopeStatus.objects.create(radec=myradec)
        #if not self.is_running_specific_cmd():
        self._save_device_status()

        self.printd("Status saved in DB")

        #time.sleep(3)



    """
    ================================================================================================
        DEVICE SPECIFIC FUNCTIONS (abstract for Agent, overriden and implemented by AgentDevice)
    ================================================================================================
    """

    '''
    # @override superclass (Agent) method
    def is_other_level_cmd(self, cmd: AgentCmd):
        return self.is_device_level_cmd(cmd)
    '''

    # I delegate this command to my DC
    # => Execute it only if I am active and no currently running another device level cmd
    # => Long execution time, so I will execute it in parallel (in a new thread or process)
    def is_device_level_cmd(self, cmd: AgentCmd):
        return self._device_ctrl.is_valid_cmd(DeviceCmd(cmd.full_name))

    def _process_device_level_cmd(self):
        log.info("(DEVICE LEVEL CMD)")
        try:
            self.exec_device_cmd_if_possible(cmd)
        except (UnimplementedGenericCmdException) as e:
        #except (UnknownGenericCmdException, UnimplementedGenericCmdException, UnknownNativeCmdException) as e:
            log.e(f"EXCEPTION caught by {type(self).__name__} (from Agent mainloop) for command '{cmd.name}'", e)
            log.e("Thus ==> ignore this command")
            cmd.set_result(e)
            #cmd.set_as_killed_by(type(self).__name__)
            cmd.set_as_skipped()
            #raise

    def is_device_generic_but_UNIMPLEMENTED_cmd(self, cmd:AgentCmd):
        return self._device_ctrl.is_generic_but_UNIMPLEMENTED_cmd(DeviceCmd(cmd.full_name))

    def _is_running_device_cmd(self):
        #return (self._current_device_cmd_thread is not None) or self._current_device_cmd_thread.is_alive()
        #return (self._current_device_cmd_thread is None) or not self._current_device_cmd_thread.is_alive()
        return self._current_device_cmd_thread and self._current_device_cmd_thread.is_alive()

    # @override superclass (Agent) method
    def exec_device_cmd_if_possible(self, cmd:AgentCmd):

        self.__set_status(self.STATUS_SPECIFIC_PROCESS)
        #self.print(f"Starting execution of a DEVICE cmd {cmd}")
        self.print("Starting execution of a DEVICE cmd...")
        self.printd(cmd)

        if self._is_idle():
            self.print("I am IDLE ==> I mark the cmd SKIPPED and ignore it")
            cmd.set_result("Skipped because AgentDevice idle")
            cmd.set_as_skipped()
            return

        # TODO: changer ça car on devrait pouvoir executer N cmds en parallèle...
        if self._is_running_device_cmd():
            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)")
            return

        # DC (DEVICE CONTROLLER) level command:
        if self.is_device_generic_but_UNIMPLEMENTED_cmd(cmd): 
            raise UnimplementedGenericCmdException()
        '''
        This is no more necessary as all these checks have already been done BEFORE calling this method
        # GENERIC device cmd
        if self.is_device_generic_cmd(cmd):
            if not device_has_generic_cmd(cmd):
                raise UnknownGenericCmdException(cmd)
            elif not device_has_native_for_generic_cmd(cmd):
                raise UnimplementedGenericCmdException(cmd)
        # NATIVE device cmd
        elif not device_has_native_cmd(cmd):
            raise UnknownNativeCmdException()
        '''

        # ==> the Agent sends (delegates) it to its dedicated DC, via a sub-thread or a process
        """
        Dans le cas du Majordome, cette methode doit lancer la prise d'une séquence.
        La sequence elle même va être une boucle. 
        Donc on peut voir une séquence comme un appel unique à une fonction qui va durer un certain
        temps (max 20 min par exemple) mais dans cette méthode
        il y a une boucle sur la prise des images. 
        """
        self.printd("Starting DEVICE cmd processing...")
        self._current_device_cmd = cmd
        # Update read time to say that the command has been READ
        cmd.set_read_time()
        #self._current_thread = threading.Thread(target=self.exec_command)

        self.print("Launching device cmd in a thread (or process)...")
        # Run in a thread
        if self.RUN_IN_THREAD:
            self.printd("(run device cmd in a thread)")
            self._current_device_cmd_thread = StoppableThreadEvenWhenSleeping(target=self._thread_exec_device_cmd)
            #self._current_device_cmd_thread = StoppableThreadEvenWhenSleeping(target=self.exec_specific_cmd, args=(cmd,))
            #self._current_thread = threading.Thread(target=self.exec_command)
            #self._current_device_cmd_thread = StoppableThread(target=self.exec_specific_cmd, args=(cmd,))
            #self._current_device_cmd_thread = threading.Thread(target=self.exec_specific_cmd, args=(cmd,))
            #self._current_device_cmd_thread = thread_with_exception('thread test')

        # Run in a process
        else:
            self.printd("(run cmd in a process)")
            # close the database connection first, it will be re-opened in each process
            db.connections.close_all()
            self._current_device_cmd_thread = multiprocessing.Process(target=self._thread_exec_device_cmd) 
            #self._current_device_cmd_thread = multiprocessing.Process(target=self.exec_specific_cmd, args=(cmd,))

        self._current_device_cmd_thread.start()
        #self._current_device_cmd_thread = threading.Thread(target=self.exec_specific_cmd, args=(cmd,))
        #self._current_device_cmd_thread = thread_with_exception('thread test')
        #my_thread.join()
        #self.waitfor(self.subloop_waittime)
        self.printd("Ending specific process (thread has been launched)")


    def _save_device_status(self):
        try:
            self._agent_device_status.status = self.get_device_status()
        except DeviceTimeoutException as e:
            self.log_c("DeviceTimeoutException while getting device status", e)
        self._agent_device_status.save()


    # To be overriden by subclass
    def get_device_status(self):
        TIMEOUT = False
        if TIMEOUT: 
            raise DeviceTimeoutException()
        return 'Abstract status'


    # @override superclass (Agent)
    def _TEST_test_results_other(self, commands):
        # (EP) moved from Agent
        # Now test that any "AD get_xx" following a "AD set_xx value" command has result = value
        for i,cmd_set in enumerate(commands):
            if cmd_set.name.startswith('set_'):
                commands_after = commands[i+1:]
                for cmd_get in commands_after:
                    if cmd_get.name.startswith('get_') and cmd_get.name[4:]==cmd_set.name[4:] and cmd_get.device_type==cmd_set.device_type:
                        log.info("cmd_get.result == cmd_set.args ?" + str(cmd_get.result) + ' ' + str(cmd_set.args))
                        assert cmd_get.get_result() == ','.join(cmd_set.args), "A get_xx command did not gave the expected result as set by a previous set_xx command"
                        break


    # @override parent class (Agent)
    def _do_things_before_exit(self, abort_cmd_sender):
        self._kill_running_device_cmd_if_exists(abort_cmd_sender)

    def _kill_running_device_cmd_if_exists(self, abort_cmd_sender):

        # AGENT level
        if not self._is_running_device_cmd():
            self.print("...No current device command thread to abort...")
        else:
            self.print(f"Killing command {self._current_device_cmd.name}")
            # Ask the thread to stop itself
            #self._current_device_cmd_thread.stop()
            #self._current_device_cmd_thread._stop()
            #self._current_device_cmd_thread.shutdown()
            #threading._shutdown()
            #self._current_device_cmd_thread.raise_exception()
            self._current_device_cmd.set_as_killed_by(abort_cmd_sender)
            self._current_device_cmd_thread.terminate()
            # Now, wait for the end of the thread
            if self.RUN_IN_THREAD:
                self._current_device_cmd_thread.join()
        self._current_device_cmd_thread = None
        #self._current_device_cmd.set_as_killed()
        self._current_device_cmd = None

        # DEVICE specific
        self.print("Close device socket")
        try:
            self._device_ctrl.close()
        except AttributeError as e:
            self.log_e("Error on closing the socket (TBC):", e)
        '''
        # Stop device simulator (only if used)
        if self.is_using_simulator():
            self.printd("Stopping device simulator")
            self._device_sim.stop()
        '''


    """
    # @override
    def specific_process(self, cmd):
        cmd.set_read_time()
        cmd.set_as_running()
        res = self._device_ctrl.execute_cmd(cmd.name)
        cmd.set_result(str(res))
        self.printd("result is", str(res))
        if res.ok: self.printd("OK")
        cmd.set_as_processed()
        time.sleep(1)
    """




    """
    =================================================================
        FUNCTIONS RUN INSIDE A SUB-THREAD (OR A PROCESS) (thread_*())
    =================================================================
    """

    def tprintd(self, *args, **kwargs):
        self.printd('(THREAD):', *args, *kwargs)

    def _thread_exec_device_cmd(self):
        assert self._current_device_cmd_thread is not None

        # thread execution setting up
        self._thread_exec_device_cmd_start()

        # Exit if I was asked to stop
        cmd = self._current_device_cmd
        if self.RUN_IN_THREAD and threading.current_thread().stopped():
            self.tprintd(f">>>>> Thread (cmd {cmd.name}): I received the stop signal, so I stop (in error)")
            exit(1)

        if cmd.name == "do_eval":
            res = eval(cmd.args)

        else:
            try:
                res = self.exec_device_cmd(cmd)
            except (DCCNotFoundException, UnknownGenericCmdException, UnimplementedGenericCmdException, UnknownNativeCmdException) as e:
                self.log_e(f"THREAD EXCEPTION caught by {type(self).__name__} (from thread)", e)
                #raise
                cmd.set_result(e)
                cmd.set_as_killed_by(type(self).__name__)
                #time.sleep(2)
                self.log_e(">>>>> Thread: execution of command", cmd.name, "is now aborted")
                self._current_device_cmd = None
                self._current_device_cmd_thread.terminate()
                # (EP) ...NOT SURE THAT BELOW THIS LINE, SOMETHING WILL BE EXECUTED, because THIS current thread is being killed...
                '''
                # Now, wait for the end of the thread
                if self.RUN_IN_THREAD:
                    self._current_device_cmd_thread.join()
                '''
                self._current_device_cmd_thread = None
                #res = "ERROR"
                return

        cmd.set_result(res)

        '''
        # processing body (main)
        # Specific case of the EVAL command
        #if self._current_device_cmd.name.startswith("eval"):
        if self._current_device_cmd.name == "do_eval":
            self.thread_set_total_steps_number(1)
            self.thread_exec_device_cmd_step(1, self.cmd_step_eval)
            #return
        # to be overriden by subclasses
        else:
            self.thread_exec_specific_cmd_main()
        '''

        # thread execution tearing down
        self._thread_exec_device_cmd_end()
        '''
        except TimeoutError:
            pass
        '''

    #def exec_specific_cmd_start(self, cmd:Command):
    def _thread_exec_device_cmd_start(self):
        cmd = self._current_device_cmd
        """ specific command execution setting up """
        #cmd = self.get_current_device_cmd()
        self.tprintd(">>>>> Thread: starting execution of command", cmd.name)
        self.tprintd(">>>>> Thread: PID: %s, Process Name: %s, Thread Name: %s" % (
            os.getpid(),
            multiprocessing.current_process().name,
            threading.current_thread().name)
        )
        cmd.set_as_running()
        """
        if self.RUN_IN_THREAD:
            cmd.set_as_running()
        else:
            with transaction.atomic():
                cmd.set_as_running()
        """


    # Define your own command step(s) here
    def cmd_step(self, step:int):
        cmd = self._current_device_cmd
        """
        cmd.result = f"in step #{step}/{self._thread_total_steps_number}"
        cmd.save()
        """
        cmd.set_result(f"in step #{step}/{self._thread_total_steps_number}")

    def cmd_step_eval(self, step:int):
        cmd = self._current_device_cmd
        #cmd_args = self._current_device_cmd.full_name.split()[1]
        """
        cmd.result = f"in step #{step}/{self._thread_total_steps_number}"
        cmd.save()
        cmd.set_result(eval(cmd_args))
        """
        cmd.set_result(eval(cmd.args))



    # Default body of the specific processing
    # Should be overriden by subclass
    def OLD_thread_exec_device_cmd_main_OLD(self):
        """
        cmd = self._current_device_cmd
        self.printd("Doing nothing, just sleeping...")
        self.sleep(3)
        """

        # This is optional
        self.thread_set_total_steps_number(5)

        # HERE, write your own scenario

        # scenario OK
        self.thread_exec_device_cmd_step(1, self.cmd_step, 1)
        self.thread_exec_device_cmd_step(2, self.cmd_step, 3)
        self.thread_exec_device_cmd_step(3, self.cmd_step, 5)
        self.thread_exec_device_cmd_step(4, self.cmd_step, 10)
        self.thread_exec_device_cmd_step(5, self.cmd_step, 4)
        # ... as many as you need

        """ other scenario
        self.thread_exec_device_cmd_step(1, self.cmd_step1, 1)
        self.thread_exec_device_cmd_step(2, self.cmd_step2, 2)
        self.thread_exec_device_cmd_step(3, self.cmd_step1, 2)
        self.thread_exec_device_cmd_step(4, self.cmd_step3, 2)
        self.thread_exec_device_cmd_step(5, self.cmd_step1, 3)
        """


    def _thread_exec_device_cmd_end(self):
        """ specific command execution tearing down """
        cmd = self._current_device_cmd
        cmd.set_as_processed()
        """
        if self.RUN_IN_THREAD:
            cmd.set_as_processed()
        else:
            with transaction.atomic():
                cmd.set_as_processed()
        """
        self.print(f">>>>> Thread: ended execution of command '{cmd.name}'")
        cmd = None
        # No more current thread
        #self._current_device_cmd_thread = None

    # Default body of a specific cmd step
    # Should be overriden by subclass
    def thread_exec_device_cmd_step(self, step:int, cmd_step_function, sleep_time:float=1.0):
        # Exit if I was asked to stop
        cmd = self._current_device_cmd
        if self.RUN_IN_THREAD and threading.current_thread().stopped():
            self.tprintd(f">>>>> Thread (cmd {cmd.name}): I received the stop signal, so I stop (in error)")
            exit(1)
        self.tprintd(f">>>>> Thread (cmd {cmd.name}): step #{step}/{self._thread_total_steps_number}")
        # call a specific function to be defined by subclass
        cmd_step_function(step)
        # Wait for a specific time (interruptible)
        self.sleep(sleep_time)

    '''
    def thread_stop_if_asked(self):
        assert self._current_device_cmd_thread is not None
        if self.RUN_IN_THREAD and threading.current_thread().stopped():
            self.printd("(Thread) I received the stop signal, so I stop (in error)")
            exit(1)
    '''

    def thread_set_total_steps_number(self, nbsteps):
        self._thread_total_steps_number = nbsteps

    # @override parent class (Agent)
    def sleep(self, nbsec:float=2.0):
        # thread
        if self._current_device_cmd_thread and self.RUN_IN_THREAD:
            self._current_device_cmd_thread.wait(nbsec)
        # process (or main thread)
        else:
            time.sleep(nbsec)


    # This method is also called DIRECTLY from routine_process().get_device_status() (NOT FROM A THREAD)
    # @override parent class (Agent)
    def exec_device_cmd(self, cmd:AgentCmd):
        #cmd = self._current_device_cmd
        self.tprintd("*** DEVICE cmd name is", cmd.name)
        self.tprintd("*** PASS IT TO DEVICE TYPE", cmd.device_type)
        time.sleep(2)
        try:
            res = self._device_ctrl.exec_cmd(cmd.full_name)
        except (UnknownGenericCmdException, UnimplementedGenericCmdException, DCCNotFoundException, UnknownNativeCmdException) as e:
            self.log_e(f"THREAD EXCEPTION caught by {type(self).__name__} (from AD)", e)
            raise
        self.tprintd("result is", str(res))
        if res.ok: self.tprintd("OK")
        time.sleep(1)
        return res

    # @override superclass (Agent) method
    def OLD_cmd_step_OLD(self, step:int):
        cmd = self._current_device_cmd
        self.printd("cmd name is", cmd.name)
        #res = self._device_ctrl.execute_cmd(cmd.name_and_args)
        res = self._device_ctrl.exec_cmd(cmd.full_name)
        ##cmd.set_result(str(res))
        self.printd("result is", str(res))
        if res.ok: self.printd("OK")
        #cmd.set_as_processed()
        time.sleep(1)
        #cmd.set_result(f"in step #{step}/{self._thread_total_steps_number}")
        cmd.set_result(res)

    # @override
    def OLD_thread_exec_specific_cmd_main_OLD(self):
        # This is optional
        self.thread_set_total_steps_number(1)
        # HERE, write your own scenario
        self.thread_exec_device_cmd_step(1, self.cmd_step, 1)
        # ... as many as you need
        """ other scenario
        self.thread_exec_device_cmd_step(1, self.cmd_step1, 1)
        self.thread_exec_device_cmd_step(2, self.cmd_step2, 2)
        self.thread_exec_device_cmd_step(3, self.cmd_step1, 2)
        self.thread_exec_device_cmd_step(4, self.cmd_step3, 2)
        self.thread_exec_device_cmd_step(5, self.cmd_step1, 3)
        """

    '''
    # @override
    def exec_specific_cmd_end(self, cmd:Command, from_thread=True):
        super().exec_specific_cmd_end(cmd, from_thread)
    '''




def build_agent(Agent_type:Agent, RUN_IN_THREAD=True):
    agent = Agent.build_agent(Agent_type)
    agent.RUN_IN_THREAD = RUN_IN_THREAD
    return agent


"""
=================================================================
    MAIN FUNCTION
=================================================================
"""
if __name__ == "__main__":
    
    # with thread
    RUN_IN_THREAD=True
    # with process
    #RUN_IN_THREAD=False

    agent = build_agent(AgentDevice, RUN_IN_THREAD=RUN_IN_THREAD)
    '''
    TEST_MODE, WITH_SIM, configfile = extract_parameters()
    #agent = AgentX()
    agent = AgentDevice("AgentDevice", configfile, RUN_IN_THREAD)
    #agent.setSimulatorMode(TEST_MODE)
    agent.setTestMode(TEST_MODE)
    agent.setWithSimulator(WITH_SIM)
    self.printd(agent)
    '''
    agent.run()