initial commit
This commit is contained in:
45
RotRouter.js
Normal file
45
RotRouter.js
Normal file
@@ -0,0 +1,45 @@
|
||||
import RotClient from './RotClient.js';
|
||||
import { XMLParser } from 'fast-xml-parser';
|
||||
import fs from 'node:fs';
|
||||
|
||||
export default class RotRouter {
|
||||
constructor() {
|
||||
this.clients = [];
|
||||
this.loadConfig();
|
||||
|
||||
this.xmlParser = new XMLParser();
|
||||
}
|
||||
|
||||
processN1mmXml(xml) {
|
||||
try {
|
||||
const msg = this.xmlParser.parse(xml);
|
||||
const band = parseInt(msg?.N1MMRotor?.freqband.split(',')[0] || 0, 10);
|
||||
const az = parseInt(msg?.N1MMRotor?.goazi.split(',')[0] || 0, 10);
|
||||
if (!band || !az) {
|
||||
console.error(`Missing band or azimuth`);
|
||||
return;
|
||||
}
|
||||
const client = this.findClientForBand(band);
|
||||
if (!client) {
|
||||
console.error(`No RotClient found for the ${band} band!`);
|
||||
return;
|
||||
}
|
||||
|
||||
client.turn(az);
|
||||
} catch (ex) {
|
||||
console.error(`Failed to parse: ${xml}`, ex);
|
||||
}
|
||||
}
|
||||
|
||||
loadConfig() {
|
||||
const raw = fs.readFileSync("config.json", 'utf-8');
|
||||
const parsed = JSON.parse(raw);
|
||||
for (const [label, clientConfig] of Object.entries(parsed)) {
|
||||
this.clients[label] = new RotClient(label, clientConfig);
|
||||
}
|
||||
}
|
||||
|
||||
findClientForBand(band) {
|
||||
return Object.values(this.clients).find(c => c.hasBand(band)) || null;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user