X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fcharging-station%2FAutomaticTransactionGenerator.ts;h=3a1c6786f9b3c9fcf5cc93f522dd541b23c1e69e;hb=44eb6026079c8dc2c77b10a96a42d0c0b2da7c8f;hp=46c595f99441643c3d184ba7f83553647c01d44f;hpb=45df3874f855f883ba8b56238973966b455555eb;p=e-mobility-charging-stations-simulator.git diff --git a/src/charging-station/AutomaticTransactionGenerator.ts b/src/charging-station/AutomaticTransactionGenerator.ts index 46c595f9..3a1c6786 100644 --- a/src/charging-station/AutomaticTransactionGenerator.ts +++ b/src/charging-station/AutomaticTransactionGenerator.ts @@ -1,7 +1,9 @@ -// Partial Copyright Jerome Benoit. 2021. All Rights Reserved. +// Partial Copyright Jerome Benoit. 2021-2023. All Rights Reserved. import { AsyncResource } from 'async_hooks'; +import type ChargingStation from './ChargingStation'; +import { ChargingStationUtils } from './ChargingStationUtils'; import BaseError from '../exception/BaseError'; import PerformanceStatistics from '../performance/PerformanceStatistics'; import { @@ -22,8 +24,6 @@ import { import Constants from '../utils/Constants'; import logger from '../utils/Logger'; import Utils from '../utils/Utils'; -import type ChargingStation from './ChargingStation'; -import { ChargingStationUtils } from './ChargingStationUtils'; const moduleName = 'AutomaticTransactionGenerator'; @@ -69,6 +69,9 @@ export default class AutomaticTransactionGenerator extends AsyncResource { } public start(): void { + if (this.checkChargingStation() === false) { + return; + } if (this.started === true) { logger.warn(`${this.logPrefix()} is already started`); return; @@ -87,16 +90,24 @@ export default class AutomaticTransactionGenerator extends AsyncResource { } 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) { this.runInAsyncScope( - this.internalStartConnector.bind(this) as (this: this, ...args: any[]) => unknown, + 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`); } @@ -138,38 +149,39 @@ export default class AutomaticTransactionGenerator extends AsyncResource { } private async internalStartConnector(connectorId: number): Promise { - this.setConnectorStatus(connectorId); - this.connectorsStatus.get(connectorId).start = true; + this.setStartConnectorStatus(connectorId); logger.info( - this.logPrefix(connectorId) + - ' started on connector and will run for ' + - Utils.formatDurationMilliSeconds( - this.connectorsStatus.get(connectorId).stopDate.getTime() - - this.connectorsStatus.get(connectorId).startDate.getTime() - ) + `${this.logPrefix( + connectorId + )} started on connector and will run for ${Utils.formatDurationMilliSeconds( + this.connectorsStatus.get(connectorId).stopDate.getTime() - + this.connectorsStatus.get(connectorId).startDate.getTime() + )}` ); while (this.connectorsStatus.get(connectorId).start === true) { if (new Date() > this.connectorsStatus.get(connectorId).stopDate) { 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' + `${this.logPrefix( + connectorId + )} entered in transaction loop while the charging station is not in accepted state` ); 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' + `${this.logPrefix( + connectorId + )} entered in transaction loop while the charging station is unavailable` ); this.stopConnector(connectorId); break; } - if (!this.chargingStation.isConnectorAvailable(connectorId)) { + if (this.chargingStation.isConnectorAvailable(connectorId) === false) { logger.info( `${this.logPrefix( connectorId @@ -194,7 +206,7 @@ export default class AutomaticTransactionGenerator extends AsyncResource { this.configuration.minDelayBetweenTwoTransactions ) * 1000; logger.info( - this.logPrefix(connectorId) + ' waiting for ' + Utils.formatDurationMilliSeconds(wait) + `${this.logPrefix(connectorId)} waiting for ${Utils.formatDurationMilliSeconds(wait)}` ); await Utils.sleep(wait); const start = Utils.secureRandom(); @@ -208,18 +220,18 @@ export default class AutomaticTransactionGenerator extends AsyncResource { Utils.getRandomInteger(this.configuration.maxDuration, this.configuration.minDuration) * 1000; logger.info( - this.logPrefix(connectorId) + - ' transaction ' + - this.chargingStation.getConnectorStatus(connectorId).transactionId.toString() + - ' started and will stop in ' + - Utils.formatDurationMilliSeconds(waitTrxEnd) + `${this.logPrefix(connectorId)} transaction ${this.chargingStation + .getConnectorStatus(connectorId) + .transactionId.toString()} started and will stop in ${Utils.formatDurationMilliSeconds( + waitTrxEnd + )}` ); await Utils.sleep(waitTrxEnd); // Stop transaction logger.info( - this.logPrefix(connectorId) + - ' stop transaction ' + - this.chargingStation.getConnectorStatus(connectorId).transactionId.toString() + `${this.logPrefix(connectorId)} stop transaction ${this.chargingStation + .getConnectorStatus(connectorId) + .transactionId.toString()}` ); await this.stopTransaction(connectorId); } @@ -227,24 +239,23 @@ export default class AutomaticTransactionGenerator extends AsyncResource { this.connectorsStatus.get(connectorId).skippedConsecutiveTransactions++; this.connectorsStatus.get(connectorId).skippedTransactions++; logger.info( - this.logPrefix(connectorId) + - ' skipped consecutively ' + - this.connectorsStatus.get(connectorId).skippedConsecutiveTransactions.toString() + - '/' + - this.connectorsStatus.get(connectorId).skippedTransactions.toString() + - ' transaction(s)' + `${this.logPrefix(connectorId)} skipped consecutively ${this.connectorsStatus + .get(connectorId) + .skippedConsecutiveTransactions.toString()}/${this.connectorsStatus + .get(connectorId) + .skippedTransactions.toString()} transaction(s)` ); } this.connectorsStatus.get(connectorId).lastRunDate = new Date(); } this.connectorsStatus.get(connectorId).stoppedDate = new Date(); logger.info( - this.logPrefix(connectorId) + - ' stopped on connector and lasted for ' + - Utils.formatDurationMilliSeconds( - this.connectorsStatus.get(connectorId).stoppedDate.getTime() - - this.connectorsStatus.get(connectorId).startDate.getTime() - ) + `${this.logPrefix( + connectorId + )} stopped on connector and lasted for ${Utils.formatDurationMilliSeconds( + this.connectorsStatus.get(connectorId).stoppedDate.getTime() - + this.connectorsStatus.get(connectorId).startDate.getTime() + )}` ); logger.debug( `${this.logPrefix(connectorId)} connector status: %j`, @@ -252,28 +263,8 @@ export default class AutomaticTransactionGenerator extends AsyncResource { ); } - private setConnectorStatus(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 @@ -289,8 +280,7 @@ export default class AutomaticTransactionGenerator extends AsyncResource { 1000 - previousRunDuration ); - this.connectorsStatus.get(connectorId).start = - this?.connectorsStatus.get(connectorId)?.start ?? false; + this.connectorsStatus.get(connectorId).start = true; } private initializeConnectorsStatus(): void { @@ -397,7 +387,7 @@ export default class AutomaticTransactionGenerator extends AsyncResource { const transactionId = this.chargingStation.getConnectorStatus(connectorId).transactionId; logger.warn( `${this.logPrefix(connectorId)} stopping a not started transaction${ - transactionId ? ' ' + transactionId.toString() : '' + transactionId ? ` ${transactionId.toString()}` : '' }` ); } @@ -460,8 +450,16 @@ export default class AutomaticTransactionGenerator extends AsyncResource { if (startResponse?.idTagInfo?.status === AuthorizationStatus.ACCEPTED) { this.connectorsStatus.get(connectorId).acceptedStartTransactionRequests++; } else { - logger.warn(this.logPrefix(connectorId) + ' start transaction rejected'); + logger.warn(`${this.logPrefix(connectorId)} start transaction rejected`); 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; + } }