Merge pull request #6 from LucasBrazi06/memory-optimization
[e-mobility-charging-stations-simulator.git] / src / charging-station / AutomaticTransactionGenerator.ts
index 6e33ae69132eec2fd5e2e78c7edaec3b7a6f84f0..363605be0e8a19d8d57718c2e3a33a1fe58ae3d0 100644 (file)
@@ -39,12 +39,12 @@ export default class AutomaticTransactionGenerator {
     if (this._chargingStation.stationInfo.AutomaticTransactionGenerator.stopAfterHours &&
       this._chargingStation.stationInfo.AutomaticTransactionGenerator.stopAfterHours > 0) {
       setTimeout(() => {
-        this.stop();
+        void this.stop();
       }, this._chargingStation.stationInfo.AutomaticTransactionGenerator.stopAfterHours * 3600 * 1000);
     }
     for (const connector in this._chargingStation.connectors) {
       if (Utils.convertToInt(connector) > 0) {
-        this.startConnector(Utils.convertToInt(connector));
+        void this.startConnector(Utils.convertToInt(connector));
       }
     }
     logger.info(this._logPrefix() + ' ATG started and will stop in ' + Utils.secondsToHHMMSS(this._chargingStation.stationInfo.AutomaticTransactionGenerator.stopAfterHours * 3600));
@@ -63,14 +63,27 @@ export default class AutomaticTransactionGenerator {
 
   async startConnector(connectorId: number): Promise<void> {
     do {
+      if (this._timeToStop) {
+        logger.error(this._logPrefix(connectorId) + ' Entered in transaction loop while a request to stop it was made');
+        break;
+      }
+      if (!this._chargingStation._isRegistered()) {
+        logger.error(this._logPrefix(connectorId) + ' Entered in transaction loop while the charging station is not registered');
+        break;
+      }
+      if (!this._chargingStation._isChargingStationAvailable()) {
+        logger.info(this._logPrefix(connectorId) + ' Entered in transaction loop while the charging station is unavailable');
+        await this.stop();
+        break;
+      }
+      if (!this._chargingStation._isConnectorAvailable(connectorId)) {
+        logger.info(`${this._logPrefix(connectorId)} Entered in transaction loop while the connector ${connectorId} is unavailable, stop it`);
+        break;
+      }
       const wait = Utils.getRandomInt(this._chargingStation.stationInfo.AutomaticTransactionGenerator.maxDelayBetweenTwoTransactions,
         this._chargingStation.stationInfo.AutomaticTransactionGenerator.minDelayBetweenTwoTransactions) * 1000;
       logger.info(this._logPrefix(connectorId) + ' wait for ' + Utils.milliSecondsToHHMMSS(wait));
       await Utils.sleep(wait);
-      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) {
@@ -84,7 +97,7 @@ export default class AutomaticTransactionGenerator {
         } else {
           startResponse = await this.startTransaction(connectorId, this);
         }
-        if (startResponse.idTagInfo.status !== AuthorizationStatus.ACCEPTED) {
+        if (startResponse?.idTagInfo?.status !== AuthorizationStatus.ACCEPTED) {
           logger.info(this._logPrefix(connectorId) + ' transaction rejected');
           await Utils.sleep(Constants.CHARGING_STATION_ATG_WAIT_TIME);
         } else {
@@ -114,18 +127,25 @@ export default class AutomaticTransactionGenerator {
   }
 
   // eslint-disable-next-line consistent-this
-  async startTransaction(connectorId: number, self: AutomaticTransactionGenerator): Promise<StartTransactionResponse> {
+  private async startTransaction(connectorId: number, self: AutomaticTransactionGenerator): Promise<StartTransactionResponse> {
     if (self._chargingStation.hasAuthorizedTags()) {
       const tagId = self._chargingStation.getRandomTagId();
       logger.info(self._logPrefix(connectorId) + ' start transaction for tagID ' + tagId);
-      return await self._chargingStation.sendStartTransaction(connectorId, tagId);
+      // Authorize tagId
+      const authorizeResponse = await self._chargingStation.sendAuthorize(tagId);
+      if (authorizeResponse?.idTagInfo?.status === AuthorizationStatus.ACCEPTED) {
+        // Start transaction
+        return await self._chargingStation.sendStartTransaction(connectorId, tagId);
+      } else {
+        return authorizeResponse as StartTransactionResponse;
+      }
     }
     logger.info(self._logPrefix(connectorId) + ' start transaction without a tagID');
     return await self._chargingStation.sendStartTransaction(connectorId);
   }
 
   // eslint-disable-next-line consistent-this
-  async stopTransaction(connectorId: number, self: AutomaticTransactionGenerator): Promise<StopTransactionResponse> {
+  private async stopTransaction(connectorId: number, self: AutomaticTransactionGenerator): Promise<StopTransactionResponse> {
     return await self._chargingStation.sendStopTransaction(self._chargingStation.getConnector(connectorId).transactionId);
   }
 }