| 1 | const logger = require('../utils/Logger'); |
| 2 | const Utils = require('../utils/Utils'); |
| 3 | const {performance, PerformanceObserver} = require('perf_hooks'); |
| 4 | |
| 5 | class AutomaticTransactionGenerator { |
| 6 | constructor(chargingStation) { |
| 7 | this._chargingStation = chargingStation; |
| 8 | this._timeToStop = true; |
| 9 | this._performanceObserver = new PerformanceObserver((list) => { |
| 10 | const entry = list.getEntries()[0]; |
| 11 | this._chargingStation._statistics.logPerformance(entry, 'AutomaticTransactionGenerator'); |
| 12 | this._performanceObserver.disconnect(); |
| 13 | }); |
| 14 | } |
| 15 | |
| 16 | get timeToStop() { |
| 17 | return this._timeToStop; |
| 18 | } |
| 19 | |
| 20 | _basicFormatLog(connectorId = null) { |
| 21 | if (connectorId) { |
| 22 | return Utils.basicFormatLog(' ' + this._chargingStation._stationInfo.name + ' ATG on connector #' + connectorId + ':'); |
| 23 | } |
| 24 | return Utils.basicFormatLog(' ' + this._chargingStation._stationInfo.name + ' ATG:'); |
| 25 | } |
| 26 | |
| 27 | async stop() { |
| 28 | logger.info(this._basicFormatLog() + ' ATG OVER => STOPPING ALL TRANSACTIONS'); |
| 29 | for (const connector in this._chargingStation._connectors) { |
| 30 | if (this._chargingStation._connectors[connector].transactionStarted) { |
| 31 | logger.info(this._basicFormatLog(connector) + ' ATG OVER. Stop transaction ' + this._chargingStation._connectors[connector].transactionId); |
| 32 | await this._chargingStation.sendStopTransaction(this._chargingStation._connectors[connector].transactionId, connector); |
| 33 | } |
| 34 | } |
| 35 | this._timeToStop = true; |
| 36 | } |
| 37 | |
| 38 | async start() { |
| 39 | this._timeToStop = false; |
| 40 | if (this._chargingStation._stationInfo.AutomaticTransactionGenerator.stopAfterHours && |
| 41 | this._chargingStation._stationInfo.AutomaticTransactionGenerator.stopAfterHours > 0) { |
| 42 | logger.info(this._basicFormatLog() + ' ATG will stop in ' + Utils.secondstoHHMMSS(this._chargingStation._stationInfo.AutomaticTransactionGenerator.stopAfterHours * 3600)); |
| 43 | setTimeout(() => { |
| 44 | this.stop(); |
| 45 | }, this._chargingStation._stationInfo.AutomaticTransactionGenerator.stopAfterHours * 3600 * 1000); |
| 46 | } |
| 47 | for (const connector in this._chargingStation._connectors) { |
| 48 | if (connector > 0) { |
| 49 | this.startConnector(connector); |
| 50 | } |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | async startConnector(connectorId) { |
| 55 | do { |
| 56 | const wait = Utils.getRandomInt(this._chargingStation._stationInfo.AutomaticTransactionGenerator.maxDelayBetweenTwoTransactions, |
| 57 | this._chargingStation._stationInfo.AutomaticTransactionGenerator.minDelayBetweenTwoTransactions) * 1000; |
| 58 | logger.info(this._basicFormatLog(connectorId) + ' wait for ' + Utils.secondstoHHMMSS(wait / 1000)); |
| 59 | await Utils.sleep(wait); |
| 60 | if (this._timeToStop) break; |
| 61 | const start = Math.random(); |
| 62 | let skip = 0; |
| 63 | if (start < this._chargingStation._stationInfo.AutomaticTransactionGenerator.probabilityOfStart) { |
| 64 | skip = 0; |
| 65 | // Start transaction |
| 66 | const startTransaction = performance.timerify(this.startTransaction); |
| 67 | this._performanceObserver.observe({entryTypes: ['function']}); |
| 68 | const startResponse = await startTransaction(connectorId, this); |
| 69 | if (startResponse.idTagInfo.status !== 'Accepted') { |
| 70 | logger.info(this._basicFormatLog(connectorId) + ' transaction rejected'); |
| 71 | await Utils.sleep(2000); |
| 72 | } else { |
| 73 | // Wait until end of transaction |
| 74 | const wait = Utils.getRandomInt(this._chargingStation._stationInfo.AutomaticTransactionGenerator.maxDuration, |
| 75 | this._chargingStation._stationInfo.AutomaticTransactionGenerator.minDuration) * 1000; |
| 76 | logger.info(this._basicFormatLog(connectorId) + ' transaction ' + this._chargingStation._connectors[connectorId].transactionId + ' will stop in ' + Utils.secondstoHHMMSS(wait / 1000)); |
| 77 | await Utils.sleep(wait); |
| 78 | // Stop transaction |
| 79 | if (this._chargingStation._connectors[connectorId].transactionStarted) { |
| 80 | logger.info(this._basicFormatLog(connectorId) + ' stop transaction ' + this._chargingStation._connectors[connectorId].transactionId); |
| 81 | const stopTransaction = performance.timerify(this.stopTransaction); |
| 82 | this._performanceObserver.observe({entryTypes: ['function']}); |
| 83 | await stopTransaction(connectorId, this); |
| 84 | } |
| 85 | } |
| 86 | } else { |
| 87 | skip++; |
| 88 | logger.info(this._basicFormatLog(connectorId) + ' transaction skipped ' + skip); |
| 89 | } |
| 90 | } while (!this._timeToStop); |
| 91 | logger.info(this._basicFormatLog() + ' ATG is STOPPED'); |
| 92 | } |
| 93 | |
| 94 | // eslint-disable-next-line class-methods-use-this |
| 95 | async startTransaction(connectorId, self) { |
| 96 | if (self._chargingStation.hasAuthorizedTags()) { |
| 97 | const tagId = self._chargingStation.getRandomTagId(); |
| 98 | logger.info(self._basicFormatLog(connectorId) + ' start transaction for tagID ' + tagId); |
| 99 | return self._chargingStation.sendStartTransaction(connectorId, tagId); |
| 100 | } |
| 101 | return self._chargingStation.sendStartTransaction(connectorId); |
| 102 | } |
| 103 | |
| 104 | // eslint-disable-next-line class-methods-use-this |
| 105 | async stopTransaction(connectorId, self) { |
| 106 | await self._chargingStation.sendStopTransaction(self._chargingStation._connectors[connectorId].transactionId, connectorId); |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | module.exports = AutomaticTransactionGenerator; |