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 this._performanceObserver
= new PerformanceObserver((list
) => {
11 const entry
= list
.getEntries()[0];
12 this._chargingStation
._statistics
.logPerformance(entry
, 'AutomaticTransactionGenerator');
13 this._performanceObserver
.disconnect();
18 return this._timeToStop
;
21 _logPrefix(connectorId
= null) {
23 return Utils
.logPrefix(' ' + this._chargingStation
._stationInfo
.name
+ ' ATG on connector #' + connectorId
+ ':');
25 return Utils
.logPrefix(' ' + this._chargingStation
._stationInfo
.name
+ ' ATG:');
29 this._timeToStop
= false;
30 if (this._chargingStation
._stationInfo
.AutomaticTransactionGenerator
.stopAfterHours
&&
31 this._chargingStation
._stationInfo
.AutomaticTransactionGenerator
.stopAfterHours
> 0) {
34 }, this._chargingStation
._stationInfo
.AutomaticTransactionGenerator
.stopAfterHours
* 3600 * 1000);
36 for (const connector
in this._chargingStation
._connectors
) {
38 this.startConnector(connector
);
41 logger
.info(this._logPrefix() + ' ATG started and will stop in ' + Utils
.secondstoHHMMSS(this._chargingStation
._stationInfo
.AutomaticTransactionGenerator
.stopAfterHours
* 3600));
44 async
stop(reason
= '') {
45 logger
.info(this._logPrefix() + ' ATG OVER => STOPPING ALL TRANSACTIONS');
46 for (const connector
in this._chargingStation
._connectors
) {
47 if (this._chargingStation
._connectors
[connector
].transactionStarted
) {
48 logger
.info(this._logPrefix(connector
) + ' ATG OVER. Stop transaction ' + this._chargingStation
._connectors
[connector
].transactionId
);
49 await
this._chargingStation
.sendStopTransaction(this._chargingStation
._connectors
[connector
].transactionId
, reason
);
52 this._timeToStop
= true;
55 async
startConnector(connectorId
) {
57 const wait
= Utils
.getRandomInt(this._chargingStation
._stationInfo
.AutomaticTransactionGenerator
.maxDelayBetweenTwoTransactions
,
58 this._chargingStation
._stationInfo
.AutomaticTransactionGenerator
.minDelayBetweenTwoTransactions
) * 1000;
59 logger
.info(this._logPrefix(connectorId
) + ' wait for ' + Utils
.secondstoHHMMSS(wait
/ 1000));
60 await Utils
.sleep(wait
);
61 if (this._timeToStop
) break;
62 const start
= Math
.random();
64 if (start
< this._chargingStation
._stationInfo
.AutomaticTransactionGenerator
.probabilityOfStart
) {
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._logPrefix(connectorId
) + ' transaction rejected');
72 await Utils
.sleep(Constants
.CHARGING_STATION_ATG_WAIT_TIME
);
74 // Wait until end of transaction
75 const wait
= Utils
.getRandomInt(this._chargingStation
._stationInfo
.AutomaticTransactionGenerator
.maxDuration
,
76 this._chargingStation
._stationInfo
.AutomaticTransactionGenerator
.minDuration
) * 1000;
77 logger
.info(this._logPrefix(connectorId
) + ' transaction ' + this._chargingStation
._connectors
[connectorId
].transactionId
+ ' will stop in ' + Utils
.secondstoHHMMSS(wait
/ 1000));
78 await Utils
.sleep(wait
);
80 if (this._chargingStation
._connectors
[connectorId
].transactionStarted
) {
81 logger
.info(this._logPrefix(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);
89 logger
.info(this._logPrefix(connectorId
) + ' transaction skipped ' + skip
);
91 } while (!this._timeToStop
);
92 logger
.info(this._logPrefix(connectorId
) + ' ATG STOPPED on the connector');
95 // eslint-disable-next-line class-methods-use-this
96 async
startTransaction(connectorId
, self
) {
97 if (self
._chargingStation
.hasAuthorizedTags()) {
98 const tagId
= self
._chargingStation
.getRandomTagId();
99 logger
.info(self
._logPrefix(connectorId
) + ' start transaction for tagID ' + tagId
);
100 return self
._chargingStation
.sendStartTransaction(connectorId
, tagId
);
102 return self
._chargingStation
.sendStartTransaction(connectorId
);
105 // eslint-disable-next-line class-methods-use-this
106 async
stopTransaction(connectorId
, self
) {
107 await self
._chargingStation
.sendStopTransaction(self
._chargingStation
._connectors
[connectorId
].transactionId
);
111 module
.exports
= AutomaticTransactionGenerator
;