Commit 0538ed53047c1c5592432d523d9d18dac6cd0d18

Authored by Etienne Pallier
1 parent 1b4dae11
Exists in dev

cleanup

src/device_controller/abstract_component/device_controller.py
... ... @@ -36,6 +36,7 @@ from src.logpyros import LogPyros
36 36 ##from src_device.client.client_channel import *
37 37 sys.path.append("../..")
38 38 ##from device_controller.logs import *
  39 +from device_controller.channels.client_channel import ChannelCommunicationException
39 40 from device_controller.channels.client_channel_socket import ClientChannelSocket
40 41 from device_controller.channels.client_channel_serial import ClientChannelSerial
41 42 from device_controller.channels.client_channel_usb import ClientChannelUSB
... ... @@ -1075,9 +1076,22 @@ class DeviceController():
1075 1076  
1076 1077 """
1077 1078 self.tprintd("NATIVE Command to send is "+ repr(native_cmd))
1078   - #self.send_request(native_cmd)
1079   - self.send_native_cmd(native_cmd)
1080   - native_res = self.receive_data()
  1079 +
  1080 + # SEND command TO my device
  1081 + try:
  1082 + #self.send_request(native_cmd)
  1083 + self.send_native_cmd(native_cmd)
  1084 + except ChannelCommunicationException as e:
  1085 + self.log_e("Channel error while sending command", e)
  1086 + raise
  1087 +
  1088 + # RECEIVE device answer FROM my device
  1089 + try:
  1090 + native_res = self.receive_data()
  1091 + except ChannelCommunicationException as e:
  1092 + self.log_e("Channel error while sending command", e)
  1093 + raise
  1094 +
1081 1095 if self.means_unknown_cmd(native_res): raise UnknownNativeCmdException(native_cmd)
1082 1096 if self.is_unknown_res(native_res): raise UnknownNativeResException(native_res)
1083 1097 return native_res
... ... @@ -1164,55 +1178,9 @@ class DeviceController():
1164 1178  
1165 1179 :param generic_cmd: str like "get_ra" or "set_ra" or "do_park"...
1166 1180 :param value: only for a "set_" cmd
1167   -
1168   - General Algo:
1169   -
1170   - A - REQUEST (QUESTION) management:
1171   -
1172   - (1) nc = get_native_cmd_for_generic(gc)
1173   - // can raise NotImplementedNativeCmdException (concrete)
1174   - // (use gen2nat_cmds)
1175   -
1176   - (2) na = get_native_answer_for_native_cmd_from_device(nc)
1177   - // can raise UnknownNativeCmdException
1178   - // or UnknownNativeAnswerException
1179   - // or ChannelCommunicationError
1180   -
1181   - B - ANSWER management:
1182   -
1183   - (3) ga = get_generic_answer_for_native(na)
1184   - // can raise NotImplementedGenericAnswerException
1185   - // (use gen2nat_cmds)
1186   -
1187   - (4) send_generic_answer(ga)
1188   - // can raise ChannelCommunicationError
1189   -
1190 1181 '''
1191   - self.tprintd("(DC):exec_generic_cmd() from", self)
1192   -
1193   - '''
1194   - # If generic_cmd is for a specific device component (dc), pass it to this dc (instead of me)
1195   - ####if dc_component_type and dc_component_type not in self.__class__.__name__ :
1196   - if dcc_type:
1197   -
1198   - # Get the dcc in charge of this command
1199   - try:
1200   - dcc = self.get_dc_component_for_type(dcc_type)
1201   - self.tprintd("*** EXECUTร‰ PAR COMPONENT", dcc)
1202   - except DCCNotFoundException as e:
1203   - self.log_e(f"THREAD EXCEPTION caught by {type(self).__name__} (from dcc)", e)
1204   - raise
1205   - #return (DCC)(self.exec_generic_cmd(generic_cmd, values_to_set, None))
1206 1182  
1207   - # Delegate cmd execution to the dcc
1208   - try:
1209   - return dcc.exec_generic_cmd(generic_cmd, values_to_set, None)
1210   - except UnimplementedGenericCmdException as e:
1211   - self.log_e(f"THREAD EXCEPTION caught by {type(self).__name__} (from dcc)", e)
1212   - raise
1213   - # not executed ?
1214   - return None
1215   - '''
  1183 + self.tprintd("(DC):exec_generic_cmd() from", self)
1216 1184 #log_d("\n\nGENERIC Command to send is "+generic_cmd)
1217 1185 self.tprintd("\n(DC): GENERIC Command to execute is ", generic_cmd)
1218 1186 ##printd("(DC): My ("+type(self).__name__+") commands are:", self._gen2nat_cmds)
... ... @@ -1239,7 +1207,9 @@ class DeviceController():
1239 1207 native_cmd = native_cmd_infos[0]
1240 1208 if not native_cmd: raise UnimplementedGenericCmdException(generic_cmd)
1241 1209  
1242   - # MACRO-COMMAND (ex: native_cmd == "do_goto", "do_init", "get_radec")
  1210 + # 2) MACRO-COMMAND or NORMAL NATIVE COMMAND ?
  1211 +
  1212 + # 2.a) MACRO-COMMAND (ex: native_cmd == "do_goto", "do_init", "get_radec")
1243 1213 if native_cmd == generic_cmd:
1244 1214 self.tprintd("MACRO-COMMAND")
1245 1215 #printd("cmd,val", native_cmd, values_to_set)
... ... @@ -1254,7 +1224,7 @@ class DeviceController():
1254 1224 res = dcc.run_func(native_cmd)
1255 1225 ##res = self.run_func(native_cmd)
1256 1226 #res = getattr(self, native_cmd)()
1257   - except AttributeError as e:
  1227 + except (AttributeError, ChannelCommunicationException) as e:
1258 1228 self.log_e("Unknown Native command ?", native_cmd)
1259 1229 raise UnknownNativeCmdException(native_cmd)
1260 1230 #if res is None: res = 'ok'
... ... @@ -1263,9 +1233,10 @@ class DeviceController():
1263 1233 # raise Exception if ERROR
1264 1234 if res.unknown_command: raise UnknownNativeCmdException(native_cmd)
1265 1235 if res.unknown_result: raise UnknownNativeResException(res.txt)
  1236 + # Return Generic result
1266 1237 return res
1267 1238  
1268   - # NATIVE COMMAND (ex: native_cmd == "GR")
  1239 + # 2.b) NORMAL NATIVE COMMAND (ex: native_cmd == "GR")
1269 1240 ##native_cmd = self.formated_cmd(native_cmd, values_to_set)
1270 1241 native_cmd = dcc.formated_cmd(native_cmd, values_to_set)
1271 1242 awaited_res_if_ok = None
... ... @@ -1274,9 +1245,11 @@ class DeviceController():
1274 1245 ##native_res = self.exec_native_cmd(native_cmd)
1275 1246 try:
1276 1247 native_res = dcc.exec_native_cmd(native_cmd)
1277   - except (UnknownNativeCmdException, UnknownNativeResException) as e: # TODO: a implementer dans exec_native_cmd()
  1248 + except (ChannelCommunicationException, UnknownNativeCmdException, UnknownNativeResException) as e: # TODO: a implementer dans exec_native_cmd()
1278 1249 self.log_e(f"THREAD Native command execution caught by {type(self).__name__} (from DC)", e)
1279 1250 raise
  1251 +
  1252 + # 3) Make Generic result from native and return it
1280 1253 ok = True if not awaited_res_if_ok else (native_res == awaited_res_if_ok)
1281 1254 return GenericResult(native_res, ok)
1282 1255  
... ...
src/device_controller/channels/client_channel.py
... ... @@ -22,6 +22,8 @@ def printd(*args, **kwargs):
22 22 if os.environ.get('PYROS_DEBUG', '0')=='1': print(*args, **kwargs)
23 23 '''
24 24  
  25 +class ChannelCommunicationException(Exception):
  26 + pass
25 27  
26 28 ##class SocketClientAbstract():
27 29 class ClientChannel():
... ...