-
Notifications
You must be signed in to change notification settings - Fork 1
/
skimmer.py
226 lines (165 loc) · 6.54 KB
/
skimmer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
import os, sys
import subprocess
import threading
from skimmer_parser import SkimmerParser
from datetime import datetime, timezone, timedelta
from skimmer_spot import Spot, SpotType
class SkccStatus:
__config = {}
__lines = []
state = 'starting'
def __str__(self) -> str:
return '\r\n'.join(self.__lines)
def add_cfg(self, cfg: str, data: str):
self.__config[cfg] = data
def add(self, line : str):
self.__lines.append(line)
def get_lines(self) -> list:
return [self.state] + self.__lines
class cSkimmer:
"""The main class for parsing output from skcc_skimmer.py
This class will spawn a python subprocess (of skcc_skimmer.py) with piped
stdout which is then read forever. Call run() then read() <= which blocks
forever
:param cfg: dictionary of configured values. Usually the ```app.config```
object from Flask
"""
def __init__(self, cfg : dict):
if sys.platform == "win32":
self.__cmd = ["skcc_skimmer.exe"]
else:
self.__cmd = ["python3", "skccskimmer/skcc_skimmer.py"]
self.__status = SkccStatus()
self.__spots = []
self.__sked_spots = []
self.__new_spot = False
self.__new_skeds = False
self.__parsing_skeds = True
self.__is_running = False
self.__config = cfg
def run(self):
"""Create the python subprocess and run the main skimmer application.
This should be done first.
"""
if not self.__is_running:
os.environ['PYTHONIOENCODING'] = 'utf-8'
print('running cmd: ' + ' '.join(self.__cmd))
print('in curr dir: ' + os.getcwd())
self.__proc = subprocess.Popen(self.__cmd,
encoding='utf-8',
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
stdin=subprocess.PIPE)
self.__is_running = True
# kick off parsing thread
self.__thread = threading.Thread(target=self.read)
self.__thread.start()
print("read thread started")
def stop(self):
"""Stops the running skcc skimmer process
"""
if (self.__proc != None and self.__is_running):
print('killing process...')
self.__proc.terminate()
print('joining read thread...')
self.__thread.join()
self.__is_running = False
self.__status.state = 'stopped'
def read(self):
"""Reads and parses the skimmer subprocess' output.
This never returns.
"""
if (self.__proc == None):
return ""
self.__status.state = 'starting'
str = ""
for line in iter(self.__proc.stdout.readline, ''):
str = line.strip()
self.__parse(str)
if "SHOW_SKIMMER_OUTPUT" in self.__config and \
self.__config['SHOW_SKIMMER_OUTPUT']:
print(str)
# we never get here
return str
def get_spots(self) -> tuple[bool, list]:
"""Returns the a tuple with the lists of spots and a flag indicating there are new spots
Calling this method sets the above flag to False
"""
result = (self.__new_spot, self.__spots)
self.__new_spot = False
return result
def get_skeds(self) -> tuple[bool, list]:
"""Returns the a tuple with the lists of skeds and a flag indicating there are new skeds
Calling this method sets the above flag to False. Also if this method is
called while new skeds are being parsed. It returns the old skeds.
"""
if (self.__parsing_skeds):
return (False, self.__sked_spots)
result = (self.__new_skeds, self.__sked_spots)
self.__new_skeds = False
return result
def get_status(self) -> SkccStatus:
''' Gets the skimmer status object '''
return self.__status
def clear_spots(self):
""" Clears the list of current spots (not skeds) from the object
"""
self.__spots.clear()
# force refresh of frontend spots list
self.__new_spot = True
def force_refresh(self):
'''Sets the flags to indicate that there are new spots to see.'''
self.__new_skeds = True
self.__new_spot = True
def __parse(self, line : str):
if (self.__status.state == 'starting'):
# if we're starting lets grab all the output for later
self.__parse_cfg_line(line, "GOALS:")
self.__parse_cfg_line(line, "TARGETS:")
self.__parse_cfg_line(line, "BANDS:")
self.__status.add(line)
if line.startswith("Running..."):
self.__status.state = 'running'
if (self.__status.state == 'running'):
# were running so lets parse all the juicy spots
if (line == "=========== SKCC Sked Page ============"):
print('new skeds incoming...')
self.__parsing_skeds = True
self.__sked_spots.clear()
elif (line == "======================================="):
print('skeds done.')
self.__parsing_skeds = False
self.__new_skeds = True
else:
self.__parse_line(line)
self.__check_spots()
def __parse_cfg_line(self, line : str, prefix : str):
if line.startswith(prefix):
self.__status.add_cfg(prefix, line)
def __parse_line(self, line : str):
spot = SkimmerParser.parse_spot(line)
if (spot == None):
return
#print(spot)
if (self.__parsing_skeds):
spot.kind = SpotType.SKED
spot.spotter = "sked"
self.__sked_spots.append(spot)
else:
spot.kind = SpotType.SPOT
self.__new_spot = True
self.__spots.append(spot)
def __check_spots(self):
'''Loop thru the spots list and remove any older than a certain time'''
if "SPOT_EXPIRE_TIME" in self.__config:
timeout = self.__config["SPOT_EXPIRE_TIME"]
else:
timeout = 3600
for spot in self.__spots:
t = datetime.now(tz=timezone.utc)
# print(f'spot time {spot.utc_time} == current {t}')
delta = t - spot.utc_time
# if its been an hour remove the spot from the list
if delta >= timedelta(seconds=timeout):
print("removing old spot")
self.__spots.remove(spot)