From d7ba6ae7c2d53d86a295117d0af8f97cacd03b3a Mon Sep 17 00:00:00 2001 From: Martin Braun Date: Wed, 2 Jun 2021 13:05:57 +0200 Subject: mpm: gpsd_iface: Make GPGGA generation more robust The GPGGA string generation relies on TPV and SKY messages being accurate, meaning they are a list of dictionaries. There have been cases where this was inaccurate (an empty list was returned). MPM would show errors as such: [ERROR] [MPM.RPCServer] Uncaught exception in method get_mb_sensor :list index out of range Traceback (most recent call last): File "/usr/lib/python3.7/site-packages/usrp_mpm/rpc_server.py", line 184, in new_claimed_function return function(*args) File "/usr/lib/python3.7/site-packages/usrp_mpm/periph_manager/base.py", line 1000, in get_mb_sensor self, self.mboard_sensor_callback_map.get(sensor_name) File "/usr/lib/python3.7/site-packages/usrp_mpm/gpsd_iface.py", line 313, in get_gps_gpgga_sensor tpv_sensor_data = gps_info.get('tpv', [{}])[0] IndexError: list index out of range This is a patch to check that the lists are not empty before directly referencing index 0 therein. --- mpm/python/usrp_mpm/gpsd_iface.py | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) (limited to 'mpm') diff --git a/mpm/python/usrp_mpm/gpsd_iface.py b/mpm/python/usrp_mpm/gpsd_iface.py index 68747dff5..8d6b7049a 100644 --- a/mpm/python/usrp_mpm/gpsd_iface.py +++ b/mpm/python/usrp_mpm/gpsd_iface.py @@ -381,12 +381,15 @@ class GPSDIfaceExtension: while True: gps_info = self._gpsd_iface.get_gps_info(resp_class='', timeout=15) self._log.trace("GPS info: {}".format(gps_info)) - tpv_sensor_data = gps_info.get('tpv', [{}])[0] - sky_sensor_data = gps_info.get('sky', [{}])[0] - if tpv_sensor_data and \ - sky_sensor_data and \ - tpv_sensor_data.get("mode", 0) > 0: - break + # Response types are 'list of dicts', but they can be empty lists so + # we need to prepare for that: + tpv_sensor_data = gps_info.get('tpv') + sky_sensor_data = gps_info.get('sky') + if tpv_sensor_data and sky_sensor_data: + tpv_sensor_data = tpv_sensor_data[0] + sky_sensor_data = sky_sensor_data[0] + if tpv_sensor_data.get("mode", 0) > 0: + break return { 'name': 'gpgga', 'type': 'STRING', -- cgit v1.2.3