From d52240d4f69221a1d6187b55dd3044bb43c13f88 Mon Sep 17 00:00:00 2001 From: Etienne Pallier Date: Tue, 1 Oct 2019 14:23:56 +0200 Subject: [PATCH] nouvelle classe DeviceCommand (dans DeviceController) --- src/core/pyros_django/agent/AgentDevice.py | 11 ++++++----- src/core/pyros_django/agent/AgentDeviceSBIG.py | 4 ++-- src/core/pyros_django/common/models.py | 4 ++-- src/device_controller/README.md | 12 +++++++----- src/device_controller/abstract_component/base.py | 117 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--------------------------------- src/device_controller/concrete_component/gemini/client_telescope_gemini_controller_run.py | 8 +++++++- 6 files changed, 108 insertions(+), 48 deletions(-) diff --git a/src/core/pyros_django/agent/AgentDevice.py b/src/core/pyros_django/agent/AgentDevice.py index 71f0e08..412cca5 100755 --- a/src/core/pyros_django/agent/AgentDevice.py +++ b/src/core/pyros_django/agent/AgentDevice.py @@ -190,7 +190,7 @@ class AgentDevice(Agent): # Save current device status to DB #AgentDeviceTelescopeStatus.objects.create(radec=myradec) #if not self.is_running_specific_cmd(): - self.save_device_status() + self._save_device_status() self.print("Status saved in DB") @@ -198,7 +198,7 @@ class AgentDevice(Agent): self.print("ROUTINE PROCESS END") - def save_device_status(self): + def _save_device_status(self): self._agent_device_status.status = self.get_device_status() self._agent_device_status.save() @@ -241,12 +241,13 @@ class AgentDevice(Agent): ================================================================= """ - # @Overwrite superclass (Agent) method + # @override superclass (Agent) method def cmd_step(self, step:int): cmd = self._current_specific_cmd print("cmd name is", cmd.name) - res = self._device_ctrl.execute_cmd(cmd.name_and_args) - cmd.set_result(str(res)) + #res = self._device_ctrl.execute_cmd(cmd.name_and_args) + res = self._device_ctrl.execute_cmd(cmd.full_name) + ##cmd.set_result(str(res)) print("result is", str(res)) if res.ok: print("OK") #cmd.set_as_processed() diff --git a/src/core/pyros_django/agent/AgentDeviceSBIG.py b/src/core/pyros_django/agent/AgentDeviceSBIG.py index 9d4fab5..e5815c1 100755 --- a/src/core/pyros_django/agent/AgentDeviceSBIG.py +++ b/src/core/pyros_django/agent/AgentDeviceSBIG.py @@ -67,8 +67,8 @@ class AgentDeviceSBIG(AgentDevice): super().__init__( config_filename, RUN_IN_THREAD, - device_controller=DeviceControllerSBIG, host=self.HOST, port=self.PORT, - device_simulator=DeviceSimulatorSBIG) + device_controller=DeviceControllerSBIG, host=self.HOST, port=self.PORT) + #device_simulator=DeviceSimulatorSBIG) # Initialize the device table status # If table is empty, create a default 1st row diff --git a/src/core/pyros_django/common/models.py b/src/core/pyros_django/common/models.py index a846303..9dc40c9 100644 --- a/src/core/pyros_django/common/models.py +++ b/src/core/pyros_django/common/models.py @@ -423,7 +423,7 @@ class Command(models.Model): "CMD_KILLED", # cde ignorée (je suis idle… et j’ai ignoré cette commande, et je passe à la cde suivante) "CMD_OUTOFDATE" # cde périmée ) - GENERIC_COMMANDS = [ + _GENERAL_COMMANDS = [ #"do_eval", "set_state:idle", "set_state:active", "do_eval", "set_state", @@ -688,7 +688,7 @@ class Command(models.Model): ##name = self.name.split(' ')[0] ##cmd_name, _ = self.tokenize() #return cmd_name in self.GENERIC_COMMANDS - return self.name in self.GENERIC_COMMANDS + return self.name in self._GENERAL_COMMANDS #"CMD_OUTOFDATE" # cde périmée def is_read(self): diff --git a/src/device_controller/README.md b/src/device_controller/README.md index 5e4dc62..81cd3a0 100644 --- a/src/device_controller/README.md +++ b/src/device_controller/README.md @@ -2,9 +2,9 @@ ************************ ## DEVICES & CHANNELS ************************ -VERSION: 0.30.0 +VERSION: 0.32 -Date: 19/09/2019 +Date: 30/09/2019 By: epallier@irap.omp.eu @@ -34,7 +34,7 @@ Là, il faut arrêter avec CTRL-C (car je ne sais pas encore comment arrêter le ******************************************************************************************** # 2) RUN -Pour lancer le client sur le telescope Gemini de Alain Klotz: +Pour lancer le client sur le telescope Gemini de Guitalens (AK) : $ cd devices_controller_concrete/device_controller_Gemini/ $ ./client_telescope_gemini_controller_run.py @@ -72,10 +72,12 @@ Pour lancer le même client seulement sur le "simulateur" de telescope (localhos $ cd devices_controller_concrete/device_controller_Gemini/ $ ./client_telescope_gemini_controller_run.py local -Dans un autre terminal, lancer le simulateur: - +Ceci n'est plus nécessaire depuis le 30/9/19 car le simulateur est lancé automatiquement par le client : +((( + Dans un autre terminal, lancer le simulateur: $ ./server_telescope_gemini_simulator_run.py (CTRL-C to stop it) +))) ******************************************************************************************** diff --git a/src/device_controller/abstract_component/base.py b/src/device_controller/abstract_component/base.py index 31fae60..e9746b6 100755 --- a/src/device_controller/abstract_component/base.py +++ b/src/device_controller/abstract_component/base.py @@ -74,6 +74,51 @@ def generic_cmd(func): return wrapper_generic_cmd +class DeviceCommand: + + full_name = None + name = None + type = None + args = None + + def __init__(self, cmd_full_name:str, cmd_type:str=None, cmd_args:str=None): + self.full_name = cmd_full_name + self.name = cmd_full_name + self.type = cmd_type + self.args = cmd_args + if self.is_generic(): + cmd_type,cmd_name,cmd_args = self.get_full_name_parts() + self.name = cmd_name + if cmd_type: self.type = cmd_type + if cmd_args: self.args = cmd_args + + def __str__(self): + return (f"Commmand '{self.full_name}'") + + def is_generic(self): + return self.name.startswith('do_') or self.name.startswith('get_') or self.name.startswith('set_') + + @property + def name_and_args(self): + cmd_name_and_args = self.full_name + if '.' in cmd_name_and_args: + cmd_name_and_args = cmd_name_and_args.split('.')[1] + return cmd_name_and_args + + def get_full_name_parts(self): + cmd_name = self.full_name + cmd_type = None + cmd_args = None + if '.' in cmd_name: + cmd_type, cmd_name = cmd_name.split('.') + if ' ' in cmd_name: + cmd_name, *cmd_args = cmd_name.split(' ') + return cmd_type, cmd_name, cmd_args + + + + + class GenericResult: ''' Usage: @@ -299,10 +344,11 @@ class DeviceControllerAbstract(): self._device_simulator.stop() + ''' def is_generic_cmd(self, raw_input_cmd:str) -> bool: print("raw_input_cmd is", raw_input_cmd) # Using Google documentation format (https://www.sphinx-doc.org/en/master/usage/extensions/example_google.html#example-google) - """ Is this a generic command ? + #"" Is this a generic command ? Args: raw_input_cmd: a command in string format (like 'set_state active' or 'get_ra' or 'set_ra 20:00:00' or 'set_radec 20:00:00 90:00:00" or 'do_park'...) @@ -310,10 +356,10 @@ class DeviceControllerAbstract(): Returns: either False or (cmd, [args]) with cmd like "get_ra" and [args] like ['20:00:00', '90:00:00'] - """ + #"" #return cmd.startswith('get_') or cmd.startswith('set_') or cmd.startswith('do_') #cmds = ['get ', 'get_', 'set ', 'set_', 'do ', 'do_'] - ''' + #'' seps = (" ", "_") #cmds = list(x+y for x in cmd for y in sep) for cmd in cmds: @@ -327,16 +373,16 @@ class DeviceControllerAbstract(): # return cmd like "get_ra", [and values] return generic_cmd.replace(' ','_'), values return False, False - ''' + #'' ##cmds = ("get","set","do") - ''' + #'' # ex: "set radec" => "set_radec" raw_input_cmd = raw_input_cmd.strip() cmd_splitted = raw_input_cmd.split(' ') if len(cmd_splitted) == 1: return False,False generic_cmd = cmd_splitted[0] + '_' + cmd_splitted[1] - ''' + #'' # Ex: "set_radec 15 30", "do_init", "get_radec", "set_state active", "do_goto_radec 15 45"... tokens = raw_input_cmd.split(' ') generic_cmd = tokens[0] @@ -349,36 +395,41 @@ class DeviceControllerAbstract(): args = tokens[1:] if len(tokens)>1 else None # ex: return "set_radec", ["20:00:00", "90:00:00"] return generic_cmd, args + ''' - + #def execute_cmd(self, cmd:DeviceCommand)->GenericResult: def execute_cmd(self, raw_input_cmd:str)->GenericResult: ''' :param raw_input_cmd: ''' - generic_cmd, args = self.is_generic_cmd(raw_input_cmd) + + #generic_cmd, args = self.is_generic_cmd(raw_input_cmd) + cmd = DeviceCommand(raw_input_cmd) # GENERIC command - if generic_cmd is not False: + #if generic_cmd is not False: + if cmd.is_generic(): print("GENERIC COMMAND") - return self.execute_generic_cmd(generic_cmd, args) + #return self.execute_generic_cmd(generic_cmd, args) + return self.execute_generic_cmd(cmd.name, cmd.args) # NATIVE command - else: - ''' - if cmd.startswith('get_'): - #generic_cmd,_ = request[4:].split('(') - generic_cmd = cmd[4:] - if (generic_cmd not in self._cmd_getset.keys()) and (generic_cmd not in self._cmd_do.keys()): - #eval(request) - return self.get_radec() - print("cmd is", generic_cmd) - return self._get(generic_cmd) - return - ''' - # NATIVE command - print("NATIVE COMMAND") - res_native = self.execute_native_cmd(raw_input_cmd) - return GenericResult(res_native) + ''' + if cmd.startswith('get_'): + #generic_cmd,_ = request[4:].split('(') + generic_cmd = cmd[4:] + if (generic_cmd not in self._cmd_getset.keys()) and (generic_cmd not in self._cmd_do.keys()): + #eval(request) + return self.get_radec() + print("cmd is", generic_cmd) + return self._get(generic_cmd) + return + ''' + # NATIVE command + print("NATIVE COMMAND") + #res_native = self.execute_native_cmd(raw_input_cmd) + res_native = self.execute_native_cmd(cmd.name_and_args) + return GenericResult(res_native) #def execute_native_cmd(self, request:str, awaited_res_if_ok=None)->GenericResult: @@ -429,11 +480,12 @@ class DeviceControllerAbstract(): def print_available_commands(self): print("\nAvailable commands are:") print("- GET commands:") - print (list(cmd.replace('_',' ') for cmd in self._cmd.keys() if cmd.startswith('get_'))) + #print (list(cmd.replace('_',' ') for cmd in self._cmd.keys() if cmd.startswith('get_'))) + print (list(cmd for cmd in self._cmd.keys() if cmd.startswith('get_'))) print("- SET commands:") - print (list(cmd.replace('_',' ') for cmd in self._cmd.keys() if cmd.startswith('set_'))) + print (list(cmd for cmd in self._cmd.keys() if cmd.startswith('set_'))) print("- DO commands:") - print (list(cmd.replace('_',' ') for cmd in self._cmd.keys() if cmd.startswith('do_'))) + print (list(cmd for cmd in self._cmd.keys() if cmd.startswith('do_'))) def available_commands(self): @@ -457,6 +509,7 @@ class DeviceControllerAbstract(): TELESCOPE COMMANDS (abstract methods) ''' + #def execute_generic_cmd(self, generic_cmd:DeviceCommand)->str: def execute_generic_cmd(self, generic_cmd:str, values_to_set:str=None)->str: ''' Execute a generic command @@ -465,7 +518,7 @@ class DeviceControllerAbstract(): ''' #log_d("\n\nGENERIC Command to send is "+generic_cmd) - print("\n\nGENERIC Command to send is "+generic_cmd) + print("\n\nGENERIC Command to send is ", generic_cmd) # Check if generic_param exists #if generic_cmd not in self._cmd.keys(): raise UnknownCommandException() # if this generic command has no corresponding native command, raise NotImplementedError @@ -493,9 +546,7 @@ class DeviceControllerAbstract(): return res # NATIVE COMMAND (ex: native_cmd == "GR") - else: - native_cmd = self.formated_cmd(native_cmd,values_to_set) - + native_cmd = self.formated_cmd(native_cmd, values_to_set) awaited_res_if_ok = None if len(native_cmd_infos) > 1: awaited_res_if_ok = native_cmd_infos[1] #native_res = self.execute_native_cmd(self.formated_cmd(native_cmd,value), awaited_res_ok) diff --git a/src/device_controller/concrete_component/gemini/client_telescope_gemini_controller_run.py b/src/device_controller/concrete_component/gemini/client_telescope_gemini_controller_run.py index fa30e39..1198638 100755 --- a/src/device_controller/concrete_component/gemini/client_telescope_gemini_controller_run.py +++ b/src/device_controller/concrete_component/gemini/client_telescope_gemini_controller_run.py @@ -14,7 +14,12 @@ ou (2) #from gemini.telescope_controller_gemini import TelescopeControllerGemini ou (3) ''' -from gemini_telescope_controller import DeviceControllerTelescopeGemini + + +sys.path.append("../../..") +from device_controller.abstract_component.base import DeviceCommand +#from gemini_telescope_controller import DeviceControllerTelescopeGemini +from device_controller.concrete_component.gemini.gemini_telescope_controller import DeviceControllerTelescopeGemini #DEBUG = False DEBUG = True @@ -102,6 +107,7 @@ def main(): cmd = input("REQUEST TO SERVER (ENTER to quit): >>> ").strip() if not cmd: break + cmd = DeviceCommand(cmd) #res = tele_ctrl.execute_cmd(cmd.replace(' ', '_')) res = tele_ctrl.execute_cmd(cmd) print("result is", str(res)) -- libgit2 0.21.2