X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fcharging-station%2FAutomaticTransactionGenerator.ts;h=d058866abae0da08c3d43937af6e3931e0872c6d;hb=517ffa58b673e8ece31c6520d0f2331848fc05ed;hp=c6056dd53af52a539e1ef1e7bc472d5184d6281e;hpb=1bf29f5be7c0ffe3d029e447ecb50da55bfd8948;p=e-mobility-charging-stations-simulator.git diff --git a/src/charging-station/AutomaticTransactionGenerator.ts b/src/charging-station/AutomaticTransactionGenerator.ts index c6056dd5..d058866a 100644 --- a/src/charging-station/AutomaticTransactionGenerator.ts +++ b/src/charging-station/AutomaticTransactionGenerator.ts @@ -2,7 +2,7 @@ import { AsyncResource } from 'node:async_hooks'; -import { type ChargingStation, ChargingStationUtils } from './internal'; +import { type ChargingStation, ChargingStationUtils, IdTagsCache } from './internal'; import { BaseError } from '../exception'; // import { PerformanceStatistics } from '../performance'; import { PerformanceStatistics } from '../performance/PerformanceStatistics'; @@ -12,7 +12,6 @@ import { type AuthorizeResponse, type AutomaticTransactionGeneratorConfiguration, ConnectorStatusEnum, - IdTagDistribution, RequestCommand, type StartTransactionRequest, type StartTransactionResponse, @@ -34,7 +33,6 @@ export class AutomaticTransactionGenerator extends AsyncResource { public readonly configuration: AutomaticTransactionGeneratorConfiguration; public started: boolean; private readonly chargingStation: ChargingStation; - private idTagIndex: number; private constructor( automaticTransactionGeneratorConfiguration: AutomaticTransactionGeneratorConfiguration, @@ -44,7 +42,6 @@ export class AutomaticTransactionGenerator extends AsyncResource { this.started = false; this.configuration = automaticTransactionGeneratorConfiguration; this.chargingStation = chargingStation; - this.idTagIndex = 0; this.connectorsStatus = new Map(); this.initializeConnectorsStatus(); } @@ -324,8 +321,12 @@ export class AutomaticTransactionGenerator extends AsyncResource { const measureId = 'StartTransaction with ATG'; const beginId = PerformanceStatistics.beginMeasure(measureId); let startResponse: StartTransactionResponse; - if (this.chargingStation.hasAuthorizedTags()) { - const idTag = this.getIdTag(connectorId); + if (this.chargingStation.hasIdTags()) { + const idTag = IdTagsCache.getInstance().getIdTag( + this.configuration?.idTagDistribution, + this.chargingStation, + connectorId + ); const startTransactionLogMsg = `${this.logPrefix( connectorId )} start transaction with an idTag '${idTag}'`; @@ -413,45 +414,10 @@ export class AutomaticTransactionGenerator extends AsyncResource { return this.configuration?.requireAuthorize ?? true; } - private getRandomIdTag(authorizationFile: string): string { - const tags = this.chargingStation.authorizedTagsCache.getAuthorizedTags(authorizationFile); - this.idTagIndex = Math.floor(Utils.secureRandom() * tags.length); - return tags[this.idTagIndex]; - } - - private getRoundRobinIdTag(authorizationFile: string): string { - const tags = this.chargingStation.authorizedTagsCache.getAuthorizedTags(authorizationFile); - const idTag = tags[this.idTagIndex]; - this.idTagIndex = this.idTagIndex === tags.length - 1 ? 0 : this.idTagIndex + 1; - return idTag; - } - - private getConnectorAffinityIdTag(authorizationFile: string, connectorId: number): string { - const tags = this.chargingStation.authorizedTagsCache.getAuthorizedTags(authorizationFile); - this.idTagIndex = (this.chargingStation.index - 1 + (connectorId - 1)) % tags.length; - return tags[this.idTagIndex]; - } - - private getIdTag(connectorId: number): string { - const authorizationFile = ChargingStationUtils.getAuthorizationFile( - this.chargingStation.stationInfo - ); - switch (this.configuration?.idTagDistribution) { - case IdTagDistribution.RANDOM: - return this.getRandomIdTag(authorizationFile); - case IdTagDistribution.ROUND_ROBIN: - return this.getRoundRobinIdTag(authorizationFile); - case IdTagDistribution.CONNECTOR_AFFINITY: - return this.getConnectorAffinityIdTag(authorizationFile, connectorId); - default: - return this.getRoundRobinIdTag(authorizationFile); - } - } - private logPrefix = (connectorId?: number): string => { return Utils.logPrefix( ` ${this.chargingStation.stationInfo.chargingStationId} | ATG${ - connectorId !== undefined ? ` on connector #${connectorId.toString()}` : '' + !Utils.isNullOrUndefined(connectorId) ? ` on connector #${connectorId.toString()}` : '' }:` ); };