diff --git a/pyros.py b/pyros.py index 76b9629..29624a6 100755 --- a/pyros.py +++ b/pyros.py @@ -224,21 +224,25 @@ GLOBAL_OPTIONS = {} def verbose_mode(): return GLOBAL_OPTIONS["verbose"] def test_mode(): return GLOBAL_OPTIONS["test"] def sim_mode(): return GLOBAL_OPTIONS["sim"] +def debug_mode(): return GLOBAL_OPTIONS["debug"] @click.group() -@click.option('--test', '-t', is_flag=True, help="don't do it for real, just show what it would do") +@click.option('--debug', '-d', is_flag=True, help='DEBUG mode') @click.option('--sim', '-s', is_flag=True, help="only for an AgentDevice, asking it to start its simulator and work with it (instead of real device)") +@click.option('--test', '-t', is_flag=True, help="don't do it for real, just show what it would do") @click.option('--verbose', '-v', is_flag=True, help='Verbose output') #@click.option('--verbose', '-v', 'verbosity', flag_value=2, default=1, help='Verbose output'), #@click.option('--quiet', '-q', 'verbosity', flag_value=0, help='Minimal output'), #@click.option('--fail-fast', '--failfast', '-f', 'fail_fast', is_flag=True, default=False, help='Stop on failure'), -def pyros_launcher(test, sim, verbose): +def pyros_launcher(debug, sim, test, verbose): #pass + if debug: click.echo('DEBUG mode') + if sim: click.echo('Starting and connecting to simulator instead of real device') if test: click.echo('Test mode') - if test: click.echo('Starting and connecting to simulator instead of real device') if verbose: click.echo('Verbose mode') - GLOBAL_OPTIONS["test"] = test + GLOBAL_OPTIONS["debug"] = debug GLOBAL_OPTIONS["sim"] = sim + GLOBAL_OPTIONS["test"] = test GLOBAL_OPTIONS["verbose"] = verbose @@ -474,8 +478,10 @@ def start(agent:str, configfile:str): #cmd = "-m AgentX" #cmd = f" Agent{agent_name[5:]}.py {configfile}" cmd = f"Agent{agent_name[5:]}.py" - if test_mode(): cmd += " -t" + if debug_mode(): cmd += " -d" if sim_mode(): cmd += " -s" + if test_mode(): cmd += " -t" + if verbose_mode(): cmd += " -v" if configfile: cmd += " {configfile}" #if not test_mode(): current_processes.append( [execProcessFromVenvAsync(cmd), agent_name, -1] ) diff --git a/src/core/pyros_django/agent/Agent.py b/src/core/pyros_django/agent/Agent.py index 1fef773..fb95bbf 100755 --- a/src/core/pyros_django/agent/Agent.py +++ b/src/core/pyros_django/agent/Agent.py @@ -2,7 +2,7 @@ VERSION = "0.5" -DEBUG=True +#DEBUG=True #DEBUG=False """TODO: @@ -219,6 +219,11 @@ class Agent: # --- - INSTANCE attributes are accessible via agent.__dict__ # --- + # Default modes + DEBUG_MODE = False + WITH_SIMULATOR = False + TEST_MODE = False + # To be overriden by subclasses (empty by default, no agent specific command) AGENT_SPECIFIC_COMMANDS = [ #"do_eval", @@ -384,11 +389,13 @@ class Agent: _log = None #def __init__(self, name:str="Agent", config_filename:str=None, RUN_IN_THREAD=True): - def __init__(self, config_filename:str=None, RUN_IN_THREAD=True): + def __init__(self, config_filename:str=None, RUN_IN_THREAD=True, DEBUG_MODE=False): #self.name = name self.name = self.__class__.__name__ - self._log = LogPyros(self.name,AgentLogs) - self._log.debug_level = DEBUG + self.DEBUG_MODE = DEBUG_MODE + self._log = LogPyros(self.name, AgentLogs) + self._log.debug_level = DEBUG_MODE + self.printd("LOG DEBUG LEVEL IS:", self._log.debug_level) # New way with PathLib my_parent_abs_dir = Path(__file__).resolve().parent @@ -414,7 +421,7 @@ class Agent: config_filename = config_filename.replace(os.sep+"monitoring"+os.sep, os.sep) config_filename = os.path.normpath(config_filename) ''' - self.printd(f"Config file used is={config_filename}") + self.printd(f"*** Config file used is={config_filename}") self.config = ConfigPyros(config_filename) if self.config.get_last_errno() != self.config.NO_ERROR: raise Exception(f"Bad config file name '{config_filename}', error {str(self.config.get_last_errno())}: {str(self.config.get_last_errmsg())}") @@ -523,7 +530,7 @@ class Agent: # TEST MODE ONLY # IF in test mode but with REAL devices (no SIMULATOR), delete all dangerous commands from the test commands list scenario: if self.TEST_MODE: - print("\n!!! In TEST mode !!! => preparing to run a scenario of test commands") + self.printd("\n!!! In TEST mode !!! => preparing to run a scenario of test commands") print("- Current test commands list scenario is:\n", self.TEST_COMMANDS_LIST) if not self.WITH_SIMULATOR: print("\n!!! In TEST but no SIMULATOR mode (using REAL device) !!! => removing dangerous commands for real devices... :") @@ -1272,11 +1279,12 @@ class Agent: ================================================================= """ - #def setSimulatorMode(self, mode): - def _set_test_mode(self, mode:bool): - self.TEST_MODE=mode + def _set_debug_mode(self, mode:bool): + self.DEBUG_MODE=mode def _set_with_simulator(self, mode:bool): self.WITH_SIMULATOR=mode + def _set_test_mode(self, mode:bool): + self.TEST_MODE=mode def _TEST_get_next_command_to_send(self)->Command: cmd_full_name = next(self.TEST_COMMANDS, None) @@ -1437,7 +1445,7 @@ class Agent: 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: print("cmd_get.result == cmd_set.args ?", cmd_get.result, cmd_set.args) - assert cmd_get.get_result() == ','.join(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 # Specific (detailed) test (to be overriden by subclass) @@ -1515,13 +1523,17 @@ def extract_parameters(): """ Usage: Agent.py [-t] [configfile] """ # arg 1 : -t # arg 2 : configfile + DEBUG_MODE = False TEST_MODE = False WITH_SIM = False + VERBOSE_MODE = False configfile = None print("args:", sys.argv) for arg in sys.argv[1:] : if arg == "-t": TEST_MODE = True elif arg == "-s": WITH_SIM = True + elif arg == "-d": DEBUG_MODE = True + elif arg == "-v": VERBOSE_MODE = True else: configfile = arg ''' if len(sys.argv) > 1: @@ -1532,17 +1544,17 @@ def extract_parameters(): else: configfile = sys.argv[1] ''' - return TEST_MODE, WITH_SIM, configfile + return DEBUG_MODE, WITH_SIM, TEST_MODE, VERBOSE_MODE, configfile #def build_agent(Agent_type:Agent, name="GenericAgent", RUN_IN_THREAD=True): def build_agent(Agent_type:Agent, RUN_IN_THREAD=True): - TEST_MODE, WITH_SIM, configfile = extract_parameters() + DEBUG_MODE, WITH_SIM, TEST_MODE, VERBOSE_MODE, configfile = extract_parameters() #agent = Agent("GenericAgent", configfile, RUN_IN_THREAD=True) - agent = Agent_type(configfile, RUN_IN_THREAD) + agent = Agent_type(configfile, RUN_IN_THREAD, DEBUG_MODE=DEBUG_MODE) #agent = Agent_type(name, configfile, RUN_IN_THREAD) - #agent.setSimulatorMode(TEST_MODE) - agent._set_test_mode(TEST_MODE) agent._set_with_simulator(WITH_SIM) + agent._set_test_mode(TEST_MODE) + #agent._set_debug_mode(DEBUG_MODE) return agent diff --git a/src/core/pyros_django/agent/AgentDevice.py b/src/core/pyros_django/agent/AgentDevice.py index dfa6719..d8de1ba 100755 --- a/src/core/pyros_django/agent/AgentDevice.py +++ b/src/core/pyros_django/agent/AgentDevice.py @@ -89,10 +89,10 @@ class AgentDevice(Agent): #def __init__(self, name:str=None, config_filename=None, RUN_IN_THREAD=True, device_controller, host, port): #def __init__(self, name:str, config_filename, RUN_IN_THREAD, device_controller, host, port, device_simulator): ##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): + def __init__(self, config_filename, RUN_IN_THREAD, device_controller:DeviceController, host, port, DEBUG_MODE=False): ##if name is None: name = self.__class__.__name__ #super().__init__(name, config_filename, RUN_IN_THREAD) - super().__init__(config_filename, RUN_IN_THREAD) + super().__init__(config_filename, RUN_IN_THREAD, DEBUG_MODE) self.HOST, self.PORT = host, port self._device_ctrl = device_controller ##self._device_sim = device_simulator @@ -139,7 +139,7 @@ class AgentDevice(Agent): # Create instance of device controller (device client) # Ex: this can be the Gemini or the SBIG (...) DC - self._device_ctrl = self._device_ctrl(self.HOST, self.PORT, True) + self._device_ctrl = self._device_ctrl(self.HOST, self.PORT, DEBUG=True) # Device socket init # (optional) Only useful for TCP (does nothing for UDP) diff --git a/src/core/pyros_django/agent/AgentDeviceGemini.py b/src/core/pyros_django/agent/AgentDeviceGemini.py index 226c03d..7d61807 100755 --- a/src/core/pyros_django/agent/AgentDeviceGemini.py +++ b/src/core/pyros_django/agent/AgentDeviceGemini.py @@ -64,7 +64,7 @@ class AgentDeviceGemini(AgentDevice): """ # @override - def __init__(self, config_filename=None, RUN_IN_THREAD=True): + def __init__(self, config_filename=None, RUN_IN_THREAD=True, DEBUG_MODE=False): ''' if self.is_in_simulator_mode() and not self.WITH_SIMULATOR: # START device SIMULATOR (in a thread) so that we can connect to it in place of the real device @@ -77,7 +77,8 @@ class AgentDeviceGemini(AgentDevice): config_filename, RUN_IN_THREAD, device_controller=DC_Gemini, - host=self.HOST, port=self.PORT) + host=self.HOST, port=self.PORT, + DEBUG_MODE=DEBUG_MODE) ###device_simulator=DeviceSimulatorTelescopeGemini) # Initialize the device table status diff --git a/src/core/pyros_django/agent/logpyros.py b/src/core/pyros_django/agent/logpyros.py index 575f3a7..e3b19ac 100644 --- a/src/core/pyros_django/agent/logpyros.py +++ b/src/core/pyros_django/agent/logpyros.py @@ -130,16 +130,16 @@ class LogPyros: # ===================================================================== def _set_debug_level(self, level:str): - if type(level)=="bool": + if type(level).__name__=="bool": if level==True: level = 1 else: level = 0 self._debug_level = level - + def _get_debug_level(self): return self._debug_level - + def _set_agent_alias(self, agent_alias:str): if agent_alias=="": self._last_errno = self.ERR_ALIAS_NAME_NOT_DEFINED @@ -227,7 +227,7 @@ class LogPyros: # Methods for users # ===================================================================== # ===================================================================== - + def print(self, *args, **kwargs): """ This is the method to print in the console display and in a log file. @@ -254,9 +254,9 @@ class LogPyros: """ Same as print method but only if debug level is > threashold """ - if self.debug_level > 0: + if self._debug_level > 0: self.print(*args, **kwargs) - + def file(self, *args, **kwargs): """ This is the method to print in a log file. diff --git a/src/device_controller/concrete_component/gemini/gemini_controller.py b/src/device_controller/concrete_component/gemini/gemini_controller.py index 2a5e85e..257b3a2 100755 --- a/src/device_controller/concrete_component/gemini/gemini_controller.py +++ b/src/device_controller/concrete_component/gemini/gemini_controller.py @@ -303,7 +303,8 @@ class DC_Gemini(DeviceController): # Gemini is using UDP #def __init__(self, device_host:str="localhost", device_port:int=11110, channel=socket, DEBUG=False): - def __init__(self, device_host:str="localhost", device_port:int=11110, DEBUG=False): + #def __init__(self, device_host:str="localhost", device_port:int=11110, DEBUG=False): + def __init__(self, device_host:str="localhost", device_port:int=11110, dcc_list=[], DEBUG=False): ##super().__init__(device_host, device_port, "SOCKET-UDP", 1024, DEBUG) super().__init__(device_host, device_port, "SOCKET-UDP", MY_DEVICE_CHANNEL_BUFFER_SIZE, protoc=self.Protocol, gen2nat_cmds=self.GEN2NAT_CMDS, device_sim=DS_Gemini, DEBUG=DEBUG) @@ -316,6 +317,9 @@ class DC_Gemini(DeviceController): - NO SIMULATOR (because my dccs will use the same as me and I have already launched it), - DEBUG ''' + # TODO: utiliser dcc_list + # Default list of DCCs (if not given) + if not dcc_list: dcc_list = [DC_Mount, DC_MountBis] # @override superclass empty list of dc components self.set_dc_components( [ -- libgit2 0.21.2