53 lines
1.5 KiB
TypeScript
53 lines
1.5 KiB
TypeScript
import { BaseMigration } from './base-migration.ts';
|
|
import { logger } from '../logger.ts';
|
|
import { getDefaultRuntimeUnitForUpsModel } from '../snmp/runtime-units.ts';
|
|
|
|
/**
|
|
* Migration from v4.2 to v4.3
|
|
*
|
|
* Changes:
|
|
* 1. Adds `runtimeUnit` to SNMP configs based on existing `upsModel`
|
|
* 2. Bumps version from '4.2' to '4.3'
|
|
*/
|
|
export class MigrationV4_2ToV4_3 extends BaseMigration {
|
|
readonly fromVersion = '4.2';
|
|
readonly toVersion = '4.3';
|
|
|
|
shouldRun(config: Record<string, unknown>): boolean {
|
|
return config.version === '4.2';
|
|
}
|
|
|
|
migrate(config: Record<string, unknown>): Record<string, unknown> {
|
|
logger.info(`${this.getName()}: Adding runtimeUnit to SNMP configs...`);
|
|
|
|
const devices = (config.upsDevices as Array<Record<string, unknown>>) || [];
|
|
const migratedDevices = devices.map((device) => {
|
|
const snmp = device.snmp as Record<string, unknown> | undefined;
|
|
if (snmp && !snmp.runtimeUnit) {
|
|
const model = snmp.upsModel as
|
|
| 'cyberpower'
|
|
| 'apc'
|
|
| 'eaton'
|
|
| 'tripplite'
|
|
| 'liebert'
|
|
| 'custom'
|
|
| undefined;
|
|
snmp.runtimeUnit = getDefaultRuntimeUnitForUpsModel(model);
|
|
logger.dim(` → ${device.name}: Set runtimeUnit to '${snmp.runtimeUnit}'`);
|
|
}
|
|
return device;
|
|
});
|
|
|
|
const result = {
|
|
...config,
|
|
version: this.toVersion,
|
|
upsDevices: migratedDevices,
|
|
};
|
|
|
|
logger.success(
|
|
`${this.getName()}: Migration complete (${migratedDevices.length} devices updated)`,
|
|
);
|
|
return result;
|
|
}
|
|
}
|