From ae5020a33e77e4fb452a5d2f54c59bf9837e5190 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Sun, 26 Mar 2023 17:08:14 +0200 Subject: [PATCH] refactor(simulator): move template keys deprecation code into helpers file MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- src/charging-station/ChargingStation.ts | 27 ++------ src/charging-station/ChargingStationUtils.ts | 71 +++++++++++++------- 2 files changed, 53 insertions(+), 45 deletions(-) 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) * -- 2.34.1