refactor: cleanup reservation handling code
[e-mobility-charging-stations-simulator.git] / src / charging-station / AutomaticTransactionGenerator.ts
index 011f1a1594af49893f94799dc5316aad562b263f..0389526fd6a9400a90404903a360d8d607448fd6 100644 (file)
@@ -31,11 +31,15 @@ export class AutomaticTransactionGenerator extends AsyncResource {
 
   public readonly connectorsStatus: Map<number, Status>;
   public started: boolean;
+  private starting: boolean;
+  private stopping: boolean;
   private readonly chargingStation: ChargingStation;
 
   private constructor(chargingStation: ChargingStation) {
     super(moduleName);
     this.started = false;
+    this.starting = false;
+    this.stopping = false;
     this.chargingStation = chargingStation;
     this.connectorsStatus = new Map<number, Status>();
     this.initializeConnectorsStatus();
@@ -63,8 +67,14 @@ export class AutomaticTransactionGenerator extends AsyncResource {
       logger.warn(`${this.logPrefix()} is already started`);
       return;
     }
+    if (this.starting === true) {
+      logger.warn(`${this.logPrefix()} is already starting`);
+      return;
+    }
+    this.starting = true;
     this.startConnectors();
     this.started = true;
+    this.starting = false;
   }
 
   public stop(): void {
@@ -72,8 +82,14 @@ export class AutomaticTransactionGenerator extends AsyncResource {
       logger.warn(`${this.logPrefix()} is already stopped`);
       return;
     }
+    if (this.stopping === true) {
+      logger.warn(`${this.logPrefix()} is already stopping`);
+      return;
+    }
+    this.stopping = true;
     this.stopConnectors();
     this.started = false;
+    this.stopping = false;
   }
 
   public startConnector(connectorId: number): void {
@@ -265,8 +281,8 @@ export class AutomaticTransactionGenerator extends AsyncResource {
           await this.stopTransaction(connectorId);
         }
       } else {
-        this.connectorsStatus.get(connectorId).skippedConsecutiveTransactions++;
-        this.connectorsStatus.get(connectorId).skippedTransactions++;
+        ++this.connectorsStatus.get(connectorId).skippedConsecutiveTransactions;
+        ++this.connectorsStatus.get(connectorId).skippedTransactions;
         logger.info(
           `${this.logPrefix(connectorId)} skipped consecutively ${this.connectorsStatus
             .get(connectorId)
@@ -303,8 +319,7 @@ export class AutomaticTransactionGenerator extends AsyncResource {
     this.connectorsStatus.get(connectorId).startDate = new Date();
     this.connectorsStatus.get(connectorId).stopDate = new Date(
       this.connectorsStatus.get(connectorId).startDate.getTime() +
-        (this.chargingStation.getAutomaticTransactionGeneratorConfiguration().stopAfterHours ??
-          Constants.CHARGING_STATION_ATG_DEFAULT_STOP_AFTER_HOURS) *
+        this.chargingStation.getAutomaticTransactionGeneratorConfiguration().stopAfterHours *
           3600 *
           1000 -
         previousRunDuration
@@ -317,45 +332,45 @@ export class AutomaticTransactionGenerator extends AsyncResource {
       for (const [evseId, evseStatus] of this.chargingStation.evses) {
         if (evseId > 0) {
           for (const connectorId of evseStatus.connectors.keys()) {
-            this.connectorsStatus.set(connectorId, {
-              start: false,
-              authorizeRequests: 0,
-              acceptedAuthorizeRequests: 0,
-              rejectedAuthorizeRequests: 0,
-              startTransactionRequests: 0,
-              acceptedStartTransactionRequests: 0,
-              rejectedStartTransactionRequests: 0,
-              stopTransactionRequests: 0,
-              acceptedStopTransactionRequests: 0,
-              rejectedStopTransactionRequests: 0,
-              skippedConsecutiveTransactions: 0,
-              skippedTransactions: 0,
-            });
+            this.connectorsStatus.set(connectorId, this.getConnectorStatus(connectorId));
           }
         }
       }
     } else {
       for (const connectorId of this.chargingStation.connectors.keys()) {
         if (connectorId > 0) {
-          this.connectorsStatus.set(connectorId, {
-            start: false,
-            authorizeRequests: 0,
-            acceptedAuthorizeRequests: 0,
-            rejectedAuthorizeRequests: 0,
-            startTransactionRequests: 0,
-            acceptedStartTransactionRequests: 0,
-            rejectedStartTransactionRequests: 0,
-            stopTransactionRequests: 0,
-            acceptedStopTransactionRequests: 0,
-            rejectedStopTransactionRequests: 0,
-            skippedConsecutiveTransactions: 0,
-            skippedTransactions: 0,
-          });
+          this.connectorsStatus.set(connectorId, this.getConnectorStatus(connectorId));
         }
       }
     }
   }
 
+  private getConnectorStatus(connectorId: number): Status {
+    const connectorStatus = Utils.cloneObject(
+      this.chargingStation.getAutomaticTransactionGeneratorStatuses()
+    )[connectorId];
+    delete connectorStatus?.startDate;
+    delete connectorStatus?.lastRunDate;
+    delete connectorStatus?.stopDate;
+    delete connectorStatus?.stoppedDate;
+    return (
+      connectorStatus ?? {
+        start: false,
+        authorizeRequests: 0,
+        acceptedAuthorizeRequests: 0,
+        rejectedAuthorizeRequests: 0,
+        startTransactionRequests: 0,
+        acceptedStartTransactionRequests: 0,
+        rejectedStartTransactionRequests: 0,
+        stopTransactionRequests: 0,
+        acceptedStopTransactionRequests: 0,
+        rejectedStopTransactionRequests: 0,
+        skippedConsecutiveTransactions: 0,
+        skippedTransactions: 0,
+      }
+    );
+  }
+
   private async startTransaction(
     connectorId: number
   ): Promise<StartTransactionResponse | undefined> {
@@ -381,9 +396,9 @@ export class AutomaticTransactionGenerator extends AsyncResource {
           >(this.chargingStation, RequestCommand.AUTHORIZE, {
             idTag,
           });
-        this.connectorsStatus.get(connectorId).authorizeRequests++;
+        ++this.connectorsStatus.get(connectorId).authorizeRequests;
         if (authorizeResponse?.idTagInfo?.status === AuthorizationStatus.ACCEPTED) {
-          this.connectorsStatus.get(connectorId).acceptedAuthorizeRequests++;
+          ++this.connectorsStatus.get(connectorId).acceptedAuthorizeRequests;
           logger.info(startTransactionLogMsg);
           // Start transaction
           startResponse = await this.chargingStation.ocppRequestService.requestHandler<
@@ -397,7 +412,7 @@ export class AutomaticTransactionGenerator extends AsyncResource {
           PerformanceStatistics.endMeasure(measureId, beginId);
           return startResponse;
         }
-        this.connectorsStatus.get(connectorId).rejectedAuthorizeRequests++;
+        ++this.connectorsStatus.get(connectorId).rejectedAuthorizeRequests;
         PerformanceStatistics.endMeasure(measureId, beginId);
         return startResponse;
       }
@@ -433,11 +448,11 @@ export class AutomaticTransactionGenerator extends AsyncResource {
     let stopResponse: StopTransactionResponse;
     if (this.chargingStation.getConnectorStatus(connectorId)?.transactionStarted === true) {
       stopResponse = await this.chargingStation.stopTransactionOnConnector(connectorId, reason);
-      this.connectorsStatus.get(connectorId).stopTransactionRequests++;
+      ++this.connectorsStatus.get(connectorId).stopTransactionRequests;
       if (stopResponse?.idTagInfo?.status === AuthorizationStatus.ACCEPTED) {
-        this.connectorsStatus.get(connectorId).acceptedStopTransactionRequests++;
+        ++this.connectorsStatus.get(connectorId).acceptedStopTransactionRequests;
       } else {
-        this.connectorsStatus.get(connectorId).rejectedStopTransactionRequests++;
+        ++this.connectorsStatus.get(connectorId).rejectedStopTransactionRequests;
       }
     } else {
       const transactionId = this.chargingStation.getConnectorStatus(connectorId)?.transactionId;
@@ -469,12 +484,12 @@ export class AutomaticTransactionGenerator extends AsyncResource {
     connectorId: number,
     startResponse: StartTransactionResponse
   ): void {
-    this.connectorsStatus.get(connectorId).startTransactionRequests++;
+    ++this.connectorsStatus.get(connectorId).startTransactionRequests;
     if (startResponse?.idTagInfo?.status === AuthorizationStatus.ACCEPTED) {
-      this.connectorsStatus.get(connectorId).acceptedStartTransactionRequests++;
+      ++this.connectorsStatus.get(connectorId).acceptedStartTransactionRequests;
     } else {
       logger.warn(`${this.logPrefix(connectorId)} start transaction rejected`);
-      this.connectorsStatus.get(connectorId).rejectedStartTransactionRequests++;
+      ++this.connectorsStatus.get(connectorId).rejectedStartTransactionRequests;
     }
   }
 }