-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.ts
More file actions
431 lines (364 loc) · 13.8 KB
/
server.ts
File metadata and controls
431 lines (364 loc) · 13.8 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
import express from "express";
import { createServer as createViteServer } from "vite";
import path from "path";
import fs from "fs";
import { fileURLToPath } from "url";
import { WebSocketServer, WebSocket } from "ws";
import net from "net";
import { createServer } from "http";
import { BAND_FREQUENCIES } from "./src/types.js";
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const CONFIG_FILE = path.join(__dirname, "config.json");
const SPOTS_FILE = path.join(__dirname, "spots.json");
const QSO_FILE = path.join(__dirname, "worked_qsos.json");
async function startServer() {
const app = express();
const server = createServer(app);
const wss = new WebSocketServer({ server });
const PORT = 3000;
app.use(express.json());
// --- Persistence ---
const defaultConfig = {
columns: [
{ id: "col1", title: "Kanal 1", band: "2m", visible: true },
{ id: "col2", title: "Kanal 2", band: "70cm", visible: true },
{ id: "col3", title: "Kanal 3", band: "23cm", visible: true },
{ id: "col4", title: "Kanal 4", band: "13cm", visible: true },
{ id: "col5", title: "Kanal 5", band: "3cm", visible: true },
],
bottomAreaVisible: true,
retentionHours: 4,
autoBandSwitch: true,
clusterHost: "dx.da0bcc.de",
clusterPort: 7300,
clusterCallsign: "DF0OT"
};
if (!fs.existsSync(CONFIG_FILE)) {
fs.writeFileSync(CONFIG_FILE, JSON.stringify(defaultConfig, null, 2));
}
const loadConfig = () => {
try {
if (fs.existsSync(CONFIG_FILE)) {
const loaded = JSON.parse(fs.readFileSync(CONFIG_FILE, "utf-8"));
return { ...defaultConfig, ...loaded };
}
} catch (e) {
console.error("Error loading config:", e);
}
return defaultConfig;
};
let spots: any[] = [];
if (fs.existsSync(SPOTS_FILE)) {
try {
spots = JSON.parse(fs.readFileSync(SPOTS_FILE, "utf-8"));
} catch (e) {
spots = [];
}
}
let workedQSOs: Record<string, string[]> = {};
if (fs.existsSync(QSO_FILE)) {
try {
workedQSOs = JSON.parse(fs.readFileSync(QSO_FILE, "utf-8"));
} catch (e) {
workedQSOs = {};
}
}
const saveSpots = () => {
fs.writeFileSync(SPOTS_FILE, JSON.stringify(spots, null, 2));
};
const saveWorkedQSOs = () => {
fs.writeFileSync(QSO_FILE, JSON.stringify(workedQSOs, null, 2));
};
const getRetentionHours = () => {
const config = loadConfig();
return config.retentionHours || 4;
};
const cleanupSpots = () => {
const hours = getRetentionHours();
const cutoff = Date.now() - (hours * 60 * 60 * 1000);
const originalCount = spots.length;
spots = spots.filter(spot => new Date(spot.timestamp).getTime() > cutoff);
if (spots.length !== originalCount) {
saveSpots();
broadcast({ type: "spots:init", spots });
}
};
setInterval(cleanupSpots, 5 * 60 * 1000); // Cleanup every 5 mins
app.get("/api/config", (req, res) => {
try {
const data = fs.readFileSync(CONFIG_FILE, "utf-8");
res.json(JSON.parse(data));
} catch (error) {
res.status(500).json({ error: "Could not read config" });
}
});
app.post("/api/config", (req, res) => {
try {
const oldConfig = loadConfig();
const newConfig = req.body;
fs.writeFileSync(CONFIG_FILE, JSON.stringify(newConfig, null, 2));
cleanupSpots(); // Run immediate cleanup if retention hours changed
// If cluster settings changed, reconnect
if (
oldConfig.clusterHost !== newConfig.clusterHost ||
oldConfig.clusterPort !== newConfig.clusterPort ||
oldConfig.clusterCallsign !== newConfig.clusterCallsign
) {
console.log("[SERVER] Cluster settings updated, reconnecting...");
connectToCluster();
}
res.json({ success: true });
} catch (error) {
res.status(500).json({ error: "Could not save config" });
}
});
// --- Frequency API ---
// Format: http://url/api/qrg/x/y where x is column index (1-5) and y is freq in MHz
app.get("/api/qrg/:column/:freq", (req, res) => {
const { column, freq } = req.params;
const colIdx = parseInt(column);
const freqVal = parseFloat(freq);
if (isNaN(colIdx) || colIdx < 1 || colIdx > 5 || isNaN(freqVal)) {
return res.status(400).json({ error: "Invalid parameters. Usage: /api/qrg/[1-5]/[MHz]" });
}
const columnId = `col${colIdx}`;
console.log(`[API] QRG Update: Column ${columnId} -> ${freqVal.toFixed(3)} MHz`);
// Auto Band Switching logic
let detectedBand: string | null = null;
try {
const currentConfig = JSON.parse(fs.readFileSync(CONFIG_FILE, "utf-8"));
if (currentConfig.autoBandSwitch) {
// Find which band this freq belongs to
for (const [bandName, range] of Object.entries(BAND_FREQUENCIES)) {
if (freqVal >= range.min && freqVal <= range.max) {
detectedBand = bandName;
break;
}
}
if (detectedBand) {
const colIndex = currentConfig.columns.findIndex((c: any) => c.id === columnId);
if (colIndex !== -1 && currentConfig.columns[colIndex].band !== detectedBand) {
console.log(`[API] Auto Band Switch: ${columnId} changed from ${currentConfig.columns[colIndex].band} to ${detectedBand}`);
currentConfig.columns[colIndex].band = detectedBand;
fs.writeFileSync(CONFIG_FILE, JSON.stringify(currentConfig, null, 2));
// Broadcast the config update so the UI refreshes the column completely
broadcast({ type: "config", config: currentConfig });
}
}
}
} catch (e) {
console.error("Error during auto band switch:", e);
}
broadcast({
type: "column:freq",
columnId,
frequency: freqVal
});
res.json({ success: true, columnId, frequency: freqVal, autoSwitchedTo: detectedBand });
});
// --- Worked QSO API ---
// Format: http://url/api/qso/band/call/freq?
app.get("/api/qso/:band/:call/:freq?", (req, res) => {
const { band, call, freq } = req.params;
const bandName = band.trim();
const callsign = call.trim().toUpperCase();
if (!bandName || !callsign) {
return res.status(400).json({ error: "Invalid parameters. Usage: /api/qso/[band]/[call]/[optional-freq-mhz]" });
}
// Add to worked list
if (!workedQSOs[bandName]) {
workedQSOs[bandName] = [];
}
if (!workedQSOs[bandName].includes(callsign)) {
workedQSOs[bandName].push(callsign);
saveWorkedQSOs();
console.log(`[API] QSO Added: ${callsign} on ${bandName}`);
broadcast({ type: "qso:update", workedQSOs });
}
// Check for optional frequency to update or create a spot
if (freq) {
const fNum = parseFloat(freq);
if (!isNaN(fNum)) {
// Find if this callsign already exists in spots
const existingSpotIndex = spots.findIndex(s => s.dxCall === callsign);
if (existingSpotIndex !== -1) {
// Update the frequency and timestamp of the existing spot
// We keep its "cluster" status (isManual: false) if it was one
spots[existingSpotIndex].frequency = fNum;
spots[existingSpotIndex].timestamp = new Date().toISOString();
saveSpots();
console.log(`[API] Existing Spot Updated: ${callsign} to ${fNum} MHz`);
// Re-broadcast all spots or just an update? Re-init is safer to ensure all bands see the freq change
broadcast({ type: "spots:init", spots });
} else {
// If not in spots, create a manual entry
const manualSpot = {
id: `m-${Math.random().toString(36).substr(2, 9)}`,
spotterCall: "DF0OT",
frequency: fNum,
dxCall: callsign,
info: "MANUAL ENTRY",
timestamp: new Date().toISOString(),
isManual: true
};
spots.push(manualSpot);
saveSpots();
console.log(`[API] Manual Spot Created: ${callsign} @ ${fNum} MHz`);
broadcast({ type: "spot:new", spot: manualSpot });
}
}
}
res.json({
success: true,
band: bandName,
call: callsign,
totalOnBand: workedQSOs[bandName].length,
workedCalls: workedQSOs[bandName]
});
});
app.post("/api/qso/reset", (req, res) => {
workedQSOs = {};
saveWorkedQSOs();
// Also remove manual spots
const originalSpotCount = spots.length;
spots = spots.filter(s => !s.isManual);
if (spots.length !== originalSpotCount) {
saveSpots();
}
console.log(`[API] Worked QSOs Reset (and Manual Spots removed)`);
// Broadcast reset to all clients
broadcast({ type: "qso:update", workedQSOs: {} });
// Also re-broadcast current spots to force columns to re-evaluate their contents
broadcast({ type: "spots:init", spots });
// Add info to history
const resetMsg = `[SYSTEM] ${new Date().toLocaleTimeString()} - WORKED QSOs RESET BY USER`;
consoleHistory.push(resetMsg);
if (consoleHistory.length > MAX_CONSOLE_HISTORY) consoleHistory.shift();
broadcast({ type: "data", lines: [resetMsg], charCount });
res.json({ success: true });
});
// --- DX Cluster Connection ---
let clusterStatus = "DISCONNECTED";
let charCount = 0;
const consoleHistory: string[] = [];
const MAX_CONSOLE_HISTORY = 100;
let activeClusterClient: net.Socket | null = null;
let reconnectTimeout: NodeJS.Timeout | null = null;
const broadcast = (data: any) => {
const msg = JSON.stringify(data);
wss.clients.forEach(client => {
if (client.readyState === WebSocket.OPEN) {
client.send(msg);
}
});
};
const connectToCluster = () => {
if (reconnectTimeout) clearTimeout(reconnectTimeout);
if (activeClusterClient) {
activeClusterClient.destroy();
activeClusterClient = null;
}
const config = loadConfig();
clusterStatus = "CONNECTING";
broadcast({ type: "status", status: clusterStatus });
console.log(`Connecting to DX Cluster: ${config.clusterHost}:${config.clusterPort}`);
const client = net.createConnection({ host: config.clusterHost, port: config.clusterPort }, () => {
console.log("Connected to DX Cluster");
clusterStatus = "CONNECTED";
broadcast({ type: "status", status: clusterStatus, loginCall: config.clusterCallsign });
const loginCall = config.clusterCallsign?.trim();
if (loginCall) {
console.log(`Logging in as ${loginCall}`);
client.write(`${loginCall}\r\n`);
} else {
console.log("No login callsign provided, skipping login command.");
}
});
activeClusterClient = client;
let clusterBuffer = "";
client.on("data", (data) => {
const str = data.toString();
charCount += str.length;
clusterBuffer += str;
const parts = clusterBuffer.split(/\r?\n/);
clusterBuffer = parts.pop() || ""; // Keep the last part in buffer
if (parts.length === 0) return;
parts.forEach(line => {
const trimmedLine = line.trim();
if (!trimmedLine) return;
consoleHistory.push(trimmedLine);
if (consoleHistory.length > MAX_CONSOLE_HISTORY) consoleHistory.shift();
// Parse DX Spot - very robust regex
// Format: DX de SPOTTER: FREQ DX-CALL INFO TIMEZ
// Optional # prefix for some cluster nodes
const match = trimmedLine.match(/^(?:#\s*)?DX\s+de\s+([^:]+):\s+([\d.]+)\s+([^\s]+)\s+(.*?)\s+(\d{4})\s*Z/i);
if (match) {
const [, spotterCall, freqStr, dxCall, info, timeStr] = match;
const freqKHz = parseFloat(freqStr);
const freqMHz = freqKHz / 1000;
console.log(`[PARSER] Match Success: ${dxCall} @ ${freqMHz.toFixed(3)}MHz (Spotter: ${spotterCall.trim()})`);
const newSpot = {
id: Math.random().toString(36).substr(2, 9),
spotterCall: spotterCall.trim(),
frequency: freqMHz,
dxCall: dxCall.trim(),
info: info.trim(),
timestamp: new Date().toISOString()
};
spots.push(newSpot);
saveSpots();
broadcast({ type: "spot:new", spot: newSpot });
} else if (/DX\s+de/i.test(trimmedLine)) {
console.log(`[PARSER] Potential DX Spot failed regex: "${trimmedLine}"`);
}
});
broadcast({ type: "data", lines: parts, charCount });
});
client.on("end", () => {
console.log("Disconnected from DX Cluster");
clusterStatus = "DISCONNECTED";
broadcast({ type: "status", status: clusterStatus });
activeClusterClient = null;
reconnectTimeout = setTimeout(connectToCluster, 5000); // Reconnect
});
client.on("error", (err) => {
console.error("Cluster connection error:", err.message);
clusterStatus = "ERROR";
broadcast({ type: "status", status: clusterStatus, error: err.message });
client.destroy();
activeClusterClient = null;
reconnectTimeout = setTimeout(connectToCluster, 10000);
});
};
connectToCluster();
// Handle WS connections
wss.on("connection", (ws) => {
ws.send(JSON.stringify({
type: "init",
history: consoleHistory,
status: clusterStatus,
charCount,
spots,
workedQSOs
}));
});
// --- Vite & Routing ---
if (process.env.NODE_ENV !== "production") {
const vite = await createViteServer({
server: { middlewareMode: true },
appType: "spa",
});
app.use(vite.middlewares);
} else {
const distPath = path.join(process.cwd(), "dist");
app.use(express.static(distPath));
app.get("*", (req, res) => {
res.sendFile(path.join(distPath, "index.html"));
});
}
server.listen(PORT, "0.0.0.0", () => {
console.log(`Server running on http://0.0.0.0:${PORT}`);
});
}
startServer();