From eeac31e1e4a428cb3edde52330c41390a71626a2 Mon Sep 17 00:00:00 2001 From: Mohammad Jafar Mashhadi Date: Mon, 13 Jul 2020 17:01:36 -0600 Subject: [PATCH] Added try...except to be more safe after pprzlink api changes. (#2548) * Updated pprzlink library to the latest version * Added try...except to crazyradio2ivy.py to be safer after pprzlink API changes. paparazzi/pprzlink#117 * Updated request messages with the new pprzlink API * Added a #noqa comment so linters won't remove the seemingly unused import. * Updated the guided_mode_example.py to have a wait timeout. --- sw/ext/pprzlink | 2 +- .../python/bitcraze/crazyradio2ivy.py | 7 ++++- .../python/guided_mode_example.py | 19 ++++++++++---- sw/lib/python/pprz_connect.py | 26 ++----------------- sw/simulator/mesonh/mesonh.py | 2 +- 5 files changed, 24 insertions(+), 32 deletions(-) diff --git a/sw/ext/pprzlink b/sw/ext/pprzlink index 408f517724..9cd22b0472 160000 --- a/sw/ext/pprzlink +++ b/sw/ext/pprzlink @@ -1 +1 @@ -Subproject commit 408f5177249fd7368d7e323564511db4188b60a3 +Subproject commit 9cd22b04722560f9299d70091d195c0519b3a98c diff --git a/sw/ground_segment/python/bitcraze/crazyradio2ivy.py b/sw/ground_segment/python/bitcraze/crazyradio2ivy.py index e1b9264207..09735b0fd5 100755 --- a/sw/ground_segment/python/bitcraze/crazyradio2ivy.py +++ b/sw/ground_segment/python/bitcraze/crazyradio2ivy.py @@ -98,7 +98,12 @@ class RadioBridge: print("Got message {} from {}".format(msg.name, sender_id)) # Forward message to Ivy bus if self.is_connected: - self._ivy.send(msg, sender_id=sender_id) + try: + self._ivy.send(msg, sender_id=sender_id) + except RuntimeError as e: + print("Runtime error {}".format(e)) + except ValueError as e: + print("Invalid message error {}".format(e)) def _link_quality_cb(self, quality): pass diff --git a/sw/ground_segment/python/guided_mode_example.py b/sw/ground_segment/python/guided_mode_example.py index 22b802a5b2..1ba7abd34f 100755 --- a/sw/ground_segment/python/guided_mode_example.py +++ b/sw/ground_segment/python/guided_mode_example.py @@ -161,17 +161,26 @@ class IvyRequester(object): self._interface = None def get_aircrafts(self): + wait_step = 0.1 + timeout = 30 / wait_step # 30 seconds + new_answer = False def aircrafts_cb(ac_id, msg): + global new_answer self.ac_list = [int(a) for a in msg['ac_list'].split(',') if a] print("aircrafts: {}".format(self.ac_list)) + new_answer = True - self._interface.subscribe(aircrafts_cb, "(.*AIRCRAFTS .*)") - sender = 'get_aircrafts' - request_id = '42_1' # fake request id, should be PID_index - self._interface.send("{} {} AIRCRAFTS_REQ".format(sender, request_id)) + self._interface.send_request('ground', "AIRCRAFTS", aircrafts_cb) # hack: sleep briefly to wait for answer - sleep(0.1) + while not new_answer and timeout > 0: + sleep(wait_step) + timeout -= 1 + + if not new_answer: + print("WARNING: Getting the list of aircraft timed out. The results might be outdated.") + # Didn't raise an exception or return None in order to not break the API + return self.ac_list diff --git a/sw/lib/python/pprz_connect.py b/sw/lib/python/pprz_connect.py index 13f0b6617c..7270f14fb9 100755 --- a/sw/lib/python/pprz_connect.py +++ b/sw/lib/python/pprz_connect.py @@ -125,7 +125,6 @@ class PprzConnect(object): """ self.verbose = verbose self._notify = notify - self._req_idx = 0 self._conf_list_by_name = {} self._conf_list_by_id = {} @@ -184,27 +183,6 @@ class PprzConnect(object): """ return self._ivy - def _get_req_id(self): - req_id = '{}_{}'.format(getpid(), self._req_idx) - self._req_idx += 1 - return req_id - - def _message_req(self, msg_name, cb, params=None): - bind_id = None - def _cb(sender, msg): - if bind_id is not None: - self._ivy.unsubscribe(bind_id) - cb(sender, msg) - req_id = self._get_req_id() - req_regex = '^{} ([^ ]* +{}( .*|$))'.format(req_id, msg_name) - bind_id = self._ivy.subscribe(_cb, req_regex) - req_msg = PprzMessage('ground','{}_REQ'.format(msg_name)) - if params is not None: - req_msg.set_values(params) - #FIXME we shouldn't use directly Ivy, but pprzlink python API is not supporting the request id for now - IvySendMsg('pprz_connect {} {} {}'.format(req_id, req_msg.name, req_msg.payload_to_ivy_string())) - #self._ivy.send(req_msg) - def get_aircrafts(self): """ request all aircrafts IDs from a runing server @@ -217,7 +195,7 @@ class PprzConnect(object): #ac_list = [int(a) for a in msg['ac_list'].split(',') if a] if self.verbose: print("aircrafts: {}".format(ac_list)) - self._message_req("AIRCRAFTS", aircrafts_cb) + self._ivy.send_request('ground', "AIRCRAFTS", aircrafts_cb) def new_ac_cb(sender, msg): ac_id = msg['ac_id'] @@ -243,7 +221,7 @@ class PprzConnect(object): self._notify(conf) # user defined general callback if self.verbose: print(conf) - self._message_req("CONFIG", conf_cb, [ac_id]) + self._ivy.send_request('ground', "CONFIG", conf_cb, ac_id=ac_id) if __name__ == '__main__': diff --git a/sw/simulator/mesonh/mesonh.py b/sw/simulator/mesonh/mesonh.py index fd91b4e1f4..2f757c82f9 100755 --- a/sw/simulator/mesonh/mesonh.py +++ b/sw/simulator/mesonh/mesonh.py @@ -124,7 +124,7 @@ def main(): ivy.subscribe(worldenv_cb,'(.* WORLD_ENV_REQ .*)') # wait for ivy to stop - from ivy.std_api import IvyMainLoop + from ivy.std_api import IvyMainLoop # noqa signal.pause()