Files
nupst/ts/snmp/runtime-units.ts

51 lines
1.2 KiB
TypeScript

import type { ISnmpConfig, TRuntimeUnit, TUpsModel } from './types.ts';
/**
* Return the runtime unit that matches the bundled OID set for a UPS model.
*/
export function getDefaultRuntimeUnitForUpsModel(
upsModel: TUpsModel | undefined,
batteryRuntime?: number,
): TRuntimeUnit {
switch (upsModel) {
case 'cyberpower':
case 'apc':
return 'ticks';
case 'eaton':
return 'seconds';
case 'custom':
case 'tripplite':
case 'liebert':
case undefined:
if (batteryRuntime !== undefined && batteryRuntime > 10000) {
return 'ticks';
}
return 'minutes';
}
}
/**
* Convert an SNMP runtime value to minutes using explicit config first, then model defaults.
*/
export function convertRuntimeValueToMinutes(
config: Pick<ISnmpConfig, 'runtimeUnit' | 'upsModel'>,
batteryRuntime: number,
): number {
if (batteryRuntime <= 0) {
return batteryRuntime;
}
const runtimeUnit = config.runtimeUnit ||
getDefaultRuntimeUnitForUpsModel(config.upsModel, batteryRuntime);
if (runtimeUnit === 'seconds') {
return Math.floor(batteryRuntime / 60);
}
if (runtimeUnit === 'ticks') {
return Math.floor(batteryRuntime / 6000);
}
return batteryRuntime;
}