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