From db328336f13d4004b76ed4ccf253919cabd9520d Mon Sep 17 00:00:00 2001 From: ericek111 Date: Sat, 14 Nov 2026 03:00:15 +0000 Subject: [PATCH] harden rotator polling - Time out requests after 2 s and never start a poll before the previous one finished; an offline rotator used to pile up hanging requests. - Fetch endpoints in parallel. - Treat non-200 responses and unparseable numbers as null instead of NaN. - Log online/offline transitions instead of every reading each second. - Log failed turn commands instead of swallowing them. - Drop unused getters. Co-Authored-By: Claude Opus 5.5 --- src/RotClient.ts | 137 +++++++++++++++++++---------------------------- 1 file changed, 55 insertions(+), 82 deletions(-) diff --git a/src/RotClient.ts b/src/RotClient.ts index c399f9b..8a55c34 100644 --- a/src/RotClient.ts +++ b/src/RotClient.ts @@ -1,16 +1,20 @@ import type { ClientConfig, DynamicData, InitData } from './types.js'; -const enum NumType { - String, - Int, - Float, -} +const ROTATOR_PORT = 88; +const POLL_INTERVAL_MS = 1000; +const REQUEST_TIMEOUT_MS = 2000; -interface EndpointDef { - key: string; - url: string; - numType: NumType; -} +type NumType = 'string' | 'int' | 'float'; + +const INIT_ENDPOINTS: ReadonlyArray<[keyof InitData, string, NumType]> = [ + ['azShift', 'readStart', 'int' ], + ['azRange', 'readMax', 'int' ], + ['antRadiationAngle', 'readAnt', 'int' ], + ['antName', 'readAntName', 'string'], + ['mapUrl', 'readMapUrl', 'string'], + ['mac', 'readMAC', 'string'], + ['elevation', 'readElevation', 'int' ], +]; /** * Client for the simple_rotator_interface_v protocol. @@ -29,62 +33,43 @@ export default class RotClient { this.ip = ip; this.bands = bands; - setInterval(() => void this.fetchDynamicData(), 1000); + const poll = async () => { + await this.fetchDynamicData(); + setTimeout(() => void poll(), POLL_INTERVAL_MS); + }; + void poll(); } async readInitData(): Promise { if (this.cachedData) return this.cachedData; - const endpoints: EndpointDef[] = [ - { key: 'azShift', url: 'readStart', numType: NumType.Int }, - { key: 'azRange', url: 'readMax', numType: NumType.Int }, - { key: 'antRadiationAngle', url: 'readAnt', numType: NumType.Int }, - { key: 'antName', url: 'readAntName', numType: NumType.String }, - { key: 'mapUrl', url: 'readMapUrl', numType: NumType.String }, - { key: 'mac', url: 'readMAC', numType: NumType.String }, - { key: 'elevation', url: 'readElevation', numType: NumType.Int }, - ]; - - this.cachedData = await this.readEndpoints(endpoints) as unknown as InitData; - console.log(`${this.label} Set the initial offset to ${this.cachedData.azShift}.`); + const values = await Promise.all(INIT_ENDPOINTS.map(([, url, type]) => this.readKey(url, type))); + this.cachedData = Object.fromEntries(INIT_ENDPOINTS.map(([key], i) => [key, values[i]])) as unknown as InitData; + console.log(`${this.label}: Loaded init data (azimuth offset ${this.cachedData.azShift}°).`); return this.cachedData; } - private async readEndpoints(endpoints: EndpointDef[]): Promise> { - const data: Record = {}; - for (const { key, url, numType } of endpoints) { - data[key] = await this.readKey(url, numType); - } - return data; - } - private async fetchDynamicData(): Promise { - const endpoints: EndpointDef[] = [ - { key: 'adc', url: 'readADC', numType: NumType.Float }, - { key: 'azimuth', url: 'readAZ', numType: NumType.Int }, - { key: 'status', url: 'readStat', numType: NumType.Int }, - ]; - - const raw = await this.readEndpoints(endpoints); + const [adc, azimuth, status] = await Promise.all([ + this.readKey('readADC', 'float'), + this.readKey('readAZ', 'int'), + this.readKey('readStat', 'int'), + ]); const data: DynamicData = { - adc: raw.adc as number | null, - azimuth: raw.azimuth as number | null, - status: raw.status as number | null, - online: raw.azimuth !== null, + adc: adc as number | null, + azimuth: azimuth as number | null, + status: status as number | null, + online: azimuth !== null, }; - if (this.dynamicData !== null) { - const oldData = this.dynamicData; - const hasChanged = (Object.keys(oldData) as Array).some( - key => oldData[key] !== data[key] - ); - if (hasChanged) { - this.dynamicHandlers.forEach(handler => handler(data)); - } - } - + const old = this.dynamicData; + const changed = old !== null && (Object.keys(data) as Array).some(k => old[k] !== data[k]); this.dynamicData = data; - console.log(`${this.label}: `, this.dynamicData); + + if (old?.online !== data.online) { + console.log(`${this.label}: ${data.online ? 'online' : 'offline'}`); + } + if (changed) this.dynamicHandlers.forEach(h => h(data)); } onDynamicDataUpdate(handler: (data: DynamicData) => void): void { @@ -100,42 +85,30 @@ export default class RotClient { } turn(az: number): void { - fetch(`http://${this.ip}:88/`, { + console.log(`Turning ${this.label} to ${az}°...`); + fetch(`${this.baseUrl()}/`, { body: `ROT=${az}`, method: 'POST', - headers: { - 'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8' - }, - }).catch(() => {}); - console.log(`Turning ${this.label} to ${az}°...`); + headers: { 'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8' }, + signal: AbortSignal.timeout(REQUEST_TIMEOUT_MS), + }).catch((ex: Error) => console.error(`${this.label}: Failed to send turn command: ${ex.cause ?? ex.message}`)); } - private async readKey(endpoint: string, numType: NumType): Promise { + private baseUrl(): string { + return `http://${this.ip}:${ROTATOR_PORT}`; + } + + private async readKey(endpoint: string, type: NumType): Promise { try { - const resp = await fetch(`http://${this.ip}:88/${endpoint}`); + const resp = await fetch(`${this.baseUrl()}/${endpoint}`, { signal: AbortSignal.timeout(REQUEST_TIMEOUT_MS) }); + if (!resp.ok) return null; const text = await resp.text(); - if (numType === NumType.String) return text; - if (numType === NumType.Int) return parseInt(text, 10); - return parseFloat(text); - } catch (ex) { - console.error(`${this.label}: Failed to read ${endpoint}:`, ex); + if (type === 'string') return text; + const value = type === 'int' ? parseInt(text, 10) : parseFloat(text); + return Number.isFinite(value) ? value : null; + } catch { + // Unreachable rotators are reported via the `online` flag rather than logged every second. return null; } } - - getDynamicData(): DynamicData | null { - return this.dynamicData; - } - - getAzimuth(): number | null { - return this.dynamicData?.azimuth ?? null; - } - - getAdc(): number | null { - return this.dynamicData?.adc ?? null; - } - - getStatus(): number | null { - return this.dynamicData?.status ?? null; - } }