add stations and an Auto tab following each station's band
Parse N1MM RadioInfo packets, map sender IPs to configured stations, and let the front-end auto-select the rotator for the selected station's current band. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
+34
-13
@@ -2,16 +2,22 @@ import dgram from 'node:dgram';
|
||||
import { XMLParser } from 'fast-xml-parser';
|
||||
import type { TurnMessageHandler } from './types.js';
|
||||
|
||||
interface N1MMRotorXml {
|
||||
interface ParsedXml {
|
||||
N1MMRotor?: {
|
||||
freqband?: string;
|
||||
goazi?: string;
|
||||
};
|
||||
RadioInfo?: {
|
||||
Freq?: number;
|
||||
RadioNr?: number;
|
||||
FocusRadioNr?: number;
|
||||
};
|
||||
}
|
||||
|
||||
export default class N1mmServer {
|
||||
private readonly xmlParser = new XMLParser();
|
||||
private readonly turnHandlers: TurnMessageHandler[] = [];
|
||||
private readonly radioInfoHandlers: Array<(sourceIp: string, band: number, freq: number) => void> = [];
|
||||
|
||||
start(): void {
|
||||
const server = dgram.createSocket('udp4');
|
||||
@@ -27,8 +33,7 @@ export default class N1mmServer {
|
||||
});
|
||||
|
||||
server.on('message', (msg, rinfo) => {
|
||||
console.log(`server got: ${msg} from ${rinfo.address}:${rinfo.port}`);
|
||||
this.processN1mmXml(msg.toString(), rinfo.address);
|
||||
this.processXml(msg.toString(), rinfo.address);
|
||||
});
|
||||
|
||||
server.bind(12040);
|
||||
@@ -38,20 +43,36 @@ export default class N1mmServer {
|
||||
this.turnHandlers.push(handler);
|
||||
}
|
||||
|
||||
private processN1mmXml(xml: string, sourceIp: string): void {
|
||||
onRadioInfo(handler: (sourceIp: string, band: number, freq: number) => void): void {
|
||||
this.radioInfoHandlers.push(handler);
|
||||
}
|
||||
|
||||
private processXml(xml: string, sourceIp: string): void {
|
||||
try {
|
||||
const parsed = this.xmlParser.parse(xml) as N1MMRotorXml;
|
||||
const band = parseInt(parsed?.N1MMRotor?.freqband?.split(',')[0] ?? '0', 10);
|
||||
const az = parseInt(parsed?.N1MMRotor?.goazi?.split(',')[0] ?? '0', 10);
|
||||
|
||||
if (!band || !az) {
|
||||
console.error('Missing band or azimuth');
|
||||
return;
|
||||
const parsed = this.xmlParser.parse(xml) as ParsedXml;
|
||||
if (parsed.N1MMRotor) {
|
||||
this.processRotor(parsed, sourceIp);
|
||||
} else if (parsed.RadioInfo) {
|
||||
this.processRadioInfo(parsed, sourceIp);
|
||||
}
|
||||
|
||||
this.turnHandlers.forEach(h => h.handleTurnMessage({ band, az, sourceIp }));
|
||||
} catch (ex) {
|
||||
console.error(`Failed to parse: ${xml}`, ex);
|
||||
}
|
||||
}
|
||||
|
||||
private processRotor(parsed: ParsedXml, sourceIp: string): void {
|
||||
const band = parseInt(String(parsed.N1MMRotor?.freqband ?? '').split(',')[0] || '0', 10);
|
||||
const az = parseInt(String(parsed.N1MMRotor?.goazi ?? '').split(',')[0] || '0', 10);
|
||||
if (!band || !az) return;
|
||||
this.turnHandlers.forEach(h => h.handleTurnMessage({ band, az, sourceIp }));
|
||||
}
|
||||
|
||||
private processRadioInfo(parsed: ParsedXml, sourceIp: string): void {
|
||||
const freq = Number(parsed.RadioInfo?.Freq ?? 0);
|
||||
const radioNr = Number(parsed.RadioInfo?.RadioNr ?? 0);
|
||||
const focusRadioNr = Number(parsed.RadioInfo?.FocusRadioNr ?? 0);
|
||||
if (!freq || radioNr !== focusRadioNr) return;
|
||||
const band = Math.floor(freq / 100000);
|
||||
this.radioInfoHandlers.forEach(h => h(sourceIp, band, freq));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user