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

StatusPlugin.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
8
import apex.aprs.util
9
10
__author__ = 'Jeffrey Phillips Freeman (WI2ARD)'
11
__maintainer__ = 'Jeffrey Phillips Freeman (WI2ARD)'
12
__email__ = '[email protected]'
13
__license__ = 'Apache License, Version 2.0'
14
__copyright__ = 'Copyright 2016, Syncleus, Inc. and contributors'
15
__credits__ = []
16
__version__ = '0.0.2'
17
18
plugin = None
19
20
21
def start(config, port_map, packet_cache, aprsis):
22
    global plugin
23
    plugin = StatusPlugin(config, port_map, packet_cache, aprsis)
24
    plugin.run()
25
26
27
def handle_packet(frame, recv_port, recv_port_name):
28
    return
29
30
31
def stop():
32
    plugin.stop()
33
34
35 View Code Duplication
class StatusPlugin(object):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
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