diff --git a/src/device_controller/abstract_component/device_controller.py b/src/device_controller/abstract_component/device_controller.py index 99ce797..9127875 100755 --- a/src/device_controller/abstract_component/device_controller.py +++ b/src/device_controller/abstract_component/device_controller.py @@ -36,6 +36,7 @@ from src.logpyros import LogPyros ##from src_device.client.client_channel import * sys.path.append("../..") ##from device_controller.logs import * +from device_controller.channels.client_channel import ChannelCommunicationException from device_controller.channels.client_channel_socket import ClientChannelSocket from device_controller.channels.client_channel_serial import ClientChannelSerial from device_controller.channels.client_channel_usb import ClientChannelUSB @@ -1075,9 +1076,22 @@ class DeviceController(): """ self.tprintd("NATIVE Command to send is "+ repr(native_cmd)) - #self.send_request(native_cmd) - self.send_native_cmd(native_cmd) - native_res = self.receive_data() + + # SEND command TO my device + try: + #self.send_request(native_cmd) + self.send_native_cmd(native_cmd) + except ChannelCommunicationException as e: + self.log_e("Channel error while sending command", e) + raise + + # RECEIVE device answer FROM my device + try: + native_res = self.receive_data() + except ChannelCommunicationException as e: + self.log_e("Channel error while sending command", e) + raise + if self.means_unknown_cmd(native_res): raise UnknownNativeCmdException(native_cmd) if self.is_unknown_res(native_res): raise UnknownNativeResException(native_res) return native_res @@ -1164,55 +1178,9 @@ class DeviceController(): :param generic_cmd: str like "get_ra" or "set_ra" or "do_park"... :param value: only for a "set_" cmd - - General Algo: - - A - REQUEST (QUESTION) management: - - (1) nc = get_native_cmd_for_generic(gc) - // can raise NotImplementedNativeCmdException (concrete) - // (use gen2nat_cmds) - - (2) na = get_native_answer_for_native_cmd_from_device(nc) - // can raise UnknownNativeCmdException - // or UnknownNativeAnswerException - // or ChannelCommunicationError - - B - ANSWER management: - - (3) ga = get_generic_answer_for_native(na) - // can raise NotImplementedGenericAnswerException - // (use gen2nat_cmds) - - (4) send_generic_answer(ga) - // can raise ChannelCommunicationError - ''' - self.tprintd("(DC):exec_generic_cmd() from", self) - - ''' - # If generic_cmd is for a specific device component (dc), pass it to this dc (instead of me) - ####if dc_component_type and dc_component_type not in self.__class__.__name__ : - if dcc_type: - - # Get the dcc in charge of this command - try: - dcc = self.get_dc_component_for_type(dcc_type) - self.tprintd("*** EXECUTÉ PAR COMPONENT", dcc) - except DCCNotFoundException as e: - self.log_e(f"THREAD EXCEPTION caught by {type(self).__name__} (from dcc)", e) - raise - #return (DCC)(self.exec_generic_cmd(generic_cmd, values_to_set, None)) - # Delegate cmd execution to the dcc - try: - return dcc.exec_generic_cmd(generic_cmd, values_to_set, None) - except UnimplementedGenericCmdException as e: - self.log_e(f"THREAD EXCEPTION caught by {type(self).__name__} (from dcc)", e) - raise - # not executed ? - return None - ''' + self.tprintd("(DC):exec_generic_cmd() from", self) #log_d("\n\nGENERIC Command to send is "+generic_cmd) self.tprintd("\n(DC): GENERIC Command to execute is ", generic_cmd) ##printd("(DC): My ("+type(self).__name__+") commands are:", self._gen2nat_cmds) @@ -1239,7 +1207,9 @@ class DeviceController(): native_cmd = native_cmd_infos[0] if not native_cmd: raise UnimplementedGenericCmdException(generic_cmd) - # MACRO-COMMAND (ex: native_cmd == "do_goto", "do_init", "get_radec") + # 2) MACRO-COMMAND or NORMAL NATIVE COMMAND ? + + # 2.a) MACRO-COMMAND (ex: native_cmd == "do_goto", "do_init", "get_radec") if native_cmd == generic_cmd: self.tprintd("MACRO-COMMAND") #printd("cmd,val", native_cmd, values_to_set) @@ -1254,7 +1224,7 @@ class DeviceController(): res = dcc.run_func(native_cmd) ##res = self.run_func(native_cmd) #res = getattr(self, native_cmd)() - except AttributeError as e: + except (AttributeError, ChannelCommunicationException) as e: self.log_e("Unknown Native command ?", native_cmd) raise UnknownNativeCmdException(native_cmd) #if res is None: res = 'ok' @@ -1263,9 +1233,10 @@ class DeviceController(): # raise Exception if ERROR if res.unknown_command: raise UnknownNativeCmdException(native_cmd) if res.unknown_result: raise UnknownNativeResException(res.txt) + # Return Generic result return res - # NATIVE COMMAND (ex: native_cmd == "GR") + # 2.b) NORMAL NATIVE COMMAND (ex: native_cmd == "GR") ##native_cmd = self.formated_cmd(native_cmd, values_to_set) native_cmd = dcc.formated_cmd(native_cmd, values_to_set) awaited_res_if_ok = None @@ -1274,9 +1245,11 @@ class DeviceController(): ##native_res = self.exec_native_cmd(native_cmd) try: native_res = dcc.exec_native_cmd(native_cmd) - except (UnknownNativeCmdException, UnknownNativeResException) as e: # TODO: a implementer dans exec_native_cmd() + except (ChannelCommunicationException, UnknownNativeCmdException, UnknownNativeResException) as e: # TODO: a implementer dans exec_native_cmd() self.log_e(f"THREAD Native command execution caught by {type(self).__name__} (from DC)", e) raise + + # 3) Make Generic result from native and return it ok = True if not awaited_res_if_ok else (native_res == awaited_res_if_ok) return GenericResult(native_res, ok) diff --git a/src/device_controller/channels/client_channel.py b/src/device_controller/channels/client_channel.py index 51b4ba2..6688ba1 100755 --- a/src/device_controller/channels/client_channel.py +++ b/src/device_controller/channels/client_channel.py @@ -22,6 +22,8 @@ def printd(*args, **kwargs): if os.environ.get('PYROS_DEBUG', '0')=='1': print(*args, **kwargs) ''' +class ChannelCommunicationException(Exception): + pass ##class SocketClientAbstract(): class ClientChannel(): -- libgit2 0.21.2