Completed
Push — master ( 0367ee...2c22a6 )
by Jeffrey
03:30
created

IdPlugin.stop()   A

Complexity

Conditions 1

Size

Total Lines 2

Duplication

Lines 2
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
c 1
b 0
f 0
dl 2
loc 2
rs 10
1
# These imports are for python3 compatability inside python2
2
from __future__ import absolute_import
3
from __future__ import division
4
from __future__ import print_function
5
6
import time
7
import click
8
9
import apex.aprs.util
10
11
__author__ = 'Jeffrey Phillips Freeman (WI2ARD)'
12
__maintainer__ = 'Jeffrey Phillips Freeman (WI2ARD)'
13
__email__ = '[email protected]'
14
__license__ = 'Apache License, Version 2.0'
15
__copyright__ = 'Copyright 2016, Syncleus, Inc. and contributors'
16
__credits__ = []
17
__version__ = '0.0.2'
18
19
plugin = None
20
21
22
def start(config, port_map, packet_cache, aprsis):
23
    global plugin
24
    plugin = IdPlugin(config, port_map, packet_cache, aprsis)
25
    plugin.run()
26
27
28
def handle_packet(frame, recv_port, recv_port_name):
29
    return
30
31
32
def stop():
33
    plugin.stop()
34
35
36 View Code Duplication
class IdPlugin(object):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
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