+ this.saveOcppConfiguration();
+ }
+
+ private initializeConnectorsOrEvses(stationInfo: ChargingStationInfo) {
+ if (stationInfo?.Connectors && !stationInfo?.Evses) {
+ this.initializeConnectors(stationInfo);
+ } else if (stationInfo?.Evses && !stationInfo?.Connectors) {
+ this.initializeEvses(stationInfo);
+ } else if (stationInfo?.Evses && stationInfo?.Connectors) {
+ const errorMsg = `Connectors and evses defined at the same time in template file ${this.templateFile}`;
+ logger.error(`${this.logPrefix()} ${errorMsg}`);
+ throw new BaseError(errorMsg);
+ } else {
+ const errorMsg = `No connectors or evses defined in template file ${this.templateFile}`;
+ logger.error(`${this.logPrefix()} ${errorMsg}`);
+ throw new BaseError(errorMsg);
+ }
+ }
+
+ private initializeConnectors(stationInfo: ChargingStationInfo): void {
+ if (!stationInfo?.Connectors && this.connectors.size === 0) {
+ const logMsg = `No already defined connectors and charging station information from template ${this.templateFile} with no connectors configuration defined`;
+ logger.error(`${this.logPrefix()} ${logMsg}`);
+ throw new BaseError(logMsg);
+ }
+ if (!stationInfo?.Connectors[0]) {
+ logger.warn(
+ `${this.logPrefix()} Charging station information from template ${
+ this.templateFile
+ } with no connector id 0 configuration`
+ );
+ }
+ if (stationInfo?.Connectors) {
+ const configuredMaxConnectors =
+ ChargingStationUtils.getConfiguredNumberOfConnectors(stationInfo);
+ ChargingStationUtils.checkConfiguredMaxConnectors(
+ configuredMaxConnectors,
+ this.templateFile,
+ this.logPrefix()
+ );
+ const connectorsConfigHash = crypto
+ .createHash(Constants.DEFAULT_HASH_ALGORITHM)
+ .update(`${JSON.stringify(stationInfo?.Connectors)}${configuredMaxConnectors.toString()}`)
+ .digest('hex');
+ const connectorsConfigChanged =
+ this.connectors?.size !== 0 && this.connectorsConfigurationHash !== connectorsConfigHash;
+ if (this.connectors?.size === 0 || connectorsConfigChanged) {
+ connectorsConfigChanged && this.connectors.clear();
+ this.connectorsConfigurationHash = connectorsConfigHash;
+ const connectorZeroStatus = stationInfo?.Connectors[0];
+ // Add connector id 0
+ if (connectorZeroStatus && this.getUseConnectorId0(stationInfo) === true) {
+ ChargingStationUtils.checkStationInfoConnectorStatus(
+ 0,
+ connectorZeroStatus,
+ this.logPrefix(),
+ this.templateFile
+ );
+ this.connectors.set(0, Utils.cloneObject<ConnectorStatus>(connectorZeroStatus));
+ this.getConnectorStatus(0).availability = AvailabilityType.Operative;
+ if (Utils.isUndefined(this.getConnectorStatus(0)?.chargingProfiles)) {
+ this.getConnectorStatus(0).chargingProfiles = [];
+ }
+ }
+ // Add remaining connectors
+ const templateMaxConnectors = ChargingStationUtils.getMaxNumberOfConnectors(
+ stationInfo.Connectors
+ );
+ ChargingStationUtils.checkTemplateMaxConnectors(
+ templateMaxConnectors,
+ this.templateFile,
+ this.logPrefix()
+ );
+ if (
+ configuredMaxConnectors >
+ (stationInfo?.Connectors[0] ? templateMaxConnectors - 1 : templateMaxConnectors) &&
+ !stationInfo?.randomConnectors
+ ) {
+ logger.warn(
+ `${this.logPrefix()} Number of connectors exceeds the number of connector configurations in template ${
+ this.templateFile
+ }, forcing random connector configurations affectation`
+ );
+ stationInfo.randomConnectors = true;
+ }
+ const templateMaxAvailableConnectors = stationInfo?.Connectors[0]
+ ? templateMaxConnectors - 1
+ : templateMaxConnectors;
+ if (templateMaxAvailableConnectors > 0) {
+ for (let connectorId = 1; connectorId <= configuredMaxConnectors; connectorId++) {
+ const templateConnectorId = stationInfo?.randomConnectors
+ ? Utils.getRandomInteger(templateMaxAvailableConnectors, 1)
+ : connectorId;
+ const connectorStatus = stationInfo?.Connectors[templateConnectorId];
+ ChargingStationUtils.checkStationInfoConnectorStatus(
+ templateConnectorId,
+ connectorStatus,
+ this.logPrefix(),
+ this.templateFile
+ );
+ this.connectors.set(connectorId, Utils.cloneObject<ConnectorStatus>(connectorStatus));
+ this.getConnectorStatus(connectorId).availability = AvailabilityType.Operative;
+ if (Utils.isUndefined(this.getConnectorStatus(connectorId)?.chargingProfiles)) {
+ this.getConnectorStatus(connectorId).chargingProfiles = [];
+ }
+ ChargingStationUtils.initializeConnectorsMapStatus(this.connectors, this.logPrefix());
+ }
+ } else {
+ logger.warn(
+ `${this.logPrefix()} Charging station information from template ${
+ this.templateFile
+ } with no connectors configuration defined, cannot create connectors`
+ );
+ }
+ }
+ } else {
+ logger.warn(
+ `${this.logPrefix()} Charging station information from template ${
+ this.templateFile
+ } with no connectors configuration defined, using already defined connectors`
+ );
+ }
+ }
+
+ private initializeEvses(stationInfo: ChargingStationInfo): void {
+ if (!stationInfo?.Evses && this.evses.size === 0) {
+ const logMsg = `No already defined evses and charging station information from template ${this.templateFile} with no evses configuration defined`;
+ logger.error(`${this.logPrefix()} ${logMsg}`);
+ throw new BaseError(logMsg);
+ }
+ if (!stationInfo?.Evses[0]) {
+ logger.warn(
+ `${this.logPrefix()} Charging station information from template ${
+ this.templateFile
+ } with no evse id 0 configuration`
+ );
+ }
+ if (stationInfo?.Evses) {
+ const evsesConfigHash = crypto
+ .createHash(Constants.DEFAULT_HASH_ALGORITHM)
+ .update(`${JSON.stringify(stationInfo?.Evses)}`)
+ .digest('hex');
+ const evsesConfigChanged =
+ this.evses?.size !== 0 && this.evsesConfigurationHash !== evsesConfigHash;
+ if (this.evses?.size === 0 || evsesConfigChanged) {
+ evsesConfigChanged && this.evses.clear();
+ this.evsesConfigurationHash = evsesConfigHash;
+ const templateMaxEvses = ChargingStationUtils.getMaxNumberOfEvses(stationInfo?.Evses);
+ if (templateMaxEvses > 0) {
+ for (const evse in stationInfo.Evses) {
+ this.evses.set(Utils.convertToInt(evse), {
+ connectors: ChargingStationUtils.buildConnectorsMap(
+ stationInfo?.Evses[evse]?.Connectors,
+ this.logPrefix(),
+ this.templateFile
+ ),
+ availability: AvailabilityType.Operative,
+ });
+ ChargingStationUtils.initializeConnectorsMapStatus(
+ this.evses.get(Utils.convertToInt(evse))?.connectors,
+ this.logPrefix()
+ );
+ }
+ } else {
+ logger.warn(
+ `${this.logPrefix()} Charging station information from template ${
+ this.templateFile
+ } with no evses configuration defined, cannot create evses`
+ );
+ }
+ } else {
+ logger.warn(
+ `${this.logPrefix()} Charging station information from template ${
+ this.templateFile
+ } with no evses configuration defined, using already defined evses`
+ );
+ }
+ }