Commit 38cf2c43eea3e65248011f051fca09bb6312e596

Authored by Etienne Pallier
1 parent 482a241f
Exists in dev

wrappers sur DC command class pour clarté et bugfix

src/core/pyros_django/agent/AgentDevice.py
... ... @@ -543,7 +543,7 @@ class AgentDevice(Agent):
543 543 self.printd("*** DEVICE cmd name is", cmd.name)
544 544 self.printd("*** PASS IT TO DEVICE TYPE", cmd.device_type)
545 545 try:
546   - res = self._device_ctrl.execute_cmd(cmd.full_name)
  546 + res = self._device_ctrl.exec_cmd(cmd.full_name)
547 547 except (DCCNotFoundException, UnimplementedGenericCmdException) as e:
548 548 print(f"EXCEPTION caught by {type(self).__name__} (from AD)", e)
549 549 raise
... ... @@ -557,7 +557,7 @@ class AgentDevice(Agent):
557 557 cmd = self._current_device_cmd
558 558 print("cmd name is", cmd.name)
559 559 #res = self._device_ctrl.execute_cmd(cmd.name_and_args)
560   - res = self._device_ctrl.execute_cmd(cmd.full_name)
  560 + res = self._device_ctrl.exec_cmd(cmd.full_name)
561 561 ##cmd.set_result(str(res))
562 562 print("result is", str(res))
563 563 if res.ok: print("OK")
... ...
src/core/pyros_django/agent/AgentDeviceGemini.py
... ... @@ -137,7 +137,7 @@ class AgentDeviceGemini(AgentDevice):
137 137  
138 138 #cmd="get date"
139 139 cmd="get_date"
140   - res = self._device_ctrl.execute_cmd(cmd)
  140 + res = self._device_ctrl.exec_cmd(cmd)
141 141 self.printd("result is", str(res))
142 142 if res.ok: print("OK")
143 143 dev_date = str(res)
... ... @@ -145,7 +145,7 @@ class AgentDeviceGemini(AgentDevice):
145 145  
146 146 #cmd="get time"
147 147 cmd="get_time"
148   - res = self._device_ctrl.execute_cmd(cmd)
  148 + res = self._device_ctrl.exec_cmd(cmd)
149 149 self.printd("result is", str(res))
150 150 if res.ok: print("OK")
151 151 dev_time = str(res)
... ... @@ -153,7 +153,7 @@ class AgentDeviceGemini(AgentDevice):
153 153  
154 154 #cmd="get radec"
155 155 cmd="get_radec"
156   - res = self._device_ctrl.execute_cmd(cmd)
  156 + res = self._device_ctrl.exec_cmd(cmd)
157 157 self.printd("result is", str(res))
158 158 if res.ok: print("OK")
159 159 dev_radec = str(res)
... ...
src/core/pyros_django/agent/AgentDeviceSBIG.py
... ... @@ -152,14 +152,14 @@ class AgentDeviceSBIG(AgentDevice):
152 152 def get_device_status(self):
153 153  
154 154 cmd="get_date"
155   - res = self._device_ctrl.execute_cmd(cmd)
  155 + res = self._device_ctrl.exec_cmd(cmd)
156 156 self.printd("result is", str(res))
157 157 if res.ok: print("OK")
158 158 dev_date = str(res)
159 159 time.sleep(1)
160 160  
161 161 cmd="get_time"
162   - res = self._device_ctrl.execute_cmd(cmd)
  162 + res = self._device_ctrl.exec_cmd(cmd)
163 163 self.printd("result is", str(res))
164 164 if res.ok: print("OK")
165 165 dev_time = str(res)
... ...
src/device_controller/abstract_component/device_controller.py
... ... @@ -68,7 +68,7 @@ def generic_cmd(func):
68 68 @functools.wraps(func)
69 69 def wrapper_generic_cmd(self, values_to_set=None):
70 70 #print("func name is", func.__name__)
71   - return self.execute_generic_cmd(func.__name__, values_to_set)
  71 + return self.exec_generic_cmd(func.__name__, values_to_set)
72 72 return wrapper_generic_cmd
73 73  
74 74  
... ... @@ -229,30 +229,35 @@ class Gen2NatCmds:
229 229 #return self.GEN2NAT_CMDS
230 230 if cmd is None: return self.GEN2NAT_CMDS
231 231 # 1) search in my MAIN commands
232   - native_cmd = self.GEN2NAT_CMDS.get(cmd)
  232 + native_infos = self.GEN2NAT_CMDS.get(cmd)
