X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fcharging-station%2FAutomaticTransactionGenerator.ts;h=80c9a5176937a035dccc699fc39e6e6b2859a88d;hb=262c47b2dbe7ad59fa523e77668dd0b994214cb2;hp=9b196ed9d15bdf6080f5eb0fb78991740dd79226;hpb=c72f6634184bc83a6476152446ac9f0d7d02acf5;p=e-mobility-charging-stations-simulator.git diff --git a/src/charging-station/AutomaticTransactionGenerator.ts b/src/charging-station/AutomaticTransactionGenerator.ts index 9b196ed9..80c9a517 100644 --- a/src/charging-station/AutomaticTransactionGenerator.ts +++ b/src/charging-station/AutomaticTransactionGenerator.ts @@ -1,4 +1,6 @@ -// Partial Copyright Jerome Benoit. 2021. All Rights Reserved. +// Partial Copyright Jerome Benoit. 2021-2023. All Rights Reserved. + +import { AsyncResource } from 'async_hooks'; import BaseError from '../exception/BaseError'; import PerformanceStatistics from '../performance/PerformanceStatistics'; @@ -23,7 +25,9 @@ import Utils from '../utils/Utils'; import type ChargingStation from './ChargingStation'; import { ChargingStationUtils } from './ChargingStationUtils'; -export default class AutomaticTransactionGenerator { +const moduleName = 'AutomaticTransactionGenerator'; + +export default class AutomaticTransactionGenerator extends AsyncResource { private static readonly instances: Map = new Map< string, AutomaticTransactionGenerator @@ -39,6 +43,7 @@ export default class AutomaticTransactionGenerator { automaticTransactionGeneratorConfiguration: AutomaticTransactionGeneratorConfiguration, chargingStation: ChargingStation ) { + super(moduleName); this.started = false; this.configuration = automaticTransactionGeneratorConfiguration; this.chargingStation = chargingStation; @@ -64,6 +69,9 @@ export default class AutomaticTransactionGenerator { } public start(): void { + if (this.checkChargingStation() === false) { + return; + } if (this.started === true) { logger.warn(`${this.logPrefix()} is already started`); return; @@ -82,16 +90,23 @@ export default class AutomaticTransactionGenerator { } public startConnector(connectorId: number): void { + if (this.checkChargingStation(connectorId) === false) { + return; + } if (this.connectorsStatus.has(connectorId) === false) { logger.error(`${this.logPrefix(connectorId)} starting on non existing connector`); throw new BaseError(`Connector ${connectorId} does not exist`); } if (this.connectorsStatus.get(connectorId)?.start === false) { - // Avoid hogging the event loop with a busy loop - setImmediate(() => { - this.internalStartConnector(connectorId).catch(() => { - /* This is intentional */ - }); + this.runInAsyncScope( + this.internalStartConnector.bind(this) as ( + this: AutomaticTransactionGenerator, + ...args: any[] + ) => Promise, + this, + connectorId + ).catch(() => { + /* This is intentional */ }); } else if (this.connectorsStatus.get(connectorId)?.start === true) { logger.warn(`${this.logPrefix(connectorId)} is already started on connector`); @@ -134,8 +149,7 @@ export default class AutomaticTransactionGenerator { } private async internalStartConnector(connectorId: number): Promise { - this.initializeConnectorStatus(connectorId); - this.connectorsStatus.get(connectorId).start = true; + this.setStartConnectorStatus(connectorId); logger.info( this.logPrefix(connectorId) + ' started on connector and will run for ' + @@ -149,7 +163,7 @@ export default class AutomaticTransactionGenerator { this.stopConnector(connectorId); break; } - if (!this.chargingStation.isInAcceptedState()) { + if (this.chargingStation.isInAcceptedState() === false) { logger.error( this.logPrefix(connectorId) + ' entered in transaction loop while the charging station is not in accepted state' @@ -157,7 +171,7 @@ export default class AutomaticTransactionGenerator { this.stopConnector(connectorId); break; } - if (!this.chargingStation.isChargingStationAvailable()) { + if (this.chargingStation.isChargingStationAvailable() === false) { logger.info( this.logPrefix(connectorId) + ' entered in transaction loop while the charging station is unavailable' @@ -165,7 +179,7 @@ export default class AutomaticTransactionGenerator { this.stopConnector(connectorId); break; } - if (!this.chargingStation.isConnectorAvailable(connectorId)) { + if (this.chargingStation.isConnectorAvailable(connectorId) === false) { logger.info( `${this.logPrefix( connectorId @@ -248,28 +262,8 @@ export default class AutomaticTransactionGenerator { ); } - private initializeConnectorStatus(connectorId: number): void { - this.connectorsStatus.get(connectorId).authorizeRequests = - this?.connectorsStatus.get(connectorId)?.authorizeRequests ?? 0; - this.connectorsStatus.get(connectorId).acceptedAuthorizeRequests = - this?.connectorsStatus.get(connectorId)?.acceptedAuthorizeRequests ?? 0; - this.connectorsStatus.get(connectorId).rejectedAuthorizeRequests = - this?.connectorsStatus.get(connectorId)?.rejectedAuthorizeRequests ?? 0; - this.connectorsStatus.get(connectorId).startTransactionRequests = - this?.connectorsStatus.get(connectorId)?.startTransactionRequests ?? 0; - this.connectorsStatus.get(connectorId).acceptedStartTransactionRequests = - this?.connectorsStatus.get(connectorId)?.acceptedStartTransactionRequests ?? 0; - this.connectorsStatus.get(connectorId).rejectedStartTransactionRequests = - this?.connectorsStatus.get(connectorId)?.rejectedStartTransactionRequests ?? 0; - this.connectorsStatus.get(connectorId).stopTransactionRequests = - this?.connectorsStatus.get(connectorId)?.stopTransactionRequests ?? 0; - this.connectorsStatus.get(connectorId).acceptedStopTransactionRequests = - this?.connectorsStatus.get(connectorId)?.acceptedStopTransactionRequests ?? 0; - this.connectorsStatus.get(connectorId).rejectedStopTransactionRequests = - this?.connectorsStatus.get(connectorId)?.rejectedStopTransactionRequests ?? 0; + private setStartConnectorStatus(connectorId: number): void { this.connectorsStatus.get(connectorId).skippedConsecutiveTransactions = 0; - this.connectorsStatus.get(connectorId).skippedTransactions = - this?.connectorsStatus.get(connectorId)?.skippedTransactions ?? 0; const previousRunDuration = this?.connectorsStatus.get(connectorId)?.startDate && this?.connectorsStatus.get(connectorId)?.lastRunDate @@ -285,8 +279,7 @@ export default class AutomaticTransactionGenerator { 1000 - previousRunDuration ); - this.connectorsStatus.get(connectorId).start = - this?.connectorsStatus.get(connectorId)?.start ?? false; + this.connectorsStatus.get(connectorId).start = true; } private initializeConnectorsStatus(): void { @@ -460,4 +453,12 @@ export default class AutomaticTransactionGenerator { this.connectorsStatus.get(connectorId).rejectedStartTransactionRequests++; } } + + private checkChargingStation(connectorId?: number): boolean { + if (this.chargingStation.started === false && this.chargingStation.starting === false) { + logger.warn(`${this.logPrefix(connectorId)} charging station is stopped, cannot proceed`); + return false; + } + return true; + } }