fix N1MM parsing: real band numbers, accept 0°, start the listener
- Map RadioInfo frequencies and rotor <freqband> to standard band numbers (1.8, 3.5, 5, 7 … 10000) instead of truncating to integers, so 160 m and 80 m no longer become bands 1 and 3. - A turn to azimuth 0 (north) was dropped by a falsy check. - Log malformed rotor packets instead of dropping them silently. - The UDP listener was left commented out; start it after the HTTP server. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
+44
-29
@@ -1,11 +1,15 @@
|
||||
import dgram from 'node:dgram';
|
||||
import { XMLParser } from 'fast-xml-parser';
|
||||
import type { TurnMessageHandler } from './types.js';
|
||||
import { freqToBand } from './bands.js';
|
||||
import type { RadioInfoMessage, TurnMessage } from './types.js';
|
||||
|
||||
const N1MM_PORT = 12040;
|
||||
|
||||
// fast-xml-parser converts numeric-looking tag values to numbers.
|
||||
interface ParsedXml {
|
||||
N1MMRotor?: {
|
||||
freqband?: string;
|
||||
goazi?: string;
|
||||
freqband?: string | number;
|
||||
goazi?: string | number;
|
||||
};
|
||||
RadioInfo?: {
|
||||
Freq?: number;
|
||||
@@ -14,65 +18,76 @@ interface ParsedXml {
|
||||
};
|
||||
}
|
||||
|
||||
/** Takes the first entry of a possibly comma-separated N1MM value. */
|
||||
function firstNumber(value: string | number | undefined): number {
|
||||
return parseFloat(String(value ?? '').split(',')[0] ?? '');
|
||||
}
|
||||
|
||||
export default class N1mmServer {
|
||||
private readonly xmlParser = new XMLParser();
|
||||
private readonly turnHandlers: TurnMessageHandler[] = [];
|
||||
private readonly radioInfoHandlers: Array<(sourceIp: string, band: number, freq: number) => void> = [];
|
||||
private readonly turnHandlers: Array<(msg: TurnMessage) => void> = [];
|
||||
private readonly radioInfoHandlers: Array<(msg: RadioInfoMessage) => void> = [];
|
||||
|
||||
start(): void {
|
||||
const server = dgram.createSocket('udp4');
|
||||
|
||||
server.on('error', err => {
|
||||
console.error(`server error:\n${err.stack}`);
|
||||
console.error('N1MM UDP server error:', err);
|
||||
server.close();
|
||||
});
|
||||
|
||||
server.on('listening', () => {
|
||||
const { address, port } = server.address();
|
||||
console.log(`server listening ${address}:${port}`);
|
||||
console.log(`N1MM UDP server listening on ${address}:${port}`);
|
||||
});
|
||||
|
||||
server.on('message', (msg, rinfo) => {
|
||||
this.processXml(msg.toString(), rinfo.address);
|
||||
});
|
||||
|
||||
server.bind(12040);
|
||||
server.bind(N1MM_PORT);
|
||||
}
|
||||
|
||||
onTurnMessage(handler: TurnMessageHandler): void {
|
||||
onTurnMessage(handler: (msg: TurnMessage) => void): void {
|
||||
this.turnHandlers.push(handler);
|
||||
}
|
||||
|
||||
onRadioInfo(handler: (sourceIp: string, band: number, freq: number) => void): void {
|
||||
onRadioInfo(handler: (msg: RadioInfoMessage) => void): void {
|
||||
this.radioInfoHandlers.push(handler);
|
||||
}
|
||||
|
||||
private processXml(xml: string, sourceIp: string): void {
|
||||
let parsed: ParsedXml;
|
||||
try {
|
||||
const parsed = this.xmlParser.parse(xml) as ParsedXml;
|
||||
if (parsed.N1MMRotor) {
|
||||
this.processRotor(parsed, sourceIp);
|
||||
} else if (parsed.RadioInfo) {
|
||||
this.processRadioInfo(parsed, sourceIp);
|
||||
}
|
||||
parsed = this.xmlParser.parse(xml) as ParsedXml;
|
||||
} catch (ex) {
|
||||
console.error(`Failed to parse: ${xml}`, ex);
|
||||
console.error(`Failed to parse N1MM packet from ${sourceIp}: ${xml}`, ex);
|
||||
return;
|
||||
}
|
||||
|
||||
if (parsed.N1MMRotor) {
|
||||
this.processRotor(parsed.N1MMRotor, sourceIp);
|
||||
} else if (parsed.RadioInfo) {
|
||||
this.processRadioInfo(parsed.RadioInfo, sourceIp);
|
||||
}
|
||||
}
|
||||
|
||||
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 processRotor(rotor: NonNullable<ParsedXml['N1MMRotor']>, sourceIp: string): void {
|
||||
const band = firstNumber(rotor.freqband);
|
||||
const az = Math.round(firstNumber(rotor.goazi));
|
||||
if (!Number.isFinite(band) || !Number.isFinite(az)) {
|
||||
console.error(`Rotor packet from ${sourceIp} is missing band or azimuth`, rotor);
|
||||
return;
|
||||
}
|
||||
this.turnHandlers.forEach(h => h({ 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));
|
||||
private processRadioInfo(info: NonNullable<ParsedXml['RadioInfo']>, sourceIp: string): void {
|
||||
const freq = Number(info.Freq ?? 0);
|
||||
// In SO2R/SO2V, only follow the radio that has keyboard focus.
|
||||
if (!freq || Number(info.RadioNr) !== Number(info.FocusRadioNr)) return;
|
||||
const band = freqToBand(freq / 100_000);
|
||||
if (band === null) return;
|
||||
this.radioInfoHandlers.forEach(h => h({ band, freq, sourceIp }));
|
||||
}
|
||||
}
|
||||
|
||||
+3
-3
@@ -1,6 +1,6 @@
|
||||
import { readFileSync } from 'node:fs';
|
||||
import RotClient from './RotClient.js';
|
||||
import type { FullConfig, StationConfig, DynamicData, InitialState, TurnMessage, TurnMessageHandler } from './types.js';
|
||||
import type { FullConfig, StationConfig, DynamicData, InitialState, RadioInfoMessage, TurnMessage, TurnMessageHandler } from './types.js';
|
||||
|
||||
export interface TurnEvent {
|
||||
label: string;
|
||||
@@ -42,7 +42,7 @@ export default class RotRouter implements TurnMessageHandler {
|
||||
return this.stations;
|
||||
}
|
||||
|
||||
handleRadioInfo(sourceIp: string, band: number, freq: number): void {
|
||||
handleRadioInfo({ sourceIp, band, freq }: RadioInfoMessage): void {
|
||||
const station = this.resolveStation(sourceIp);
|
||||
if (!station) return;
|
||||
this.radioInfoHandlers.forEach(h => h(station, band, freq));
|
||||
@@ -59,7 +59,7 @@ export default class RotRouter implements TurnMessageHandler {
|
||||
handleTurnMessage(msg: TurnMessage): void {
|
||||
const client = this.findClientForBand(msg.band);
|
||||
if (!client) {
|
||||
console.error(`No RotClient found for the ${msg.band} band!`);
|
||||
console.error(`No rotator configured for the ${msg.band} MHz band!`);
|
||||
return;
|
||||
}
|
||||
client.turn(msg.az);
|
||||
|
||||
@@ -0,0 +1,37 @@
|
||||
/**
|
||||
* Amateur bands as [lower edge in MHz, band number]. The band number matches what
|
||||
* N1MM sends in `<freqband>` and what `config.json` uses in `bands`.
|
||||
*/
|
||||
const BANDS: ReadonlyArray<readonly [number, number]> = [
|
||||
[1.8, 1.8],
|
||||
[3.5, 3.5],
|
||||
[5, 5],
|
||||
[7, 7],
|
||||
[10, 10],
|
||||
[14, 14],
|
||||
[18, 18],
|
||||
[21, 21],
|
||||
[24, 24],
|
||||
[28, 28],
|
||||
[50, 50],
|
||||
[70, 70],
|
||||
[144, 144],
|
||||
[222, 222],
|
||||
[420, 432],
|
||||
[902, 902],
|
||||
[1240, 1296],
|
||||
[2300, 2304],
|
||||
[3300, 3400],
|
||||
[5650, 5760],
|
||||
[10000, 10000],
|
||||
];
|
||||
|
||||
/** Maps a frequency in MHz to its band number, or null if below the lowest band. */
|
||||
export function freqToBand(mhz: number): number | null {
|
||||
let band: number | null = null;
|
||||
for (const [edge, number] of BANDS) {
|
||||
if (mhz < edge) break;
|
||||
band = number;
|
||||
}
|
||||
return band;
|
||||
}
|
||||
+4
-3
@@ -10,9 +10,8 @@ import { getConnInfo } from '@hono/node-server/conninfo';
|
||||
const router = new RotRouter();
|
||||
|
||||
const n1mm = new N1mmServer();
|
||||
n1mm.onTurnMessage(router);
|
||||
n1mm.onRadioInfo((sourceIp, band, freq) => router.handleRadioInfo(sourceIp, band, freq));
|
||||
// n1mm.start();
|
||||
n1mm.onTurnMessage(msg => router.handleTurnMessage(msg));
|
||||
n1mm.onRadioInfo(msg => router.handleRadioInfo(msg));
|
||||
|
||||
const app = new Hono();
|
||||
app.use('/*', serveStatic({ root: './public' }));
|
||||
@@ -68,3 +67,5 @@ const server = serve(
|
||||
);
|
||||
|
||||
injectWebSocket(server);
|
||||
|
||||
n1mm.start();
|
||||
|
||||
@@ -4,6 +4,13 @@ export interface TurnMessage {
|
||||
sourceIp: string;
|
||||
}
|
||||
|
||||
export interface RadioInfoMessage {
|
||||
band: number;
|
||||
/** Receive frequency in tens of Hz, as sent by N1MM. */
|
||||
freq: number;
|
||||
sourceIp: string;
|
||||
}
|
||||
|
||||
export interface TurnMessageHandler {
|
||||
handleTurnMessage(msg: TurnMessage): void;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user