233 233 # native_cmd can be None, [], or [infos]
234   - if native_cmd is not None: return native_cmd
  234 + if native_infos is not None: return native_infos
  235 + #print(self.GEN2NAT_CMDS)
  236 + '''
235 237 # 2) search in each DCC commands
236   - print(self.GEN2NAT_CMDS)
  238 + # !! BAD !! because general dict is not complete for each dcc (especially, does not contain macro-commands, like get_radec, ...)
  239 + # So, better not to use this
237 240 for key in self.GEN2NAT_CMDS.keys():
238 241 if isinstance(self.GEN2NAT_CMDS[key], dict):
239   - native_cmd = self.GEN2NAT_CMDS[key].get(cmd)
240   - print('key is', key, 'native cmd is', native_cmd)
241   - if native_cmd is not None: return native_cmd
  242 + native_infos = self.GEN2NAT_CMDS[key].get(cmd)
  243 + print('key is', key, 'native cmd is', native_infos)
  244 + if native_infos is not None: return native_infos
  245 + '''
  246 + # Native infos not found
242 247 return None
243 248 #def update(self, newdict:dict): self.GEN2NAT_CMDS = { **self.GEN2NAT_CMDS, **newdict }
244 249 def get_native_infos_for_generic_cmd(self, cmd:str)->str: return self.get(cmd)
245   - def get_native_for_generic_cmd(self, cmd:str)->str:
  250 + def get_native_cmd_for_generic(self, cmd:str)->str:
246 251 val = self.get_native_infos_for_generic_cmd(cmd)
247 252 if not val: return None
248 253 return val[0]
249   - def get_answer_for_generic_cmd(self, cmd:str)->str:
  254 + def get_simulated_answer_for_generic_cmd(self, cmd:str)->str:
250 255 val = self.get(cmd)
251 256 if not val: return None
252 257 # no answer available
253 258 if len(val) == 1: return None
254 259 return val[1]
255   - def get_answer_for_native_cmd(self, cmd:str)->str:
  260 + def get_simulated_answer_for_native_cmd(self, cmd:str)->str:
256 261 for val in self.GEN2NAT_CMDS.values():
257 262 if cmd in val:
258 263 # no answer available
... ... @@ -268,8 +273,6 @@ class Gen2NatCmds:
268 273 if isinstance(self.GEN2NAT_CMDS[key], dict):
269 274 if cmd in self.GEN2NAT_CMDS[key].values(): return True
270 275 return False
271   -
272   -
273 276 def print_available_cmds(self):
274 277 #print("All commands are:", self._gen2nat_cmds.keys())
275 278 print("\nAvailable commands:")
... ... @@ -291,77 +294,7 @@ class Gen2NatCmds:
291 294 print("- DO commands:")
292 295 print (list(cmd for cmd in d.keys() if cmd.startswith('do_')))
293 296  
294   -''' STATIC version
295   -class Gen2NatCmds:
296   - GEN2NAT_CMDS = {
297   - # GET-SET commands:
298   -
299   - 'get_timezone': [],
300   - 'set_timezone': [],
301   -
302   - 'get_date': [],
303   - 'set_date': [],
304   -
305   - 'get_time': [],
306   - 'set_time': [],
307   -
308   - # for test only
309   - 'dc_only':[],
310 297  
311   - # DO commands:
312   - 'do_init': ['do_init'],
313   - 'do_park': [],
314   - }
315   - @classmethod
316   - def __str__(cls)->str: return str(cls.GEN2NAT_CMDS)
317   - @classmethod
318   - def get(cls, cmd:str)->str: return cls.GEN2NAT_CMDS.get(cmd)
319   - @classmethod
320   - def update(cls, newdict:dict): cls.GEN2NAT_CMDS = { **cls.GEN2NAT_CMDS, **newdict }
321   - @classmethod
322   - def get_native_for_generic_cmd(cls, cmd:str)->str:
323   - val = cls.get(cmd)
324   - if not val: return None
325   - return val[0]
326   - @classmethod
327   - def get_answer_for_generic_cmd(cls, cmd:str)->str:
328   - val = cls.get(cmd)
329   - if not val: return None
330   - # no answer available
331   - if len(val) == 1: return None
332   - return val[1]
333   - @classmethod
334   - def get_answer_for_native_cmd(cls, cmd:str)->str:
335   - for val in cls.GEN2NAT_CMDS.values():
336   - if cmd in val:
337   - # no answer available
338   - if len(val) == 1: return None
339   - # return 1st answer available
340   - return val[1]
341   - return None
342   - @classmethod
343   - def print_available_commands(cls):
344   - #print("All commands are:", self._gen2nat_cmds.keys())
345   - print("\nAvailable commands:")
346   - print("=======================")
347   - # 1) print general commands
348   - cls.print_available_commands_for_dcc("General")
349   - # 2) print commands for each DCC:
350   - for key in cls.GEN2NAT_CMDS.keys():
351   - if isinstance(cls.GEN2NAT_CMDS[key], dict):
352   - cls.print_available_commands_for_dcc(key)
353   - @classmethod
354   - def print_available_commands_for_dcc(cls, dcc_key):
355   - d = cls.GEN2NAT_CMDS if dcc_key=="General" else cls.GEN2NAT_CMDS[dcc_key]
356   - print(f"\n{dcc_key} commands are:")
357   - print("- GET commands:")
358   - #print (list(cmd.replace('_',' ') for cmd in self._gen2nat_cmds.keys() if cmd.startswith('get_')))
359   - print (list(cmd for cmd in d.keys() if cmd.startswith('get_')))
360   - print("- SET commands:")
361   - print (list(cmd for cmd in d.keys() if cmd.startswith('set_')))
362   - print("- DO commands:")
363   - print (list(cmd for cmd in d.keys() if cmd.startswith('do_')))
364   -'''
365 298  
366 299  
367 300  
... ... @@ -450,6 +383,7 @@ class DeviceController():
450 383 return self._my_channel.uncap_received_data(data_received)
451 384 '''
452 385  
  386 + # WRAPPER methods
453 387 def formated_cmd(self, cmd:str, value:str=None)->str:
454 388 ##return self._protoc.formated_cmd(self, cmd, value)
455 389 return self._protoc.formated_cmd(cmd, value)
... ... @@ -541,6 +475,7 @@ class DeviceController():
541 475 self._my_channel.set_logger(DEBUG)
542 476 '''
543 477  
  478 +
