From 9664ec50ce58f2fa682ad687a214d090a95d51f7 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Sat, 25 Sep 2021 22:25:18 +0200 Subject: [PATCH] Push down at the connector level ATG states MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- .../AutomaticTransactionGenerator.ts | 72 ++++++++++--------- src/types/AutomaticTransactionGenerator.ts | 9 +++ 2 files changed, 47 insertions(+), 34 deletions(-) create mode 100644 src/types/AutomaticTransactionGenerator.ts diff --git a/src/charging-station/AutomaticTransactionGenerator.ts b/src/charging-station/AutomaticTransactionGenerator.ts index b32e7948..37cbe1b0 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,7 +37,6 @@ 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 { @@ -71,31 +61,30 @@ 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(); + 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 +95,50 @@ 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); if (startResponse?.idTagInfo?.status !== AuthorizationStatus.ACCEPTED) { - logger.warn(this.logPrefix(connectorId) + ' transaction rejected'); + logger.warn(this.logPrefix(connectorId) + ' start transaction rejected'); await Utils.sleep(Constants.CHARGING_STATION_ATG_WAIT_TIME); } 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)); 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 stopConnector(connectorId: number): void { - this.connectorsStartStatus[connectorId] = false; + this.connectorsStatus.set(connectorId, { start: false }); + } + + private initStartConnectorStatus(connectorId: number): void { + this.connectorsStatus.get(connectorId).skippedConsecutiveTransactions = 0; + 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 { diff --git a/src/types/AutomaticTransactionGenerator.ts b/src/types/AutomaticTransactionGenerator.ts new file mode 100644 index 00000000..e7fe178f --- /dev/null +++ b/src/types/AutomaticTransactionGenerator.ts @@ -0,0 +1,9 @@ +export interface Status { + start?: boolean; + startDate?: Date; + lastRunDate?: Date; + stopDate?: Date; + stoppedDate?: Date; + skippedConsecutiveTransactions?: number; + skippedTransactions?: number; +} -- 2.34.1