X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fcharging-station%2FAutomaticTransactionGenerator.ts;h=76758c14ea1a1cd6e22fbf968105865f98688617;hb=ad2f27c30a5a29e0ce3a6e2be5a3385061ada313;hp=6e9d263cc3c6c6b84eea4e45ef802d607a565a46;hpb=6af9012e5b9ef2ed6f4fe8a9696b40ac0e8da4d0;p=e-mobility-charging-stations-simulator.git diff --git a/src/charging-station/AutomaticTransactionGenerator.ts b/src/charging-station/AutomaticTransactionGenerator.ts index 6e9d263c..76758c14 100644 --- a/src/charging-station/AutomaticTransactionGenerator.ts +++ b/src/charging-station/AutomaticTransactionGenerator.ts @@ -1,3 +1,4 @@ +import { AuthorizationStatus, AuthorizeResponse, StartTransactionResponse, StopTransactionReason, StopTransactionResponse } from '../types/ocpp/1.6/Transaction'; import { PerformanceObserver, performance } from 'perf_hooks'; import ChargingStation from './ChargingStation'; @@ -6,123 +7,145 @@ import Utils from '../utils/Utils'; import logger from '../utils/Logger'; export default class AutomaticTransactionGenerator { - private _chargingStation: ChargingStation; - private _timeToStop: boolean; - private _performanceObserver: PerformanceObserver; + public timeToStop: boolean; + private chargingStation: ChargingStation; + private performanceObserver: PerformanceObserver; constructor(chargingStation: ChargingStation) { - this._chargingStation = chargingStation; - this._timeToStop = true; - if (this._chargingStation.getEnableStatistics()) { - this._performanceObserver = new PerformanceObserver((list) => { + this.chargingStation = chargingStation; + this.timeToStop = true; + if (this.chargingStation.getEnableStatistics()) { + this.performanceObserver = new PerformanceObserver((list) => { const entry = list.getEntries()[0]; - this._chargingStation.statistics.logPerformance(entry, 'AutomaticTransactionGenerator'); - this._performanceObserver.disconnect(); + this.chargingStation.statistics.logPerformance(entry, Constants.ENTITY_AUTOMATIC_TRANSACTION_GENERATOR); + this.performanceObserver.disconnect(); }); } } - get timeToStop(): boolean { - return this._timeToStop; - } - _logPrefix(connectorId: number = null): string { if (connectorId) { - return Utils.logPrefix(' ' + this._chargingStation.stationInfo.name + ' ATG on connector #' + connectorId + ':'); + return Utils.logPrefix(' ' + this.chargingStation.stationInfo.chargingStationId + ' ATG on connector #' + connectorId.toString() + ':'); } - return Utils.logPrefix(' ' + this._chargingStation.stationInfo.name + ' ATG:'); + return Utils.logPrefix(' ' + this.chargingStation.stationInfo.chargingStationId + ' ATG:'); } start(): void { - this._timeToStop = false; - if (this._chargingStation.stationInfo.AutomaticTransactionGenerator.stopAfterHours && - this._chargingStation.stationInfo.AutomaticTransactionGenerator.stopAfterHours > 0) { + this.timeToStop = false; + if (this.chargingStation.stationInfo.AutomaticTransactionGenerator.stopAfterHours && + this.chargingStation.stationInfo.AutomaticTransactionGenerator.stopAfterHours > 0) { setTimeout(() => { - this.stop(); - }, this._chargingStation.stationInfo.AutomaticTransactionGenerator.stopAfterHours * 3600 * 1000); + void this.stop(); + }, this.chargingStation.stationInfo.AutomaticTransactionGenerator.stopAfterHours * 3600 * 1000); } - for (const connector in this._chargingStation.connectors) { + for (const connector in this.chargingStation.connectors) { if (Utils.convertToInt(connector) > 0) { - this.startConnector(Utils.convertToInt(connector)); + void this.startConnector(Utils.convertToInt(connector)); } } - logger.info(this._logPrefix() + ' ATG started and will stop in ' + Utils.secondstoHHMMSS(this._chargingStation.stationInfo.AutomaticTransactionGenerator.stopAfterHours * 3600)); + logger.info(this._logPrefix() + ' ATG started and will stop in ' + Utils.secondsToHHMMSS(this.chargingStation.stationInfo.AutomaticTransactionGenerator.stopAfterHours * 3600)); } - async stop(reason = ''): Promise { + async stop(reason: StopTransactionReason = StopTransactionReason.NONE): Promise { logger.info(this._logPrefix() + ' ATG OVER => STOPPING ALL TRANSACTIONS'); - for (const connector in this._chargingStation.connectors) { - if (this._chargingStation.getConnector(Utils.convertToInt(connector)).transactionStarted) { - logger.info(this._logPrefix(Utils.convertToInt(connector)) + ' ATG OVER. Stop transaction ' + this._chargingStation.getConnector(Utils.convertToInt(connector)).transactionId); - await this._chargingStation.sendStopTransaction(this._chargingStation.getConnector(Utils.convertToInt(connector)).transactionId, reason); + for (const connector in this.chargingStation.connectors) { + if (this.chargingStation.getConnector(Utils.convertToInt(connector)).transactionStarted) { + logger.info(this._logPrefix(Utils.convertToInt(connector)) + ' ATG OVER. Stop transaction ' + this.chargingStation.getConnector(Utils.convertToInt(connector)).transactionId.toString()); + await this.chargingStation.sendStopTransaction(this.chargingStation.getConnector(Utils.convertToInt(connector)).transactionId, reason); } } - this._timeToStop = true; + this.timeToStop = true; } async startConnector(connectorId: number): Promise { do { - const wait = Utils.getRandomInt(this._chargingStation.stationInfo.AutomaticTransactionGenerator.maxDelayBetweenTwoTransactions, - this._chargingStation.stationInfo.AutomaticTransactionGenerator.minDelayBetweenTwoTransactions) * 1000; - logger.info(this._logPrefix(connectorId) + ' wait for ' + Utils.secondstoHHMMSS(wait / 1000)); - await Utils.sleep(wait); - if (this._timeToStop) { - logger.debug(this._logPrefix(connectorId) + ' Entered in transaction loop while a request to stop it was made'); + if (this.timeToStop) { + logger.error(this._logPrefix(connectorId) + ' Entered in transaction loop while a request to stop it was made'); + break; + } + if (!this.chargingStation._isRegistered()) { + logger.error(this._logPrefix(connectorId) + ' Entered in transaction loop while the charging station is not registered'); + break; + } + if (!this.chargingStation._isChargingStationAvailable()) { + logger.info(this._logPrefix(connectorId) + ' Entered in transaction loop while the charging station is unavailable'); + await this.stop(); + break; + } + if (!this.chargingStation._isConnectorAvailable(connectorId)) { + logger.info(`${this._logPrefix(connectorId)} Entered in transaction loop while the connector ${connectorId} is unavailable, stop it`); break; } + const wait = Utils.getRandomInt(this.chargingStation.stationInfo.AutomaticTransactionGenerator.maxDelayBetweenTwoTransactions, + this.chargingStation.stationInfo.AutomaticTransactionGenerator.minDelayBetweenTwoTransactions) * 1000; + logger.info(this._logPrefix(connectorId) + ' wait for ' + Utils.milliSecondsToHHMMSS(wait)); + await Utils.sleep(wait); const start = Math.random(); let skip = 0; - if (start < this._chargingStation.stationInfo.AutomaticTransactionGenerator.probabilityOfStart) { + if (start < this.chargingStation.stationInfo.AutomaticTransactionGenerator.probabilityOfStart) { skip = 0; // Start transaction - let startResponse; - if (this._chargingStation.getEnableStatistics()) { + let startResponse: StartTransactionResponse | AuthorizeResponse; + if (this.chargingStation.getEnableStatistics()) { const startTransaction = performance.timerify(this.startTransaction); - this._performanceObserver.observe({ entryTypes: ['function'] }); - startResponse = await startTransaction(connectorId); + this.performanceObserver.observe({ entryTypes: ['function'] }); + startResponse = await startTransaction(connectorId, this); } else { - startResponse = await this.startTransaction(connectorId); + startResponse = await this.startTransaction(connectorId, this); } - if (startResponse.idTagInfo.status !== 'Accepted') { + if (startResponse?.idTagInfo?.status !== AuthorizationStatus.ACCEPTED) { logger.info(this._logPrefix(connectorId) + ' transaction rejected'); await Utils.sleep(Constants.CHARGING_STATION_ATG_WAIT_TIME); } else { // Wait until end of transaction - const waitTrxEnd = Utils.getRandomInt(this._chargingStation.stationInfo.AutomaticTransactionGenerator.maxDuration, - this._chargingStation.stationInfo.AutomaticTransactionGenerator.minDuration) * 1000; - logger.info(this._logPrefix(connectorId) + ' transaction ' + this._chargingStation.getConnector(connectorId).transactionId + ' will stop in ' + Utils.secondstoHHMMSS(waitTrxEnd / 1000)); + const waitTrxEnd = Utils.getRandomInt(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.milliSecondsToHHMMSS(waitTrxEnd)); await Utils.sleep(waitTrxEnd); // Stop transaction - if (this._chargingStation.getConnector(connectorId).transactionStarted) { - logger.info(this._logPrefix(connectorId) + ' stop transaction ' + this._chargingStation.getConnector(connectorId).transactionId); - if (this._chargingStation.getEnableStatistics()) { + if (this.chargingStation.getConnector(connectorId)?.transactionStarted) { + logger.info(this._logPrefix(connectorId) + ' stop transaction ' + this.chargingStation.getConnector(connectorId).transactionId.toString()); + if (this.chargingStation.getEnableStatistics()) { const stopTransaction = performance.timerify(this.stopTransaction); - this._performanceObserver.observe({ entryTypes: ['function'] }); - await stopTransaction(connectorId); + this.performanceObserver.observe({ entryTypes: ['function'] }); + await stopTransaction(connectorId, this); } else { - await this.stopTransaction(connectorId); + await this.stopTransaction(connectorId, this); } } } } else { skip++; - logger.info(this._logPrefix(connectorId) + ' transaction skipped ' + skip); + logger.info(this._logPrefix(connectorId) + ' transaction skipped ' + skip.toString()); } - } while (!this._timeToStop); + } while (!this.timeToStop); logger.info(this._logPrefix(connectorId) + ' ATG STOPPED on the connector'); } - async startTransaction(connectorId: number): Promise { - if (this._chargingStation.hasAuthorizedTags()) { - const tagId = this._chargingStation.getRandomTagId(); - logger.info(this._logPrefix(connectorId) + ' start transaction for tagID ' + tagId); - return this._chargingStation.sendStartTransaction(connectorId, tagId); + // eslint-disable-next-line consistent-this + private async startTransaction(connectorId: number, self: AutomaticTransactionGenerator): Promise { + if (self.chargingStation.hasAuthorizedTags()) { + const tagId = self.chargingStation.getRandomTagId(); + if (self.chargingStation.stationInfo.AutomaticTransactionGenerator.requireAuthorize) { + // Authorize tagId + const authorizeResponse = await self.chargingStation.sendAuthorize(tagId); + if (authorizeResponse?.idTagInfo?.status === AuthorizationStatus.ACCEPTED) { + logger.info(self._logPrefix(connectorId) + ' start transaction for tagID ' + tagId); + // Start transaction + return await self.chargingStation.sendStartTransaction(connectorId, tagId); + } + return authorizeResponse; + } + logger.info(self._logPrefix(connectorId) + ' start transaction for tagID ' + tagId); + // Start transaction + return await self.chargingStation.sendStartTransaction(connectorId, tagId); } - logger.info(this._logPrefix(connectorId) + ' start transaction without a tagID'); - return this._chargingStation.sendStartTransaction(connectorId); + logger.info(self._logPrefix(connectorId) + ' start transaction without a tagID'); + return await self.chargingStation.sendStartTransaction(connectorId); } - async stopTransaction(connectorId: number): Promise { - await this._chargingStation.sendStopTransaction(this._chargingStation.getConnector(connectorId).transactionId); + // eslint-disable-next-line consistent-this + private async stopTransaction(connectorId: number, self: AutomaticTransactionGenerator): Promise { + return await self.chargingStation.sendStopTransaction(self.chargingStation.getConnector(connectorId).transactionId); } }