import type { IUpsStatus as IProtocolUpsStatus } from './snmp/types.ts'; export interface IShutdownMonitoringRow extends Record { name: string; battery: string; runtime: string; status: string; } export interface IShutdownRowFormatters { battery: (batteryCapacity: number) => string; runtime: (batteryRuntime: number) => string; ok: (text: string) => string; critical: (text: string) => string; error: (text: string) => string; } export interface IShutdownEmergencyCandidate { ups: TUps; status: IProtocolUpsStatus; } export function isEmergencyRuntime( batteryRuntime: number, emergencyRuntimeMinutes: number, ): boolean { return batteryRuntime < emergencyRuntimeMinutes; } export function buildShutdownStatusRow( upsName: string, status: IProtocolUpsStatus, emergencyRuntimeMinutes: number, formatters: IShutdownRowFormatters, ): { row: IShutdownMonitoringRow; isCritical: boolean } { const isCritical = isEmergencyRuntime(status.batteryRuntime, emergencyRuntimeMinutes); return { row: { name: upsName, battery: formatters.battery(status.batteryCapacity), runtime: formatters.runtime(status.batteryRuntime), status: isCritical ? formatters.critical('CRITICAL!') : formatters.ok('OK'), }, isCritical, }; } export function buildShutdownErrorRow( upsName: string, errorFormatter: (text: string) => string, ): IShutdownMonitoringRow { return { name: upsName, battery: errorFormatter('N/A'), runtime: errorFormatter('N/A'), status: errorFormatter('ERROR'), }; } export function selectEmergencyCandidate( currentCandidate: IShutdownEmergencyCandidate | null, ups: TUps, status: IProtocolUpsStatus, emergencyRuntimeMinutes: number, ): IShutdownEmergencyCandidate | null { if (currentCandidate || !isEmergencyRuntime(status.batteryRuntime, emergencyRuntimeMinutes)) { return currentCandidate; } return { ups, status }; }