544 479 def device_simulator_run(self):
545 480 #HOST, PORT = "localhost", 11110
546 481 #with get_SocketServer_UDP_TCP(HOST, PORT, "UDP") as myserver:
... ... @@ -717,25 +652,61 @@ class DeviceController():
717 652 def is_generic_but_UNIMPLEMENTED_cmd(self, cmd:DeviceCommand):
718 653 return cmd.is_generic() and not self.is_implemented_generic_cmd(cmd)
719 654  
  655 + # WRAPPER methods
  656 + def has_generic_command(self, generic_cmd:DeviceCommand):
  657 + #return self._my_cmds.get(cmd.name) is not None
  658 + return generic_cmd.name in self._my_cmds.get()
  659 + def has_native_cmd_for_generic(self, generic_cmd:DeviceCommand):
  660 + return self._my_cmds.get_native_infos_for_generic_cmd(generic_cmd.name) not in [None, []]
  661 +
720 662 # check if generic cmd exists (in dictionary)
721 663 def is_valid_generic_cmd(self, cmd:DeviceCommand):
722 664 #print("_my_cmds", self._my_cmds)
723   - if not cmd.devtype: return self._my_cmds.get(cmd.name) is not None
  665 + ##if not cmd.devtype: return self._my_cmds.get(cmd.name) is not None
  666 + if not cmd.devtype: return self.has_generic_command(cmd)
724 667 if not self.has_dc_component_for_type(cmd.devtype): return False
725   - return self.get_dc_component_for_type(cmd.devtype)._my_cmds.get(cmd.name) is not None
  668 + ##return self.get_dc_component_for_type(cmd.devtype)._my_cmds.get(cmd.name) is not None
  669 + return self.get_dc_component_for_type(cmd.devtype).has_generic_command(cmd)
726 670  
727 671 # check if generic cmd exists and is implemented as a native cmd (in dictionary)
728 672 def is_implemented_generic_cmd(self, cmd:DeviceCommand):
729 673 #print("_my_cmds", self._my_cmds)
730 674 #return self._my_cmds.get_native_infos_for_generic_cmd(cmd.name) not in [None, []]
731   - if not cmd.devtype: return self._my_cmds.get_native_infos_for_generic_cmd(cmd.name) not in [None, []]
  675 + ##if not cmd.devtype: return self._my_cmds.get_native_infos_for_generic_cmd(cmd.name) not in [None, []]
  676 + if not cmd.devtype: return self.has_native_cmd_for_generic(cmd)
732 677 if not self.has_dc_component_for_type(cmd.devtype): return False
733   - return self.get_dc_component_for_type(cmd.devtype)._my_cmds.get(cmd.name) not in [None, []]
  678 + ##return self.get_dc_component_for_type(cmd.devtype)._my_cmds.get(cmd.name) not in [None, []]
  679 + return self.get_dc_component_for_type(cmd.devtype).has_native_cmd_for_generic(cmd)
