From: Jérôme Benoit Date: Sun, 26 Mar 2023 15:08:14 +0000 (+0200) Subject: refactor(simulator): move template keys deprecation code into helpers X-Git-Tag: v1.2.0-0~3 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=ae5020a33e77e4fb452a5d2f54c59bf9837e5190;p=e-mobility-charging-stations-simulator.git refactor(simulator): move template keys deprecation code into helpers file Signed-off-by: Jérôme Benoit --- diff --git a/src/charging-station/ChargingStation.ts b/src/charging-station/ChargingStation.ts index 43fb6889..61a4d962 100644 --- a/src/charging-station/ChargingStation.ts +++ b/src/charging-station/ChargingStation.ts @@ -854,27 +854,6 @@ export class ChargingStation { return template; } - private warnTemplateKeysDeprecation(stationTemplate: ChargingStationTemplate) { - const templateKeys: { key: string; deprecatedKey: string }[] = [ - { key: 'supervisionUrls', deprecatedKey: 'supervisionUrl' }, - { key: 'idTagsFile', deprecatedKey: 'authorizationFile' }, - ]; - for (const templateKey of templateKeys) { - ChargingStationUtils.warnDeprecatedTemplateKey( - stationTemplate, - templateKey.deprecatedKey, - this.templateFile, - this.logPrefix(), - `Use '${templateKey.key}' instead` - ); - ChargingStationUtils.convertDeprecatedTemplateKey( - stationTemplate, - templateKey.deprecatedKey, - templateKey.key - ); - } - } - private getStationInfoFromTemplate(): ChargingStationInfo { const stationTemplate: ChargingStationTemplate | undefined = this.getTemplateFromFile(); if (Utils.isNullOrUndefined(stationTemplate)) { @@ -887,7 +866,11 @@ export class ChargingStation { logger.error(`${this.logPrefix()} ${errorMsg}`); throw new BaseError(errorMsg); } - this.warnTemplateKeysDeprecation(stationTemplate); + ChargingStationUtils.warnTemplateKeysDeprecation( + this.templateFile, + stationTemplate, + this.logPrefix() + ); const stationInfo: ChargingStationInfo = ChargingStationUtils.stationTemplateToStationInfo(stationTemplate); stationInfo.hashId = ChargingStationUtils.getHashId(this.index, stationTemplate); diff --git a/src/charging-station/ChargingStationUtils.ts b/src/charging-station/ChargingStationUtils.ts index bca8c088..0138d785 100644 --- a/src/charging-station/ChargingStationUtils.ts +++ b/src/charging-station/ChargingStationUtils.ts @@ -212,30 +212,28 @@ export class ChargingStationUtils { return Configuration.getWorker().processType === WorkerProcessType.dynamicPool; } - public static warnDeprecatedTemplateKey( - template: ChargingStationTemplate, - key: string, + public static warnTemplateKeysDeprecation( templateFile: string, - logPrefix: string, - logMsgToAppend = '' - ): void { - if (!Utils.isUndefined(template[key])) { - const logMsg = `Deprecated template key '${key}' usage in file '${templateFile}'${ - Utils.isNotEmptyString(logMsgToAppend) ? `. ${logMsgToAppend}` : '' - }`; - logger.warn(`${logPrefix} ${logMsg}`); - console.warn(chalk.yellow(`${logMsg}`)); - } - } - - public static convertDeprecatedTemplateKey( - template: ChargingStationTemplate, - deprecatedKey: string, - key: string - ): void { - if (!Utils.isUndefined(template[deprecatedKey])) { - template[key] = template[deprecatedKey] as unknown; - delete template[deprecatedKey]; + stationTemplate: ChargingStationTemplate, + logPrefix: string + ) { + const templateKeys: { key: string; deprecatedKey: string }[] = [ + { key: 'supervisionUrls', deprecatedKey: 'supervisionUrl' }, + { key: 'idTagsFile', deprecatedKey: 'authorizationFile' }, + ]; + for (const templateKey of templateKeys) { + ChargingStationUtils.warnDeprecatedTemplateKey( + stationTemplate, + templateKey.deprecatedKey, + templateFile, + logPrefix, + `Use '${templateKey.key}' instead` + ); + ChargingStationUtils.convertDeprecatedTemplateKey( + stationTemplate, + templateKey.deprecatedKey, + templateKey.key + ); } } @@ -425,6 +423,33 @@ export class ChargingStationUtils { ); } + private static warnDeprecatedTemplateKey( + template: ChargingStationTemplate, + key: string, + templateFile: string, + logPrefix: string, + logMsgToAppend = '' + ): void { + if (!Utils.isUndefined(template[key])) { + const logMsg = `Deprecated template key '${key}' usage in file '${templateFile}'${ + Utils.isNotEmptyString(logMsgToAppend) ? `. ${logMsgToAppend}` : '' + }`; + logger.warn(`${logPrefix} ${logMsg}`); + console.warn(chalk.yellow(`${logMsg}`)); + } + } + + private static convertDeprecatedTemplateKey( + template: ChargingStationTemplate, + deprecatedKey: string, + key: string + ): void { + if (!Utils.isUndefined(template[deprecatedKey])) { + template[key] = template[deprecatedKey] as unknown; + delete template[deprecatedKey]; + } + } + /** * Charging profiles should already be sorted by connectorId and stack level (highest stack level has priority) *