-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathpskreporter.py
More file actions
378 lines (330 loc) · 12.3 KB
/
pskreporter.py
File metadata and controls
378 lines (330 loc) · 12.3 KB
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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
import os
import logging
import threading
import time
import random
import socket
SERVER_NAME = ("report.pskreporter.info", 4739)
logging.basicConfig(
format="%(levelname)s:%(name)s:%(asctime)s %(message)s",
level=logging.WARNING,
datefmt="%Y-%m-%d %H:%M:%S",
)
REPORT_ALL_INTERVAL = 1200
REPORT_ALL_FOR = 59
class PskReporter(object):
sharedInstance = {}
creationLock = threading.Lock()
interval = 180
@staticmethod
def getSharedInstance(station: str):
with PskReporter.creationLock:
if PskReporter.sharedInstance.get(station) is None:
PskReporter.sharedInstance[station] = PskReporter(station)
return PskReporter.sharedInstance[station]
@staticmethod
def stop():
[psk.cancelTimer() for psk in PskReporter.sharedInstance.values()]
def __init__(
self,
callsign: str,
grid: str,
antenna: str,
dummy: bool = False,
tcp: bool = False,
):
self.spots = []
self.oldSpots = {} # Indexed by timestamp
self.spotLock = threading.Lock()
self.station = {"callsign": callsign, "grid": grid, "antenna": antenna}
self.uploader = Uploader(self.station, tcp=tcp)
self.timer = None
self.dummy = dummy
self.total_spots = 0
def getOldSpots(self):
cutoff = time.time() - 1200
for t in list(self.oldSpots.keys()):
if t < cutoff:
del self.oldSpots[t]
else:
for spot in self.oldSpots[t]:
yield spot
def scheduleNextUpload(self):
if self.timer:
return
delay = PskReporter.interval + random.uniform(0, 15)
logging.info("scheduling next pskreporter upload in %3.2f seconds", delay)
self.timer = threading.Timer(delay, self.upload)
self.timer.name = "psk.uploader-%s" % self.station
self.timer.start()
def spotEquals(self, s1, s2):
# s1 is the new spot
keys = ["callsign", "timestamp", "locator", "db", "freq", "mode"]
return (
s1["callsign"] == s2["callsign"]
and abs(s1["timestamp"] - s2["timestamp"]) < 1200
and (s1["locator"] == s2["locator"] or not s1["locator"])
and abs(s1["freq"] - s2["freq"]) < 10000
and s1["mode"] == s2["mode"]
)
def addSpot(self, spot):
self.spots.append(spot)
ts = spot["timestamp"]
if ts not in self.oldSpots:
self.oldSpots[ts] = []
self.oldSpots[ts].append(spot)
def spot(
self,
callsign,
frequency,
mode,
timestamp=0,
db=None,
locator=None,
hexbytes=None,
dt=None,
):
if not timestamp:
timestamp = time.time()
spot = {
"callsign": callsign,
"mode": mode,
"locator": locator or "",
"freq": frequency,
"db": -128 if db is None else db,
"timestamp": timestamp,
"bytes": bytes.fromhex(hexbytes or ""),
"dt": -32768 if dt is None else dt,
}
spot["upload_all"] = self.upload_all(spot)
if self.dummy:
print(spot)
return
with self.spotLock:
if not spot["upload_all"] and any(x for x in self.getOldSpots() if self.spotEquals(spot, x)):
# dupe
pass
else:
self.addSpot(spot)
self.scheduleNextUpload()
def upload_all(self, spot) -> bool:
return spot["timestamp"] % REPORT_ALL_INTERVAL < REPORT_ALL_FOR
def upload(self):
try:
with self.spotLock:
self.timer = None
spots = self.spots
self.spots = []
if spots:
# Filter out very old spots
cutoff = time.time() - 3000
spot_count = len(spots)
spots = [x for x in spots if x["timestamp"] >= cutoff]
if len(spots) < spot_count:
logging.warning(
f"Dropping {spot_count - len(spots)} spots as too old (without connectivity). {len(spots)} left."
)
if spots:
self.total_spots += len(spots)
unsent = self.uploader.upload(spots)
if unsent:
self.total_spots -= len(unsent)
# We want to save these for later
with self.spotLock:
self.spots = unsent + self.spots
except Exception:
logging.exception("Failed to upload spots")
def cancelTimer(self):
if self.timer:
self.timer.cancel()
self.timer.join()
self.timer = None
def close(self):
self.cancelTimer()
self.upload()
if self.spots:
logging.warning(f"Failed to upload {len(self.spots)} spots on close")
logging.warning(f"Uploaded {self.total_spots} spots total")
class Uploader(object):
receiverDelimiter = [0x99, 0x92]
senderDelimiter = [0x99, 0x96]
def __init__(self, station, tcp: bool = False):
self.station = station
# logging.debug("Station: %s", self.station)
self.sequence = 0
if tcp:
self.upload = self.tcp_upload
self.socket = None
else:
self.upload = self.udp_upload
self.socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
self.id = os.urandom(4)
def udp_upload(self, spots: list) -> list | None:
logging.info("uploading %i spots using UDP", len(spots))
for packet, chunk in self.getPackets(spots):
self.sequence += len(chunk)
self.socket.sendto(packet, SERVER_NAME)
return None
def tcp_upload(self, spots: list) -> list | None:
logging.info("uploading %i spots using TCP", len(spots))
failed_to_send = []
sent_attempt = 0
for packet, chunk in self.getPackets(spots, max_packet_length=25000):
while sent_attempt < 5:
sent_attempt += 1
try:
if not self.socket:
self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.socket.connect(SERVER_NAME)
self.socket.setsockopt(
socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1
)
self.socket_connected = True
self.socket.send(packet)
self.sequence += len(chunk)
sent_attempt = 0
break
except Exception:
self.socket.close()
self.socket = None
else:
failed_to_send.extend(chunk)
if failed_to_send:
logging.warning(
f"Failed to send {len(failed_to_send)} spots. Will retry later."
)
return failed_to_send
def getPackets(self, spots, max_packet_length: int = 1400):
encoded = []
to_spot = {}
for spot in spots:
enc = self.encodeSpot(spot)
if enc:
encoded.append(enc)
to_spot[enc] = spot
def chunks(l, n):
"""Yield successive chunks from with a total length < n"""
i = 0
while i < len(l):
length = n
def inner(l, j, remaining):
while j < len(l) and remaining >= len(l[j]):
remaining -= len(l[j])
yield l[j]
j += 1
chunk = list(inner(l, i, n))
i += len(chunk)
yield chunk
rHeader = self.getReceiverInformationHeader()
rInfo = self.getReceiverInformation()
sHeader = self.getSenderInformationHeader()
packets = []
header_length = 16 + len(rHeader) + len(sHeader) + len(rInfo)
for chunk in chunks(encoded, max_packet_length - header_length):
sInfo = self.getSenderInformation(chunk)
length = header_length + len(sInfo)
header = self.getHeader(length)
yield (
header + rHeader + sHeader + rInfo + sInfo,
[to_spot[item] for item in chunk],
)
def getHeader(self, length):
return bytes(
# protocol version
[0x00, 0x0A]
+ list(length.to_bytes(2, "big"))
+ list(int(time.time()).to_bytes(4, "big"))
+ list(self.sequence.to_bytes(4, "big"))
+ list(self.id)
)
def encodeString(self, s):
return [len(s)] + list(s.encode("utf-8"))
def encodeBytes(self, b):
return [len(b)] + list(b)
def encodeSpot(self, spot):
try:
return bytes(
self.encodeString(spot["callsign"])
# freq in Hz to pskreporter
+ list(int(spot["freq"]).to_bytes(4, "big"))
+ list(int(spot["db"]).to_bytes(1, "big", signed=True))
+ self.encodeString(spot["mode"])
+ self.encodeString(spot["locator"])
# informationsource. 1 means "automatically extracted
+ [0x01]
+ list(int(spot["timestamp"]).to_bytes(4, "big"))
+ self.encodeBytes(spot["bytes"])
+ list(int(spot["dt"]).to_bytes(2, "big", signed=True))
)
except Exception:
logging.exception("Error while encoding spot for pskreporter")
return None
def getReceiverInformationHeader(self):
return bytes(
# id, length
[0x00, 0x03, 0x00, 0x2C]
+ Uploader.receiverDelimiter
# number of fields
+ [0x00, 0x04, 0x00, 0x01]
# receiverCallsign
+ [0x80, 0x02, 0xFF, 0xFF, 0x00, 0x00, 0x76, 0x8F]
# receiverLocator
+ [0x80, 0x04, 0xFF, 0xFF, 0x00, 0x00, 0x76, 0x8F]
# decodingSoftware
+ [0x80, 0x08, 0xFF, 0xFF, 0x00, 0x00, 0x76, 0x8F]
# antennaInformation
+ [0x80, 0x09, 0xFF, 0xFF, 0x00, 0x00, 0x76, 0x8F]
# padding
+ [0x00, 0x00]
)
def getReceiverInformation(self):
callsign = self.station["callsign"]
locator = self.station["grid"]
antennaInformation = (
self.station["antenna"] if "antenna" in self.station else ""
)
decodingSoftware = "N1DQ-KA9Q-Radio/1.4"
body = [
b
for s in [callsign, locator, decodingSoftware, antennaInformation]
for b in self.encodeString(s or "")
]
body = self.pad(body, 4)
body = bytes(
Uploader.receiverDelimiter + list((len(body) + 4).to_bytes(2, "big")) + body
)
return body
def getSenderInformationHeader(self):
return bytes(
# id, length
[0x00, 0x02, 0x00, 0x4C]
+ Uploader.senderDelimiter
# number of fields
+ [0x00, 0x09]
# senderCallsign
+ [0x80, 0x01, 0xFF, 0xFF, 0x00, 0x00, 0x76, 0x8F]
# frequency
+ [0x80, 0x05, 0x00, 0x04, 0x00, 0x00, 0x76, 0x8F]
# sNR
+ [0x80, 0x06, 0x00, 0x01, 0x00, 0x00, 0x76, 0x8F]
# mode
+ [0x80, 0x0A, 0xFF, 0xFF, 0x00, 0x00, 0x76, 0x8F]
# senderLocator
+ [0x80, 0x03, 0xFF, 0xFF, 0x00, 0x00, 0x76, 0x8F]
# informationSource
+ [0x80, 0x0B, 0x00, 0x01, 0x00, 0x00, 0x76, 0x8F]
# flowStartSeconds
+ [0x00, 0x96, 0x00, 0x04]
# messageBits
+ [0x80, 0x0E, 0xFF, 0xFF, 0x00, 0x00, 0x76, 0x8F]
# deltaT
+ [0x80, 0x0F, 0x00, 0x02, 0x00, 0x00, 0x76, 0x8F]
)
def getSenderInformation(self, chunk):
sInfo = self.padBytes(b"".join(chunk), 4)
sInfoLength = len(sInfo) + 4
return bytes(Uploader.senderDelimiter) + sInfoLength.to_bytes(2, "big") + sInfo
def pad(self, b, l):
return b + [0x00 for _ in range(0, -1 * len(b) % l)]
def padBytes(self, b, l):
return b + bytes([0x00 for _ in range(0, -1 * len(b) % l)])