X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fcharging-station%2FAutomaticTransactionGenerator.ts;h=a2854507d98b452d3ea92389e877ed49f6039978;hb=5dc8b1b58356265bfe4c704c72874c8dc3d94450;hp=b32e794885326c6c768dbdf2857ba4d65a7e1693;hpb=9c7195b209ec5072ba96581f7c2a30314c59fb76;p=e-mobility-charging-stations-simulator.git diff --git a/src/charging-station/AutomaticTransactionGenerator.ts b/src/charging-station/AutomaticTransactionGenerator.ts index b32e7948..a2854507 100644 --- a/src/charging-station/AutomaticTransactionGenerator.ts +++ b/src/charging-station/AutomaticTransactionGenerator.ts @@ -5,20 +5,18 @@ import { AuthorizationStatus, AuthorizeResponse, StartTransactionResponse, StopT import ChargingStation from './ChargingStation'; import Constants from '../utils/Constants'; import PerformanceStatistics from '../performance/PerformanceStatistics'; +import { Status } from '../types/AutomaticTransactionGenerator'; import Utils from '../utils/Utils'; import logger from '../utils/Logger'; export default class AutomaticTransactionGenerator { public started: boolean; private chargingStation: ChargingStation; - private connectorsStartStatus: Record; - private startDate!: Date; - private lastRunDate!: Date; - private stopDate!: Date; + private connectorsStatus: Map; constructor(chargingStation: ChargingStation) { this.chargingStation = chargingStation; - this.connectorsStartStatus = {} as Record; + this.connectorsStatus = new Map(); this.stopConnectors(); this.started = false; } @@ -28,15 +26,8 @@ export default class AutomaticTransactionGenerator { logger.error(`${this.logPrefix()} trying to start while already started`); return; } - const previousRunDuration = (this?.startDate && this?.lastRunDate) ? (this.lastRunDate.getTime() - this.startDate.getTime()) : 0; - this.startDate = new Date(); - this.lastRunDate = this.startDate; - this.stopDate = new Date(this.startDate.getTime() - + (this.chargingStation.stationInfo?.AutomaticTransactionGenerator?.stopAfterHours ?? Constants.CHARGING_STATION_ATG_DEFAULT_STOP_AFTER_HOURS) * 3600 * 1000 - - previousRunDuration); this.startConnectors(); this.started = true; - logger.info(this.logPrefix() + ' started and will run for ' + Utils.formatDurationMilliSeconds(this.stopDate.getTime() - this.startDate.getTime())); } public stop(): void { @@ -46,17 +37,13 @@ export default class AutomaticTransactionGenerator { } this.stopConnectors(); this.started = false; - logger.info(`${this.logPrefix()} over and lasted for ${Utils.formatDurationMilliSeconds(this.lastRunDate.getTime() - this.startDate.getTime())}. Stopping all transactions`); } private startConnectors(): void { for (const connector in this.chargingStation.connectors) { const connectorId = Utils.convertToInt(connector); if (connectorId > 0) { - // Avoid hogging the event loop with a busy loop - setImmediate(() => { - this.startConnector(connectorId).catch(() => { /* This is intentional */ }); - }); + this.startConnector(connectorId); } } } @@ -70,32 +57,31 @@ export default class AutomaticTransactionGenerator { } } - private async startConnector(connectorId: number): Promise { - logger.info(this.logPrefix(connectorId) + ' started on connector'); - let skippedTransactions = 0; - let skippedTransactionsTotal = 0; - this.connectorsStartStatus[connectorId] = true; - while (this.connectorsStartStatus[connectorId]) { - if ((new Date()) > this.stopDate) { - this.stop(); + private async internalStartConnector(connectorId: number): Promise { + this.initStartConnectorStatus(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())); + while (this.connectorsStatus.get(connectorId).start) { + if ((new Date()) > this.connectorsStatus.get(connectorId).stopDate) { + this.stopConnector(connectorId); break; } if (!this.chargingStation.isRegistered()) { - logger.error(this.logPrefix(connectorId) + ' Entered in transaction loop while the charging station is not registered'); + logger.error(this.logPrefix(connectorId) + ' entered in transaction loop while the charging station is not registered'); + this.stopConnector(connectorId); break; } if (!this.chargingStation.isChargingStationAvailable()) { - logger.info(this.logPrefix(connectorId) + ' Entered in transaction loop while the charging station is unavailable'); - this.stop(); + logger.info(this.logPrefix(connectorId) + ' entered in transaction loop while the charging station is unavailable'); + this.stopConnector(connectorId); break; } if (!this.chargingStation.isConnectorAvailable(connectorId)) { - logger.info(`${this.logPrefix(connectorId)} Entered in transaction loop while the connector ${connectorId} is unavailable, stop it`); + logger.info(`${this.logPrefix(connectorId)} entered in transaction loop while the connector ${connectorId} is unavailable`); this.stopConnector(connectorId); break; } if (!this.chargingStation?.ocppRequestService) { - logger.info(`${this.logPrefix(connectorId)} Transaction loop waiting for charging station service to be initialized`); + logger.info(`${this.logPrefix(connectorId)} transaction loop waiting for charging station service to be initialized`); do { await Utils.sleep(Constants.CHARGING_STATION_ATG_INITIALIZATION_TIME); } while (!this.chargingStation?.ocppRequestService); @@ -106,35 +92,66 @@ export default class AutomaticTransactionGenerator { await Utils.sleep(wait); const start = Utils.secureRandom(); if (start < this.chargingStation.stationInfo.AutomaticTransactionGenerator.probabilityOfStart) { - skippedTransactions = 0; + this.connectorsStatus.get(connectorId).skippedConsecutiveTransactions = 0; // Start transaction const startResponse = await this.startTransaction(connectorId); + this.connectorsStatus.get(connectorId).startTransactionRequests++; if (startResponse?.idTagInfo?.status !== AuthorizationStatus.ACCEPTED) { - logger.warn(this.logPrefix(connectorId) + ' transaction rejected'); - await Utils.sleep(Constants.CHARGING_STATION_ATG_WAIT_TIME); + logger.warn(this.logPrefix(connectorId) + ' start transaction rejected'); + this.connectorsStatus.get(connectorId).rejectedStartTransactionRequests++; } else { // Wait until end of transaction const waitTrxEnd = Utils.getRandomInteger(this.chargingStation.stationInfo.AutomaticTransactionGenerator.maxDuration, this.chargingStation.stationInfo.AutomaticTransactionGenerator.minDuration) * 1000; - logger.info(this.logPrefix(connectorId) + ' transaction ' + this.chargingStation.getConnector(connectorId).transactionId.toString() + ' will stop in ' + Utils.formatDurationMilliSeconds(waitTrxEnd)); + logger.info(this.logPrefix(connectorId) + ' transaction ' + this.chargingStation.getConnector(connectorId).transactionId.toString() + ' started and will stop in ' + Utils.formatDurationMilliSeconds(waitTrxEnd)); + this.connectorsStatus.get(connectorId).acceptedStartTransactionRequests++; await Utils.sleep(waitTrxEnd); // Stop transaction logger.info(this.logPrefix(connectorId) + ' stop transaction ' + this.chargingStation.getConnector(connectorId).transactionId.toString()); await this.stopTransaction(connectorId); } } else { - skippedTransactions++; - skippedTransactionsTotal++; - logger.info(this.logPrefix(connectorId) + ' skipped transaction ' + skippedTransactions.toString() + '/' + skippedTransactionsTotal.toString()); + 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.lastRunDate = new Date(); + this.connectorsStatus.get(connectorId).lastRunDate = new Date(); } await this.stopTransaction(connectorId); - logger.info(this.logPrefix(connectorId) + ' stopped on connector'); + 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())); + logger.debug(`${this.logPrefix(connectorId)} connector status %j`, this.connectorsStatus.get(connectorId)); + } + + private startConnector(connectorId: number): void { + // Avoid hogging the event loop with a busy loop + setImmediate(() => { + this.internalStartConnector(connectorId).catch(() => { /* This is intentional */ }); + }); } private stopConnector(connectorId: number): void { - this.connectorsStartStatus[connectorId] = false; + this.connectorsStatus.set(connectorId, { ...this.connectorsStatus.get(connectorId), start: false }); + } + + private initStartConnectorStatus(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).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) + ? (this.connectorsStatus.get(connectorId).lastRunDate.getTime() - this.connectorsStatus.get(connectorId).startDate.getTime()) + : 0; + this.connectorsStatus.get(connectorId).startDate = new Date(); + this.connectorsStatus.get(connectorId).stopDate = new Date(this.connectorsStatus.get(connectorId).startDate.getTime() + + (this.chargingStation.stationInfo?.AutomaticTransactionGenerator?.stopAfterHours ?? Constants.CHARGING_STATION_ATG_DEFAULT_STOP_AFTER_HOURS) * 3600 * 1000 + - previousRunDuration); + this.connectorsStatus.get(connectorId).start = true; } private async startTransaction(connectorId: number): Promise { @@ -146,13 +163,16 @@ export default class AutomaticTransactionGenerator { if (this.chargingStation.getAutomaticTransactionGeneratorRequireAuthorize()) { // Authorize idTag const authorizeResponse = await this.chargingStation.ocppRequestService.sendAuthorize(connectorId, idTag); + this.connectorsStatus.get(connectorId).authorizeRequests++; if (authorizeResponse?.idTagInfo?.status === AuthorizationStatus.ACCEPTED) { + this.connectorsStatus.get(connectorId).acceptedAuthorizeRequests++; logger.info(this.logPrefix(connectorId) + ' start transaction for idTag ' + idTag); // Start transaction startResponse = await this.chargingStation.ocppRequestService.sendStartTransaction(connectorId, idTag); PerformanceStatistics.endMeasure(measureId, beginId); return startResponse; } + this.connectorsStatus.get(connectorId).rejectedAuthorizeRequests++; PerformanceStatistics.endMeasure(measureId, beginId); return authorizeResponse; } @@ -179,6 +199,7 @@ export default class AutomaticTransactionGenerator { this.chargingStation.getEnergyActiveImportRegisterByTransactionId(transactionId), this.chargingStation.getTransactionIdTag(transactionId), reason); + this.connectorsStatus.get(connectorId).stopTransactionRequests++; } else { logger.warn(`${this.logPrefix(connectorId)} trying to stop a not started transaction${transactionId ? ' ' + transactionId.toString() : ''}`); }