Be consistent in variables naming.
[e-mobility-charging-stations-simulator.git] / src / charging-station / AutomaticTransactionGenerator.js
index 6b3e2e76cac1c1f9bfe7f930ec80e596c436d3e1..1bb56425b1a89e88991cf2eb3b7b1ee7800945c0 100644 (file)
@@ -1,11 +1,12 @@
 const logger = require('../utils/Logger');
+const Constants = require('../utils/Constants');
 const Utils = require('../utils/Utils');
 const {performance, PerformanceObserver} = require('perf_hooks');
 
 class AutomaticTransactionGenerator {
   constructor(chargingStation) {
     this._chargingStation = chargingStation;
-    this._timeToStop = false;
+    this._timeToStop = true;
     this._performanceObserver = new PerformanceObserver((list) => {
       const entry = list.getEntries()[0];
       this._chargingStation._statistics.logPerformance(entry, 'AutomaticTransactionGenerator');
@@ -13,43 +14,48 @@ class AutomaticTransactionGenerator {
     });
   }
 
+  get timeToStop() {
+    return this._timeToStop;
+  }
+
   _basicFormatLog(connectorId = null) {
     if (connectorId) {
-      return Utils.basicFormatLog(' ' + this._chargingStation._stationInfo.name + ' ATG on connector #' + connectorId);
+      return Utils.basicFormatLog(' ' + this._chargingStation._stationInfo.name + ' ATG on connector #' + connectorId + ':');
     }
     return Utils.basicFormatLog(' ' + this._chargingStation._stationInfo.name + ' ATG:');
   }
 
-  async stop() {
-    logger.info(this._basicFormatLog() + ' ATG OVER => STOPPING ALL TRANSACTIONS');
-    for (const connector in this._chargingStation._connectors) {
-      if (this._chargingStation._connectors[connector].transactionStarted) {
-        logger.info(this._basicFormatLog(connector) + ' ATG OVER. Stop transaction ' + this._chargingStation._connectors[connector].transactionId);
-        await this._chargingStation.sendStopTransaction(this._chargingStation._connectors[connector].transactionId, connector);
-      }
-    }
-    this._timeToStop = true;
-  }
-
   async start() {
     this._timeToStop = false;
-    if (this._chargingStation._stationInfo.AutomaticTransactionGenerator.stopAutomaticTransactionGeneratorAfterHours &&
-      this._chargingStation._stationInfo.AutomaticTransactionGenerator.stopAutomaticTransactionGeneratorAfterHours > 0) {
-      logger.info(this._basicFormatLog() + ' ATG will stop in ' + Utils.secondstoHHMMSS(this._chargingStation._stationInfo.AutomaticTransactionGenerator.stopAutomaticTransactionGeneratorAfterHours * 3600));
+    if (this._chargingStation._stationInfo.AutomaticTransactionGenerator.stopAfterHours &&
+      this._chargingStation._stationInfo.AutomaticTransactionGenerator.stopAfterHours > 0) {
       setTimeout(() => {
         this.stop();
-      }, this._chargingStation._stationInfo.AutomaticTransactionGenerator.stopAutomaticTransactionGeneratorAfterHours * 3600 * 1000);
+      }, this._chargingStation._stationInfo.AutomaticTransactionGenerator.stopAfterHours * 3600 * 1000);
     }
     for (const connector in this._chargingStation._connectors) {
       if (connector > 0) {
         this.startConnector(connector);
       }
     }
+    logger.info(this._basicFormatLog() + ' ATG started and will stop in ' + Utils.secondstoHHMMSS(this._chargingStation._stationInfo.AutomaticTransactionGenerator.stopAfterHours * 3600));
+  }
+
+  async stop(reason = '') {
+    logger.info(this._basicFormatLog() + ' ATG OVER => STOPPING ALL TRANSACTIONS');
+    for (const connector in this._chargingStation._connectors) {
+      if (this._chargingStation._connectors[connector].transactionStarted) {
+        logger.info(this._basicFormatLog(connector) + ' ATG OVER. Stop transaction ' + this._chargingStation._connectors[connector].transactionId);
+        await this._chargingStation.sendStopTransaction(this._chargingStation._connectors[connector].transactionId, reason);
+      }
+    }
+    this._timeToStop = true;
   }
 
   async startConnector(connectorId) {
     do {
-      const wait = Utils.getRandomInt(this._chargingStation._stationInfo.AutomaticTransactionGenerator.maxDelayBetweenTwoTransaction, this._chargingStation._stationInfo.AutomaticTransactionGenerator.minDelayBetweenTwoTransaction) * 1000;
+      const wait = Utils.getRandomInt(this._chargingStation._stationInfo.AutomaticTransactionGenerator.maxDelayBetweenTwoTransactions,
+          this._chargingStation._stationInfo.AutomaticTransactionGenerator.minDelayBetweenTwoTransactions) * 1000;
       logger.info(this._basicFormatLog(connectorId) + ' wait for ' + Utils.secondstoHHMMSS(wait / 1000));
       await Utils.sleep(wait);
       if (this._timeToStop) break;
@@ -58,16 +64,16 @@ class AutomaticTransactionGenerator {
       if (start < this._chargingStation._stationInfo.AutomaticTransactionGenerator.probabilityOfStart) {
         skip = 0;
         // Start transaction
-        logger.info(this._basicFormatLog(connectorId) + ' start transaction');
         const startTransaction = performance.timerify(this.startTransaction);
         this._performanceObserver.observe({entryTypes: ['function']});
         const startResponse = await startTransaction(connectorId, this);
         if (startResponse.idTagInfo.status !== 'Accepted') {
           logger.info(this._basicFormatLog(connectorId) + ' transaction rejected');
-          await Utils.sleep(2000);
+          await Utils.sleep(Constants.CHARGING_STATION_ATG_WAIT_TIME);
         } else {
           // Wait until end of transaction
-          const wait = Utils.getRandomInt(this._chargingStation._stationInfo.AutomaticTransactionGenerator.maxDuration, this._chargingStation._stationInfo.AutomaticTransactionGenerator.minDuration) * 1000;
+          const wait = Utils.getRandomInt(this._chargingStation._stationInfo.AutomaticTransactionGenerator.maxDuration,
+              this._chargingStation._stationInfo.AutomaticTransactionGenerator.minDuration) * 1000;
           logger.info(this._basicFormatLog(connectorId) + ' transaction ' + this._chargingStation._connectors[connectorId].transactionId + ' will stop in ' + Utils.secondstoHHMMSS(wait / 1000));
           await Utils.sleep(wait);
           // Stop transaction
@@ -83,12 +89,12 @@ class AutomaticTransactionGenerator {
         logger.info(this._basicFormatLog(connectorId) + ' transaction skipped ' + skip);
       }
     } while (!this._timeToStop);
-    logger.info(this._basicFormatLog() + ' ATG is STOPPED');
+    logger.info(this._basicFormatLog(connectorId) + ' ATG STOPPED on the connector');
   }
 
   // eslint-disable-next-line class-methods-use-this
   async startTransaction(connectorId, self) {
-    if (self._chargingStation.isAuthorizationRequested()) {
+    if (self._chargingStation.hasAuthorizedTags()) {
       const tagId = self._chargingStation.getRandomTagId();
       logger.info(self._basicFormatLog(connectorId) + ' start transaction for tagID ' + tagId);
       return self._chargingStation.sendStartTransaction(connectorId, tagId);
@@ -98,7 +104,7 @@ class AutomaticTransactionGenerator {
 
   // eslint-disable-next-line class-methods-use-this
   async stopTransaction(connectorId, self) {
-    await self._chargingStation.sendStopTransaction(self._chargingStation._connectors[connectorId].transactionId, connectorId);
+    await self._chargingStation.sendStopTransaction(self._chargingStation._connectors[connectorId].transactionId);
   }
 }