734 680  
735   - # TODO:
736   - def is_valid_native_cmd(self, cmd:DeviceCommand):
737   - return self._my_cmds.is_valid_native_cmd(cmd.name)
  681 + def is_valid_native_cmd(self, native_cmd:DeviceCommand):
  682 + return self._my_cmds.is_valid_native_cmd(native_cmd.name)
  683 +
  684 + def get_dcc_and_native_cmd_for_generic(self, generic_cmd:str):
  685 + #if generic_cmd not in self._gen2nat_cmds.keys(): raise UnknownNativeCmdException()
  686 + # Is it a general command for the (general) controller ?
  687 + '''
  688 + if generic_cmd in self._gen2nat_cmds:
  689 + return self, self._gen2nat_cmds[generic_cmd]
  690 + '''
  691 + # 1) Search in my MAIN commands
  692 + nc_infos = self._my_cmds.get_native_infos_for_generic_cmd(generic_cmd)
  693 + if nc_infos: return self, nc_infos
738 694  
  695 + # 2) Search in each DCC commands
  696 + # Is it a command for one of my dcc ?
  697 + '''
  698 + # Bad because general dict is not complete for each dcc (especially, does not contain macro-commands, like get_radec, ...)
  699 + for key in self._gen2nat_cmds.keys():
  700 + if isinstance(self._gen2nat_cmds[key], dict):
  701 + # dcc = key
  702 + if generic_cmd in self._gen2nat_cmds[key]:
  703 + return key, self._gen2nat_cmds[key][generic_cmd]
  704 + '''
  705 + for dcc in self._my_dc_components:
  706 + _, native_cmd_infos = dcc.get_dcc_and_native_cmd_for_generic(generic_cmd)
  707 + if native_cmd_infos: return dcc, native_cmd_infos
  708 + # Native command not found
  709 + return None, None
739 710  
740 711 '''
741 712 This method is either called
... ... @@ -744,7 +715,7 @@ class DeviceController():
744 715 - FROM A THREAD from AgentDevice._thread_exec_specific_cmd().exec_specific_cmd()
745 716 '''
746 717 #def execute_cmd(self, cmd:DeviceCommand)->GenericResult:
747   - def execute_cmd(self, raw_input_cmd:str)->GenericResult:
  718 + def exec_cmd(self, raw_input_cmd:str)->GenericResult:
748 719 '''
749 720 :param raw_input_cmd:
750 721 '''
... ... @@ -758,9 +729,9 @@ class DeviceController():
758 729 if cmd.is_generic():
759 730 if not self.is_valid_generic_cmd(cmd): raise UnknownGenericCmdException(cmd.name)
760 731 print("GENERIC COMMAND")
761   - #return self.execute_generic_cmd(generic_cmd, args)
  732 + #return self.exec_generic_cmd(generic_cmd, args)
762 733 try:
763   - res = self.execute_generic_cmd(cmd.name, cmd.args, cmd.devtype)
  734 + res = self.exec_generic_cmd(cmd.name, cmd.args, cmd.devtype)
764 735 except (DCCNotFoundException, UnimplementedGenericCmdException) as e:
765 736 print(f"EXCEPTION caught by {type(self).__name__} (from DC)", e)
766 737 raise
... ... @@ -835,6 +806,7 @@ class DeviceController():
835 806 return self.send_native_cmd(request)
836 807  
837 808  
  809 + # wrapper methods
838 810 def print_available_cmds(self): self._my_cmds.print_available_cmds()
839 811 def print_available_cmds_for_dcc(self, dcc_key): self._my_cmds.print_available_cmds_for_dcc(dcc_key)
840 812  
... ... @@ -874,39 +846,17 @@ class DeviceController():
874 846 return getattr(self, func)()
875 847  
876 848  
877   - def getNativeCommandForGeneric(self, generic_cmd:str):
878   - #if generic_cmd not in self._gen2nat_cmds.keys(): raise UnknownNativeCmdException()
879   - # Is it a general command for the (general) controller ?
880   - '''
881   - if generic_cmd in self._gen2nat_cmds:
882   - return self, self._gen2nat_cmds[generic_cmd]
883   - '''
884   - nc_infos = self._my_cmds.get_native_infos_for_generic_cmd(generic_cmd)
885   - if nc_infos: return self, nc_infos
886   - # Is it a command for one of my dcc ?
887   - '''
888   - # Bad because general dict is not complete for each dcc (especially, does not contain macro-commands, like get_radec, ...)
889   - for key in self._gen2nat_cmds.keys():
890   - if isinstance(self._gen2nat_cmds[key], dict):
891   - # dcc = key
892   - if generic_cmd in self._gen2nat_cmds[key]:
893   - return key, self._gen2nat_cmds[key][generic_cmd]
894   - '''
895   - for dcc in self._my_dc_components:
896   - _, native_cmd_infos = dcc.getNativeCommandForGeneric(generic_cmd)
897   - if native_cmd_infos: return dcc, native_cmd_infos
898   - # Native command not found
899   - return None, None
900 849  
901 850  
902   - #def execute_generic_cmd(self, generic_cmd:DeviceCommand)->str:
903   - def execute_generic_cmd(self, generic_cmd:str, values_to_set:str=None, dcc_type:str=None)->str:
  851 +
  852 + #def exec_generic_cmd(self, generic_cmd:DeviceCommand)->str:
  853 + def exec_generic_cmd(self, generic_cmd:str, values_to_set:str=None, dcc_type:str=None)->str:
