claude: move to typescript
This commit is contained in:
@@ -0,0 +1,47 @@
|
||||
import { readFileSync } from 'node:fs';
|
||||
import RotClient from './RotClient.js';
|
||||
import type { Config, DynamicData, InitialState, TurnMessage, TurnMessageHandler } from './types.js';
|
||||
|
||||
export default class RotRouter implements TurnMessageHandler {
|
||||
private readonly clients: Record<string, RotClient> = {};
|
||||
private readonly routerDataHandlers: Array<(rotator: RotClient, data: DynamicData) => void> = [];
|
||||
|
||||
constructor() {
|
||||
this.loadConfig();
|
||||
}
|
||||
|
||||
private loadConfig(): void {
|
||||
const raw = readFileSync('config.json', 'utf-8');
|
||||
const parsed = JSON.parse(raw) as Config;
|
||||
for (const [label, clientConfig] of Object.entries(parsed)) {
|
||||
const client = new RotClient(label, clientConfig);
|
||||
client.onDynamicDataUpdate(data => this.routerDataHandlers.forEach(h => h(client, data)));
|
||||
this.clients[label] = client;
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
if (!client) {
|
||||
console.error(`No RotClient found for the ${msg.band} band!`);
|
||||
return;
|
||||
}
|
||||
client.turn(msg.az);
|
||||
}
|
||||
|
||||
onRotatorData(handler: (rotator: RotClient, data: DynamicData) => void): void {
|
||||
this.routerDataHandlers.push(handler);
|
||||
}
|
||||
|
||||
async readInitialState(): Promise<InitialState> {
|
||||
const state: InitialState = {};
|
||||
for (const client of Object.values(this.clients)) {
|
||||
state[client.label] = { initData: await client.readInitData() };
|
||||
}
|
||||
return state;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user