From 38cf2c43eea3e65248011f051fca09bb6312e596 Mon Sep 17 00:00:00 2001 From: Etienne Pallier Date: Wed, 20 Nov 2019 11:35:16 +0100 Subject: [PATCH] wrappers sur DC command class pour clarté et bugfix --- src/core/pyros_django/agent/AgentDevice.py | 4 ++-- src/core/pyros_django/agent/AgentDeviceGemini.py | 6 +++--- src/core/pyros_django/agent/AgentDeviceSBIG.py | 4 ++-- src/device_controller/abstract_component/device_controller.py | 201 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++------------------------------------------------------------------------------------------------------------------------------- 4 files changed, 81 insertions(+), 134 deletions(-) diff --git a/src/core/pyros_django/agent/AgentDevice.py b/src/core/pyros_django/agent/AgentDevice.py index 4b3e8a2..69227fc 100755 --- a/src/core/pyros_django/agent/AgentDevice.py +++ b/src/core/pyros_django/agent/AgentDevice.py @@ -543,7 +543,7 @@ class AgentDevice(Agent): self.printd("*** DEVICE cmd name is", cmd.name) self.printd("*** PASS IT TO DEVICE TYPE", cmd.device_type) try: - res = self._device_ctrl.execute_cmd(cmd.full_name) + res = self._device_ctrl.exec_cmd(cmd.full_name) except (DCCNotFoundException, UnimplementedGenericCmdException) as e: print(f"EXCEPTION caught by {type(self).__name__} (from AD)", e) raise @@ -557,7 +557,7 @@ class AgentDevice(Agent): cmd = self._current_device_cmd print("cmd name is", cmd.name) #res = self._device_ctrl.execute_cmd(cmd.name_and_args) - res = self._device_ctrl.execute_cmd(cmd.full_name) + res = self._device_ctrl.exec_cmd(cmd.full_name) ##cmd.set_result(str(res)) print("result is", str(res)) if res.ok: print("OK") diff --git a/src/core/pyros_django/agent/AgentDeviceGemini.py b/src/core/pyros_django/agent/AgentDeviceGemini.py index 524a7d2..daf7341 100755 --- a/src/core/pyros_django/agent/AgentDeviceGemini.py +++ b/src/core/pyros_django/agent/AgentDeviceGemini.py @@ -137,7 +137,7 @@ class AgentDeviceGemini(AgentDevice): #cmd="get date" cmd="get_date" - res = self._device_ctrl.execute_cmd(cmd) + res = self._device_ctrl.exec_cmd(cmd) self.printd("result is", str(res)) if res.ok: print("OK") dev_date = str(res) @@ -145,7 +145,7 @@ class AgentDeviceGemini(AgentDevice): #cmd="get time" cmd="get_time" - res = self._device_ctrl.execute_cmd(cmd) + res = self._device_ctrl.exec_cmd(cmd) self.printd("result is", str(res)) if res.ok: print("OK") dev_time = str(res) @@ -153,7 +153,7 @@ class AgentDeviceGemini(AgentDevice): #cmd="get radec" cmd="get_radec" - res = self._device_ctrl.execute_cmd(cmd) + res = self._device_ctrl.exec_cmd(cmd) self.printd("result is", str(res)) if res.ok: print("OK") dev_radec = str(res) diff --git a/src/core/pyros_django/agent/AgentDeviceSBIG.py b/src/core/pyros_django/agent/AgentDeviceSBIG.py index 403895f..99d4fee 100755 --- a/src/core/pyros_django/agent/AgentDeviceSBIG.py +++ b/src/core/pyros_django/agent/AgentDeviceSBIG.py @@ -152,14 +152,14 @@ class AgentDeviceSBIG(AgentDevice): def get_device_status(self): cmd="get_date" - res = self._device_ctrl.execute_cmd(cmd) + res = self._device_ctrl.exec_cmd(cmd) self.printd("result is", str(res)) if res.ok: print("OK") dev_date = str(res) time.sleep(1) cmd="get_time" - res = self._device_ctrl.execute_cmd(cmd) + res = self._device_ctrl.exec_cmd(cmd) self.printd("result is", str(res)) if res.ok: print("OK") dev_time = str(res) diff --git a/src/device_controller/abstract_component/device_controller.py b/src/device_controller/abstract_component/device_controller.py index fc2b0f9..9877cb1 100755 --- a/src/device_controller/abstract_component/device_controller.py +++ b/src/device_controller/abstract_component/device_controller.py @@ -68,7 +68,7 @@ def generic_cmd(func): @functools.wraps(func) def wrapper_generic_cmd(self, values_to_set=None): #print("func name is", func.__name__) - return self.execute_generic_cmd(func.__name__, values_to_set) + return self.exec_generic_cmd(func.__name__, values_to_set) return wrapper_generic_cmd @@ -229,30 +229,35 @@ class Gen2NatCmds: #return self.GEN2NAT_CMDS if cmd is None: return self.GEN2NAT_CMDS # 1) search in my MAIN commands - native_cmd = self.GEN2NAT_CMDS.get(cmd) + native_infos = self.GEN2NAT_CMDS.get(cmd) # native_cmd can be None, [], or [infos] - if native_cmd is not None: return native_cmd + if native_infos is not None: return native_infos + #print(self.GEN2NAT_CMDS) + ''' # 2) search in each DCC commands - print(self.GEN2NAT_CMDS) + # !! BAD !! because general dict is not complete for each dcc (especially, does not contain macro-commands, like get_radec, ...) + # So, better not to use this for key in self.GEN2NAT_CMDS.keys(): if isinstance(self.GEN2NAT_CMDS[key], dict): - native_cmd = self.GEN2NAT_CMDS[key].get(cmd) - print('key is', key, 'native cmd is', native_cmd) - if native_cmd is not None: return native_cmd + native_infos = self.GEN2NAT_CMDS[key].get(cmd) + print('key is', key, 'native cmd is', native_infos) + if native_infos is not None: return native_infos + ''' + # Native infos not found return None #def update(self, newdict:dict): self.GEN2NAT_CMDS = { **self.GEN2NAT_CMDS, **newdict } def get_native_infos_for_generic_cmd(self, cmd:str)->str: return self.get(cmd) - def get_native_for_generic_cmd(self, cmd:str)->str: + def get_native_cmd_for_generic(self, cmd:str)->str: val = self.get_native_infos_for_generic_cmd(cmd) if not val: return None return val[0] - def get_answer_for_generic_cmd(self, cmd:str)->str: + def get_simulated_answer_for_generic_cmd(self, cmd:str)->str: val = self.get(cmd) if not val: return None # no answer available if len(val) == 1: return None return val[1] - def get_answer_for_native_cmd(self, cmd:str)->str: + def get_simulated_answer_for_native_cmd(self, cmd:str)->str: for val in self.GEN2NAT_CMDS.values(): if cmd in val: # no answer available @@ -268,8 +273,6 @@ class Gen2NatCmds: if isinstance(self.GEN2NAT_CMDS[key], dict): if cmd in self.GEN2NAT_CMDS[key].values(): return True return False - - def print_available_cmds(self): #print("All commands are:", self._gen2nat_cmds.keys()) print("\nAvailable commands:") @@ -291,77 +294,7 @@ class Gen2NatCmds: print("- DO commands:") print (list(cmd for cmd in d.keys() if cmd.startswith('do_'))) -''' STATIC version -class Gen2NatCmds: - GEN2NAT_CMDS = { - # GET-SET commands: - - 'get_timezone': [], - 'set_timezone': [], - - 'get_date': [], - 'set_date': [], - - 'get_time': [], - 'set_time': [], - - # for test only - 'dc_only':[], - # DO commands: - 'do_init': ['do_init'], - 'do_park': [], - } - @classmethod - def __str__(cls)->str: return str(cls.GEN2NAT_CMDS) - @classmethod - def get(cls, cmd:str)->str: return cls.GEN2NAT_CMDS.get(cmd) - @classmethod - def update(cls, newdict:dict): cls.GEN2NAT_CMDS = { **cls.GEN2NAT_CMDS, **newdict } - @classmethod - def get_native_for_generic_cmd(cls, cmd:str)->str: - val = cls.get(cmd) - if not val: return None - return val[0] - @classmethod - def get_answer_for_generic_cmd(cls, cmd:str)->str: - val = cls.get(cmd) - if not val: return None - # no answer available - if len(val) == 1: return None - return val[1] - @classmethod - def get_answer_for_native_cmd(cls, cmd:str)->str: - for val in cls.GEN2NAT_CMDS.values(): - if cmd in val: - # no answer available - if len(val) == 1: return None - # return 1st answer available - return val[1] - return None - @classmethod - def print_available_commands(cls): - #print("All commands are:", self._gen2nat_cmds.keys()) - print("\nAvailable commands:") - print("=======================") - # 1) print general commands - cls.print_available_commands_for_dcc("General") - # 2) print commands for each DCC: - for key in cls.GEN2NAT_CMDS.keys(): - if isinstance(cls.GEN2NAT_CMDS[key], dict): - cls.print_available_commands_for_dcc(key) - @classmethod - def print_available_commands_for_dcc(cls, dcc_key): - d = cls.GEN2NAT_CMDS if dcc_key=="General" else cls.GEN2NAT_CMDS[dcc_key] - print(f"\n{dcc_key} commands are:") - print("- GET commands:") - #print (list(cmd.replace('_',' ') for cmd in self._gen2nat_cmds.keys() if cmd.startswith('get_'))) - print (list(cmd for cmd in d.keys() if cmd.startswith('get_'))) - print("- SET commands:") - print (list(cmd for cmd in d.keys() if cmd.startswith('set_'))) - print("- DO commands:") - print (list(cmd for cmd in d.keys() if cmd.startswith('do_'))) -''' @@ -450,6 +383,7 @@ class DeviceController(): return self._my_channel.uncap_received_data(data_received) ''' + # WRAPPER methods def formated_cmd(self, cmd:str, value:str=None)->str: ##return self._protoc.formated_cmd(self, cmd, value) return self._protoc.formated_cmd(cmd, value) @@ -541,6 +475,7 @@ class DeviceController(): self._my_channel.set_logger(DEBUG) ''' + def device_simulator_run(self): #HOST, PORT = "localhost", 11110 #with get_SocketServer_UDP_TCP(HOST, PORT, "UDP") as myserver: @@ -717,25 +652,61 @@ class DeviceController(): def is_generic_but_UNIMPLEMENTED_cmd(self, cmd:DeviceCommand): return cmd.is_generic() and not self.is_implemented_generic_cmd(cmd) + # WRAPPER methods + def has_generic_command(self, generic_cmd:DeviceCommand): + #return self._my_cmds.get(cmd.name) is not None + return generic_cmd.name in self._my_cmds.get() + def has_native_cmd_for_generic(self, generic_cmd:DeviceCommand): + return self._my_cmds.get_native_infos_for_generic_cmd(generic_cmd.name) not in [None, []] + # check if generic cmd exists (in dictionary) def is_valid_generic_cmd(self, cmd:DeviceCommand): #print("_my_cmds", self._my_cmds) - if not cmd.devtype: return self._my_cmds.get(cmd.name) is not None + ##if not cmd.devtype: return self._my_cmds.get(cmd.name) is not None + if not cmd.devtype: return self.has_generic_command(cmd) if not self.has_dc_component_for_type(cmd.devtype): return False - return self.get_dc_component_for_type(cmd.devtype)._my_cmds.get(cmd.name) is not None + ##return self.get_dc_component_for_type(cmd.devtype)._my_cmds.get(cmd.name) is not None + return self.get_dc_component_for_type(cmd.devtype).has_generic_command(cmd) # check if generic cmd exists and is implemented as a native cmd (in dictionary) def is_implemented_generic_cmd(self, cmd:DeviceCommand): #print("_my_cmds", self._my_cmds) #return self._my_cmds.get_native_infos_for_generic_cmd(cmd.name) not in [None, []] - if not cmd.devtype: return self._my_cmds.get_native_infos_for_generic_cmd(cmd.name) not in [None, []] + ##if not cmd.devtype: return self._my_cmds.get_native_infos_for_generic_cmd(cmd.name) not in [None, []] + if not cmd.devtype: return self.has_native_cmd_for_generic(cmd) if not self.has_dc_component_for_type(cmd.devtype): return False - return self.get_dc_component_for_type(cmd.devtype)._my_cmds.get(cmd.name) not in [None, []] + ##return self.get_dc_component_for_type(cmd.devtype)._my_cmds.get(cmd.name) not in [None, []] + return self.get_dc_component_for_type(cmd.devtype).has_native_cmd_for_generic(cmd) - # TODO: - def is_valid_native_cmd(self, cmd:DeviceCommand): - return self._my_cmds.is_valid_native_cmd(cmd.name) + def is_valid_native_cmd(self, native_cmd:DeviceCommand): + return self._my_cmds.is_valid_native_cmd(native_cmd.name) + + def get_dcc_and_native_cmd_for_generic(self, generic_cmd:str): + #if generic_cmd not in self._gen2nat_cmds.keys(): raise UnknownNativeCmdException() + # Is it a general command for the (general) controller ? + ''' + if generic_cmd in self._gen2nat_cmds: + return self, self._gen2nat_cmds[generic_cmd] + ''' + # 1) Search in my MAIN commands + nc_infos = self._my_cmds.get_native_infos_for_generic_cmd(generic_cmd) + if nc_infos: return self, nc_infos + # 2) Search in each DCC commands + # Is it a command for one of my dcc ? + ''' + # Bad because general dict is not complete for each dcc (especially, does not contain macro-commands, like get_radec, ...) + for key in self._gen2nat_cmds.keys(): + if isinstance(self._gen2nat_cmds[key], dict): + # dcc = key + if generic_cmd in self._gen2nat_cmds[key]: + return key, self._gen2nat_cmds[key][generic_cmd] + ''' + for dcc in self._my_dc_components: + _, native_cmd_infos = dcc.get_dcc_and_native_cmd_for_generic(generic_cmd) + if native_cmd_infos: return dcc, native_cmd_infos + # Native command not found + return None, None ''' This method is either called @@ -744,7 +715,7 @@ class DeviceController(): - FROM A THREAD from AgentDevice._thread_exec_specific_cmd().exec_specific_cmd() ''' #def execute_cmd(self, cmd:DeviceCommand)->GenericResult: - def execute_cmd(self, raw_input_cmd:str)->GenericResult: + def exec_cmd(self, raw_input_cmd:str)->GenericResult: ''' :param raw_input_cmd: ''' @@ -758,9 +729,9 @@ class DeviceController(): if cmd.is_generic(): if not self.is_valid_generic_cmd(cmd): raise UnknownGenericCmdException(cmd.name) print("GENERIC COMMAND") - #return self.execute_generic_cmd(generic_cmd, args) + #return self.exec_generic_cmd(generic_cmd, args) try: - res = self.execute_generic_cmd(cmd.name, cmd.args, cmd.devtype) + res = self.exec_generic_cmd(cmd.name, cmd.args, cmd.devtype) except (DCCNotFoundException, UnimplementedGenericCmdException) as e: print(f"EXCEPTION caught by {type(self).__name__} (from DC)", e) raise @@ -835,6 +806,7 @@ class DeviceController(): return self.send_native_cmd(request) + # wrapper methods def print_available_cmds(self): self._my_cmds.print_available_cmds() def print_available_cmds_for_dcc(self, dcc_key): self._my_cmds.print_available_cmds_for_dcc(dcc_key) @@ -874,39 +846,17 @@ class DeviceController(): return getattr(self, func)() - def getNativeCommandForGeneric(self, generic_cmd:str): - #if generic_cmd not in self._gen2nat_cmds.keys(): raise UnknownNativeCmdException() - # Is it a general command for the (general) controller ? - ''' - if generic_cmd in self._gen2nat_cmds: - return self, self._gen2nat_cmds[generic_cmd] - ''' - nc_infos = self._my_cmds.get_native_infos_for_generic_cmd(generic_cmd) - if nc_infos: return self, nc_infos - # Is it a command for one of my dcc ? - ''' - # Bad because general dict is not complete for each dcc (especially, does not contain macro-commands, like get_radec, ...) - for key in self._gen2nat_cmds.keys(): - if isinstance(self._gen2nat_cmds[key], dict): - # dcc = key - if generic_cmd in self._gen2nat_cmds[key]: - return key, self._gen2nat_cmds[key][generic_cmd] - ''' - for dcc in self._my_dc_components: - _, native_cmd_infos = dcc.getNativeCommandForGeneric(generic_cmd) - if native_cmd_infos: return dcc, native_cmd_infos - # Native command not found - return None, None - #def execute_generic_cmd(self, generic_cmd:DeviceCommand)->str: - def execute_generic_cmd(self, generic_cmd:str, values_to_set:str=None, dcc_type:str=None)->str: + + #def exec_generic_cmd(self, generic_cmd:DeviceCommand)->str: + def exec_generic_cmd(self, generic_cmd:str, values_to_set:str=None, dcc_type:str=None)->str: ''' Execute a generic command :param generic_cmd: str like "get_ra" or "set_ra" or "do_park"... :param value: only for a "set_" cmd ''' - print("(DC):execute_generic_cmd() from", self) + print("(DC):exec_generic_cmd() from", self) # If generic_cmd is for a specific device component (dc), pass it to this dc (instead of me) print("(DC):dc_component_type is: ", dcc_type) ####if dc_component_type and dc_component_type not in self.__class__.__name__ : @@ -919,11 +869,11 @@ class DeviceController(): except DCCNotFoundException as e: print(f"EXCEPTION caught by {type(self).__name__} (from dcc)", e) raise - #return (DCC)(self.execute_generic_cmd(generic_cmd, values_to_set, None)) + #return (DCC)(self.exec_generic_cmd(generic_cmd, values_to_set, None)) # Delegate cmd execution to the dcc try: - return dcc.execute_generic_cmd(generic_cmd, values_to_set, None) + return dcc.exec_generic_cmd(generic_cmd, values_to_set, None) except UnimplementedGenericCmdException as e: print(f"EXCEPTION caught by {type(self).__name__} (from dcc)", e) raise @@ -935,17 +885,14 @@ class DeviceController(): ##print("(DC): My ("+type(self).__name__+") commands are:", self._gen2nat_cmds) print("(DC): My ("+type(self).__name__+") commands are:", self._my_cmds.get()) - ##if generic_cmd not in self._gen2nat_cmds: raise UnimplementedGenericCmdException - #if generic_cmd not in self._gen2nat_cmds.keys(): raise UnknownNativeCmdException() - ##native_cmd_infos = self._gen2nat_cmds[generic_cmd] - dcc, native_cmd_infos = self.getNativeCommandForGeneric(generic_cmd) + dcc, native_cmd_infos = self.get_dcc_and_native_cmd_for_generic(generic_cmd) print("native_cmd_infos", native_cmd_infos) # Command is not implemented if not native_cmd_infos: raise UnimplementedGenericCmdException(generic_cmd) # Command is one of my dcc's, so pass it the command for exec if dcc != self: #dcc = self.get_dc_component_for_type(dcc_type) - return dcc.execute_generic_cmd(generic_cmd, values_to_set, None) + return dcc.exec_generic_cmd(generic_cmd, values_to_set, None) # Get corresponding native command: native_cmd = native_cmd_infos[0] @@ -998,10 +945,10 @@ class DeviceController(): @generic_cmd def get_timezone(self): pass - #def get_timezone(self): return self.execute_generic_cmd('get_timezone') + #def get_timezone(self): return self.exec_generic_cmd('get_timezone') @generic_cmd def set_timezone(self, hh): pass - #def set_timezone(self, hh): return self.execute_generic_cmd('set_timezone', hh) + #def set_timezone(self, hh): return self.exec_generic_cmd('set_timezone', hh) @generic_cmd def get_date(self): pass -- libgit2 0.21.2