claude: move to typescript
This commit is contained in:
@@ -0,0 +1,57 @@
|
||||
import dgram from 'node:dgram';
|
||||
import { XMLParser } from 'fast-xml-parser';
|
||||
import type { TurnMessageHandler } from './types.js';
|
||||
|
||||
interface N1MMRotorXml {
|
||||
N1MMRotor?: {
|
||||
freqband?: string;
|
||||
goazi?: string;
|
||||
};
|
||||
}
|
||||
|
||||
export default class N1mmServer {
|
||||
private readonly xmlParser = new XMLParser();
|
||||
private readonly turnHandlers: TurnMessageHandler[] = [];
|
||||
|
||||
start(): void {
|
||||
const server = dgram.createSocket('udp4');
|
||||
|
||||
server.on('error', err => {
|
||||
console.error(`server error:\n${err.stack}`);
|
||||
server.close();
|
||||
});
|
||||
|
||||
server.on('listening', () => {
|
||||
const { address, port } = server.address();
|
||||
console.log(`server listening ${address}:${port}`);
|
||||
});
|
||||
|
||||
server.on('message', (msg, rinfo) => {
|
||||
console.log(`server got: ${msg} from ${rinfo.address}:${rinfo.port}`);
|
||||
this.processN1mmXml(msg.toString());
|
||||
});
|
||||
|
||||
server.bind(12040);
|
||||
}
|
||||
|
||||
onTurnMessage(handler: TurnMessageHandler): void {
|
||||
this.turnHandlers.push(handler);
|
||||
}
|
||||
|
||||
private processN1mmXml(xml: 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;
|
||||
}
|
||||
|
||||
this.turnHandlers.forEach(h => h.handleTurnMessage({ band, az }));
|
||||
} catch (ex) {
|
||||
console.error(`Failed to parse: ${xml}`, ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user