send full rotator state to new clients over a typed WebSocket protocol

- 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>
This commit is contained in:
2026-11-14 03:00:34 +00:00
co-authored by Claude Opus 5.5
parent db328336f1
commit 1fa6f2251c
6 changed files with 334 additions and 330 deletions
+70 -57
View File
@@ -1,97 +1,110 @@
import { readFileSync } from 'node:fs';
import RotClient from './RotClient.js';
import type { FullConfig, StationConfig, DynamicData, InitialState, RadioInfoMessage, TurnMessage, TurnMessageHandler } from './types.js';
import type {
Config,
DynamicData,
InitData,
RadioInfoMessage,
RotatorState,
Setpoint,
StationConfig,
StationRadio,
TurnMessage,
} from './types.js';
export interface TurnEvent {
label: string;
az: number;
sourceIp: string;
export interface RadioInfoEvent extends StationRadio {
station: string;
}
export default class RotRouter implements TurnMessageHandler {
private readonly clients: Record<string, RotClient> = {};
private readonly stations: Record<string, StationConfig> = {};
private readonly stationsByIp: Record<string, string> = {};
private readonly routerDataHandlers: Array<(rotator: RotClient, data: DynamicData) => void> = [];
private readonly turnEventHandlers: Array<(event: TurnEvent) => void> = [];
private readonly radioInfoHandlers: Array<(station: string, band: number, freq: number) => void> = [];
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() {
this.loadConfig();
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);
}
}
private loadConfig(): void {
const raw = readFileSync('config.json', 'utf-8');
const parsed = JSON.parse(raw) as FullConfig;
for (const [label, clientConfig] of Object.entries(parsed.rotators)) {
const client = new RotClient(label, clientConfig);
client.onDynamicDataUpdate(data => this.routerDataHandlers.forEach(h => h(client, data)));
this.clients[label] = client;
}
for (const [name, stationConfig] of Object.entries(parsed.stations)) {
this.stations[name] = stationConfig;
this.stationsByIp[stationConfig.ip] = name;
}
start(): void {
this.clients.forEach(c => c.start());
}
resolveStation(ip: string): string | null {
return this.stationsByIp[ip] ?? 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.radioInfoHandlers.forEach(h => h(station, band, freq));
this.stationRadio[station] = { band, freq };
this.radioInfoHandlers.forEach(h => h({ station, band, freq }));
}
onRadioInfo(handler: (station: string, band: number, freq: number) => void): void {
this.radioInfoHandlers.push(handler);
}
private findClientForBand(band: number): RotClient | null {
return Object.values(this.clients).find(c => c.hasBand(band)) ?? null;
}
handleTurnMessage(msg: TurnMessage): void {
const client = this.findClientForBand(msg.band);
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 ${msg.band} MHz band!`);
console.error(`No rotator configured for the ${band} MHz band!`);
return;
}
client.turn(msg.az);
this.fireTurnEvent(client.label, msg.az, msg.sourceIp);
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[label];
const client = this.clients.get(label);
if (!client) return false;
client.turn(az);
this.fireTurnEvent(label, az, sourceIp);
this.turnClient(client, az, sourceIp);
return true;
}
private fireTurnEvent(label: string, az: number, sourceIp: string): void {
const event: TurnEvent = { label, az, sourceIp };
this.turnEventHandlers.forEach(h => h(event));
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));
}
onTurnEvent(handler: (event: TurnEvent) => void): void {
this.turnEventHandlers.push(handler);
onDynamicData(handler: (label: string, data: DynamicData) => void): void {
this.dynamicDataHandlers.push(handler);
}
onRotatorData(handler: (rotator: RotClient, data: DynamicData) => void): void {
this.routerDataHandlers.push(handler);
onInitData(handler: (label: string, data: InitData) => void): void {
this.initDataHandlers.push(handler);
}
async readInitialState(): Promise<InitialState> {
const state: InitialState = {};
for (const client of Object.values(this.clients)) {
state[client.label] = { initData: await client.readInitData(), bands: client.getBands() };
}
return state;
onSetpoint(handler: (label: string, setpoint: Setpoint) => void): void {
this.setpointHandlers.push(handler);
}
onRadioInfo(handler: (event: RadioInfoEvent) => void): void {
this.radioInfoHandlers.push(handler);
}
}