X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fcharging-station%2FAutomaticTransactionGenerator.js;h=1551a7140348993a0c1cd927a128c97efa6e5839;hb=8c4da34193ab5407e3989a76360f1d909c21dc76;hp=90576e9e16aaf8e0ea33e6de1ced1efde4b0e56f;hpb=0a60c33c4f6592f6223136704fa4513b68603f2d;p=e-mobility-charging-stations-simulator.git diff --git a/src/charging-station/AutomaticTransactionGenerator.js b/src/charging-station/AutomaticTransactionGenerator.js index 90576e9e..1551a714 100644 --- a/src/charging-station/AutomaticTransactionGenerator.js +++ b/src/charging-station/AutomaticTransactionGenerator.js @@ -1,34 +1,37 @@ -const logger = require('../utils/Logger'); -const Utils = require('../utils/Utils'); -const {performance, PerformanceObserver} = require('perf_hooks'); +import {PerformanceObserver, performance} from 'perf_hooks'; -class AutomaticTransactionGenerator { +import Constants from '../utils/Constants.js'; +import Utils from '../utils/Utils.js'; +import logger from '../utils/Logger.js'; + +export default class AutomaticTransactionGenerator { constructor(chargingStation) { this._chargingStation = chargingStation; this._timeToStop = true; - this._performanceObserver = new PerformanceObserver((list) => { - const entry = list.getEntries()[0]; - this._chargingStation._statistics.logPerformance(entry, 'AutomaticTransactionGenerator'); - this._performanceObserver.disconnect(); - }); + if (this._chargingStation.getEnableStatistics()) { + this._performanceObserver = new PerformanceObserver((list) => { + const entry = list.getEntries()[0]; + this._chargingStation._statistics.logPerformance(entry, 'AutomaticTransactionGenerator'); + this._performanceObserver.disconnect(); + }); + } } get timeToStop() { return this._timeToStop; } - _basicFormatLog(connectorId = null) { + _logPrefix(connectorId = null) { if (connectorId) { - return Utils.basicFormatLog(' ' + this._chargingStation._stationInfo.name + ' ATG on connector #' + connectorId + ':'); + return Utils.logPrefix(' ' + this._chargingStation._stationInfo.name + ' ATG on connector #' + connectorId + ':'); } - return Utils.basicFormatLog(' ' + this._chargingStation._stationInfo.name + ' ATG:'); + return Utils.logPrefix(' ' + this._chargingStation._stationInfo.name + ' ATG:'); } async start() { this._timeToStop = false; if (this._chargingStation._stationInfo.AutomaticTransactionGenerator.stopAfterHours && this._chargingStation._stationInfo.AutomaticTransactionGenerator.stopAfterHours > 0) { - logger.info(this._basicFormatLog() + ' ATG will stop in ' + Utils.secondstoHHMMSS(this._chargingStation._stationInfo.AutomaticTransactionGenerator.stopAfterHours * 3600)); setTimeout(() => { this.stop(); }, this._chargingStation._stationInfo.AutomaticTransactionGenerator.stopAfterHours * 3600 * 1000); @@ -38,14 +41,15 @@ class AutomaticTransactionGenerator { this.startConnector(connector); } } + logger.info(this._logPrefix() + ' ATG started and will stop in ' + Utils.secondstoHHMMSS(this._chargingStation._stationInfo.AutomaticTransactionGenerator.stopAfterHours * 3600)); } - async stop(type = '') { - logger.info(this._basicFormatLog() + ' ATG OVER => STOPPING ALL TRANSACTIONS'); + async stop(reason = '') { + logger.info(this._logPrefix() + ' ATG OVER => STOPPING ALL TRANSACTIONS'); for (const connector in this._chargingStation._connectors) { - if (this._chargingStation._connectors[connector].transactionStarted) { - logger.info(this._basicFormatLog(connector) + ' ATG OVER. Stop transaction ' + this._chargingStation._connectors[connector].transactionId); - await this._chargingStation.sendStopTransaction(this._chargingStation._connectors[connector].transactionId, type ? type + 'Reset' : ''); + if (this._chargingStation.getConnector(connector).transactionStarted) { + logger.info(this._logPrefix(connector) + ' ATG OVER. Stop transaction ' + this._chargingStation.getConnector(connector).transactionId); + await this._chargingStation.sendStopTransaction(this._chargingStation.getConnector(connector).transactionId, reason); } } this._timeToStop = true; @@ -55,47 +59,59 @@ class AutomaticTransactionGenerator { do { const wait = Utils.getRandomInt(this._chargingStation._stationInfo.AutomaticTransactionGenerator.maxDelayBetweenTwoTransactions, this._chargingStation._stationInfo.AutomaticTransactionGenerator.minDelayBetweenTwoTransactions) * 1000; - logger.info(this._basicFormatLog(connectorId) + ' wait for ' + Utils.secondstoHHMMSS(wait / 1000)); + logger.info(this._logPrefix(connectorId) + ' wait for ' + Utils.secondstoHHMMSS(wait / 1000)); await Utils.sleep(wait); - if (this._timeToStop) break; + if (this._timeToStop) { + logger.debug(this._logPrefix(connectorId) + ' Entered in transaction loop while a request to stop it was made'); + break; + } const start = Math.random(); let skip = 0; if (start < this._chargingStation._stationInfo.AutomaticTransactionGenerator.probabilityOfStart) { skip = 0; // Start transaction - const startTransaction = performance.timerify(this.startTransaction); - this._performanceObserver.observe({entryTypes: ['function']}); - const startResponse = await startTransaction(connectorId, this); + let startResponse; + if (this._chargingStation.getEnableStatistics()) { + const startTransaction = performance.timerify(this.startTransaction); + this._performanceObserver.observe({entryTypes: ['function']}); + startResponse = await startTransaction(connectorId, this); + } else { + startResponse = await this.startTransaction(connectorId, this); + } if (startResponse.idTagInfo.status !== 'Accepted') { - logger.info(this._basicFormatLog(connectorId) + ' transaction rejected'); - await Utils.sleep(2000); + logger.info(this._logPrefix(connectorId) + ' transaction rejected'); + await Utils.sleep(Constants.CHARGING_STATION_ATG_WAIT_TIME); } else { // Wait until end of transaction const wait = Utils.getRandomInt(this._chargingStation._stationInfo.AutomaticTransactionGenerator.maxDuration, this._chargingStation._stationInfo.AutomaticTransactionGenerator.minDuration) * 1000; - logger.info(this._basicFormatLog(connectorId) + ' transaction ' + this._chargingStation._connectors[connectorId].transactionId + ' will stop in ' + Utils.secondstoHHMMSS(wait / 1000)); + logger.info(this._logPrefix(connectorId) + ' transaction ' + this._chargingStation.getConnector(connectorId).transactionId + ' will stop in ' + Utils.secondstoHHMMSS(wait / 1000)); await Utils.sleep(wait); // Stop transaction - if (this._chargingStation._connectors[connectorId].transactionStarted) { - logger.info(this._basicFormatLog(connectorId) + ' stop transaction ' + this._chargingStation._connectors[connectorId].transactionId); - const stopTransaction = performance.timerify(this.stopTransaction); - this._performanceObserver.observe({entryTypes: ['function']}); - await stopTransaction(connectorId, this); + if (this._chargingStation.getConnector(connectorId).transactionStarted) { + logger.info(this._logPrefix(connectorId) + ' stop transaction ' + this._chargingStation.getConnector(connectorId).transactionId); + if (this._chargingStation.getEnableStatistics()) { + const stopTransaction = performance.timerify(this.stopTransaction); + this._performanceObserver.observe({entryTypes: ['function']}); + await stopTransaction(connectorId, this); + } else { + await this.stopTransaction(connectorId, this); + } } } } else { skip++; - logger.info(this._basicFormatLog(connectorId) + ' transaction skipped ' + skip); + logger.info(this._logPrefix(connectorId) + ' transaction skipped ' + skip); } } while (!this._timeToStop); - logger.info(this._basicFormatLog(connectorId) + ' ATG STOPPED on the connector'); + logger.info(this._logPrefix(connectorId) + ' ATG STOPPED on the connector'); } // eslint-disable-next-line class-methods-use-this async startTransaction(connectorId, self) { if (self._chargingStation.hasAuthorizedTags()) { const tagId = self._chargingStation.getRandomTagId(); - logger.info(self._basicFormatLog(connectorId) + ' start transaction for tagID ' + tagId); + logger.info(self._logPrefix(connectorId) + ' start transaction for tagID ' + tagId); return self._chargingStation.sendStartTransaction(connectorId, tagId); } return self._chargingStation.sendStartTransaction(connectorId); @@ -103,8 +119,6 @@ class AutomaticTransactionGenerator { // eslint-disable-next-line class-methods-use-this async stopTransaction(connectorId, self) { - await self._chargingStation.sendStopTransaction(self._chargingStation._connectors[connectorId].transactionId); + await self._chargingStation.sendStopTransaction(self._chargingStation.getConnector(connectorId).transactionId); } } - -module.exports = AutomaticTransactionGenerator;