-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathindex.js
More file actions
executable file
·661 lines (565 loc) · 20.6 KB
/
index.js
File metadata and controls
executable file
·661 lines (565 loc) · 20.6 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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
#!/usr/bin/env -S bun
"use strict";
const DXCluster = require('./dxcluster');
const POTASpots = require('./pota');
const express = require("express");
const app = express()
const path = require("path")
const cors = require('cors');
const morgan = require('morgan');
const fetch = require('node-fetch'); // Fetch for API requests
const WebSocket = require('ws'); // WebSocket server
var dxcc;
//Load config from file or from environment variables
var config = {};
if (process.env.WEBPORT === undefined) {
config = require("./config.js");
} else {
config={maxcache: process.env.MAXCACHE, webport: process.env.WEBPORT, baseUrl: process.env.WEBURL, dxcc_lookup_wavelog_url: process.env.WAVELOG_URL, dxcc_lookup_wavelog_key: process.env.WAVELOG_KEY, includepotaspots: process.env.POTA_INTEGRATION, potapollinterval: process.env.POTA_POLLING_INTERVAL };
config.clusters=JSON.parse(process.env.CLUSTERS || '[]');
config.dxc={ host: process.env.DXHOST, port: process.env.DXPORT, loginPrompt: 'login:', call: process.env.DXCALL, password: process.env.DXPASSWORD };
}
let clusters = [];
if (config.clusters.length>0) {
// New format: host:port,host:port,host:port
clusters = config.clusters;
} else {
// Old format: single cluster via CLUSTER_HOST/CLUSTER_PORT
clusters[0] = config.dxc;
}
morgan.token('remote-addr', function (req, res) {
var ffHeaderValue = req.headers['x-forwarded-for'];
return ffHeaderValue || req.connection.remoteAddress;
});
app.disable('x-powered-by');
app.use(express.json());
app.use(morgan(':remote-addr - :remote-user [:date[clf]] ":method :url HTTP/:http-version" :status :res[content-length] ":referrer" ":user-agent" :response-time ms'));
app.use(cors({ origin: '*' }));
// Serve static files (for demo page)
app.use(config.baseUrl + '/demo', express.static(path.join(__dirname, 'public')));
// DXCluster connection and spot cache
let spots=[];
// Indexes for faster lookups
const bandIndex = new Map(); // Map<band, Set<spot>>
const frequencyIndex = new Map(); // Map<frequency, spot>
const sourceIndex = new Map(); // Map<source, Set<spot>>
// WebSocket clients
const wsClients = new Set();
/**
* Broadcasts a new spot to all connected WebSocket clients
*/
function broadcastSpot(spot) {
if (wsClients.size === 0) return;
const message = JSON.stringify({
type: 'spot',
data: spot
});
wsClients.forEach((client) => {
if (client.readyState === WebSocket.OPEN) {
try {
client.send(message);
} catch (error) {
console.error('Error sending to WebSocket client:', error);
wsClients.delete(client);
}
}
});
}
// -----------------------------------
// Utility Functions
// -----------------------------------
/**
* Helper function to log connection state changes.
* @param {string} state - The current state of the connection.
* @param {string} server - The server being connected to.
* @param {string} reason - The reason for the connection.
* @param {Error} [error=null] - Optional error object for logging.
*/
function logConnectionState(state, server = '', error = null) {
const timestamp = new Date().toISOString();
const serverInfo = typeof server === 'object' ? `${server.host}:${server.port}` : server;
if (error && error instanceof Error) {
console.error(`[${timestamp}] - Error while connecting to ${serverInfo}:`, error);
} else if (state === 'attempting') {
console.log(`[${timestamp}] - Attempting to connect to ${serverInfo}`);
} else if (state === 'connected') {
console.log(`[${timestamp}] - Successfully connected to ${serverInfo}`);
} else if (state === 'closed') {
console.log(`[${timestamp}] - Connection to ${serverInfo} closed`);
} else if (state === 'timeout') {
console.log(`[${timestamp}] - Connection to ${serverInfo} timed out`);
}
}
/**
* Converts a string to title case (capitalize each word).
* @param {string} string - The input string to convert.
* @returns {string} - The title-cased string.
*/
function toUcWord(string) {
let words = string.toLowerCase().split(" ");
for (let i = 0; i < words.length; i++) {
words[i] = words[i][0].toUpperCase() + words[i].substr(1);
}
return words.join(" ");
}
// -----------------------------------
// DXCluster Connection Logic
// -----------------------------------
/**
* Initiates a connection to the DXCluster and logs events.
*/
function reconnect() {
function conn_one(cluster) {
logConnectionState('attempting', cluster.host, 'DXCluster server for receiving spots');
const conn = new DXCluster();
try {
conn.connect(cluster).then(() => {
logConnectionState('connected', cluster.host, 'DXCluster server for receiving spots');
})
.catch((err) => {
logConnectionState('failed', cluster.host, 'DXCluster server for receiving spots', err);
conn_one(cluster);
});
// Event listeners for connection status changes
conn.on('close', () => {
logConnectionState('closed', cluster.host, 'DXCluster server connection closed');
conn_one(cluster);
});
conn.on('timeout', () => {
logConnectionState('timeout', cluster.host, 'DXCluster server connection timed out');
conn_one(cluster);
});
conn.on('error', (err) => {
logConnectionState('error', cluster.host, 'DXCluster server connection error', err);
conn_one(cluster);
});
// -----------------------------------
// DXCluster Spot Handling
// -----------------------------------
/**
* Processes spots received from DXCluster.
*/
conn.on('spot', async function x(spot) {
await handlespot(spot, (cluster.cluster || 'cluster'));
});
} catch (e) {
logConnectionState('error', config.host, 'DXCluster not reachable ');
console.log(e);
}
}
clusters.forEach(cluster => {
conn_one(cluster);
});
}
// -----------------------------------
// API Endpoints
// -----------------------------------
/**
* GET /spot/:qrg - Retrieve the latest spot for a given frequency (QRG in kHz).
*/
app.get(config.baseUrl + '/spot/:qrg', (req, res) => {
const qrg = req.params.qrg;
const single_spot = get_singlespot(qrg);
res.json(single_spot);
});
/**
* GET /spots - Retrieve all cached spots.
*/
app.get(config.baseUrl + '/spots', (req, res) => {
res.json(spots);
});
/**
* GET /spots/:band - Retrieve all cached spots for a given band.
*/
app.get(config.baseUrl + '/spots/:band', (req, res) => {
const bandspots = get_bandspots(req.params.band);
res.json(bandspots);
});
/**
* GET /spots/source/:source - Retrieve all cached spots from a given source.
*/
app.get(config.baseUrl + '/spots/source/:source', (req, res) => {
const sourcespots = get_sourcespots(req.params.source);
res.json(sourcespots);
});
/**
* GET /stats - Retrieve statistics about the cached spots.
*/
app.get(config.baseUrl + '/stats', (req, res) => {
const stats = {
entries: spots.length,
cluster: spots.filter(item => item.source === 'cluster').length,
pota: spots.filter(item => item.source === 'pota').length,
freshest: get_freshest(spots),
oldest: get_oldest(spots)
};
res.json(stats);
});
// -----------------------------------
// Server Start
// -----------------------------------
/**
* Starts the server and initializes the DXCluster connection.
*/
async function main() {
try {
const server = app.listen(config.webport, '0.0.0.0', () => {
console.log(`Listener started on Port ${config.webport}`);
});
// Create WebSocket server
const wss = new WebSocket.Server({ server });
wss.on('connection', (ws) => {
console.log('New WebSocket client connected');
wsClients.add(ws);
ws.on('close', () => {
console.log('WebSocket client disconnected');
wsClients.delete(ws);
});
ws.on('error', (error) => {
console.error('WebSocket error:', error);
wsClients.delete(ws);
});
});
console.log(`WebSocket server started on Port ${config.webport}`);
reconnect(); // Start the connection to DXCluster
} catch (e) {
console.error("Error starting server:", e);
process.exit(99);
}
}
main();
// -----------------------------------
// POTA Spot Handling
// -----------------------------------
/**
* Processes spots received from POTA integration.
*/
//only initialize pota component if configured
if(config.includepotaspots || false){
//get potapollinterval
let potapollinterval = config.potapollinterval || 120;
var pota = new POTASpots({potapollinterval: potapollinterval});
pota.run();
pota.on('spot', async function x(spot) {
await handlespot(spot, "pota");
})
}
// -----------------------------------
// General Spot Handling
// -----------------------------------
/**
* Processes spots received from different sources and may add additional data points
*/
async function handlespot(spot, spot_source = "cluster"){
try {
//construct a clean spot
let dxSpot = {
spotter: spot.spotter,
spotted: spot.spotted,
frequency: spot.frequency,
message: spot.message,
when: spot.when,
source: spot_source,
}
//do DXCC lookup
dxSpot.dxcc_spotter=await dxcc_lookup(spot.spotter);
dxSpot.dxcc_spotted=await dxcc_lookup(spot.spotted);
//add pota specific data
if(spot_source == "pota"){
dxSpot.dxcc_spotted["pota_ref"] = spot.additional_data.pota_ref
dxSpot.dxcc_spotted["pota_mode"] = spot.additional_data.pota_mode
}
//lookup band
dxSpot.band=qrg2band(dxSpot.frequency*1000);
//push spot to cache
spots.push(dxSpot);
// Update indexes
updateIndexes(dxSpot);
// Broadcast to WebSocket clients
broadcastSpot(dxSpot);
//empty out spots if maximum retainment is reached
if (spots.length>config.maxcache) {
const removed = spots.shift();
removeFromIndexes(removed);
}
//reduce spots
const oldLength = spots.length;
spots=reduce_spots(spots);
// If spots were reduced, rebuild indexes
if (spots.length !== oldLength) {
rebuildIndexes();
}
} catch(e) {
console.error("Error processing spot:", e);
}
}
// -----------------------------------
// Index Management Functions
// -----------------------------------
/**
* Updates all indexes when a new spot is added
*/
function updateIndexes(spot) {
// Update band index
if (spot.band) {
if (!bandIndex.has(spot.band)) {
bandIndex.set(spot.band, new Set());
}
bandIndex.get(spot.band).add(spot);
}
// Update frequency index (keep only latest spot per frequency)
const existing = frequencyIndex.get(spot.frequency);
if (!existing || Date.parse(spot.when) > Date.parse(existing.when)) {
frequencyIndex.set(spot.frequency, spot);
}
// Update source index
if (spot.source) {
if (!sourceIndex.has(spot.source)) {
sourceIndex.set(spot.source, new Set());
}
sourceIndex.get(spot.source).add(spot);
}
}
/**
* Removes a spot from all indexes
*/
function removeFromIndexes(spot) {
if (!spot) return;
// Remove from band index
if (spot.band && bandIndex.has(spot.band)) {
bandIndex.get(spot.band).delete(spot);
if (bandIndex.get(spot.band).size === 0) {
bandIndex.delete(spot.band);
}
}
// Remove from frequency index if this is the current spot
if (frequencyIndex.get(spot.frequency) === spot) {
frequencyIndex.delete(spot.frequency);
}
// Remove from source index
if (spot.source && sourceIndex.has(spot.source)) {
sourceIndex.get(spot.source).delete(spot);
if (sourceIndex.get(spot.source).size === 0) {
sourceIndex.delete(spot.source);
}
}
}
/**
* Rebuilds all indexes from scratch
*/
function rebuildIndexes() {
bandIndex.clear();
frequencyIndex.clear();
sourceIndex.clear();
spots.forEach(spot => updateIndexes(spot));
}
function get_singlespot (qrg) {
let ret={};
let youngest=Date.parse('1970-01-01T00:00:00.000Z');
spots.forEach((single) => {
if( (qrg*1 === single.frequency) && (Date.parse(single.when)>youngest)) {
ret=single;
youngest=Date.parse(single.when);
}
});
return ret;
}
// -----------------------------------
// Helper Functions
// -----------------------------------
let consecutiveErrorCount = 0;
const dxccServer = config.dxcc_lookup_wavelog_url; // The WaveLog server
let abortController = null; // For aborting ongoing requests
// DXCC cache: Map<callsign, {data, timestamp}>
const dxccCache = new Map();
const DXCC_CACHE_TTL = 24 * 60 * 60 * 1000; // 24 hours in milliseconds
async function dxcc_lookup(call) {
// Check cache first
const cached = dxccCache.get(call);
if (cached) {
const age = Date.now() - cached.timestamp;
if (age < DXCC_CACHE_TTL) {
return cached.data;
} else {
// Expired, remove from cache
dxccCache.delete(call);
}
}
let timeoutId = null; // Initialize timeoutId to null
try {
// Initialize the abort controller for the request
abortController = new AbortController();
timeoutId = setTimeout(() => {
if (abortController) {
abortController.abort();
}
}, 5000); // Set timeout for 5 seconds
const payload = {
key: config.dxcc_lookup_wavelog_key,
callsign: call
};
// Make the fetch request to the DXCC lookup server
const response = await fetch(dxccServer, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(payload),
signal: abortController.signal // Use the abort controller's signal
});
clearTimeout(timeoutId); // Clear the timeout if the request succeeds
if (!response.ok) {
throw new Error(`DXCC lookup failed with status: ${response.status}`);
}
const result = await response.json();
const returner = {
cont: result.cont,
entity: result.dxcc ? toUcWord(result.dxcc) : '',
flag: result.dxcc_flag,
dxcc_id: result.dxcc_id,
lotw_user: result.lotw_member,
lat: result.dxcc_lat || null,
lng: result.dxcc_long || null,
cqz: result.dxcc_cqz || null,
};
// Cache the result
dxccCache.set(call, {
data: returner,
timestamp: Date.now()
});
consecutiveErrorCount = 0; // Reset error count after a successful lookup
abortController = null; // Clear the abort controller after success
return returner;
} catch (error) {
clearTimeout(timeoutId); // Ensure the timeout is cleared on failure
abortController = null; // Clear the abort controller after failure
consecutiveErrorCount++; // Increment error count on failure
// Log the error with the WaveLog server info, but only for the first failure
if (consecutiveErrorCount === 1) {
console.error(`[${new Date().toISOString()}] - DXCC lookup failed for callsign: ${call} (Consecutive errors: ${consecutiveErrorCount}).`);
console.error(`Served by WaveLog server: ${dxccServer}`);
} else {
console.error(`[${new Date().toISOString()}] - DXCC lookup failed for callsign: ${call} (Consecutive errors: ${consecutiveErrorCount}).`);
}
}
}
/**
* Retrieves the latest spot for a given frequency (QRG in kHz).
* @param {number} qrg - The frequency (in kHz) to search for.
* @returns {object} - The latest spot for the given frequency.
*/
function get_singlespot(qrg) {
// Use frequency index for O(1) lookup
const spot = frequencyIndex.get(qrg * 1);
return spot || {};
}
/**
* Retrieves all spots for a given band.
* @param {string} band - The band to search for.
* @returns {array} - An array of spots for the given band.
*/
function get_bandspots(band) {
// Use band index for O(1) lookup
const spotSet = bandIndex.get(band);
return spotSet ? Array.from(spotSet) : [];
}
/**
* Retrieves all spots for a given source.
* @param {string} source - The source to search for.
* @returns {array} - An array of spots for the given source.
*/
function get_sourcespots(source) {
// Use source index for O(1) lookup
const spotSet = sourceIndex.get(source);
return spotSet ? Array.from(spotSet) : [];
}
/**
* Retrieves the most recent spot from the cache.
* @param {array} spotobj - Array of spot objects.
* @returns {string} - The timestamp of the most recent spot.
*/
function get_freshest(spotobj) {
let youngest = Date.parse('1970-01-01T00:00:00.000Z');
spotobj.forEach((single) => {
if (Date.parse(single.when) > youngest) {
youngest = Date.parse(single.when);
}
});
return new Date(youngest).toISOString();
}
/**
* Retrieves the oldest spot from the cache.
* @param {array} spotobj - Array of spot objects.
* @returns {string} - The timestamp of the oldest spot.
*/
function get_oldest(spotobj) {
let oldest = Date.parse('2032-01-01T00:00:00.000Z');
spotobj.forEach((single) => {
if (Date.parse(single.when) < oldest) {
oldest = Date.parse(single.when);
}
});
return new Date(oldest).toISOString();
}
/**
* Deduplicates the spot cache, keeping only the latest spots.
* @param {array} spotobject - Array of spot objects.
* @returns {array} - Deduplicated array of spots.
*/
function reduce_spots(spotobject) {
// Use a Map to track the latest spot for each unique combination
// Key: spotted_continent_frequency, Value: spot object
const latestSpots = new Map();
spotobject.forEach((single) => {
// Skip spots without required DXCC data
if (!single.dxcc_spotter || !single.dxcc_spotted) {
return;
}
// Create unique key for deduplication
const key = `${single.spotted}_${single.dxcc_spotter.cont}_${single.frequency}`;
const timestamp = Date.parse(single.when);
// Check if we already have a spot with this key
const existing = latestSpots.get(key);
// Keep this spot if it's newer or if no existing spot
if (!existing || timestamp > Date.parse(existing.when)) {
latestSpots.set(key, single);
}
});
// Convert Map values back to array
return Array.from(latestSpots.values());
}
/**
* Maps frequency (in Hz) to corresponding ham radio bands.
* @param {number} Frequency - Frequency in Hz.
* @returns {string} - The corresponding ham radio band.
*/
function qrg2band(Frequency) {
let Band = '';
if (Frequency > 1000000 && Frequency < 2000000) Band = "160m";
else if (Frequency > 3000000 && Frequency < 4000000) Band = "80m";
else if (Frequency > 6000000 && Frequency < 8000000) Band = "40m";
else if (Frequency > 9000000 && Frequency < 11000000) Band = "30m";
else if (Frequency > 13000000 && Frequency < 15000000) Band = "20m";
else if (Frequency > 17000000 && Frequency < 19000000) Band = "17m";
else if (Frequency > 20000000 && Frequency < 22000000) Band = "15m";
else if (Frequency > 23000000 && Frequency < 25000000) Band = "12m";
else if (Frequency > 27000000 && Frequency < 30000000) Band = "10m";
else if (Frequency > 40660000 && Frequency < 40690000) Band = "8m";
else if (Frequency > 49000000 && Frequency < 52000000) Band = "6m";
else if (Frequency > 69000000 && Frequency < 71000000) Band = "4m";
else if (Frequency > 140000000 && Frequency < 150000000) Band = "2m";
else if (Frequency > 218000000 && Frequency < 226000000) Band = "1.25m";
else if (Frequency > 430000000 && Frequency < 440000000) Band = "70cm";
else if (Frequency > 900000000 && Frequency < 930000000) Band = "33cm";
else if (Frequency > 1200000000 && Frequency < 1300000000) Band = "23cm";
else if (Frequency > 2200000000 && Frequency < 2600000000) Band = "13cm";
else if (Frequency > 3000000000 && Frequency < 4000000000) Band = "9cm";
else if (Frequency > 5000000000 && Frequency < 6000000000) Band = "6cm";
else if (Frequency > 9000000000 && Frequency < 11000000000) Band = "3cm";
else if (Frequency > 23000000000 && Frequency < 25000000000) Band = "1.2cm";
else if (Frequency > 46000000000 && Frequency < 55000000000) Band = "6mm";
else if (Frequency > 75000000000 && Frequency < 82000000000) Band = "4mm";
else if (Frequency > 120000000000 && Frequency < 125000000000) Band = "2.5mm";
else if (Frequency > 133000000000 && Frequency < 150000000000) Band = "2mm";
else if (Frequency > 240000000000 && Frequency < 250000000000) Band = "1mm";
else if (Frequency >= 250000000000) Band = "<1mm";
return Band;
}