- Build the init message per connection from live state. Dynamic data is only broadcast on change, so new browsers showed rotators as OFFLINE until something moved. - Load init data once a rotator is reachable and push it to clients; rotators offline at startup used to stay without name/map/offsets. - Replace label-keyed messages (which could collide with 'radioInfo') with typed ServerMessage variants: init, initData, dynamic, setpoint, radioInfo. - Keep setpoints in RotClient and station radio state in RotRouter instead of mutating a snapshot in index.ts. - Include the resolved station in setpoints; the UI shows the station name in its colour instead of the source IP. - Close stale WebSocket clients; drop the empty handleMessage stub. - Replace TurnMessageHandler with plain callbacks; rename config types. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
111 lines
3.5 KiB
TypeScript
111 lines
3.5 KiB
TypeScript
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<string, RotClient>();
|
|
private readonly stations: Record<string, StationConfig>;
|
|
private readonly stationsByIp = new Map<string, string>();
|
|
private readonly stationRadio: Record<string, StationRadio> = {};
|
|
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<string, StationConfig> {
|
|
return this.stations;
|
|
}
|
|
|
|
getStationRadio(): Record<string, StationRadio> {
|
|
return this.stationRadio;
|
|
}
|
|
|
|
getRotatorStates(): Record<string, RotatorState> {
|
|
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);
|
|
}
|
|
}
|