X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fcharging-station%2FAutomaticTransactionGenerator.js;h=ed7cf3ce5e841a7309568c5385eda6e15324abf6;hb=d182a67ab98336e0b047620f52097e948b3cf70e;hp=ebb0a8e0a5d17baf73bb1de4733e8717559bc2b8;hpb=d3a7883e3b5f20baee6fff45ee599341300d7564;p=e-mobility-charging-stations-simulator.git diff --git a/src/charging-station/AutomaticTransactionGenerator.js b/src/charging-station/AutomaticTransactionGenerator.js index ebb0a8e0..ed7cf3ce 100644 --- a/src/charging-station/AutomaticTransactionGenerator.js +++ b/src/charging-station/AutomaticTransactionGenerator.js @@ -1,4 +1,5 @@ const logger = require('../utils/Logger'); +const Constants = require('../utils/Constants'); const Utils = require('../utils/Utils'); const {performance, PerformanceObserver} = require('perf_hooks'); @@ -17,29 +18,17 @@ class AutomaticTransactionGenerator { 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:'); - } - - async stop() { - logger.info(this._basicFormatLog() + ' 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); - } - } - this._timeToStop = true; + 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); @@ -49,13 +38,25 @@ 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(reason = '') { + logger.info(this._logPrefix() + ' ATG OVER => STOPPING ALL TRANSACTIONS'); + for (const connector in this._chargingStation._connectors) { + 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; } async startConnector(connectorId) { 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; const start = Math.random(); @@ -67,17 +68,17 @@ class AutomaticTransactionGenerator { this._performanceObserver.observe({entryTypes: ['function']}); const startResponse = await 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); + if (this._chargingStation._getConnector(connectorId).transactionStarted) { + logger.info(this._logPrefix(connectorId) + ' stop transaction ' + this._chargingStation._getConnector(connectorId).transactionId); const stopTransaction = performance.timerify(this.stopTransaction); this._performanceObserver.observe({entryTypes: ['function']}); await stopTransaction(connectorId, this); @@ -85,17 +86,17 @@ class AutomaticTransactionGenerator { } } 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,7 +104,7 @@ 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); } }