1 const logger
= require('../utils/Logger');
2 const Utils
= require('../utils/Utils');
3 const {performance
, PerformanceObserver
} = require('perf_hooks');
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();
17 return this._timeToStop
;
20 _basicFormatLog(connectorId
= null) {
22 return Utils
.basicFormatLog(' ' + this._chargingStation
._stationInfo
.name
+ ' ATG on connector #' + connectorId
+ ':');
24 return Utils
.basicFormatLog(' ' + this._chargingStation
._stationInfo
.name
+ ' ATG:');
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
);
35 this._timeToStop
= true;
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));
45 }, this._chargingStation
._stationInfo
.AutomaticTransactionGenerator
.stopAfterHours
* 3600 * 1000);
47 for (const connector
in this._chargingStation
._connectors
) {
49 this.startConnector(connector
);
54 async
startConnector(connectorId
) {
56 const wait
= Utils
.getRandomInt(this._chargingStation
._stationInfo
.AutomaticTransactionGenerator
.maxDelayBetweenTwoTransaction
,
57 this._chargingStation
._stationInfo
.AutomaticTransactionGenerator
.minDelayBetweenTwoTransaction
) * 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();
63 if (start
< this._chargingStation
._stationInfo
.AutomaticTransactionGenerator
.probabilityOfStart
) {
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);
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
);
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);
88 logger
.info(this._basicFormatLog(connectorId
) + ' transaction skipped ' + skip
);
90 } while (!this._timeToStop
);
91 logger
.info(this._basicFormatLog() + ' ATG is STOPPED');
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
);
101 return self
._chargingStation
.sendStartTransaction(connectorId
);
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
);
110 module
.exports
= AutomaticTransactionGenerator
;