X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fcharging-station%2FChargingStation.ts;h=54af77fc0351bb3d918536c046ecab95c080594b;hb=0b22144c135af531858c989650e864edb352764d;hp=1e95df4da9b5918a8f30c10386f4109156f0328d;hpb=7d34a2f48aed78f2e398cae994351c6fa1bd29dc;p=e-mobility-charging-stations-simulator.git diff --git a/src/charging-station/ChargingStation.ts b/src/charging-station/ChargingStation.ts index 1e95df4d..54af77fc 100644 --- a/src/charging-station/ChargingStation.ts +++ b/src/charging-station/ChargingStation.ts @@ -35,7 +35,6 @@ import { type BootNotificationRequest, type BootNotificationResponse, type CachedRequest, - type ChargingStationAutomaticTransactionGeneratorConfiguration, type ChargingStationConfiguration, type ChargingStationInfo, type ChargingStationOcppConfiguration, @@ -93,6 +92,9 @@ import { FileUtils, MessageChannelUtils, Utils, + buildChargingStationAutomaticTransactionGeneratorConfiguration, + buildConnectorsStatus, + buildEvsesStatus, logger, } from '../utils'; @@ -158,11 +160,17 @@ export class ChargingStation { return new URL( `${ this.getSupervisionUrlOcppConfiguration() && - Utils.isNotEmptyString(this.getSupervisionUrlOcppKey()) + Utils.isNotEmptyString(this.getSupervisionUrlOcppKey()) && + Utils.isNotEmptyString( + ChargingStationConfigurationUtils.getConfigurationKey( + this, + this.getSupervisionUrlOcppKey() + )?.value + ) ? ChargingStationConfigurationUtils.getConfigurationKey( this, this.getSupervisionUrlOcppKey() - )?.value + ).value : this.configuredSupervisionUrl.href }/${this.stationInfo.chargingStationId}` ); @@ -810,12 +818,22 @@ export class ChargingStation { public getAutomaticTransactionGeneratorConfiguration(): | AutomaticTransactionGeneratorConfiguration | undefined { + let automaticTransactionGeneratorConfiguration: + | AutomaticTransactionGeneratorConfiguration + | undefined; const automaticTransactionGeneratorConfigurationFromFile = this.getConfigurationFromFile()?.automaticTransactionGenerator; if (automaticTransactionGeneratorConfigurationFromFile) { - return automaticTransactionGeneratorConfigurationFromFile; + automaticTransactionGeneratorConfiguration = + automaticTransactionGeneratorConfigurationFromFile; + } else { + automaticTransactionGeneratorConfiguration = + this.getTemplateFromFile()?.AutomaticTransactionGenerator; } - return this.getTemplateFromFile()?.AutomaticTransactionGenerator; + return { + ...Constants.DEFAULT_ATG_CONFIGURATION, + ...automaticTransactionGeneratorConfiguration, + }; } public startAutomaticTransactionGenerator(connectorIds?: number[]): void { @@ -943,7 +961,7 @@ export class ChargingStation { private getStationInfoFromTemplate(): ChargingStationInfo { const stationTemplate: ChargingStationTemplate | undefined = this.getTemplateFromFile(); - ChargingStationUtils.checkTemplateFile(stationTemplate, this.logPrefix(), this.templateFile); + ChargingStationUtils.checkTemplate(stationTemplate, this.logPrefix(), this.templateFile); ChargingStationUtils.warnTemplateKeysDeprecation( stationTemplate, this.logPrefix(), @@ -1058,7 +1076,7 @@ export class ChargingStation { private initialize(): void { const stationTemplate = this.getTemplateFromFile(); - ChargingStationUtils.checkTemplateFile(stationTemplate, this.logPrefix(), this.templateFile); + ChargingStationUtils.checkTemplate(stationTemplate, this.logPrefix(), this.templateFile); this.configurationFile = path.join( path.dirname(this.templateFile.replace('station-templates', 'configurations')), `${ChargingStationUtils.getHashId(this.index, stationTemplate)}.json` @@ -1493,7 +1511,7 @@ export class ChargingStation { private getConfigurationFromFile(): ChargingStationConfiguration | undefined { let configuration: ChargingStationConfiguration | undefined; - if (this.configurationFile && fs.existsSync(this.configurationFile)) { + if (Utils.isNotEmptyString(this.configurationFile) && fs.existsSync(this.configurationFile)) { try { if (this.sharedLRUCache.hasChargingStationConfiguration(this.configurationFileHash)) { configuration = this.sharedLRUCache.getChargingStationConfiguration( @@ -1521,18 +1539,8 @@ export class ChargingStation { return configuration; } - private saveChargingStationAutomaticTransactionGeneratorConfiguration( - stationTemplate?: ChargingStationTemplate - ): void { - this.saveConfiguration({ - automaticTransactionGenerator: (stationTemplate ?? this.getTemplateFromFile()) - .AutomaticTransactionGenerator, - ...(!Utils.isNullOrUndefined(this.automaticTransactionGenerator?.connectorsStatus) && { - automaticTransactionGeneratorStatuses: [ - ...this.automaticTransactionGenerator.connectorsStatus.values(), - ], - }), - }); + private saveChargingStationAutomaticTransactionGeneratorConfiguration(): void { + this.saveConfiguration(); } private saveConnectorsStatus() { @@ -1543,10 +1551,8 @@ export class ChargingStation { this.saveConfiguration(); } - private saveConfiguration( - chargingStationAutomaticTransactionGeneratorConfiguration?: ChargingStationAutomaticTransactionGeneratorConfiguration - ): void { - if (this.configurationFile) { + private saveConfiguration(): void { + if (Utils.isNotEmptyString(this.configurationFile)) { try { if (!fs.existsSync(path.dirname(this.configurationFile))) { fs.mkdirSync(path.dirname(this.configurationFile), { recursive: true }); @@ -1559,30 +1565,15 @@ export class ChargingStation { if (this.getOcppPersistentConfiguration() && this.ocppConfiguration?.configurationKey) { configurationData.configurationKey = this.ocppConfiguration.configurationKey; } - if (chargingStationAutomaticTransactionGeneratorConfiguration) { - configurationData = merge( - configurationData, - chargingStationAutomaticTransactionGeneratorConfiguration - ); - } + configurationData = merge( + configurationData, + buildChargingStationAutomaticTransactionGeneratorConfiguration(this) + ); if (this.connectors.size > 0) { - configurationData.connectorsStatus = [...this.connectors.values()].map( - // eslint-disable-next-line @typescript-eslint/no-unused-vars - ({ transactionSetInterval, ...connectorStatusRest }) => connectorStatusRest - ); + configurationData.connectorsStatus = buildConnectorsStatus(this); } if (this.evses.size > 0) { - configurationData.evsesStatus = [...this.evses.values()].map((evseStatus) => { - const status = { - ...evseStatus, - connectorsStatus: [...evseStatus.connectors.values()].map( - // eslint-disable-next-line @typescript-eslint/no-unused-vars - ({ transactionSetInterval, ...connectorStatusRest }) => connectorStatusRest - ), - }; - delete status.connectors; - return status as EvseStatusConfiguration; - }); + configurationData.evsesStatus = buildEvsesStatus(this); } delete configurationData.configurationHash; const configurationHash = crypto @@ -1641,9 +1632,11 @@ export class ChargingStation { } private getOcppConfigurationFromFile(): ChargingStationOcppConfiguration | undefined { - if (this.getOcppPersistentConfiguration() === true) { - return { configurationKey: this.getConfigurationFromFile()?.configurationKey }; + const configurationKey = this.getConfigurationFromFile()?.configurationKey; + if (this.getOcppPersistentConfiguration() === true && configurationKey) { + return { configurationKey }; } + return undefined; } private getOcppConfiguration(): ChargingStationOcppConfiguration | undefined { @@ -1671,7 +1664,7 @@ export class ChargingStation { skipBufferingOnError: true, }); if (this.isRegistered() === false) { - this.getRegistrationMaxRetries() !== -1 && registrationRetryCount++; + this.getRegistrationMaxRetries() !== -1 && ++registrationRetryCount; await Utils.sleep( this?.bootNotificationResponse?.interval ? this.bootNotificationResponse.interval * 1000 @@ -2168,6 +2161,7 @@ export class ChargingStation { } private getConfiguredSupervisionUrl(): URL { + let configuredSupervisionUrl: string; const supervisionUrls = this.stationInfo?.supervisionUrls ?? Configuration.getSupervisionUrls(); if (Utils.isNotEmptyArray(supervisionUrls)) { let configuredSupervisionUrlIndex: number; @@ -2189,9 +2183,16 @@ export class ChargingStation { configuredSupervisionUrlIndex = (this.index - 1) % supervisionUrls.length; break; } - return new URL(supervisionUrls[configuredSupervisionUrlIndex]); + configuredSupervisionUrl = supervisionUrls[configuredSupervisionUrlIndex]; + } else { + configuredSupervisionUrl = supervisionUrls as string; } - return new URL(supervisionUrls as string); + if (Utils.isNotEmptyString(configuredSupervisionUrl)) { + return new URL(configuredSupervisionUrl); + } + const errorMsg = 'No supervision url(s) configured'; + logger.error(`${this.logPrefix()} ${errorMsg}`); + throw new BaseError(`${errorMsg}`); } private stopHeartbeat(): void { @@ -2225,7 +2226,7 @@ export class ChargingStation { this.autoReconnectRetryCount < this.getAutoReconnectMaxRetries() || this.getAutoReconnectMaxRetries() === -1 ) { - this.autoReconnectRetryCount++; + ++this.autoReconnectRetryCount; const reconnectDelay = this.getReconnectExponentialDelay() ? Utils.exponentialDelay(this.autoReconnectRetryCount) : this.getConnectionTimeout() * 1000;