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 dgram from 'node:dgram';
|
||||||
import { XMLParser } from 'fast-xml-parser';
|
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 {
|
interface ParsedXml {
|
||||||
N1MMRotor?: {
|
N1MMRotor?: {
|
||||||
freqband?: string;
|
freqband?: string | number;
|
||||||
goazi?: string;
|
goazi?: string | number;
|
||||||
};
|
};
|
||||||
RadioInfo?: {
|
RadioInfo?: {
|
||||||
Freq?: number;
|
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 {
|
export default class N1mmServer {
|
||||||
private readonly xmlParser = new XMLParser();
|
private readonly xmlParser = new XMLParser();
|
||||||
private readonly turnHandlers: TurnMessageHandler[] = [];
|
private readonly turnHandlers: Array<(msg: TurnMessage) => void> = [];
|
||||||
private readonly radioInfoHandlers: Array<(sourceIp: string, band: number, freq: number) => void> = [];
|
private readonly radioInfoHandlers: Array<(msg: RadioInfoMessage) => void> = [];
|
||||||
|
|
||||||
start(): void {
|
start(): void {
|
||||||
const server = dgram.createSocket('udp4');
|
const server = dgram.createSocket('udp4');
|
||||||
|
|
||||||
server.on('error', err => {
|
server.on('error', err => {
|
||||||
console.error(`server error:\n${err.stack}`);
|
console.error('N1MM UDP server error:', err);
|
||||||
server.close();
|
server.close();
|
||||||
});
|
});
|
||||||
|
|
||||||
server.on('listening', () => {
|
server.on('listening', () => {
|
||||||
const { address, port } = server.address();
|
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) => {
|
server.on('message', (msg, rinfo) => {
|
||||||
this.processXml(msg.toString(), rinfo.address);
|
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);
|
this.turnHandlers.push(handler);
|
||||||
}
|
}
|
||||||
|
|
||||||
onRadioInfo(handler: (sourceIp: string, band: number, freq: number) => void): void {
|
onRadioInfo(handler: (msg: RadioInfoMessage) => void): void {
|
||||||
this.radioInfoHandlers.push(handler);
|
this.radioInfoHandlers.push(handler);
|
||||||
}
|
}
|
||||||
|
|
||||||
private processXml(xml: string, sourceIp: string): void {
|
private processXml(xml: string, sourceIp: string): void {
|
||||||
|
let parsed: ParsedXml;
|
||||||
try {
|
try {
|
||||||
const parsed = this.xmlParser.parse(xml) as ParsedXml;
|
parsed = this.xmlParser.parse(xml) as ParsedXml;
|
||||||
if (parsed.N1MMRotor) {
|
|
||||||
this.processRotor(parsed, sourceIp);
|
|
||||||
} else if (parsed.RadioInfo) {
|
|
||||||
this.processRadioInfo(parsed, sourceIp);
|
|
||||||
}
|
|
||||||
} catch (ex) {
|
} 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 {
|
private processRotor(rotor: NonNullable<ParsedXml['N1MMRotor']>, sourceIp: string): void {
|
||||||
const band = parseInt(String(parsed.N1MMRotor?.freqband ?? '').split(',')[0] || '0', 10);
|
const band = firstNumber(rotor.freqband);
|
||||||
const az = parseInt(String(parsed.N1MMRotor?.goazi ?? '').split(',')[0] || '0', 10);
|
const az = Math.round(firstNumber(rotor.goazi));
|
||||||
if (!band || !az) return;
|
if (!Number.isFinite(band) || !Number.isFinite(az)) {
|
||||||
this.turnHandlers.forEach(h => h.handleTurnMessage({ band, az, sourceIp }));
|
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 {
|
private processRadioInfo(info: NonNullable<ParsedXml['RadioInfo']>, sourceIp: string): void {
|
||||||
const freq = Number(parsed.RadioInfo?.Freq ?? 0);
|
const freq = Number(info.Freq ?? 0);
|
||||||
const radioNr = Number(parsed.RadioInfo?.RadioNr ?? 0);
|
// In SO2R/SO2V, only follow the radio that has keyboard focus.
|
||||||
const focusRadioNr = Number(parsed.RadioInfo?.FocusRadioNr ?? 0);
|
if (!freq || Number(info.RadioNr) !== Number(info.FocusRadioNr)) return;
|
||||||
if (!freq || radioNr !== focusRadioNr) return;
|
const band = freqToBand(freq / 100_000);
|
||||||
const band = Math.floor(freq / 100000);
|
if (band === null) return;
|
||||||
this.radioInfoHandlers.forEach(h => h(sourceIp, band, freq));
|
this.radioInfoHandlers.forEach(h => h({ band, freq, sourceIp }));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+3
-3
@@ -1,6 +1,6 @@
|
|||||||
import { readFileSync } from 'node:fs';
|
import { readFileSync } from 'node:fs';
|
||||||
import RotClient from './RotClient.js';
|
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 {
|
export interface TurnEvent {
|
||||||
label: string;
|
label: string;
|
||||||
@@ -42,7 +42,7 @@ export default class RotRouter implements TurnMessageHandler {
|
|||||||
return this.stations;
|
return this.stations;
|
||||||
}
|
}
|
||||||
|
|
||||||
handleRadioInfo(sourceIp: string, band: number, freq: number): void {
|
handleRadioInfo({ sourceIp, band, freq }: RadioInfoMessage): void {
|
||||||
const station = this.resolveStation(sourceIp);
|
const station = this.resolveStation(sourceIp);
|
||||||
if (!station) return;
|
if (!station) return;
|
||||||
this.radioInfoHandlers.forEach(h => h(station, band, freq));
|
this.radioInfoHandlers.forEach(h => h(station, band, freq));
|
||||||
@@ -59,7 +59,7 @@ export default class RotRouter implements TurnMessageHandler {
|
|||||||
handleTurnMessage(msg: TurnMessage): void {
|
handleTurnMessage(msg: TurnMessage): void {
|
||||||
const client = this.findClientForBand(msg.band);
|
const client = this.findClientForBand(msg.band);
|
||||||
if (!client) {
|
if (!client) {
|
||||||
console.error(`No RotClient found for the ${msg.band} band!`);
|
console.error(`No rotator configured for the ${msg.band} MHz band!`);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
client.turn(msg.az);
|
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 router = new RotRouter();
|
||||||
|
|
||||||
const n1mm = new N1mmServer();
|
const n1mm = new N1mmServer();
|
||||||
n1mm.onTurnMessage(router);
|
n1mm.onTurnMessage(msg => router.handleTurnMessage(msg));
|
||||||
n1mm.onRadioInfo((sourceIp, band, freq) => router.handleRadioInfo(sourceIp, band, freq));
|
n1mm.onRadioInfo(msg => router.handleRadioInfo(msg));
|
||||||
// n1mm.start();
|
|
||||||
|
|
||||||
const app = new Hono();
|
const app = new Hono();
|
||||||
app.use('/*', serveStatic({ root: './public' }));
|
app.use('/*', serveStatic({ root: './public' }));
|
||||||
@@ -68,3 +67,5 @@ const server = serve(
|
|||||||
);
|
);
|
||||||
|
|
||||||
injectWebSocket(server);
|
injectWebSocket(server);
|
||||||
|
|
||||||
|
n1mm.start();
|
||||||
|
|||||||
@@ -4,6 +4,13 @@ export interface TurnMessage {
|
|||||||
sourceIp: string;
|
sourceIp: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export interface RadioInfoMessage {
|
||||||
|
band: number;
|
||||||
|
/** Receive frequency in tens of Hz, as sent by N1MM. */
|
||||||
|
freq: number;
|
||||||
|
sourceIp: string;
|
||||||
|
}
|
||||||
|
|
||||||
export interface TurnMessageHandler {
|
export interface TurnMessageHandler {
|
||||||
handleTurnMessage(msg: TurnMessage): void;
|
handleTurnMessage(msg: TurnMessage): void;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user