diff --git a/scriptsapi/Readme.md b/scriptsapi/Readme.md index 1afec56e5..ed8dc7401 100644 --- a/scriptsapi/Readme.md +++ b/scriptsapi/Readme.md @@ -252,3 +252,9 @@ This file drives how channels in the connected SDRangel instance are managed.

sdrangel.py

Holds constants related to SDRangel software required by other scripts + +

Unit tests

+ +Run as `python ` in the virtual environment + + - `test_superscanner.py` is testing `superscanner.py` \ No newline at end of file diff --git a/scriptsapi/requirements.txt b/scriptsapi/requirements.txt index 37822b3db..ed4a8fc3c 100644 --- a/scriptsapi/requirements.txt +++ b/scriptsapi/requirements.txt @@ -2,4 +2,5 @@ requests Flask numpy websocket -websocket-client \ No newline at end of file +websocket-client +mock \ No newline at end of file diff --git a/scriptsapi/superscanner.py b/scriptsapi/superscanner.py index 5a9fc712e..6620fef19 100644 --- a/scriptsapi/superscanner.py +++ b/scriptsapi/superscanner.py @@ -293,12 +293,18 @@ def process_hotspots(scanned_hotspots): # calculate hotspot distances for each used channel and reuse the channel for the closest hotspot channels = CONFIG['channel_info'] used_channels = [channel for channel in channels if channel['usage'] == 1] + consolidated_distances = [] for channel in used_channels: # loop on used channels distances = [[abs(channel['frequency'] - get_hotspot_frequency(channel, hotspot)), hotspot] for hotspot in hotspots] distances = sorted(distances, key=operator.itemgetter(0)) - print(f'channel {channel["index"]} distances: {distances}') if distances: - hotspot = distances[0][1] + consolidated_distances.append([distances[0][0], channel, distances[0][1]]) # [distance, channel, hotspot] + consolidated_distances = sorted(consolidated_distances, key=operator.itemgetter(0)) # get (channel, hotspot) pair with shortest distance first + # reallocate used channels on their closest hotspot + for distance in consolidated_distances: + channel = distance[1] + hotspot = distance[2] + if hotspot in hotspots: # hotspot is not processed yet channel['usage'] = 2 # mark channel used on this pass channel['frequency'] = get_hotspot_frequency(channel, hotspot) set_channel_frequency(channel) diff --git a/scriptsapi/test_superscanner.py b/scriptsapi/test_superscanner.py index f070989f5..bd849f219 100644 --- a/scriptsapi/test_superscanner.py +++ b/scriptsapi/test_superscanner.py @@ -2,9 +2,11 @@ import unittest import mock import superscanner +# ====================================================================== def print_hex(bytestring): print('\\x' + '\\x'.join('{:02x}'.format(x) for x in bytestring)) +# ====================================================================== def get_deviceset_info(deviceset_index): return { "channelcount": 4, @@ -56,12 +58,15 @@ def get_deviceset_info(deviceset_index): } } +# ====================================================================== def set_channel_frequency(channel): pass +# ====================================================================== def set_channel_mute(channel): pass +# ====================================================================== class TestStringMethods(unittest.TestCase): def test_upper(self): @@ -78,12 +83,14 @@ class TestStringMethods(unittest.TestCase): with self.assertRaises(TypeError): s.split(2) +# ====================================================================== class TestSuperScannerOptions(unittest.TestCase): def test_options_minimal(self): options = superscanner.get_input_options(["-ctoto"]) self.assertEqual(options.config_file, 'toto') +# ====================================================================== class TestSuperScannerDecode(unittest.TestCase): def test_decode_bytes(self): @@ -98,6 +105,7 @@ class TestSuperScannerDecode(unittest.TestCase): msg_struct = superscanner.decode_message(msg_bytes) self.assertEqual(msg_struct['fft_size'], 1024) +# ====================================================================== class TestSuperScannerProcessHotspots(unittest.TestCase): @mock.patch('superscanner.get_deviceset_info', side_effect=get_deviceset_info) @@ -159,7 +167,6 @@ class TestSuperScannerProcessHotspots(unittest.TestCase): ] superscanner.process_hotspots(hotspots1) channel_info = superscanner.CONFIG['channel_info'] - print(channel_info) self.assertEqual(channel_info[0]['usage'], 1) self.assertEqual(channel_info[1]['usage'], 0) self.assertEqual(channel_info[2]['usage'], 0) @@ -178,7 +185,6 @@ class TestSuperScannerProcessHotspots(unittest.TestCase): ] superscanner.process_hotspots(hotspots2) channel_info = superscanner.CONFIG['channel_info'] - print(channel_info) self.assertEqual(channel_info[0]['usage'], 1) self.assertEqual(channel_info[1]['usage'], 1) self.assertEqual(channel_info[2]['usage'], 0) @@ -193,13 +199,13 @@ class TestSuperScannerProcessHotspots(unittest.TestCase): ] superscanner.process_hotspots(hotspots3) channel_info = superscanner.CONFIG['channel_info'] - print(channel_info) self.assertEqual(channel_info[0]['usage'], 0) self.assertEqual(channel_info[1]['usage'], 1) self.assertEqual(channel_info[2]['usage'], 0) self.assertEqual(channel_info[1]['frequency'], 145200000) +# ====================================================================== if __name__ == '__main__': unittest.main()