From: Jérôme Benoit Date: Sat, 29 Apr 2023 08:59:38 +0000 (+0200) Subject: refactor: factor out template file check X-Git-Tag: v1.2.12~42 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=8a133cc8eea914f56bb8e00aecd9d9954414cce6;p=e-mobility-charging-stations-simulator.git refactor: factor out template file check Signed-off-by: Jérôme Benoit --- diff --git a/src/charging-station/ChargingStation.ts b/src/charging-station/ChargingStation.ts index 419e89e3..f1109aa1 100644 --- a/src/charging-station/ChargingStation.ts +++ b/src/charging-station/ChargingStation.ts @@ -313,8 +313,8 @@ export class ChargingStation { public getVoltageOut(stationInfo?: ChargingStationInfo): number | undefined { const defaultVoltageOut = ChargingStationUtils.getDefaultVoltageOut( this.getCurrentOutType(stationInfo), - this.templateFile, - this.logPrefix() + this.logPrefix(), + this.templateFile ); const localStationInfo: ChargingStationInfo = stationInfo ?? this.stationInfo; return !Utils.isUndefined(localStationInfo.voltageOut) @@ -942,21 +942,19 @@ export class ChargingStation { private getStationInfoFromTemplate(): ChargingStationInfo { const stationTemplate: ChargingStationTemplate | undefined = this.getTemplateFromFile(); - if (Utils.isNullOrUndefined(stationTemplate)) { - const errorMsg = `Failed to read charging station template file ${this.templateFile}`; - logger.error(`${this.logPrefix()} ${errorMsg}`); - throw new BaseError(errorMsg); - } - if (Utils.isEmptyObject(stationTemplate)) { - const errorMsg = `Empty charging station information from template file ${this.templateFile}`; - logger.error(`${this.logPrefix()} ${errorMsg}`); - throw new BaseError(errorMsg); - } + ChargingStationUtils.checkTemplateFile(stationTemplate, this.logPrefix(), this.templateFile); ChargingStationUtils.warnTemplateKeysDeprecation( - this.templateFile, stationTemplate, - this.logPrefix() + this.logPrefix(), + this.templateFile ); + if (stationTemplate?.Connectors) { + ChargingStationUtils.checkConnectorsConfiguration( + stationTemplate, + this.logPrefix(), + this.templateFile + ); + } const stationInfo: ChargingStationInfo = ChargingStationUtils.stationTemplateToStationInfo(stationTemplate); stationInfo.hashId = ChargingStationUtils.getHashId(this.index, stationTemplate); @@ -1005,15 +1003,6 @@ export class ChargingStation { ? stationTemplate.resetTime * 1000 : Constants.CHARGING_STATION_DEFAULT_RESET_TIME; stationInfo.maximumAmperage = this.getMaximumAmperage(stationInfo); - if (stationInfo?.Connectors) { - ChargingStationUtils.checkConnectorsConfiguration( - stationInfo, - this.templateFile, - this.logPrefix() - ); - } - delete stationInfo?.Connectors; - delete stationInfo?.Evses; return stationInfo; } @@ -1068,11 +1057,7 @@ export class ChargingStation { private initialize(): void { const stationTemplate = this.getTemplateFromFile(); - if (Utils.isNullOrUndefined(stationTemplate)) { - const errorMsg = `Failed to read charging station template file ${this.templateFile}`; - logger.error(`${this.logPrefix()} ${errorMsg}`); - throw new BaseError(errorMsg); - } + ChargingStationUtils.checkTemplateFile(stationTemplate, this.logPrefix(), this.templateFile); this.configurationFile = path.join( path.dirname(this.templateFile.replace('station-templates', 'configurations')), `${ChargingStationUtils.getHashId(this.index, stationTemplate)}.json` @@ -1346,8 +1331,8 @@ export class ChargingStation { const { configuredMaxConnectors, templateMaxConnectors, templateMaxAvailableConnectors } = ChargingStationUtils.checkConnectorsConfiguration( stationTemplate, - this.templateFile, - this.logPrefix() + this.logPrefix(), + this.templateFile ); const connectorsConfigHash = crypto .createHash(Constants.DEFAULT_HASH_ALGORITHM) diff --git a/src/charging-station/ChargingStationUtils.ts b/src/charging-station/ChargingStationUtils.ts index 517a3c9a..dbfbe41f 100644 --- a/src/charging-station/ChargingStationUtils.ts +++ b/src/charging-station/ChargingStationUtils.ts @@ -153,10 +153,27 @@ export class ChargingStationUtils { return connectorBootStatus; } + public static checkTemplateFile( + stationTemplate: ChargingStationTemplate, + logPrefix: string, + templateFile: string + ) { + if (Utils.isNullOrUndefined(stationTemplate)) { + const errorMsg = `Failed to read charging station template file ${templateFile}`; + logger.error(`${logPrefix} ${errorMsg}`); + throw new BaseError(errorMsg); + } + if (Utils.isEmptyObject(stationTemplate)) { + const errorMsg = `Empty charging station information from template file ${templateFile}`; + logger.error(`${logPrefix} ${errorMsg}`); + throw new BaseError(errorMsg); + } + } + public static checkConnectorsConfiguration( - stationTemplate: ChargingStationTemplate | ChargingStationInfo, - templateFile: string, - logPrefix: string + stationTemplate: ChargingStationTemplate, + logPrefix: string, + templateFile: string ): { configuredMaxConnectors: number; templateMaxConnectors: number; @@ -166,13 +183,13 @@ export class ChargingStationUtils { ChargingStationUtils.getConfiguredNumberOfConnectors(stationTemplate); ChargingStationUtils.checkConfiguredMaxConnectors( configuredMaxConnectors, - templateFile, - logPrefix + logPrefix, + templateFile ); const templateMaxConnectors = ChargingStationUtils.getMaxNumberOfConnectors( stationTemplate.Connectors ); - ChargingStationUtils.checkTemplateMaxConnectors(templateMaxConnectors, templateFile, logPrefix); + ChargingStationUtils.checkTemplateMaxConnectors(templateMaxConnectors, logPrefix, templateFile); const templateMaxAvailableConnectors = stationTemplate?.Connectors[0] ? templateMaxConnectors - 1 : templateMaxConnectors; @@ -330,9 +347,9 @@ export class ChargingStationUtils { } public static warnTemplateKeysDeprecation( - templateFile: string, stationTemplate: ChargingStationTemplate, - logPrefix: string + logPrefix: string, + templateFile: string ) { const templateKeys: { key: string; deprecatedKey: string }[] = [ { key: 'supervisionUrls', deprecatedKey: 'supervisionUrl' }, @@ -342,8 +359,8 @@ export class ChargingStationUtils { ChargingStationUtils.warnDeprecatedTemplateKey( stationTemplate, templateKey.deprecatedKey, - templateFile, logPrefix, + templateFile, `Use '${templateKey.key}' instead` ); ChargingStationUtils.convertDeprecatedTemplateKey( @@ -360,6 +377,8 @@ export class ChargingStationUtils { stationTemplate = Utils.cloneObject(stationTemplate); delete stationTemplate.power; delete stationTemplate.powerUnit; + delete stationTemplate?.Connectors; + delete stationTemplate?.Evses; delete stationTemplate.Configuration; delete stationTemplate.AutomaticTransactionGenerator; delete stationTemplate.chargeBoxSerialNumberPrefix; @@ -493,8 +512,8 @@ export class ChargingStationUtils { public static getDefaultVoltageOut( currentType: CurrentType, - templateFile: string, - logPrefix: string + logPrefix: string, + templateFile: string ): Voltage { const errorMsg = `Unknown ${currentType} currentOutType in template file ${templateFile}, cannot define default voltage out`; let defaultVoltageOut: number; @@ -523,9 +542,7 @@ export class ChargingStationUtils { ); } - private static getConfiguredNumberOfConnectors( - stationTemplate: ChargingStationTemplate | ChargingStationInfo - ): number { + private static getConfiguredNumberOfConnectors(stationTemplate: ChargingStationTemplate): number { let configuredMaxConnectors: number; if (Utils.isNotEmptyArray(stationTemplate.numberOfConnectors) === true) { const numberOfConnectors = stationTemplate.numberOfConnectors as number[]; @@ -553,8 +570,8 @@ export class ChargingStationUtils { private static checkConfiguredMaxConnectors( configuredMaxConnectors: number, - templateFile: string, - logPrefix: string + logPrefix: string, + templateFile: string ): void { if (configuredMaxConnectors <= 0) { logger.warn( @@ -565,8 +582,8 @@ export class ChargingStationUtils { private static checkTemplateMaxConnectors( templateMaxConnectors: number, - templateFile: string, - logPrefix: string + logPrefix: string, + templateFile: string ): void { if (templateMaxConnectors === 0) { logger.warn( @@ -595,8 +612,8 @@ export class ChargingStationUtils { private static warnDeprecatedTemplateKey( template: ChargingStationTemplate, key: string, - templateFile: string, logPrefix: string, + templateFile: string, logMsgToAppend = '' ): void { if (!Utils.isUndefined(template[key])) { diff --git a/src/types/ChargingStationTemplate.ts b/src/types/ChargingStationTemplate.ts index 200307ab..23644c7f 100644 --- a/src/types/ChargingStationTemplate.ts +++ b/src/types/ChargingStationTemplate.ts @@ -114,5 +114,5 @@ export type ChargingStationTemplate = { Configuration?: ChargingStationOcppConfiguration; AutomaticTransactionGenerator?: AutomaticTransactionGeneratorConfiguration; Evses?: Record; - Connectors: Record; + Connectors?: Record; };