Fix unit conversion.
[e-mobility-charging-stations-simulator.git] / src / charging-station / AutomaticTransactionGenerator.js
CommitLineData
7dde0b73
JB
1const logger = require('../utils/Logger');
2const Utils = require('../utils/Utils');
3const {performance, PerformanceObserver} = require('perf_hooks');
4
5class AutomaticTransactionGenerator {
6 constructor(chargingStation) {
7 this._chargingStation = chargingStation;
34dcb3b5 8 this._timeToStop = true;
7dde0b73
JB
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
34dcb3b5
JB
16 get timeToStop() {
17 return this._timeToStop;
18 }
19
7dde0b73
JB
20 _basicFormatLog(connectorId = null) {
21 if (connectorId) {
34dcb3b5 22 return Utils.basicFormatLog(' ' + this._chargingStation._stationInfo.name + ' ATG on connector #' + connectorId + ':');
7dde0b73
JB
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);
d3a7883e 32 await this._chargingStation.sendStopTransaction(this._chargingStation._connectors[connector].transactionId);
7dde0b73
JB
33 }
34 }
35 this._timeToStop = true;
36 }
37
38 async start() {
39 this._timeToStop = false;
34dcb3b5
JB
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));
7dde0b73
JB
43 setTimeout(() => {
44 this.stop();
34dcb3b5 45 }, this._chargingStation._stationInfo.AutomaticTransactionGenerator.stopAfterHours * 3600 * 1000);
7dde0b73
JB
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 {
0bbcb3dc
JB
56 const wait = Utils.getRandomInt(this._chargingStation._stationInfo.AutomaticTransactionGenerator.maxDelayBetweenTwoTransactions,
57 this._chargingStation._stationInfo.AutomaticTransactionGenerator.minDelayBetweenTwoTransactions) * 1000;
7dde0b73
JB
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
7dde0b73
JB
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
34dcb3b5
JB
74 const wait = Utils.getRandomInt(this._chargingStation._stationInfo.AutomaticTransactionGenerator.maxDuration,
75 this._chargingStation._stationInfo.AutomaticTransactionGenerator.minDuration) * 1000;
7dde0b73
JB
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);
d3a7883e 91 logger.info(this._basicFormatLog(connectorId) + ' ATG STOPPED on the connector');
7dde0b73
JB
92 }
93
94 // eslint-disable-next-line class-methods-use-this
95 async startTransaction(connectorId, self) {
2e6f5966 96 if (self._chargingStation.hasAuthorizedTags()) {
7dde0b73
JB
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) {
d3a7883e 106 await self._chargingStation.sendStopTransaction(self._chargingStation._connectors[connectorId].transactionId);
7dde0b73
JB
107 }
108}
109
110module.exports = AutomaticTransactionGenerator;