Code Duplication    Length = 46-49 lines in 3 locations

src/apex/plugins/status/__init__.py 1 location

@@ 35-83 (lines=49) @@
32
    plugin.stop()
33
34
35
class StatusPlugin(object):
36
37
    def __init__(self, config, port_map, packet_cache, aprsis):
38
        self.port_map = port_map
39
        self.packet_cache = packet_cache
40
        self.aprsis = aprsis
41
        self.running = False
42
43
        for section in config.sections():
44
            if section.startswith('TNC '):
45
                tnc_name = section.split(' ')[1]
46
                for port_id in range(1, 1+int(config.get(section, 'port_count'))):
47
                    port_name = tnc_name + '-' + str(port_id)
48
                    port = port_map[port_name]
49
                    port_section = 'PORT ' + port_name
50
                    port['status_text'] = config.get(port_section, 'status_text')
51
                    port['status_path'] = config.get(port_section, 'status_path')
52
53
    def stop(self):
54
        self.running = False
55
56
    def run(self):
57
        self.running = True
58
59
        # Don't do anything in the first 60 seconds
60
        last_trigger = time.time()
61
        while self.running and time.time() - last_trigger < 60:
62
            time.sleep(1)
63
64
        # run the id every 600 seconds
65
        last_trigger = time.time()
66
        while self.running:
67
            if time.time() - last_trigger >= 600:
68
                last_trigger = time.time()
69
                for port_name in self.port_map.keys():
70
                    port = self.port_map[port_name]
71
72
                    status_frame = {
73
                        'source': port['identifier'],
74
                        'destination': 'APRS',
75
                        'path': port['status_path'].split(','),
76
                        'text': port['status_text']}
77
                    frame_hash = apex.aprs.util.hash_frame(status_frame)
78
                    if frame_hash not in self.packet_cache.values():
79
                        self.packet_cache[str(frame_hash)] = frame_hash
80
                        port['tnc'].write(status_frame, port['tnc_port'])
81
                        print(port_name + ' >> ' + apex.aprs.util.format_aprs_frame(status_frame))
82
            else:
83
                time.sleep(1)
84

src/apex/plugins/id/__init__.py 1 location

@@ 36-81 (lines=46) @@
33
    plugin.stop()
34
35
36
class IdPlugin(object):
37
38
    def __init__(self, config, port_map, packet_cache, aprsis):
39
        self.port_map = port_map
40
        self.packet_cache = packet_cache
41
        self.aprsis = aprsis
42
        self.running = False
43
44
        for section in config.sections():
45
            if section.startswith('TNC '):
46
                tnc_name = section.split(' ')[1]
47
                for port_id in range(1, 1+int(config.get(section, 'port_count'))):
48
                    port_name = tnc_name + '-' + str(port_id)
49
                    port = port_map[port_name]
50
                    port_section = 'PORT ' + port_name
51
                    port['id_text'] = config.get(port_section, 'id_text')
52
                    port['id_path'] = config.get(port_section, 'id_path')
53
54
    def stop(self):
55
        self.running = False
56
57
    def run(self):
58
        self.running = True
59
60
        # Don't do anything in the first 30 seconds
61
        last_trigger = time.time()
62
        while self.running and time.time() - last_trigger < 30:
63
            time.sleep(1)
64
65
        # run every 600 second
66
        last_trigger = time.time()
67
        while self.running:
68
            if time.time() - last_trigger >= 600:
69
                last_trigger = time.time()
70
                for port_name in self.port_map.keys():
71
                    port = self.port_map[port_name]
72
73
                    id_frame = {'source': port['identifier'], 'destination': 'ID', 'path': port['id_path'].split(','),
74
                                'text': port['id_text']}
75
                    frame_hash = apex.aprs.util.hash_frame(id_frame)
76
                    if frame_hash not in self.packet_cache.values():
77
                        self.packet_cache[str(frame_hash)] = frame_hash
78
                        port['tnc'].write(id_frame, port['tnc_port'])
79
                        click.echo(port_name + ' >> ' + apex.aprs.util.format_aprs_frame(id_frame))
80
            else:
81
                time.sleep(1)
82

src/apex/plugins/beacon/__init__.py 1 location

@@ 36-81 (lines=46) @@
33
    plugin.stop()
34
35
36
class BeaconPlugin(object):
37
38
    def __init__(self, config, port_map, packet_cache, aprsis):
39
        self.port_map = port_map
40
        self.packet_cache = packet_cache
41
        self.aprsis = aprsis
42
        self.running = False
43
44
        for section in config.sections():
45
            if section.startswith('TNC '):
46
                tnc_name = section.split(' ')[1]
47
                for port_id in range(1, 1+int(config.get(section, 'port_count'))):
48
                    port_name = tnc_name + '-' + str(port_id)
49
                    port = port_map[port_name]
50
                    port_section = 'PORT ' + port_name
51
                    port['beacon_text'] = config.get(port_section, 'beacon_text')
52
                    port['beacon_path'] = config.get(port_section, 'beacon_path')
53
54
    def stop(self):
55
        self.running = False
56
57
    def run(self):
58
        self.running = True
59
60
        # Don't do anything in the first 30 seconds
61
        last_trigger = time.time()
62
        while self.running and time.time() - last_trigger < 30:
63
            time.sleep(1)
64
65
        # run every 600 second
66
        last_trigger = time.time()
67
        while self.running:
68
            if time.time() - last_trigger >= 600:
69
                last_trigger = time.time()
70
                for port_name in self.port_map.keys():
71
                    port = self.port_map[port_name]
72
73
                    beacon_frame = {'source': port['identifier'], 'destination': 'APRS',
74
                                    'path': port['beacon_path'].split(','), 'text': port['beacon_text']}
75
                    frame_hash = apex.aprs.util.hash_frame(beacon_frame)
76
                    if frame_hash not in self.packet_cache.values():
77
                        self.packet_cache[str(frame_hash)] = frame_hash
78
                        port['tnc'].write(beacon_frame, port['tnc_port'])
79
                        click.echo(port_name + ' >> ' + apex.aprs.util.format_aprs_frame(beacon_frame))
80
            else:
81
                time.sleep(1)
82