Ensure the UUID generator define a value
[e-mobility-charging-stations-simulator.git] / src / charging-station / AutomaticTransactionGenerator.ts
index 9b196ed9d15bdf6080f5eb0fb78991740dd79226..a6d3031c88336801233beaba865ab68bf2c5abe6 100644 (file)
@@ -1,5 +1,7 @@
 // Partial Copyright Jerome Benoit. 2021. All Rights Reserved.
 
+import { AsyncResource } from 'async_hooks';
+
 import BaseError from '../exception/BaseError';
 import PerformanceStatistics from '../performance/PerformanceStatistics';
 import {
@@ -23,7 +25,9 @@ import Utils from '../utils/Utils';
 import type ChargingStation from './ChargingStation';
 import { ChargingStationUtils } from './ChargingStationUtils';
 
-export default class AutomaticTransactionGenerator {
+const moduleName = 'AutomaticTransactionGenerator';
+
+export default class AutomaticTransactionGenerator extends AsyncResource {
   private static readonly instances: Map<string, AutomaticTransactionGenerator> = new Map<
     string,
     AutomaticTransactionGenerator
@@ -39,6 +43,7 @@ export default class AutomaticTransactionGenerator {
     automaticTransactionGeneratorConfiguration: AutomaticTransactionGeneratorConfiguration,
     chargingStation: ChargingStation
   ) {
+    super(moduleName);
     this.started = false;
     this.configuration = automaticTransactionGeneratorConfiguration;
     this.chargingStation = chargingStation;
@@ -87,11 +92,15 @@ export default class AutomaticTransactionGenerator {
       throw new BaseError(`Connector ${connectorId} does not exist`);
     }
     if (this.connectorsStatus.get(connectorId)?.start === false) {
-      // Avoid hogging the event loop with a busy loop
-      setImmediate(() => {
-        this.internalStartConnector(connectorId).catch(() => {
-          /* This is intentional */
-        });
+      this.runInAsyncScope(
+        this.internalStartConnector.bind(this) as (
+          this: AutomaticTransactionGenerator,
+          ...args: any[]
+        ) => Promise<void>,
+        this,
+        connectorId
+      ).catch(() => {
+        /* This is intentional */
       });
     } else if (this.connectorsStatus.get(connectorId)?.start === true) {
       logger.warn(`${this.logPrefix(connectorId)} is already started on connector`);
@@ -134,8 +143,7 @@ export default class AutomaticTransactionGenerator {
   }
 
   private async internalStartConnector(connectorId: number): Promise<void> {
-    this.initializeConnectorStatus(connectorId);
-    this.connectorsStatus.get(connectorId).start = true;
+    this.setStartConnectorStatus(connectorId);
     logger.info(
       this.logPrefix(connectorId) +
         ' started on connector and will run for ' +
@@ -149,7 +157,7 @@ export default class AutomaticTransactionGenerator {
         this.stopConnector(connectorId);
         break;
       }
-      if (!this.chargingStation.isInAcceptedState()) {
+      if (this.chargingStation.isInAcceptedState() === false) {
         logger.error(
           this.logPrefix(connectorId) +
             ' entered in transaction loop while the charging station is not in accepted state'
@@ -157,7 +165,7 @@ export default class AutomaticTransactionGenerator {
         this.stopConnector(connectorId);
         break;
       }
-      if (!this.chargingStation.isChargingStationAvailable()) {
+      if (this.chargingStation.isChargingStationAvailable() === false) {
         logger.info(
           this.logPrefix(connectorId) +
             ' entered in transaction loop while the charging station is unavailable'
@@ -165,7 +173,7 @@ export default class AutomaticTransactionGenerator {
         this.stopConnector(connectorId);
         break;
       }
-      if (!this.chargingStation.isConnectorAvailable(connectorId)) {
+      if (this.chargingStation.isConnectorAvailable(connectorId) === false) {
         logger.info(
           `${this.logPrefix(
             connectorId
@@ -248,28 +256,8 @@ export default class AutomaticTransactionGenerator {
     );
   }
 
-  private initializeConnectorStatus(connectorId: number): void {
-    this.connectorsStatus.get(connectorId).authorizeRequests =
-      this?.connectorsStatus.get(connectorId)?.authorizeRequests ?? 0;
-    this.connectorsStatus.get(connectorId).acceptedAuthorizeRequests =
-      this?.connectorsStatus.get(connectorId)?.acceptedAuthorizeRequests ?? 0;
-    this.connectorsStatus.get(connectorId).rejectedAuthorizeRequests =
-      this?.connectorsStatus.get(connectorId)?.rejectedAuthorizeRequests ?? 0;
-    this.connectorsStatus.get(connectorId).startTransactionRequests =
-      this?.connectorsStatus.get(connectorId)?.startTransactionRequests ?? 0;
-    this.connectorsStatus.get(connectorId).acceptedStartTransactionRequests =
-      this?.connectorsStatus.get(connectorId)?.acceptedStartTransactionRequests ?? 0;
-    this.connectorsStatus.get(connectorId).rejectedStartTransactionRequests =
-      this?.connectorsStatus.get(connectorId)?.rejectedStartTransactionRequests ?? 0;
-    this.connectorsStatus.get(connectorId).stopTransactionRequests =
-      this?.connectorsStatus.get(connectorId)?.stopTransactionRequests ?? 0;
-    this.connectorsStatus.get(connectorId).acceptedStopTransactionRequests =
-      this?.connectorsStatus.get(connectorId)?.acceptedStopTransactionRequests ?? 0;
-    this.connectorsStatus.get(connectorId).rejectedStopTransactionRequests =
-      this?.connectorsStatus.get(connectorId)?.rejectedStopTransactionRequests ?? 0;
+  private setStartConnectorStatus(connectorId: number): void {
     this.connectorsStatus.get(connectorId).skippedConsecutiveTransactions = 0;
-    this.connectorsStatus.get(connectorId).skippedTransactions =
-      this?.connectorsStatus.get(connectorId)?.skippedTransactions ?? 0;
     const previousRunDuration =
       this?.connectorsStatus.get(connectorId)?.startDate &&
       this?.connectorsStatus.get(connectorId)?.lastRunDate
@@ -285,8 +273,7 @@ export default class AutomaticTransactionGenerator {
           1000 -
         previousRunDuration
     );
-    this.connectorsStatus.get(connectorId).start =
-      this?.connectorsStatus.get(connectorId)?.start ?? false;
+    this.connectorsStatus.get(connectorId).start = true;
   }
 
   private initializeConnectorsStatus(): void {