904 854 ''' Execute a generic command
905 855  
906 856 :param generic_cmd: str like "get_ra" or "set_ra" or "do_park"...
907 857 :param value: only for a "set_" cmd
908 858 '''
909   - print("(DC):execute_generic_cmd() from", self)
  859 + print("(DC):exec_generic_cmd() from", self)
910 860 # If generic_cmd is for a specific device component (dc), pass it to this dc (instead of me)
911 861 print("(DC):dc_component_type is: ", dcc_type)
912 862 ####if dc_component_type and dc_component_type not in self.__class__.__name__ :
... ... @@ -919,11 +869,11 @@ class DeviceController():
919 869 except DCCNotFoundException as e:
920 870 print(f"EXCEPTION caught by {type(self).__name__} (from dcc)", e)
921 871 raise
922   - #return (DCC)(self.execute_generic_cmd(generic_cmd, values_to_set, None))
  872 + #return (DCC)(self.exec_generic_cmd(generic_cmd, values_to_set, None))
923 873  
924 874 # Delegate cmd execution to the dcc
925 875 try:
926   - return dcc.execute_generic_cmd(generic_cmd, values_to_set, None)
  876 + return dcc.exec_generic_cmd(generic_cmd, values_to_set, None)
927 877 except UnimplementedGenericCmdException as e:
928 878 print(f"EXCEPTION caught by {type(self).__name__} (from dcc)", e)
929 879 raise
... ... @@ -935,17 +885,14 @@ class DeviceController():
935 885 ##print("(DC): My ("+type(self).__name__+") commands are:", self._gen2nat_cmds)
936 886 print("(DC): My ("+type(self).__name__+") commands are:", self._my_cmds.get())
937 887  
938   - ##if generic_cmd not in self._gen2nat_cmds: raise UnimplementedGenericCmdException
939   - #if generic_cmd not in self._gen2nat_cmds.keys(): raise UnknownNativeCmdException()
940   - ##native_cmd_infos = self._gen2nat_cmds[generic_cmd]
941   - dcc, native_cmd_infos = self.getNativeCommandForGeneric(generic_cmd)
  888 + dcc, native_cmd_infos = self.get_dcc_and_native_cmd_for_generic(generic_cmd)
942 889 print("native_cmd_infos", native_cmd_infos)
943 890 # Command is not implemented
944 891 if not native_cmd_infos: raise UnimplementedGenericCmdException(generic_cmd)
945 892 # Command is one of my dcc's, so pass it the command for exec
946 893 if dcc != self:
947 894 #dcc = self.get_dc_component_for_type(dcc_type)
948   - return dcc.execute_generic_cmd(generic_cmd, values_to_set, None)
  895 + return dcc.exec_generic_cmd(generic_cmd, values_to_set, None)
949 896  
950 897 # Get corresponding native command:
951 898 native_cmd = native_cmd_infos[0]
... ... @@ -998,10 +945,10 @@ class DeviceController():
998 945  
999 946 @generic_cmd
1000 947 def get_timezone(self): pass
1001   - #def get_timezone(self): return self.execute_generic_cmd('get_timezone')
  948 + #def get_timezone(self): return self.exec_generic_cmd('get_timezone')
1002 949 @generic_cmd
1003 950 def set_timezone(self, hh): pass
1004   - #def set_timezone(self, hh): return self.execute_generic_cmd('set_timezone', hh)
  951 + #def set_timezone(self, hh): return self.exec_generic_cmd('set_timezone', hh)
1005 952  
1006 953 @generic_cmd
1007 954 def get_date(self): pass
... ...