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
= false;
9 this._performanceObserver
= new PerformanceObserver((list
) => {
10 const entry
= list
.getEntries()[0];
11 this._chargingStation
._statistics
.logPerformance(entry
, 'AutomaticTransactionGenerator');
12 this._performanceObserver
.disconnect();
16 _basicFormatLog(connectorId
= null) {
18 return Utils
.basicFormatLog(' ' + this._chargingStation
._stationInfo
.name
+ ' ATG on connector #' + connectorId
);
20 return Utils
.basicFormatLog(' ' + this._chargingStation
._stationInfo
.name
+ ' ATG:');
24 logger
.info(this._basicFormatLog() + ' ATG OVER => STOPPING ALL TRANSACTIONS');
25 for (const connector
in this._chargingStation
._connectors
) {
26 if (this._chargingStation
._connectors
[connector
].transactionStarted
) {
27 logger
.info(this._basicFormatLog(connector
) + ' ATG OVER. Stop transaction ' + this._chargingStation
._connectors
[connector
].transactionId
);
28 await
this._chargingStation
.sendStopTransaction(this._chargingStation
._connectors
[connector
].transactionId
, connector
);
31 this._timeToStop
= true;
35 this._timeToStop
= false;
36 if (this._chargingStation
._stationInfo
.AutomaticTransactionGenerator
.stopAutomaticTransactionGeneratorAfterHours
&&
37 this._chargingStation
._stationInfo
.AutomaticTransactionGenerator
.stopAutomaticTransactionGeneratorAfterHours
> 0) {
38 logger
.info(this._basicFormatLog() + ' ATG will stop in ' + Utils
.secondstoHHMMSS(this._chargingStation
._stationInfo
.AutomaticTransactionGenerator
.stopAutomaticTransactionGeneratorAfterHours
* 3600));
41 }, this._chargingStation
._stationInfo
.AutomaticTransactionGenerator
.stopAutomaticTransactionGeneratorAfterHours
* 3600 * 1000);
43 for (const connector
in this._chargingStation
._connectors
) {
45 this.startConnector(connector
);
50 async
startConnector(connectorId
) {
52 const wait
= Utils
.getRandomInt(this._chargingStation
._stationInfo
.AutomaticTransactionGenerator
.maxDelayBetweenTwoTransaction
, this._chargingStation
._stationInfo
.AutomaticTransactionGenerator
.minDelayBetweenTwoTransaction
) * 1000;
53 logger
.info(this._basicFormatLog(connectorId
) + ' wait for ' + Utils
.secondstoHHMMSS(wait
/ 1000));
54 await Utils
.sleep(wait
);
55 if (this._timeToStop
) break;
56 const start
= Math
.random();
58 if (start
< this._chargingStation
._stationInfo
.AutomaticTransactionGenerator
.probabilityOfStart
) {
61 logger
.info(this._basicFormatLog(connectorId
) + ' start transaction');
62 const startTransaction
= performance
.timerify(this.startTransaction
);
63 this._performanceObserver
.observe({entryTypes
: ['function']});
64 const startResponse
= await
startTransaction(connectorId
, this);
65 if (startResponse
.idTagInfo
.status
!== 'Accepted') {
66 logger
.info(this._basicFormatLog(connectorId
) + ' transaction rejected');
67 await Utils
.sleep(2000);
69 // Wait until end of transaction
70 const wait
= Utils
.getRandomInt(this._chargingStation
._stationInfo
.AutomaticTransactionGenerator
.maxDuration
, this._chargingStation
._stationInfo
.AutomaticTransactionGenerator
.minDuration
) * 1000;
71 logger
.info(this._basicFormatLog(connectorId
) + ' transaction ' + this._chargingStation
._connectors
[connectorId
].transactionId
+ ' will stop in ' + Utils
.secondstoHHMMSS(wait
/ 1000));
72 await Utils
.sleep(wait
);
74 if (this._chargingStation
._connectors
[connectorId
].transactionStarted
) {
75 logger
.info(this._basicFormatLog(connectorId
) + ' stop transaction ' + this._chargingStation
._connectors
[connectorId
].transactionId
);
76 const stopTransaction
= performance
.timerify(this.stopTransaction
);
77 this._performanceObserver
.observe({entryTypes
: ['function']});
78 await
stopTransaction(connectorId
, this);
83 logger
.info(this._basicFormatLog(connectorId
) + ' transaction skipped ' + skip
);
85 } while (!this._timeToStop
);
86 logger
.info(this._basicFormatLog() + ' ATG is STOPPED');
89 // eslint-disable-next-line class-methods-use-this
90 async
startTransaction(connectorId
, self
) {
91 if (self
._chargingStation
.isAuthorizationRequested()) {
92 const tagId
= self
._chargingStation
.getRandomTagId();
93 logger
.info(self
._basicFormatLog(connectorId
) + ' start transaction for tagID ' + tagId
);
94 return self
._chargingStation
.sendStartTransaction(connectorId
, tagId
);
96 return self
._chargingStation
.sendStartTransaction(connectorId
);
99 // eslint-disable-next-line class-methods-use-this
100 async
stopTransaction(connectorId
, self
) {
101 await self
._chargingStation
.sendStopTransaction(self
._chargingStation
._connectors
[connectorId
].transactionId
, connectorId
);
105 module
.exports
= AutomaticTransactionGenerator
;