From 455a4794e3e00416da143448a60ef66018777a1f Mon Sep 17 00:00:00 2001 From: ericek111 Date: Sat, 14 Nov 2026 02:59:40 +0000 Subject: [PATCH] =?UTF-8?q?fix=20N1MM=20parsing:=20real=20band=20numbers,?= =?UTF-8?q?=20accept=200=C2=B0,=20start=20the=20listener?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Map RadioInfo frequencies and rotor 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 --- src/N1mmServer.ts | 73 ++++++++++++++++++++++++++++------------------- src/RotRouter.ts | 6 ++-- src/bands.ts | 37 ++++++++++++++++++++++++ src/index.ts | 7 +++-- src/types.ts | 7 +++++ 5 files changed, 95 insertions(+), 35 deletions(-) create mode 100644 src/bands.ts diff --git a/src/N1mmServer.ts b/src/N1mmServer.ts index e319d79..ededf09 100644 --- a/src/N1mmServer.ts +++ b/src/N1mmServer.ts @@ -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, 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, 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 })); } } diff --git a/src/RotRouter.ts b/src/RotRouter.ts index dc86065..c5071e0 100644 --- a/src/RotRouter.ts +++ b/src/RotRouter.ts @@ -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); diff --git a/src/bands.ts b/src/bands.ts new file mode 100644 index 0000000..968b7ab --- /dev/null +++ b/src/bands.ts @@ -0,0 +1,37 @@ +/** + * Amateur bands as [lower edge in MHz, band number]. The band number matches what + * N1MM sends in `` and what `config.json` uses in `bands`. + */ +const BANDS: ReadonlyArray = [ + [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; +} diff --git a/src/index.ts b/src/index.ts index cc1ac96..a9584eb 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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(); diff --git a/src/types.ts b/src/types.ts index b5b2526..69bdc2e 100644 --- a/src/types.ts +++ b/src/types.ts @@ -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; }