X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fcharging-station%2FAutomaticTransactionGenerator.ts;h=efc3769ac45b273c65cefbc2f5beea549dc3224d;hb=ee7c1da0e12c70134f31537a1c1e7040d309af5a;hp=e15ce4accd220b98c3658f05d724f79dc5404dc1;hpb=ff5813598de0ed1056d6e4fa7966330728bcb987;p=e-mobility-charging-stations-simulator.git diff --git a/src/charging-station/AutomaticTransactionGenerator.ts b/src/charging-station/AutomaticTransactionGenerator.ts index e15ce4ac..efc3769a 100644 --- a/src/charging-station/AutomaticTransactionGenerator.ts +++ b/src/charging-station/AutomaticTransactionGenerator.ts @@ -12,7 +12,6 @@ import { BaseError } from '../exception'; import { PerformanceStatistics } from '../performance'; import { AuthorizationStatus, - ConnectorStatusEnum, RequestCommand, type StartTransactionRequest, type StartTransactionResponse, @@ -86,7 +85,7 @@ export class AutomaticTransactionGenerator extends AsyncResource { this.starting = false; } - public async stop(): Promise { + public stop(): void { if (this.started === false) { logger.warn(`${this.logPrefix()} is already stopped`); return; @@ -96,7 +95,7 @@ export class AutomaticTransactionGenerator extends AsyncResource { return; } this.stopping = true; - await this.stopConnectors(); + this.stopConnectors(); this.started = false; this.stopping = false; } @@ -123,14 +122,13 @@ export class AutomaticTransactionGenerator extends AsyncResource { } } - public async stopConnector(connectorId: number): Promise { + public stopConnector(connectorId: number): void { if (this.connectorsStatus.has(connectorId) === false) { logger.error(`${this.logPrefix(connectorId)} stopping on non existing connector`); throw new BaseError(`Connector ${connectorId} does not exist`); } if (this.connectorsStatus.get(connectorId)?.start === true) { this.connectorsStatus.get(connectorId)!.start = false; - await this.stopTransaction(connectorId); } else if (this.connectorsStatus.get(connectorId)?.start === false) { logger.warn(`${this.logPrefix(connectorId)} is already stopped on connector`); } @@ -161,19 +159,19 @@ export class AutomaticTransactionGenerator extends AsyncResource { } } - private async stopConnectors(): Promise { + private stopConnectors(): void { if (this.chargingStation.hasEvses) { for (const [evseId, evseStatus] of this.chargingStation.evses) { if (evseId > 0) { for (const connectorId of evseStatus.connectors.keys()) { - await this.stopConnector(connectorId); + this.stopConnector(connectorId); } } } } else { for (const connectorId of this.chargingStation.connectors.keys()) { if (connectorId > 0) { - await this.stopConnector(connectorId); + this.stopConnector(connectorId); } } } @@ -190,20 +188,13 @@ export class AutomaticTransactionGenerator extends AsyncResource { )}`, ); while (this.connectorsStatus.get(connectorId)?.start === true) { + await this.waitChargingStationServiceInitialization(connectorId); + await this.waitChargingStationAvailable(connectorId); + await this.waitConnectorAvailable(connectorId); if (!this.canStartConnector(connectorId)) { - await this.stopConnector(connectorId); + this.stopConnector(connectorId); break; } - if (!this.chargingStation?.ocppRequestService) { - logger.info( - `${this.logPrefix( - connectorId, - )} transaction loop waiting for charging station service to be initialized`, - ); - do { - await sleep(Constants.CHARGING_STATION_ATG_INITIALIZATION_TIME); - } while (!this.chargingStation?.ocppRequestService); - } const wait = secondsToMilliseconds( getRandomInteger( this.chargingStation.getAutomaticTransactionGeneratorConfiguration() @@ -313,20 +304,54 @@ export class AutomaticTransactionGenerator extends AsyncResource { ); return false; } - if ( - this.chargingStation.getConnectorStatus(connectorId)?.status === - ConnectorStatusEnum.Unavailable - ) { - logger.info( - `${this.logPrefix( - connectorId, - )} entered in transaction loop while the connector ${connectorId} status is unavailable`, - ); - return false; - } return true; } + private async waitChargingStationServiceInitialization(connectorId: number): Promise { + let logged = false; + while (!this.chargingStation?.ocppRequestService) { + if (!logged) { + logger.info( + `${this.logPrefix( + connectorId, + )} transaction loop waiting for charging station service to be initialized`, + ); + logged = true; + } + await sleep(Constants.CHARGING_STATION_ATG_INITIALIZATION_TIME); + } + } + + private async waitChargingStationAvailable(connectorId: number): Promise { + let logged = false; + while (!this.chargingStation.isChargingStationAvailable()) { + if (!logged) { + logger.info( + `${this.logPrefix( + connectorId, + )} transaction loop waiting for charging station to be available`, + ); + logged = true; + } + await sleep(Constants.CHARGING_STATION_ATG_AVAILABILITY_TIME); + } + } + + private async waitConnectorAvailable(connectorId: number): Promise { + let logged = false; + while (!this.chargingStation.isConnectorAvailable(connectorId)) { + if (!logged) { + logger.info( + `${this.logPrefix( + connectorId, + )} transaction loop waiting for connector ${connectorId} to be available`, + ); + logged = true; + } + await sleep(Constants.CHARGING_STATION_ATG_AVAILABILITY_TIME); + } + } + private initializeConnectorsStatus(): void { if (this.chargingStation.hasEvses) { for (const [evseId, evseStatus] of this.chargingStation.evses) { @@ -353,13 +378,7 @@ export class AutomaticTransactionGenerator extends AsyncResource { this.chargingStation.getAutomaticTransactionGeneratorStatuses()![connectorId], ) : undefined; - delete connectorStatus?.startDate; - delete connectorStatus?.lastRunDate; - delete connectorStatus?.stopDate; - delete connectorStatus?.stoppedDate; - if (!this.started && connectorStatus?.start === true) { - connectorStatus.start = false; - } + this.resetConnectorStatus(connectorStatus); return ( connectorStatus ?? { start: false, @@ -378,6 +397,20 @@ export class AutomaticTransactionGenerator extends AsyncResource { ); } + private resetConnectorStatus(connectorStatus: Status | undefined): void { + delete connectorStatus?.startDate; + delete connectorStatus?.lastRunDate; + delete connectorStatus?.stopDate; + delete connectorStatus?.stoppedDate; + if ( + !this.started && + (connectorStatus?.start === true || + this.chargingStation.getAutomaticTransactionGeneratorConfiguration().enable === false) + ) { + connectorStatus!.start = false; + } + } + private async startTransaction( connectorId: number, ): Promise { @@ -439,7 +472,7 @@ export class AutomaticTransactionGenerator extends AsyncResource { private async stopTransaction( connectorId: number, - reason: StopTransactionReason = StopTransactionReason.LOCAL, + reason = StopTransactionReason.LOCAL, ): Promise { const measureId = 'StopTransaction with ATG'; const beginId = PerformanceStatistics.beginMeasure(measureId);