-
Notifications
You must be signed in to change notification settings - Fork 0
/
pskreporter.py
263 lines (225 loc) · 8.62 KB
/
pskreporter.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
import os
import logging
import threading
import time
import random
import socket
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):
self.spots = []
self.oldSpots = {} # Indexed by timestamp
self.spotLock = threading.Lock()
self.station = {"callsign": callsign, "grid": grid, "antenna": antenna}
self.uploader = Uploader(self.station)
self.timer = None
self.dummy = dummy
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):
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,
}
if self.dummy:
print(spot)
return
with self.spotLock:
if any(x for x in self.getOldSpots() if self.spotEquals(spot, x)):
# dupe
pass
else:
self.addSpot(spot)
self.scheduleNextUpload()
def upload(self):
try:
with self.spotLock:
self.timer = None
spots = self.spots
self.spots = []
if spots:
self.uploader.upload(spots)
except Exception:
logging.exception("Failed to upload spots")
def cancelTimer(self):
if self.timer:
self.timer.cancel()
self.timer.join()
self.timer = None
class Uploader(object):
receiverDelimiter = [0x99, 0x92]
senderDelimiter = [0x99, 0x93]
def __init__(self, station):
self.station = station
# logging.debug("Station: %s", self.station)
self.sequence = 0
self.socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
self.id = os.urandom(4)
def upload(self, spots):
logging.warning("uploading %i spots ", len(spots))
for packet in self.getPackets(spots):
self.socket.sendto(packet, ("report.pskreporter.info", 4739))
def getPackets(self, spots):
encoded = [self.encodeSpot(spot) for spot in spots]
# filter out any erroneous encodes
encoded = [e for e in encoded if e is not None]
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)
# 75 seems to be a safe bet
for chunk in chunks(encoded, 1400 - header_length):
sInfo = self.getSenderInformation(chunk)
length = header_length + len(sInfo)
header = self.getHeader(length)
yield header + rHeader + sHeader + rInfo + sInfo
self.sequence += len(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 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"))
)
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-Importer-KA9Q-Radio"
body = [
b
for s in [callsign, locator, decodingSoftware, antennaInformation]
for b in self.encodeString(s)
]
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, 0x3C]
+ Uploader.senderDelimiter
# number of fields
+ [0x00, 0x07]
# 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]
)
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)])