import { readFileSync } from 'node:fs'; import RotClient from './RotClient.js'; import type { Config, DynamicData, InitData, RadioInfoMessage, RotatorState, Setpoint, StationConfig, StationRadio, TurnMessage, } from './types.js'; export interface RadioInfoEvent extends StationRadio { station: string; } export default class RotRouter { private readonly clients = new Map(); private readonly stations: Record; private readonly stationsByIp = new Map(); private readonly stationRadio: Record = {}; private readonly dynamicDataHandlers: Array<(label: string, data: DynamicData) => void> = []; private readonly initDataHandlers: Array<(label: string, data: InitData) => void> = []; private readonly setpointHandlers: Array<(label: string, setpoint: Setpoint) => void> = []; private readonly radioInfoHandlers: Array<(event: RadioInfoEvent) => void> = []; constructor(configPath: string) { const config = JSON.parse(readFileSync(configPath, 'utf-8')) as Config; for (const [label, rotatorConfig] of Object.entries(config.rotators)) { const client = new RotClient(label, rotatorConfig); client.onDynamicDataUpdate(data => this.dynamicDataHandlers.forEach(h => h(label, data))); client.onInitDataLoaded(data => this.initDataHandlers.forEach(h => h(label, data))); this.clients.set(label, client); } this.stations = config.stations; for (const [name, { ip }] of Object.entries(config.stations)) { this.stationsByIp.set(ip, name); } } start(): void { this.clients.forEach(c => c.start()); } resolveStation(ip: string): string | null { return this.stationsByIp.get(ip) ?? null; } getStations(): Record { return this.stations; } getStationRadio(): Record { return this.stationRadio; } getRotatorStates(): Record { return Object.fromEntries([...this.clients].map(([label, c]) => [label, c.getState()])); } handleRadioInfo({ sourceIp, band, freq }: RadioInfoMessage): void { const station = this.resolveStation(sourceIp); if (!station) return; this.stationRadio[station] = { band, freq }; this.radioInfoHandlers.forEach(h => h({ station, band, freq })); } handleTurnMessage({ band, az, sourceIp }: TurnMessage): void { const client = [...this.clients.values()].find(c => c.hasBand(band)); if (!client) { console.error(`No rotator configured for the ${band} MHz band!`); return; } this.turnClient(client, az, sourceIp); } /** Turns the rotator with the given label. Returns false if no such rotator exists. */ turn(label: string, az: number, sourceIp: string): boolean { const client = this.clients.get(label); if (!client) return false; this.turnClient(client, az, sourceIp); return true; } private turnClient(client: RotClient, targetAz: number, sourceIp: string): void { const setpoint: Setpoint = { targetAz, sourceIp, station: this.resolveStation(sourceIp) }; client.turn(setpoint); this.setpointHandlers.forEach(h => h(client.label, setpoint)); } onDynamicData(handler: (label: string, data: DynamicData) => void): void { this.dynamicDataHandlers.push(handler); } onInitData(handler: (label: string, data: InitData) => void): void { this.initDataHandlers.push(handler); } onSetpoint(handler: (label: string, setpoint: Setpoint) => void): void { this.setpointHandlers.push(handler); } onRadioInfo(handler: (event: RadioInfoEvent) => void): void { this.radioInfoHandlers.push(handler); } }