1 const logger
= require('../utils/Logger');
2 const Constants
= require('../utils/Constants');
3 const Utils
= require('../utils/Utils');
4 const {performance
, PerformanceObserver
} = require('perf_hooks');
6 class AutomaticTransactionGenerator
{
7 constructor(chargingStation
) {
8 this._chargingStation
= chargingStation
;
9 this._timeToStop
= true;
10 if (this._chargingStation
.getEnableStatistics()) {
11 this._performanceObserver
= new PerformanceObserver((list
) => {
12 const entry
= list
.getEntries()[0];
13 this._chargingStation
._statistics
.logPerformance(entry
, 'AutomaticTransactionGenerator');
14 this._performanceObserver
.disconnect();
20 return this._timeToStop
;
23 _logPrefix(connectorId
= null) {
25 return Utils
.logPrefix(' ' + this._chargingStation
._stationInfo
.name
+ ' ATG on connector #' + connectorId
+ ':');
27 return Utils
.logPrefix(' ' + this._chargingStation
._stationInfo
.name
+ ' ATG:');
31 this._timeToStop
= false;
32 if (this._chargingStation
._stationInfo
.AutomaticTransactionGenerator
.stopAfterHours
&&
33 this._chargingStation
._stationInfo
.AutomaticTransactionGenerator
.stopAfterHours
> 0) {
36 }, this._chargingStation
._stationInfo
.AutomaticTransactionGenerator
.stopAfterHours
* 3600 * 1000);
38 for (const connector
in this._chargingStation
._connectors
) {
40 this.startConnector(connector
);
43 logger
.info(this._logPrefix() + ' ATG started and will stop in ' + Utils
.secondstoHHMMSS(this._chargingStation
._stationInfo
.AutomaticTransactionGenerator
.stopAfterHours
* 3600));
46 async
stop(reason
= '') {
47 logger
.info(this._logPrefix() + ' ATG OVER => STOPPING ALL TRANSACTIONS');
48 for (const connector
in this._chargingStation
._connectors
) {
49 if (this._chargingStation
.getConnector(connector
).transactionStarted
) {
50 logger
.info(this._logPrefix(connector
) + ' ATG OVER. Stop transaction ' + this._chargingStation
.getConnector(connector
).transactionId
);
51 await
this._chargingStation
.sendStopTransaction(this._chargingStation
.getConnector(connector
).transactionId
, reason
);
54 this._timeToStop
= true;
57 async
startConnector(connectorId
) {
59 const wait
= Utils
.getRandomInt(this._chargingStation
._stationInfo
.AutomaticTransactionGenerator
.maxDelayBetweenTwoTransactions
,
60 this._chargingStation
._stationInfo
.AutomaticTransactionGenerator
.minDelayBetweenTwoTransactions
) * 1000;
61 logger
.info(this._logPrefix(connectorId
) + ' wait for ' + Utils
.secondstoHHMMSS(wait
/ 1000));
62 await Utils
.sleep(wait
);
63 if (this._timeToStop
) {
64 logger
.debug(this._logPrefix(connectorId
) + ' Entered in transaction loop while a request to stop it was made');
67 const start
= Math
.random();
69 if (start
< this._chargingStation
._stationInfo
.AutomaticTransactionGenerator
.probabilityOfStart
) {
73 if (this._chargingStation
.getEnableStatistics()) {
74 const startTransaction
= performance
.timerify(this.startTransaction
);
75 this._performanceObserver
.observe({entryTypes
: ['function']});
76 startResponse
= await
startTransaction(connectorId
, this);
78 startResponse
= await
this.startTransaction(connectorId
, this);
80 if (startResponse
.idTagInfo
.status
!== 'Accepted') {
81 logger
.info(this._logPrefix(connectorId
) + ' transaction rejected');
82 await Utils
.sleep(Constants
.CHARGING_STATION_ATG_WAIT_TIME
);
84 // Wait until end of transaction
85 const wait
= Utils
.getRandomInt(this._chargingStation
._stationInfo
.AutomaticTransactionGenerator
.maxDuration
,
86 this._chargingStation
._stationInfo
.AutomaticTransactionGenerator
.minDuration
) * 1000;
87 logger
.info(this._logPrefix(connectorId
) + ' transaction ' + this._chargingStation
.getConnector(connectorId
).transactionId
+ ' will stop in ' + Utils
.secondstoHHMMSS(wait
/ 1000));
88 await Utils
.sleep(wait
);
90 if (this._chargingStation
.getConnector(connectorId
).transactionStarted
) {
91 logger
.info(this._logPrefix(connectorId
) + ' stop transaction ' + this._chargingStation
.getConnector(connectorId
).transactionId
);
92 if (this._chargingStation
.getEnableStatistics()) {
93 const stopTransaction
= performance
.timerify(this.stopTransaction
);
94 this._performanceObserver
.observe({entryTypes
: ['function']});
95 await
stopTransaction(connectorId
, this);
97 await
this.stopTransaction(connectorId
, this);
103 logger
.info(this._logPrefix(connectorId
) + ' transaction skipped ' + skip
);
105 } while (!this._timeToStop
);
106 logger
.info(this._logPrefix(connectorId
) + ' ATG STOPPED on the connector');
109 // eslint-disable-next-line class-methods-use-this
110 async
startTransaction(connectorId
, self
) {
111 if (self
._chargingStation
.hasAuthorizedTags()) {
112 const tagId
= self
._chargingStation
.getRandomTagId();
113 logger
.info(self
._logPrefix(connectorId
) + ' start transaction for tagID ' + tagId
);
114 return self
._chargingStation
.sendStartTransaction(connectorId
, tagId
);
116 return self
._chargingStation
.sendStartTransaction(connectorId
);
119 // eslint-disable-next-line class-methods-use-this
120 async
stopTransaction(connectorId
, self
) {
121 await self
._chargingStation
.sendStopTransaction(self
._chargingStation
.getConnector(connectorId
).transactionId
);
125 module
.exports
= AutomaticTransactionGenerator
;