From: Jérôme Benoit Date: Sat, 29 Apr 2023 19:55:33 +0000 (+0200) Subject: feat: add helper to build connectors/evses from configuration X-Git-Tag: v1.2.12~39 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=a4f7c75ff42b0545400ca0e9ddbbf9b72f32774f;p=e-mobility-charging-stations-simulator.git feat: add helper to build connectors/evses from configuration Signed-off-by: Jérôme Benoit --- diff --git a/src/charging-station/ChargingStation.ts b/src/charging-station/ChargingStation.ts index 07070762..2456d0d6 100644 --- a/src/charging-station/ChargingStation.ts +++ b/src/charging-station/ChargingStation.ts @@ -108,7 +108,7 @@ export class ChargingStation { public automaticTransactionGenerator!: AutomaticTransactionGenerator | undefined; public ocppConfiguration!: ChargingStationOcppConfiguration | undefined; public wsConnection!: WebSocket | null; - public readonly connectors: Map; + public connectors: Map; public readonly evses: Map; public readonly requests: Map; public performanceStatistics!: PerformanceStatistics | undefined; @@ -1062,7 +1062,16 @@ export class ChargingStation { path.dirname(this.templateFile.replace('station-templates', 'configurations')), `${ChargingStationUtils.getHashId(this.index, stationTemplate)}.json` ); - this.initializeConnectorsOrEvsesFromTemplate(stationTemplate); + const chargingStationConfiguration = this.getConfigurationFromFile(); + const featureFlag = false; + if ( + featureFlag && + (chargingStationConfiguration?.connectorsStatus || chargingStationConfiguration?.evsesStatus) + ) { + this.initializeConnectorsOrEvsesFromFile(chargingStationConfiguration); + } else { + this.initializeConnectorsOrEvsesFromTemplate(stationTemplate); + } this.stationInfo = this.getStationInfo(); if ( this.stationInfo.firmwareStatus === FirmwareStatus.Installing && @@ -1298,6 +1307,39 @@ export class ChargingStation { this.saveOcppConfiguration(); } + private initializeConnectorsOrEvsesFromFile(configuration: ChargingStationConfiguration): void { + if (configuration?.connectorsStatus && !configuration?.evsesStatus) { + this.connectors = new Map( + configuration?.connectorsStatus.map((connectorStatus, connectorId) => [ + connectorId, + connectorStatus, + ]) + ); + } else if (configuration?.evsesStatus && !configuration?.connectorsStatus) { + for (const [evseId, evseStatusConfiguration] of configuration.evsesStatus.entries()) { + const evseStatus = Utils.cloneObject(evseStatusConfiguration); + delete evseStatus.connectorsStatus; + this.evses.set(evseId, { + ...evseStatus, + connectors: new Map( + evseStatusConfiguration.connectorsStatus.map((connectorStatus, connectorId) => [ + connectorId, + connectorStatus, + ]) + ), + }); + } + } else if (configuration?.evsesStatus && configuration?.connectorsStatus) { + const errorMsg = `Connectors and evses defined at the same time in configuration file ${this.configurationFile}`; + logger.error(`${this.logPrefix()} ${errorMsg}`); + throw new BaseError(errorMsg); + } else { + const errorMsg = `No connectors or evses defined in configuration file ${this.configurationFile}`; + logger.error(`${this.logPrefix()} ${errorMsg}`); + throw new BaseError(errorMsg); + } + } + private initializeConnectorsOrEvsesFromTemplate(stationTemplate: ChargingStationTemplate) { if (stationTemplate?.Connectors && !stationTemplate?.Evses) { this.initializeConnectorsFromTemplate(stationTemplate); @@ -1572,6 +1614,8 @@ export class ChargingStation { } if (!Utils.isNullOrUndefined(configuration)) { delete configuration.stationInfo; + delete configuration.connectorsStatus; + delete configuration.evsesStatus; delete configuration.configurationHash; } return configuration;