-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathspotter-loop.py
executable file
·330 lines (285 loc) · 10.5 KB
/
spotter-loop.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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# Copyright (c) 2022 Saul St John
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, version 3.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
import asyncio
from contextlib import contextmanager
from datetime import datetime
from glob import glob
import json
from math import ceil
from queue import Queue
import os
from random import choice
import stat
from subprocess import Popen
import sys
from threading import Thread
from time import sleep
from traceback import print_exc
from apscheduler.schedulers.asyncio import AsyncIOScheduler
from apscheduler.triggers.cron import CronTrigger
import dbussy
import Hamlib
import ravel
import requests
CALL=""
GRID=""
BANDS = {
"160": {
"frequency": 1836600,
},
"80": {
"frequency": 3568600,
},
"60": {
"frequency": 5287200,
},
"40": {
"frequency": 7038600,
},
"30": {
"frequency": 10138700,
},
"20": {
"frequency": 14095600,
},
"17": {
"frequency": 18104600,
},
"15": {
"frequency": 21094600,
},
"12": {
"frequency": 24924600,
},
"10": {
"frequency": 28124600,
},
"6": {
"frequency": 50293000,
}
}
ALL_BAND_DEFAULTS = {'enabled': True}
HOPPING_SCHEDULE = ["160", "80", "60", "40", "30", "20", "17", "15", "12", "10"]
TX_CHANCE = 0.15
DATA_DIR="/root"
WSPRD_ARGS=f"-w -a {DATA_DIR}"
usb_loopback = False
def check_setup(retry=False):
try:
resolv = open('/etc/resolv.conf', 'r').readlines()
except FileNotFoundError:
resolv = []
if not 'nameserver' in ''.join(resolv):
if os.path.exists("/run/NetworkManager/resolv.conf") and not ''.join(resolv) and not retry:
os.system("ln -sf /run/NetworkManager/resolv.conf /etc/resolv.conf")
return check_setup(True)
else:
print("Set up /etc/resolv.conf for DNS resolution to spot to wsprnet.org.")
if os.system("command -v wsprd 2>&1 >/dev/null"):
os.environ['PATH'] += os.pathsep + "/usr/local/bin"
if os.system("command -v wsprd 2>&1 >/dev/null"):
print("Put wsprd directory on PATH first.")
return False
if os.system('lsusb | grep -q "0d8c:0012"'):
globals()['usb_loopback'] = True
if os.path.exists(f"{DATA_DIR}/spotter-loop.conf"):
user_conf = json.load(open(f"{DATA_DIR}/spotter-loop.conf", "r"))
if 'TX_CHANCE' in user_conf:
if 0 < float(user_conf['TX_CHANCE']) < 1:
globals()['TX_CHANCE'] = float(user_conf['TX_CHANCE'])
else:
print(f"TX_CHANCE from user configuration is not between 0 and 1, ignoring.")
if "ALL_BAND_DEFAULTS" in user_conf:
ALL_BAND_DEFAULTS.update(user_conf["ALL_BAND_DEFAULTS"])
if ALL_BAND_DEFAULTS.get("tx_enable") and not usb_loopback:
print("TX enabled but USB lookpack unavailable, disabling.")
ALL_BAND_DEFAULTS['tx_enable'] = False
if "BANDS" in user_conf:
for band in user_conf["BANDS"]:
try:
BANDS[band].update(user_conf["BANDS"][band])
except KeyError:
BANDS[band] = user_conf["BANDS"][band]
if "HOPPING_SCHEDULE" in user_conf:
if len(user_conf["HOPPING_SCHEDULE"]) == 10:
globals()["HOPPING_SCHEDULE"] = user_conf["HOPPING_SCHEDULE"]
else:
print("HOPPING_SCHEDULE in spotter-loop.conf is not ten items long, ignoring.")
for band in HOPPING_SCHEDULE:
if not band in BANDS:
print(f"{band} in HOPPING_SCHEDULE but not BANDS, disabling.")
BANDS[band] = {'enabled': False}
elif not BANDS[band].get("frequency"):
print(f"No frequency for {band}, disabling.")
BANDS[band]["enabled"] = False
if BANDS[band].get("tx_enable") and not usb_loopback:
print(f"TX enabled for {band} but USB loopback unavailable, disabling.")
BANDS[band]["tx_enable"] = False
else:
user_conf = {}
for i in ("CALL", "GRID"):
if not i in globals() or not globals()[i]:
if i in user_conf:
globals()[i] = user_conf[i]
else:
print(f"You should probably create/edit spotter-loop.conf to set {i} first.")
if not os.path.exists("/dev/tnt0"):
if os.system("insmod /lib/modules/`uname -r`/extra/tty0tty.ko"):
print("Load tty0tty.ko first.")
return False
return True
@contextmanager
def redirect_qt_app():
if os.path.exists("/dev/ttyS2.old"):
yield None
return
os.rename("/dev/ttyS2", "/dev/ttyS2.old")
try:
tnt0_stat = os.lstat("/dev/tnt0")
newdev = os.makedev(os.major(tnt0_stat.st_rdev), os.minor(tnt0_stat.st_rdev))
os.mknod("/dev/ttyS2", tnt0_stat.st_mode, newdev)
if os.path.exists("/etc/init.d/S99userappstart"):
os.system("/etc/init.d/S99userappstart stop")
elif os.path.exists("/etc/init.d/S99-1-monit"):
os.system("monit stop x6100_ui_v100")
else:
print("Don't know how to restart QT app.")
exit(1)
sleep(1)
ui_proc = Popen(["/usr/app_qt/x6100_ui_v100"], env=dict(os.environ, **{
"QINJ_TEXT": "WSPR",
"LD_PRELOAD": "libqinj.so.1.0.0"
}))
sleep(5)
yield None
finally:
ui_proc.terminate()
try:
ui_proc.wait(5)
except TimeoutError:
ui_proc.kill()
os.rename("/dev/ttyS2.old", "/dev/ttyS2")
if os.path.exists("/etc/init.d/S99userappstart"):
os.system("/etc/init.d/S99userappstart start")
else:
os.system("monit start x6100_ui_v100")
@contextmanager
def get_rig():
Hamlib.rig_set_debug(Hamlib.RIG_DEBUG_NONE)
rig = Hamlib.Rig(3087)
rig.set_conf("rig_pathname", "/dev/tnt1")
rig.open()
try:
yield rig
finally:
rig.close()
def hop_bands(rig):
schedule_index = int(((datetime.utcnow().minute + 2) % 20) / 2)
chosen_band = HOPPING_SCHEDULE[schedule_index]
band_params = dict(ALL_BAND_DEFAULTS, **BANDS[chosen_band])
if not band_params['enabled']:
chosen_band = choice(list(filter(lambda b: BANDS[b].get("enabled", ALL_BAND_DEFAULTS["enabled"]), BANDS)))
print(f"hopping to {chosen_band}m band ({BANDS[chosen_band]['frequency']}c)")
rig.set_mode(4, 700)
rig.set_freq(Hamlib.RIG_VFO_A, BANDS[chosen_band]["frequency"])
rig.set_level("AF", 0.0)
rig.set_level("AGC", 2)
rig.set_level("ATT", 1 if band_params.get("attenuator") else 0)
rig.set_level("PREAMP", 10 if band_params.get("preamp") else 0)
rig.set_level("RF", (1 + band_params.get("gain", 50)) / 100)
def decode_thread_main(recordings_queue):
while True:
next_recording, frequency = recordings_queue.get()
if not next_recording:
return
os.system(f"time wsprd {WSPRD_ARGS} -f {frequency} {next_recording}")
os.system(f"rm {next_recording}")
add_spots_to_ui(next_recording)
upload_spots(next_recording)
def add_spots_to_ui(recording):
if os.path.getsize(f"{DATA_DIR}/wspr_spots.txt") == 0:
return
try:
injection_proxy = ravel.system_bus()['lol.ssj.xwspr']['/'].get_interface("lol.ssj.xwspr")
except dbussy.DBusError:
return
with open(f"{DATA_DIR}/wspr_spots.txt", "r") as spotfile:
for line in spotfile.readlines():
parts = line.split(" ")
parts = list(filter(None, parts))
injection_proxy.wsprReceived(
f"{parts[0]} {parts[1]}",
parts[3], *parts[5:9]
)
def upload_spots(recording=None, spotfile="wspr_spots.txt"):
if os.path.getsize(f"{DATA_DIR}/{spotfile}") == 0:
print("no spots")
return True
files = {'allmept': open(f"{DATA_DIR}/{spotfile}", 'r')}
params = {'call': CALL, 'grid': GRID, 'version': 'x6w-0.9.8'}
response = None
try:
response = requests.post('http://wsprnet.org/post', files=files, params=params)
response.raise_for_status()
if "Log rejected" in response.text:
raise Exception("log rejected")
return True
except Exception as e:
print(f"failed to upload spotfile {spotfile}: {e}")
if spotfile == "wspr_spots.txt":
timestamp = os.path.splitext(os.path.basename(recording))[0]
os.system(f"mv {DATA_DIR}/wspr_spots.txt {DATA_DIR}/wspr_spots-{timestamp}.rej")
return False
finally:
if response:
print(response.text)
def retry_failed_spot_uploads():
rejects = glob(f"{DATA_DIR}/*.rej")
if not rejects:
return
print("retrying failed spot uploads")
for reject in rejects:
if upload_spots(spotfile=os.path.basename(reject)):
os.system(f"rm {reject}")
def do_rx(recordings_queue, rig):
fname = "/tmp/" + datetime.utcnow().strftime("%y%m%d_%H%M") + ".wav"
res = os.system(f"arecord -D mixcapture -d 114 -f S16_LE -r 12000 {fname}")
if 0 == res:
recordings_queue.put([fname, rig.get_freq() / 1e6])
hop_bands(rig)
def main():
if not check_setup():
return 1
with redirect_qt_app():
with get_rig() as rig:
recordings_queue = Queue()
decode_thread = Thread(target=decode_thread_main, args=[recordings_queue])
decode_thread.start()
try:
loop = asyncio.new_event_loop()
scheduler = AsyncIOScheduler(event_loop=loop)
scheduler.add_job(do_rx, CronTrigger(minute='*/2', second=0), args=[recordings_queue, rig])
scheduler.add_job(retry_failed_spot_uploads, CronTrigger(minute='*/30'))
hop_bands(rig)
scheduler.start()
loop.run_forever()
finally:
try: recordings_queue.put((None, None), block=False)
except Exception: pass
if __name__ == "__main__":
sys.exit(main())