X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fcharging-station%2FAutomaticTransactionGenerator.ts;h=4185215aae0d25ceccf9618bb289712d46d96ed2;hb=59b6ed8d1db313ef3371efd8ab5e039cf3dedab0;hp=2548836022a7e7381d51002a37a487f52309380e;hpb=d1c6c833e3dcec4fbd2beacf221e87ad3b407634;p=e-mobility-charging-stations-simulator.git diff --git a/src/charging-station/AutomaticTransactionGenerator.ts b/src/charging-station/AutomaticTransactionGenerator.ts index 25488360..4185215a 100644 --- a/src/charging-station/AutomaticTransactionGenerator.ts +++ b/src/charging-station/AutomaticTransactionGenerator.ts @@ -1,33 +1,29 @@ -// Partial Copyright Jerome Benoit. 2021. All Rights Reserved. +// Partial Copyright Jerome Benoit. 2021-2023. All Rights Reserved. -import { AsyncResource } from 'async_hooks'; +import { AsyncResource } from 'node:async_hooks'; -import BaseError from '../exception/BaseError'; -import PerformanceStatistics from '../performance/PerformanceStatistics'; -import { - type AutomaticTransactionGeneratorConfiguration, - IdTagDistribution, - type Status, -} from '../types/AutomaticTransactionGenerator'; -import { RequestCommand } from '../types/ocpp/Requests'; +import { type ChargingStation, ChargingStationUtils } from './internal'; +import { BaseError } from '../exception'; +// import { PerformanceStatistics } from '../performance'; +import { PerformanceStatistics } from '../performance/PerformanceStatistics'; import { AuthorizationStatus, type AuthorizeRequest, type AuthorizeResponse, + type AutomaticTransactionGeneratorConfiguration, + IdTagDistribution, + RequestCommand, type StartTransactionRequest, type StartTransactionResponse, + type Status, StopTransactionReason, type StopTransactionResponse, -} from '../types/ocpp/Transaction'; -import Constants from '../utils/Constants'; -import logger from '../utils/Logger'; -import Utils from '../utils/Utils'; -import type ChargingStation from './ChargingStation'; -import { ChargingStationUtils } from './ChargingStationUtils'; +} from '../types'; +import { Constants, Utils, logger } from '../utils'; const moduleName = 'AutomaticTransactionGenerator'; -export default class AutomaticTransactionGenerator extends AsyncResource { +export class AutomaticTransactionGenerator extends AsyncResource { private static readonly instances: Map = new Map< string, AutomaticTransactionGenerator @@ -55,7 +51,7 @@ export default class AutomaticTransactionGenerator extends AsyncResource { public static getInstance( automaticTransactionGeneratorConfiguration: AutomaticTransactionGeneratorConfiguration, chargingStation: ChargingStation - ): AutomaticTransactionGenerator { + ): AutomaticTransactionGenerator | undefined { if (AutomaticTransactionGenerator.instances.has(chargingStation.stationInfo.hashId) === false) { AutomaticTransactionGenerator.instances.set( chargingStation.stationInfo.hashId, @@ -105,9 +101,7 @@ export default class AutomaticTransactionGenerator extends AsyncResource { ) => Promise, this, connectorId - ).catch(() => { - /* This is intentional */ - }); + ).catch(Constants.EMPTY_FUNCTION); } else if (this.connectorsStatus.get(connectorId)?.start === true) { logger.warn(`${this.logPrefix(connectorId)} is already started on connector`); } @@ -151,30 +145,32 @@ export default class AutomaticTransactionGenerator extends AsyncResource { private async internalStartConnector(connectorId: number): Promise { 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) { + while (this.connectorsStatus.get(connectorId)?.start === true) { if (new Date() > this.connectorsStatus.get(connectorId).stopDate) { this.stopConnector(connectorId); break; } 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() === 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; @@ -204,7 +200,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(); @@ -218,18 +214,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); } @@ -237,24 +233,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`, @@ -265,8 +260,8 @@ export default class AutomaticTransactionGenerator extends AsyncResource { private setStartConnectorStatus(connectorId: number): void { this.connectorsStatus.get(connectorId).skippedConsecutiveTransactions = 0; const previousRunDuration = - this?.connectorsStatus.get(connectorId)?.startDate && - this?.connectorsStatus.get(connectorId)?.lastRunDate + this.connectorsStatus.get(connectorId)?.startDate && + this.connectorsStatus.get(connectorId)?.lastRunDate ? this.connectorsStatus.get(connectorId).lastRunDate.getTime() - this.connectorsStatus.get(connectorId).startDate.getTime() : 0; @@ -383,10 +378,10 @@ export default class AutomaticTransactionGenerator extends AsyncResource { this.connectorsStatus.get(connectorId).rejectedStopTransactionRequests++; } } else { - const transactionId = this.chargingStation.getConnectorStatus(connectorId).transactionId; + const transactionId = this.chargingStation.getConnectorStatus(connectorId)?.transactionId; logger.warn( `${this.logPrefix(connectorId)} stopping a not started transaction${ - transactionId ? ' ' + transactionId.toString() : '' + !Utils.isNullOrUndefined(transactionId) ? ` ${transactionId?.toString()}` : '' }` ); } @@ -433,13 +428,13 @@ export default class AutomaticTransactionGenerator extends AsyncResource { } } - private logPrefix(connectorId?: number): string { + private logPrefix = (connectorId?: number): string => { return Utils.logPrefix( ` ${this.chargingStation.stationInfo.chargingStationId} | ATG${ connectorId !== undefined ? ` on connector #${connectorId.toString()}` : '' }:` ); - } + }; private handleStartTransactionResponse( connectorId: number, @@ -449,15 +444,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) { + if (this.chargingStation.started === false && this.chargingStation.starting === false) { logger.warn(`${this.logPrefix(connectorId)} charging station is stopped, cannot proceed`); + return false; } - return this.chargingStation.started; + return true; } }