Skip to content

Commit

Permalink
[fix] Fixed mobile signal missing case
Browse files Browse the repository at this point in the history
In some cases, the mobile signal info may be missing,
therefore we need the code to be resilient and not
crash in this case (simply do not try to write mobile
signal data).
  • Loading branch information
nemesifier committed Sep 13, 2022
1 parent 4ef6fba commit 67bc886
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
4 changes: 4 additions & 0 deletions openwisp_monitoring/device/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,8 @@ def _get_extra_tags(self, device):
return tags

def _get_mobile_signal_type(self, signal):
if not signal:
return
# if only one access technology in use, return that
sections = list(signal.keys())
if len(sections) == 1:
Expand All @@ -221,6 +223,8 @@ def _get_mobile_signal_type(self, signal):

def _write_mobile_signal(self, interface, ifname, ct, pk, current=False, time=None):
access_type = self._get_mobile_signal_type(interface['mobile']['signal'])
if not access_type:
return
data = interface['mobile']['signal'][access_type]
signal_power = signal_strength = None
extra_values = {}
Expand Down
36 changes: 36 additions & 0 deletions openwisp_monitoring/device/tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -906,6 +906,42 @@ def test_gsm_charts(self):
self.assertEqual(charts[0]['summary']['signal_power'], None)
self.assertEqual(charts[0]['summary']['signal_strength'], -70.0)

def test_mobile_signal_missing(self):
org = self._create_org()
device = self._create_device(organization=org)
data = {
'type': 'DeviceMonitoring',
'interfaces': [
{
'name': 'mobile0',
'mac': '00:00:00:00:00:00',
'mtu': 1900,
'multicast': True,
'txqueuelen': 1000,
'type': 'modem-manager',
'up': True,
'mobile': {
'connection_status': 'connected',
'imei': '865847055230161',
'manufacturer': 'QUALCOMM INCORPORATED',
'model': 'QUECTEL Mobile Broadband Module',
'operator_code': '22250',
'operator_name': 'Iliad',
'power_status': 'on',
'signal': {},
},
}
],
}
self._post_data(device.id, device.key, data)
response = self.client.get(self._url(device.pk.hex, device.key))
self.assertEqual(response.status_code, 200)
self.assertEqual(response.data['charts'], [])
dd = DeviceData(name=device.name, pk=device.pk)
self.assertEqual(
dd.data['interfaces'][0]['mobile'], data['interfaces'][0]['mobile']
)

def test_pre_metric_write_signal(self):
d = self._create_device(organization=self._create_org())
data = {'type': 'DeviceMonitoring', 'resources': {'cpus': 1, 'load': [0, 0, 0]}}
Expand Down

0 comments on commit 67bc886

Please sign in to comment.