Allow to disable statistics at the template level.
[e-mobility-charging-stations-simulator.git] / src / charging-station / AutomaticTransactionGenerator.js
index ed7cf3ce5e841a7309568c5385eda6e15324abf6..23b247d353b1a37a1c1f2bb041069464921a5864 100644 (file)
@@ -7,11 +7,13 @@ class AutomaticTransactionGenerator {
   constructor(chargingStation) {
     this._chargingStation = chargingStation;
     this._timeToStop = true;
-    this._performanceObserver = new PerformanceObserver((list) => {
-      const entry = list.getEntries()[0];
-      this._chargingStation._statistics.logPerformance(entry, 'AutomaticTransactionGenerator');
-      this._performanceObserver.disconnect();
-    });
+    if (this._chargingStation.getEnableStatistics()) {
+      this._performanceObserver = new PerformanceObserver((list) => {
+        const entry = list.getEntries()[0];
+        this._chargingStation._statistics.logPerformance(entry, 'AutomaticTransactionGenerator');
+        this._performanceObserver.disconnect();
+      });
+    }
   }
 
   get timeToStop() {
@@ -44,9 +46,9 @@ class AutomaticTransactionGenerator {
   async stop(reason = '') {
     logger.info(this._logPrefix() + ' ATG OVER => STOPPING ALL TRANSACTIONS');
     for (const connector in this._chargingStation._connectors) {
-      if (this._chargingStation._getConnector(connector).transactionStarted) {
-        logger.info(this._logPrefix(connector) + ' ATG OVER. Stop transaction ' + this._chargingStation._getConnector(connector).transactionId);
-        await this._chargingStation.sendStopTransaction(this._chargingStation._getConnector(connector).transactionId, reason);
+      if (this._chargingStation.getConnector(connector).transactionStarted) {
+        logger.info(this._logPrefix(connector) + ' ATG OVER. Stop transaction ' + this._chargingStation.getConnector(connector).transactionId);
+        await this._chargingStation.sendStopTransaction(this._chargingStation.getConnector(connector).transactionId, reason);
       }
     }
     this._timeToStop = true;
@@ -58,15 +60,23 @@ class AutomaticTransactionGenerator {
           this._chargingStation._stationInfo.AutomaticTransactionGenerator.minDelayBetweenTwoTransactions) * 1000;
       logger.info(this._logPrefix(connectorId) + ' wait for ' + Utils.secondstoHHMMSS(wait / 1000));
       await Utils.sleep(wait);
-      if (this._timeToStop) break;
+      if (this._timeToStop) {
+        logger.debug(this._logPrefix(connectorId) + ' Entered in transaction loop while a request to stop it was made');
+        break;
+      }
       const start = Math.random();
       let skip = 0;
       if (start < this._chargingStation._stationInfo.AutomaticTransactionGenerator.probabilityOfStart) {
         skip = 0;
         // Start transaction
-        const startTransaction = performance.timerify(this.startTransaction);
-        this._performanceObserver.observe({entryTypes: ['function']});
-        const startResponse = await startTransaction(connectorId, this);
+        let startResponse;
+        if (this._chargingStation.getEnableStatistics()) {
+          const startTransaction = performance.timerify(this.startTransaction);
+          this._performanceObserver.observe({entryTypes: ['function']});
+          startResponse = await startTransaction(connectorId, this);
+        } else {
+          startResponse = await this.startTransaction(connectorId, this);
+        }
         if (startResponse.idTagInfo.status !== 'Accepted') {
           logger.info(this._logPrefix(connectorId) + ' transaction rejected');
           await Utils.sleep(Constants.CHARGING_STATION_ATG_WAIT_TIME);
@@ -74,14 +84,18 @@ class AutomaticTransactionGenerator {
           // Wait until end of transaction
           const wait = Utils.getRandomInt(this._chargingStation._stationInfo.AutomaticTransactionGenerator.maxDuration,
               this._chargingStation._stationInfo.AutomaticTransactionGenerator.minDuration) * 1000;
-          logger.info(this._logPrefix(connectorId) + ' transaction ' + this._chargingStation._getConnector(connectorId).transactionId + ' will stop in ' + Utils.secondstoHHMMSS(wait / 1000));
+          logger.info(this._logPrefix(connectorId) + ' transaction ' + this._chargingStation.getConnector(connectorId).transactionId + ' will stop in ' + Utils.secondstoHHMMSS(wait / 1000));
           await Utils.sleep(wait);
           // Stop transaction
-          if (this._chargingStation._getConnector(connectorId).transactionStarted) {
-            logger.info(this._logPrefix(connectorId) + ' stop transaction ' + this._chargingStation._getConnector(connectorId).transactionId);
-            const stopTransaction = performance.timerify(this.stopTransaction);
-            this._performanceObserver.observe({entryTypes: ['function']});
-            await stopTransaction(connectorId, this);
+          if (this._chargingStation.getConnector(connectorId).transactionStarted) {
+            logger.info(this._logPrefix(connectorId) + ' stop transaction ' + this._chargingStation.getConnector(connectorId).transactionId);
+            if (this._chargingStation.getEnableStatistics()) {
+              const stopTransaction = performance.timerify(this.stopTransaction);
+              this._performanceObserver.observe({entryTypes: ['function']});
+              await stopTransaction(connectorId, this);
+            } else {
+              await this.stopTransaction(connectorId, this);
+            }
           }
         }
       } else {
@@ -104,7 +118,7 @@ class AutomaticTransactionGenerator {
 
   // eslint-disable-next-line class-methods-use-this
   async stopTransaction(connectorId, self) {
-    await self._chargingStation.sendStopTransaction(self._chargingStation._getConnector(connectorId).transactionId);
+    await self._chargingStation.sendStopTransaction(self._chargingStation.getConnector(connectorId).transactionId);
   }
 }