-
Notifications
You must be signed in to change notification settings - Fork 15
/
fetch.py
executable file
·238 lines (204 loc) · 7.73 KB
/
fetch.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
227
228
229
230
231
232
233
234
235
236
237
238
#!/usr/bin/env python3
# -*- python -*-
#
# Copyright(c) 2020 BD7MQB Michael Choi <[email protected]>
# This is free software, licensed under the GNU GENERAL PUBLIC LICENSE, Version 2.0
#
from digiskr import Option, Config
from kiwi import KiwiWorker
import timespan
from digiskr import config, DecoderQueue
from digiskr.pskreporter import PskReporter
from digiskr.wsprnet import Wsprnet
from digiskr.audio import WsjtSoundRecorder
import logging
import os
import sys
import time
import threading
import gc
from datetime import datetime
from copy import copy
sys.path.append('./lib')
_conf = Config.get()
_run_event = threading.Event()
_run_event.set()
_sr_tasks = []
threading.current_thread().name = "main"
def setup_logger():
debug = _conf["DEBUG"] if "DEBUG" in _conf else False
log_to_file = _conf["LOG_TO_FILE"] if "LOG_TO_FILE" in _conf else False
log_backup_count = _conf["LOG_BACKUP_COUNT"] if "LOG_BACKUP_COUNT" in _conf else 30
try:
# if colorlog installed (pip install colorlog)
from colorlog import ColoredFormatter
import logging.config
logconf = {
'version': 1,
'formatters': {
'colored': {
'()': 'colorlog.ColoredFormatter',
'format':
"%(asctime)-15s %(log_color)s%(levelname)-5s %(process)5d [%(threadName)s] %(message)s"
}
},
'handlers': {
'stream': {
'class': 'logging.StreamHandler',
'formatter': 'colored',
'level': 'DEBUG' if debug else 'INFO'
},
},
'loggers': {
'': {
'handlers': ["stream"],
'level': 'DEBUG' if debug else 'INFO',
},
},
}
logging.config.dictConfig(logconf)
except ImportError:
import logging
import logging.handlers
FORMAT = "%(asctime)-15s %(levelname)-5s %(process)5d [%(threadName)s] %(message)s"
logging.basicConfig(
level=logging.DEBUG if debug else logging.INFO, format=FORMAT)
# log to file
if log_to_file:
os.makedirs(Config.logdir(), exist_ok=True)
filehandler = logging.handlers.TimedRotatingFileHandler(os.path.join(
Config.logdir(), "digiskr.log"), when="midnight", interval=1, backupCount=log_backup_count)
filehandler.setLevel(logging.DEBUG)
filehandler.setFormatter(logging.Formatter(
"%(asctime)-15s %(levelname)-5s %(process)5d [%(threadName)s] %(message)s"))
logging.getLogger('').addHandler(filehandler)
def setup_kiwistation(station, station_name):
options = Option(**station)
options.station = station_name
options.user = _conf["KIWI_USER"] if "KIWI_USER" in _conf else config.KIWI_USER
return options
def new_kiwiworker(o, band_hops_str, idx):
options = copy(o)
def _extract_band(band_hops_str):
local = str(band_hops_str).split('|')
# ['FT8', 'FT4', 'FT8', 'FT8']
mode_hops = [config.MODES[b[-1]] if b[-1]
in config.MODES else "FT8" for b in local]
# ['20', '30', '40', '60']
band_hops = [b[:-1] if b[-1]
not in [str(n) for n in range(0, 9)] else b for b in local]
# [14074, 10140, 7074, 5357] in KHz
freq_hops = [config.BANDS[mode_hops[i]][b]
* 1000 for i, b in enumerate(band_hops)]
return mode_hops, band_hops, freq_hops
options.band_hops_str = band_hops_str
options.mode_hops, options.band_hops, options.freq_hops = _extract_band(
band_hops_str)
options.idx = idx
options.timestamp = int(time.time() + os.getpid() + idx) & 0xffffffff
# tmp dirs preparation
for i, mode in enumerate(options.mode_hops):
dir = os.path.join(Config.tmpdir(), options.station,
mode, options.band_hops[i])
if not os.path.isdir(dir):
os.makedirs(dir, exist_ok=True)
else:
os.popen("rm -f %s/*.wav" % dir)
worker = KiwiWorker(
target=WsjtSoundRecorder(options),
name="%s-%s" % (options.station, options.band_hops_str)
)
return worker
def cleanup():
_run_event.clear()
PskReporter.stop()
Wsprnet.stop()
[w.stop() for w in DecoderQueue.instance().workers]
[r.stop() for r in _sr_tasks]
[t.join() for t in threading.enumerate() if t is not threading.current_thread()]
def remove_thread(snd, r):
r.stop()
if snd.__contains__(r):
snd.remove(r)
logging.info("Task #%s removed", r.getName())
def match_schedule(schedules):
for (ts, schedule) in schedules.items():
if timespan.match(ts, datetime.now()):
return schedule
return None
def main():
idx = 0
schedule = match_schedule(_conf["SCHEDULES"])
if schedule is not None:
logging.info("current schedule is: %s", schedule)
for (st, bands) in schedule.items():
options = setup_kiwistation(_conf["STATIONS"][st], st)
for band in bands:
_sr_tasks.append(new_kiwiworker(options, band, idx))
idx += 1
try:
if len(_sr_tasks) == 0:
logging.warning("No tasks in queue.")
logging.warning("I\'m out")
exit(0)
else:
DecoderQueue.instance()
for i, r in enumerate(_sr_tasks):
r.start()
# keeper
while _run_event.is_set():
time.sleep(1)
schedule = match_schedule(_conf["SCHEDULES"])
if schedule is not None:
# logging.debug('current schedule is: %s', schedule)
# remove out-of-date tasks
for r in _sr_tasks:
bands = schedule.get(r._options.station)
keep_it = False
if bands is not None:
for band in bands:
if band == r._options.band_hops_str:
keep_it = True
break
else:
remove_thread(_sr_tasks, r)
if not keep_it:
remove_thread(_sr_tasks, r)
# add new tasks
for (st, bands) in schedule.items():
for band in bands:
exsit_task = False
for r in _sr_tasks:
if r.name == "%s-%s" % (st, band):
exsit_task = True
break
if not exsit_task:
options = setup_kiwistation(_conf["STATIONS"][st], st)
task = new_kiwiworker(options, band, len(_sr_tasks)+1)
logging.info("# wait a second to let pre-tasks have a clean quit")
time.sleep(1)
task.start()
_sr_tasks.append(task)
else: # no tasks available
[remove_thread(_sr_tasks, r) for r in _sr_tasks]
logging.warning("There is no tasks")
logging.warning("Quit...")
exit(0)
except KeyboardInterrupt:
logging.warning("KeyboardInterrupt: exiting...")
cleanup()
logging.info("KeyboardInterrupt: threads successfully closed")
except Exception:
cleanup()
logging.exception("Exception: threads successfully closed")
logging.debug("gc %s" % gc.garbage)
if __name__ == '__main__':
setup_logger()
fail = False
for e in Config.validateConfig():
logging.fatal(e)
fail = True
if fail:
exit(1)
main()
# EOF