-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathpskreporter.js
More file actions
185 lines (157 loc) · 5.12 KB
/
pskreporter.js
File metadata and controls
185 lines (157 loc) · 5.12 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
const config = require('./config');
const hamutil = require('./hamutil');
const EventEmitter = require('events');
const axios = require('axios');
const ndjson = require('ndjson');
const sprintf = require('sprintf-js').sprintf;
const TTLCache = require('@isaacs/ttlcache');
const https = require('https');
class PskReporterReceiver extends EventEmitter {
start() {
if (config.pskreporter.disabled) {
return;
}
this.spotCache = new TTLCache({ttl: config.pskreporter.quorumInterval});
this.restartConnection();
}
restartConnection() {
if (this.abortController) {
this.abortController.abort();
}
this.abortController = new AbortController();
this.resetTimer();
axios({
url: config.pskreporter.url,
timeout: config.pskreporter.timeout,
responseType: 'stream',
signal: this.abortController.signal,
httpsAgent: new https.Agent({ keepAlive: false })
})
.then(response => {
response.data
.on('error', (err) => {
console.error(`PSK Reporter: connection error (${err}), reconnecting`);
setTimeout(() => {
this.restartConnection();
}, 5000);
})
.pipe(ndjson.parse({strict: false}))
.on('data', (obj) => {
this.processSpot(obj);
});
})
.catch(err => {
console.error(`PSK Reporter: connection error (${err}), reconnecting`);
setTimeout(() => {
this.restartConnection();
}, 5000);
});
}
resetTimer() {
if (this.timeout) {
clearTimeout(this.timeout);
}
this.timeout = setTimeout(() => {
console.error("PSK Reporter: timeout, reconnecting");
setTimeout(() => {
this.restartConnection();
}, 5000);
}, config.pskreporter.timeout);
}
processSpot(rawSpot) {
this.resetTimer();
if (!rawSpot.mode) {
return;
}
if (rawSpot.mode == 'CW') {
return; // ignore CW spots from PSK reporter; RBN is good enough
}
if (rawSpot.mode.startsWith('OLIVIA')) {
rawSpot.mode = 'OLIVIA';
}
if (rawSpot.receiverDecoderSoftware && rawSpot.receiverDecoderSoftware.includes('N1DQ-Importer-KA9Q-Radio')) {
return; // ignore these as they will usually have made-up spotter calls with strange suffixes
}
if (!rawSpot.senderCallsign || rawSpot.senderCallsign.startsWith('TNX') || rawSpot.senderCallsign.endsWith('/R') || rawSpot.senderCallsign.endsWith('/RX')) {
return;
}
if (rawSpot.mode === 'WSPR' || rawSpot.mode === 'FST4W') {
return;
}
let spotTime = new Date(rawSpot.flowStartSeconds*1000);
if ((new Date() - spotTime) > config.pskreporter.maxAge) {
return; // ignore old spots
}
let spot = {
source: 'pskreporter',
time: ('0' + spotTime.getHours()).slice(-2) + ':' + ('0' + spotTime.getMinutes()).slice(-2),
fullCallsign: rawSpot.senderCallsign,
spotter: rawSpot.receiverCallsign,
frequency: rawSpot.frequency/1000000,
mode: rawSpot.mode,
spotterSoftware: rawSpot.receiverDecoderSoftware,
date: spotTime
};
if (rawSpot.sNR !== null) {
spot.snr = parseInt(rawSpot.sNR);
}
spot.rawText = `DX de ${spot.spotter}: ${sprintf("%.01f", spot.frequency*1000)} ${spot.fullCallsign} ${spot.mode}`;
if (spot.snr !== undefined) {
spot.rawText += ` ${spot.snr} dB`;
}
spot.rawText += ` ${spot.time.replace(':', '')}Z`;
if (spot.spotterSoftware) {
spot.rawText += ` (${spot.spotterSoftware})`;
}
spot.title = `PSK Reporter spot ${spot.fullCallsign} (${hamutil.formatFrequency(spot.frequency)} ${spot.mode})`;
if (!config.pskreporter.spotterFilterRegex.test(rawSpot.receiverCallsign)) {
console.log(`DXCC lookup blocked for spotter callsign ${rawSpot.receiverCallsign}`);
spot.noSpotterDxccLookup = true;
}
this.checkQuorumAndEmit(spot);
}
checkQuorumAndEmit(spot) {
let band = config.bands.find((element) => {
return (element.from <= spot.frequency && element.to >= spot.frequency)
});
if (band === undefined) {
console.log(`PSK Reporter: unknown band for frequency ${spot.frequency}`);
return;
}
let key = `${spot.fullCallsign}-${band.band}-${spot.mode}`;
let cachedSpots = this.spotCache.get(key);
if (cachedSpots === undefined) {
cachedSpots = [];
}
// Add spot to cache entry
cachedSpots.push(spot);
// Filter out expired spots
let now = new Date();
cachedSpots = cachedSpots.filter(cachedSpot => (now - cachedSpot.date) < config.pskreporter.quorumInterval);
// Count number of unique spotters
let uniqueSpotterCount = cachedSpots.reduce((resultSet, item) => resultSet.add(item.spotter), new Set).size;
if (uniqueSpotterCount >= config.pskreporter.quorum) {
// Quorum met; emit any held back spots
let minifiedSpots = [];
for (let cachedSpot of cachedSpots) {
if (cachedSpot.emitted) {
minifiedSpots.push(cachedSpot);
continue;
}
if ((now - cachedSpot.date) < config.pskreporter.maxAge) {
this.emit("spot", cachedSpot);
// Only retain minimal data about emitted spots (to save memory)
minifiedSpots.push({
emitted: true,
date: cachedSpot.date,
spotter: cachedSpot.spotter
});
}
}
cachedSpots = minifiedSpots;
}
// Save to cache (updates recently used date)
this.spotCache.set(key, cachedSpots);
}
}
module.exports = PskReporterReceiver;