X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fcharging-station%2FChargingStation.ts;h=b3d4bdd98016cbf5d35288c15bff552417859497;hb=d5575b174bef0e79d2a600ae24be2789d6cf3451;hp=4f65a4e6a0c28122cf37e877daf7b4d953973670;hpb=9bb1159e3a17ace0e8502e7c4e727219a2b033f2;p=e-mobility-charging-stations-simulator.git diff --git a/src/charging-station/ChargingStation.ts b/src/charging-station/ChargingStation.ts index 4f65a4e6..b3d4bdd9 100644 --- a/src/charging-station/ChargingStation.ts +++ b/src/charging-station/ChargingStation.ts @@ -53,6 +53,7 @@ import { type ErrorCallback, type ErrorResponse, ErrorType, + type EvseStatus, FileType, FirmwareStatus, type FirmwareStatusNotificationRequest, @@ -108,6 +109,7 @@ export class ChargingStation { public ocppConfiguration!: ChargingStationOcppConfiguration | undefined; public wsConnection!: WebSocket | null; public readonly connectors: Map; + public readonly evses: Map; public readonly requests: Map; public performanceStatistics!: PerformanceStatistics | undefined; public heartbeatSetInterval!: NodeJS.Timeout; @@ -119,6 +121,7 @@ export class ChargingStation { private configurationFile!: string; private configurationFileHash!: string; private connectorsConfigurationHash!: string; + private evsesConfigurationHash!: string; private ocppIncomingRequestService!: OCPPIncomingRequestService; private readonly messageBuffer: Set; private configuredSupervisionUrl!: URL; @@ -138,6 +141,7 @@ export class ChargingStation { this.index = index; this.templateFile = templateFile; this.connectors = new Map(); + this.evses = new Map(); this.requests = new Map(); this.messageBuffer = new Set(); this.sharedLRUCache = SharedLRUCache.getInstance(); @@ -233,11 +237,11 @@ export class ChargingStation { } public isChargingStationAvailable(): boolean { - return this.getConnectorStatus(0)?.availability === AvailabilityType.OPERATIVE; + return this.getConnectorStatus(0)?.availability === AvailabilityType.Operative; } public isConnectorAvailable(id: number): boolean { - return id > 0 && this.getConnectorStatus(id)?.availability === AvailabilityType.OPERATIVE; + return id > 0 && this.getConnectorStatus(id)?.availability === AvailabilityType.Operative; } public getNumberOfConnectors(): number { @@ -471,19 +475,19 @@ export class ChargingStation { public startMeterValues(connectorId: number, interval: number): void { if (connectorId === 0) { logger.error( - `${this.logPrefix()} Trying to start MeterValues on connector Id ${connectorId.toString()}` + `${this.logPrefix()} Trying to start MeterValues on connector id ${connectorId.toString()}` ); return; } if (!this.getConnectorStatus(connectorId)) { logger.error( - `${this.logPrefix()} Trying to start MeterValues on non existing connector Id ${connectorId.toString()}` + `${this.logPrefix()} Trying to start MeterValues on non existing connector id ${connectorId.toString()}` ); return; } if (this.getConnectorStatus(connectorId)?.transactionStarted === false) { logger.error( - `${this.logPrefix()} Trying to start MeterValues on connector Id ${connectorId} with no transaction started` + `${this.logPrefix()} Trying to start MeterValues on connector id ${connectorId} with no transaction started` ); return; } else if ( @@ -491,7 +495,7 @@ export class ChargingStation { Utils.isNullOrUndefined(this.getConnectorStatus(connectorId)?.transactionId) ) { logger.error( - `${this.logPrefix()} Trying to start MeterValues on connector Id ${connectorId} with no transaction id` + `${this.logPrefix()} Trying to start MeterValues on connector id ${connectorId} with no transaction id` ); return; } @@ -530,6 +534,12 @@ export class ChargingStation { } } + public stopMeterValues(connectorId: number) { + if (this.getConnectorStatus(connectorId)?.transactionSetInterval) { + clearInterval(this.getConnectorStatus(connectorId)?.transactionSetInterval); + } + } + public start(): void { if (this.started === false) { if (this.starting === false) { @@ -625,21 +635,6 @@ export class ChargingStation { } } - public resetConnectorStatus(connectorId: number): void { - this.getConnectorStatus(connectorId).idTagLocalAuthorized = false; - this.getConnectorStatus(connectorId).idTagAuthorized = false; - this.getConnectorStatus(connectorId).transactionRemoteStarted = false; - this.getConnectorStatus(connectorId).transactionStarted = false; - delete this.getConnectorStatus(connectorId)?.localAuthorizeIdTag; - delete this.getConnectorStatus(connectorId)?.authorizeIdTag; - delete this.getConnectorStatus(connectorId)?.transactionId; - delete this.getConnectorStatus(connectorId)?.transactionIdTag; - this.getConnectorStatus(connectorId).transactionEnergyActiveImportRegisterValue = 0; - delete this.getConnectorStatus(connectorId)?.transactionBeginMeterValue; - this.stopMeterValues(connectorId); - parentPort?.postMessage(MessageChannelUtils.buildUpdatedMessage(this)); - } - public hasFeatureProfile(featureProfile: SupportedFeatureProfiles): boolean | undefined { return ChargingStationConfigurationUtils.getConfigurationKey( this, @@ -796,7 +791,7 @@ export class ChargingStation { private flushMessageBuffer(): void { if (this.messageBuffer.size > 0) { - this.messageBuffer.forEach((message) => { + for (const message of this.messageBuffer.values()) { let beginId: string; let commandName: RequestCommand; const [messageType] = JSON.parse(message) as OutgoingRequest | Response | ErrorResponse; @@ -813,7 +808,7 @@ export class ChargingStation { )} payload sent: ${message}` ); this.messageBuffer.delete(message); - }); + } } } @@ -918,34 +913,8 @@ export class ChargingStation { stationInfo.resetTime = !Utils.isNullOrUndefined(stationTemplate?.resetTime) ? stationTemplate.resetTime * 1000 : Constants.CHARGING_STATION_DEFAULT_RESET_TIME; - const configuredMaxConnectors = - ChargingStationUtils.getConfiguredNumberOfConnectors(stationTemplate); - ChargingStationUtils.checkConfiguredMaxConnectors( - configuredMaxConnectors, - this.templateFile, - this.logPrefix() - ); - const templateMaxConnectors = - ChargingStationUtils.getTemplateMaxNumberOfConnectors(stationTemplate); - ChargingStationUtils.checkTemplateMaxConnectors( - templateMaxConnectors, - this.templateFile, - this.logPrefix() - ); - if ( - configuredMaxConnectors > - (stationTemplate?.Connectors[0] ? templateMaxConnectors - 1 : templateMaxConnectors) && - !stationTemplate?.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; - } - // Build connectors if needed (FIXME: should be factored out) - this.initializeConnectors(stationInfo, configuredMaxConnectors, templateMaxConnectors); + // Initialize evses or connectors if needed (FIXME: should be factored out) + this.initializeConnectorsOrEvses(stationInfo); stationInfo.maximumAmperage = this.getMaximumAmperage(stationInfo); ChargingStationUtils.createStationInfoHash(stationInfo); return stationInfo; @@ -962,7 +931,10 @@ export class ChargingStation { private getStationInfo(): ChargingStationInfo { const stationInfoFromTemplate: ChargingStationInfo = this.getStationInfoFromTemplate(); const stationInfoFromFile: ChargingStationInfo | undefined = this.getStationInfoFromFile(); - // Priority: charging station info from template > charging station info from configuration file > charging station info attribute + // Priority: + // 1. charging station info from template + // 2. charging station info from configuration file + // 3. charging station info attribute if (stationInfoFromFile?.templateHash === stationInfoFromTemplate.templateHash) { if (this.stationInfo?.infoHash === stationInfoFromFile?.infoHash) { return this.stationInfo; @@ -1023,8 +995,9 @@ export class ChargingStation { this.stationInfo.firmwareVersion = match?.join('.'); } this.saveStationInfo(); - // Avoid duplication of connectors related information in RAM + // Avoid duplication of connectors or evses related information in RAM delete this.stationInfo?.Connectors; + delete this.stationInfo?.Evses; this.configuredSupervisionUrl = this.getConfiguredSupervisionUrl(); if (this.getEnableStatistics() === true) { this.performanceStatistics = PerformanceStatistics.getInstance( @@ -1238,11 +1211,23 @@ export class ChargingStation { this.saveOcppConfiguration(); } - private initializeConnectors( - stationInfo: ChargingStationInfo, - configuredMaxConnectors: number, - templateMaxConnectors: number - ): void { + 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}`); @@ -1252,10 +1237,17 @@ export class ChargingStation { logger.warn( `${this.logPrefix()} Charging station information from template ${ this.templateFile - } with no connector Id 0 configuration` + } 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()}`) @@ -1265,41 +1257,70 @@ export class ChargingStation { if (this.connectors?.size === 0 || connectorsConfigChanged) { connectorsConfigChanged && this.connectors.clear(); this.connectorsConfigurationHash = connectorsConfigHash; - // Add connector Id 0 - let lastConnector = '0'; - for (lastConnector in stationInfo?.Connectors) { - const connectorStatus = stationInfo?.Connectors[lastConnector]; - const lastConnectorId = Utils.convertToInt(lastConnector); - if ( - lastConnectorId === 0 && - this.getUseConnectorId0(stationInfo) === true && - connectorStatus - ) { - this.checkStationInfoConnectorStatus(lastConnectorId, connectorStatus); - this.connectors.set( - lastConnectorId, - Utils.cloneObject(connectorStatus) - ); - this.getConnectorStatus(lastConnectorId).availability = AvailabilityType.OPERATIVE; - if (Utils.isUndefined(this.getConnectorStatus(lastConnectorId)?.chargingProfiles)) { - this.getConnectorStatus(lastConnectorId).chargingProfiles = []; - } + 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(connectorZeroStatus)); + this.getConnectorStatus(0).availability = AvailabilityType.Operative; + if (Utils.isUndefined(this.getConnectorStatus(0)?.chargingProfiles)) { + this.getConnectorStatus(0).chargingProfiles = []; } } - // Generate all connectors - if ((stationInfo?.Connectors[0] ? templateMaxConnectors - 1 : templateMaxConnectors) > 0) { - for (let index = 1; index <= configuredMaxConnectors; index++) { - const randConnectorId = stationInfo?.randomConnectors - ? Utils.getRandomInteger(Utils.convertToInt(lastConnector), 1) - : index; - const connectorStatus = stationInfo?.Connectors[randConnectorId.toString()]; - this.checkStationInfoConnectorStatus(randConnectorId, connectorStatus); - this.connectors.set(index, Utils.cloneObject(connectorStatus)); - this.getConnectorStatus(index).availability = AvailabilityType.OPERATIVE; - if (Utils.isUndefined(this.getConnectorStatus(index)?.chargingProfiles)) { - this.getConnectorStatus(index).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)); + 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 { @@ -1309,35 +1330,61 @@ export class ChargingStation { } with no connectors configuration defined, using already defined connectors` ); } - // Initialize transaction attributes on connectors - for (const connectorId of this.connectors.keys()) { - if (connectorId > 0 && this.getConnectorStatus(connectorId)?.transactionStarted === true) { - logger.warn( - `${this.logPrefix()} Connector ${connectorId} at initialization has a transaction started: ${ - this.getConnectorStatus(connectorId)?.transactionId - }` - ); - } - if ( - connectorId > 0 && - Utils.isNullOrUndefined(this.getConnectorStatus(connectorId)?.transactionStarted) - ) { - this.initializeConnectorStatus(connectorId); - } - } } - private checkStationInfoConnectorStatus( - connectorId: number, - connectorStatus: ConnectorStatus - ): void { - if (!Utils.isNullOrUndefined(connectorStatus?.status)) { + 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 connector ${connectorId} status configuration defined, undefine it` + } with no evse id 0 configuration` ); - delete connectorStatus.status; + } + 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` + ); + } } } @@ -1844,15 +1891,7 @@ export class ChargingStation { // Set default status connectorStatus = ConnectorStatusEnum.Available; } - await this.ocppRequestService.requestHandler< - StatusNotificationRequest, - StatusNotificationResponse - >( - this, - RequestCommand.STATUS_NOTIFICATION, - OCPPServiceUtils.buildStatusNotificationRequest(this, connectorId, connectorStatus) - ); - this.getConnectorStatus(connectorId).status = connectorStatus; + await OCPPServiceUtils.sendAndSetConnectorStatus(this, connectorId, connectorStatus); } if (this.stationInfo?.firmwareStatus === FirmwareStatus.Installing) { await this.ocppRequestService.requestHandler< @@ -1987,12 +2026,6 @@ export class ChargingStation { } } - private stopMeterValues(connectorId: number) { - if (this.getConnectorStatus(connectorId)?.transactionSetInterval) { - clearInterval(this.getConnectorStatus(connectorId)?.transactionSetInterval); - } - } - private getReconnectExponentialDelay(): boolean { return this.stationInfo?.reconnectExponentialDelay ?? false; } @@ -2051,13 +2084,4 @@ export class ChargingStation { | undefined { return this.getTemplateFromFile()?.AutomaticTransactionGenerator; } - - private initializeConnectorStatus(connectorId: number): void { - this.getConnectorStatus(connectorId).idTagLocalAuthorized = false; - this.getConnectorStatus(connectorId).idTagAuthorized = false; - this.getConnectorStatus(connectorId).transactionRemoteStarted = false; - this.getConnectorStatus(connectorId).transactionStarted = false; - this.getConnectorStatus(connectorId).energyActiveImportRegisterValue = 0; - this.getConnectorStatus(connectorId).transactionEnergyActiveImportRegisterValue = 0; - } }