perf: reduce OCPPUtils memory usage
[e-mobility-charging-stations-simulator.git] / src / charging-station / AutomaticTransactionGenerator.ts
index ebfd8c196fd026b0ca673bba3cb5a38bff8a3e9d..12cf7ee4b3fdd495f1dd8bbbbb9b72033e1abbac 100644 (file)
@@ -7,12 +7,11 @@ import { hoursToMilliseconds, secondsToMilliseconds } from 'date-fns';
 import type { ChargingStation } from './ChargingStation';
 import { checkChargingStation } from './Helpers';
 import { IdTagsCache } from './IdTagsCache';
-import { OCPPServiceUtils } from './ocpp';
+import { isIdTagAuthorized } from './ocpp';
 import { BaseError } from '../exception';
 import { PerformanceStatistics } from '../performance';
 import {
   AuthorizationStatus,
-  ConnectorStatusEnum,
   RequestCommand,
   type StartTransactionRequest,
   type StartTransactionResponse,
@@ -305,56 +304,51 @@ export class AutomaticTransactionGenerator extends AsyncResource {
       );
       return false;
     }
-    if (
-      this.chargingStation.getConnectorStatus(connectorId)?.status ===
-      ConnectorStatusEnum.Unavailable
-    ) {
-      logger.info(
-        `${this.logPrefix(
-          connectorId,
-        )} entered in transaction loop while the connector ${connectorId} status is unavailable`,
-      );
-      return false;
-    }
     return true;
   }
 
   private async waitChargingStationServiceInitialization(connectorId: number): Promise<void> {
-    if (!this.chargingStation?.ocppRequestService) {
-      logger.info(
-        `${this.logPrefix(
-          connectorId,
-        )} transaction loop waiting for charging station service to be initialized`,
-      );
-      do {
-        await sleep(Constants.CHARGING_STATION_ATG_INITIALIZATION_TIME);
-      } while (!this.chargingStation?.ocppRequestService);
+    let logged = false;
+    while (!this.chargingStation?.ocppRequestService) {
+      if (!logged) {
+        logger.info(
+          `${this.logPrefix(
+            connectorId,
+          )} transaction loop waiting for charging station service to be initialized`,
+        );
+        logged = true;
+      }
+      await sleep(Constants.CHARGING_STATION_ATG_INITIALIZATION_TIME);
     }
   }
 
   private async waitChargingStationAvailable(connectorId: number): Promise<void> {
-    if (!this.chargingStation.isChargingStationAvailable()) {
-      logger.info(
-        `${this.logPrefix(
-          connectorId,
-        )} transaction loop waiting for charging station to be available`,
-      );
-      do {
-        await sleep(Constants.CHARGING_STATION_ATG_AVAILABILITY_TIME);
-      } while (!this.chargingStation.isChargingStationAvailable());
+    let logged = false;
+    while (!this.chargingStation.isChargingStationAvailable()) {
+      if (!logged) {
+        logger.info(
+          `${this.logPrefix(
+            connectorId,
+          )} transaction loop waiting for charging station to be available`,
+        );
+        logged = true;
+      }
+      await sleep(Constants.CHARGING_STATION_ATG_AVAILABILITY_TIME);
     }
   }
 
   private async waitConnectorAvailable(connectorId: number): Promise<void> {
-    if (!this.chargingStation.isConnectorAvailable(connectorId)) {
-      logger.info(
-        `${this.logPrefix(
-          connectorId,
-        )} transaction loop waiting for connector ${connectorId} to be available`,
-      );
-      do {
-        await sleep(Constants.CHARGING_STATION_ATG_AVAILABILITY_TIME);
-      } while (!this.chargingStation.isConnectorAvailable(connectorId));
+    let logged = false;
+    while (!this.chargingStation.isConnectorAvailable(connectorId)) {
+      if (!logged) {
+        logger.info(
+          `${this.logPrefix(
+            connectorId,
+          )} transaction loop waiting for connector ${connectorId} to be available`,
+        );
+        logged = true;
+      }
+      await sleep(Constants.CHARGING_STATION_ATG_AVAILABILITY_TIME);
     }
   }
 
@@ -404,16 +398,19 @@ export class AutomaticTransactionGenerator extends AsyncResource {
   }
 
   private resetConnectorStatus(connectorStatus: Status | undefined): void {
+    if (connectorStatus === undefined) {
+      return;
+    }
     delete connectorStatus?.startDate;
     delete connectorStatus?.lastRunDate;
     delete connectorStatus?.stopDate;
     delete connectorStatus?.stoppedDate;
     if (
       !this.started &&
-      (connectorStatus?.start === true ||
+      (connectorStatus.start === true ||
         this.chargingStation.getAutomaticTransactionGeneratorConfiguration().enable === false)
     ) {
-      connectorStatus!.start = false;
+      connectorStatus.start = false;
     }
   }
 
@@ -434,7 +431,7 @@ export class AutomaticTransactionGenerator extends AsyncResource {
       )} start transaction with an idTag '${idTag}'`;
       if (this.getRequireAuthorize()) {
         ++this.connectorsStatus.get(connectorId)!.authorizeRequests!;
-        if (await OCPPServiceUtils.isIdTagAuthorized(this.chargingStation, connectorId, idTag)) {
+        if (await isIdTagAuthorized(this.chargingStation, connectorId, idTag)) {
           ++this.connectorsStatus.get(connectorId)!.acceptedAuthorizeRequests!;
           logger.info(startTransactionLogMsg);
           // Start transaction