README.md: Refine list of OCPP versions
[e-mobility-charging-stations-simulator.git] / src / charging-station / AutomaticTransactionGenerator.ts
index 46c595f99441643c3d184ba7f83553647c01d44f..80c9a5176937a035dccc699fc39e6e6b2859a88d 100644 (file)
@@ -1,4 +1,4 @@
-// Partial Copyright Jerome Benoit. 2021. All Rights Reserved.
+// Partial Copyright Jerome Benoit. 2021-2023. All Rights Reserved.
 
 import { AsyncResource } from 'async_hooks';
 
@@ -69,6 +69,9 @@ export default class AutomaticTransactionGenerator extends AsyncResource {
   }
 
   public start(): void {
+    if (this.checkChargingStation() === false) {
+      return;
+    }
     if (this.started === true) {
       logger.warn(`${this.logPrefix()} is already started`);
       return;
@@ -87,16 +90,24 @@ export default class AutomaticTransactionGenerator extends AsyncResource {
   }
 
   public startConnector(connectorId: number): void {
+    if (this.checkChargingStation(connectorId) === false) {
+      return;
+    }
     if (this.connectorsStatus.has(connectorId) === false) {
       logger.error(`${this.logPrefix(connectorId)} starting on non existing connector`);
       throw new BaseError(`Connector ${connectorId} does not exist`);
     }
     if (this.connectorsStatus.get(connectorId)?.start === false) {
       this.runInAsyncScope(
-        this.internalStartConnector.bind(this) as (this: this, ...args: any[]) => unknown,
+        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`);
     }
@@ -138,8 +149,7 @@ export default class AutomaticTransactionGenerator extends AsyncResource {
   }
 
   private async internalStartConnector(connectorId: number): Promise<void> {
-    this.setConnectorStatus(connectorId);
-    this.connectorsStatus.get(connectorId).start = true;
+    this.setStartConnectorStatus(connectorId);
     logger.info(
       this.logPrefix(connectorId) +
         ' started on connector and will run for ' +
@@ -153,7 +163,7 @@ export default class AutomaticTransactionGenerator extends AsyncResource {
         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'
@@ -161,7 +171,7 @@ export default class AutomaticTransactionGenerator extends AsyncResource {
         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'
@@ -169,7 +179,7 @@ export default class AutomaticTransactionGenerator extends AsyncResource {
         this.stopConnector(connectorId);
         break;
       }
-      if (!this.chargingStation.isConnectorAvailable(connectorId)) {
+      if (this.chargingStation.isConnectorAvailable(connectorId) === false) {
         logger.info(
           `${this.logPrefix(
             connectorId
@@ -252,28 +262,8 @@ export default class AutomaticTransactionGenerator extends AsyncResource {
     );
   }
 
-  private setConnectorStatus(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
@@ -289,8 +279,7 @@ export default class AutomaticTransactionGenerator extends AsyncResource {
           1000 -
         previousRunDuration
     );
-    this.connectorsStatus.get(connectorId).start =
-      this?.connectorsStatus.get(connectorId)?.start ?? false;
+    this.connectorsStatus.get(connectorId).start = true;
   }
 
   private initializeConnectorsStatus(): void {
@@ -464,4 +453,12 @@ export default class AutomaticTransactionGenerator extends AsyncResource {
       this.connectorsStatus.get(connectorId).rejectedStartTransactionRequests++;
     }
   }
+
+  private checkChargingStation(connectorId?: number): boolean {
+    if (this.chargingStation.started === false && this.chargingStation.starting === false) {
+      logger.warn(`${this.logPrefix(connectorId)} charging station is stopped, cannot proceed`);
+      return false;
+    }
+    return true;
+  }
 }