Improve and unify log messages
[e-mobility-charging-stations-simulator.git] / src / charging-station / AutomaticTransactionGenerator.ts
index e8a4b50f1a1b19a9c72c44d0d0f7117beec6a9e7..03297e4a8c99dafdd375f8714db9717d1e78ebbc 100644 (file)
@@ -8,12 +8,12 @@ import type {
 import { RequestCommand } from '../types/ocpp/Requests';
 import {
   AuthorizationStatus,
-  AuthorizeRequest,
-  AuthorizeResponse,
-  StartTransactionRequest,
-  StartTransactionResponse,
+  type AuthorizeRequest,
+  type AuthorizeResponse,
+  type StartTransactionRequest,
+  type StartTransactionResponse,
   StopTransactionReason,
-  StopTransactionResponse,
+  type StopTransactionResponse,
 } from '../types/ocpp/Transaction';
 import Constants from '../utils/Constants';
 import logger from '../utils/Logger';
@@ -35,11 +35,11 @@ export default class AutomaticTransactionGenerator {
     automaticTransactionGeneratorConfiguration: AutomaticTransactionGeneratorConfiguration,
     chargingStation: ChargingStation
   ) {
+    this.started = false;
     this.configuration = automaticTransactionGeneratorConfiguration;
     this.chargingStation = chargingStation;
     this.connectorsStatus = new Map<number, Status>();
     this.stopConnectors();
-    this.started = false;
   }
 
   public static getInstance(
@@ -60,7 +60,7 @@ export default class AutomaticTransactionGenerator {
 
   public start(): void {
     if (this.started === true) {
-      logger.warn(`${this.logPrefix()} trying to start while already started`);
+      logger.warn(`${this.logPrefix()} is already started`);
       return;
     }
     this.startConnectors();
@@ -69,7 +69,7 @@ export default class AutomaticTransactionGenerator {
 
   public stop(): void {
     if (this.started === false) {
-      logger.warn(`${this.logPrefix()} trying to stop while not started`);
+      logger.warn(`${this.logPrefix()} is already stopped`);
       return;
     }
     this.stopConnectors();
@@ -78,7 +78,7 @@ export default class AutomaticTransactionGenerator {
 
   public startConnector(connectorId: number): void {
     if (this.chargingStation.connectors.has(connectorId) === false) {
-      logger.warn(`${this.logPrefix(connectorId)} trying to start on non existing connector`);
+      logger.warn(`${this.logPrefix(connectorId)} starting on non existing connector`);
       return;
     }
     if (this.connectorsStatus.get(connectorId)?.start === false) {
@@ -89,15 +89,23 @@ export default class AutomaticTransactionGenerator {
         });
       });
     } else if (this.connectorsStatus.get(connectorId)?.start === true) {
-      logger.warn(`${this.logPrefix(connectorId)} already started on connector`);
+      logger.warn(`${this.logPrefix(connectorId)} is already started on connector`);
     }
   }
 
   public stopConnector(connectorId: number): void {
-    this.connectorsStatus.set(connectorId, {
-      ...this.connectorsStatus.get(connectorId),
-      start: false,
-    });
+    if (this.chargingStation.connectors.has(connectorId) === false) {
+      logger.warn(`${this.logPrefix(connectorId)} stopping on non existing connector`);
+      return;
+    }
+    if (this.connectorsStatus.get(connectorId)?.start === true) {
+      this.connectorsStatus.set(connectorId, {
+        ...this.connectorsStatus.get(connectorId),
+        start: false,
+      });
+    } else if (this.connectorsStatus.get(connectorId)?.start === false) {
+      logger.warn(`${this.logPrefix(connectorId)} is already stopped on connector`);
+    }
   }
 
   private startConnectors(): void {
@@ -186,11 +194,7 @@ export default class AutomaticTransactionGenerator {
         this.connectorsStatus.get(connectorId).skippedConsecutiveTransactions = 0;
         // Start transaction
         const startResponse = await this.startTransaction(connectorId);
-        this.connectorsStatus.get(connectorId).startTransactionRequests++;
-        if (startResponse?.idTagInfo?.status !== AuthorizationStatus.ACCEPTED) {
-          logger.warn(this.logPrefix(connectorId) + ' start transaction rejected');
-          this.connectorsStatus.get(connectorId).rejectedStartTransactionRequests++;
-        } else {
+        if (startResponse?.idTagInfo?.status === AuthorizationStatus.ACCEPTED) {
           // Wait until end of transaction
           const waitTrxEnd =
             Utils.getRandomInteger(this.configuration.maxDuration, this.configuration.minDuration) *
@@ -202,7 +206,6 @@ export default class AutomaticTransactionGenerator {
               ' started and will stop in ' +
               Utils.formatDurationMilliSeconds(waitTrxEnd)
           );
-          this.connectorsStatus.get(connectorId).acceptedStartTransactionRequests++;
           await Utils.sleep(waitTrxEnd);
           // Stop transaction
           logger.info(
@@ -226,7 +229,6 @@ export default class AutomaticTransactionGenerator {
       }
       this.connectorsStatus.get(connectorId).lastRunDate = new Date();
     }
-    await this.stopTransaction(connectorId);
     this.connectorsStatus.get(connectorId).stoppedDate = new Date();
     logger.info(
       this.logPrefix(connectorId) +
@@ -257,6 +259,10 @@ export default class AutomaticTransactionGenerator {
       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;
     this.connectorsStatus.get(connectorId).skippedConsecutiveTransactions = 0;
     this.connectorsStatus.get(connectorId).skippedTransactions =
       this?.connectorsStatus.get(connectorId)?.skippedTransactions ?? 0;
@@ -280,7 +286,7 @@ export default class AutomaticTransactionGenerator {
 
   private async startTransaction(
     connectorId: number
-  ): Promise<StartTransactionResponse | AuthorizeResponse> {
+  ): Promise<StartTransactionResponse | undefined> {
     const measureId = 'StartTransaction with ATG';
     const beginId = PerformanceStatistics.beginMeasure(measureId);
     let startResponse: StartTransactionResponse;
@@ -288,7 +294,7 @@ export default class AutomaticTransactionGenerator {
       const idTag = this.chargingStation.getRandomIdTag();
       const startTransactionLogMsg = `${this.logPrefix(
         connectorId
-      )} start transaction for idTag '${idTag}'`;
+      )} start transaction with an idTag '${idTag}'`;
       if (this.getRequireAuthorize()) {
         this.chargingStation.getConnectorStatus(connectorId).authorizeIdTag = idTag;
         // Authorize idTag
@@ -311,12 +317,13 @@ export default class AutomaticTransactionGenerator {
             connectorId,
             idTag,
           });
+          this.handleStartTransactionResponse(connectorId, startResponse);
           PerformanceStatistics.endMeasure(measureId, beginId);
           return startResponse;
         }
         this.connectorsStatus.get(connectorId).rejectedAuthorizeRequests++;
         PerformanceStatistics.endMeasure(measureId, beginId);
-        return authorizeResponse;
+        return startResponse;
       }
       logger.info(startTransactionLogMsg);
       // Start transaction
@@ -327,6 +334,7 @@ export default class AutomaticTransactionGenerator {
         connectorId,
         idTag,
       });
+      this.handleStartTransactionResponse(connectorId, startResponse);
       PerformanceStatistics.endMeasure(measureId, beginId);
       return startResponse;
     }
@@ -335,6 +343,7 @@ export default class AutomaticTransactionGenerator {
       StartTransactionRequest,
       StartTransactionResponse
     >(this.chargingStation, RequestCommand.START_TRANSACTION, { connectorId });
+    this.handleStartTransactionResponse(connectorId, startResponse);
     PerformanceStatistics.endMeasure(measureId, beginId);
     return startResponse;
   }
@@ -346,13 +355,18 @@ export default class AutomaticTransactionGenerator {
     const measureId = 'StopTransaction with ATG';
     const beginId = PerformanceStatistics.beginMeasure(measureId);
     let stopResponse: StopTransactionResponse;
-    if (this.chargingStation.getConnectorStatus(connectorId)?.transactionStarted) {
+    if (this.chargingStation.getConnectorStatus(connectorId)?.transactionStarted === true) {
       stopResponse = await this.chargingStation.stopTransactionOnConnector(connectorId, reason);
       this.connectorsStatus.get(connectorId).stopTransactionRequests++;
+      if (stopResponse?.idTagInfo?.status === AuthorizationStatus.ACCEPTED) {
+        this.connectorsStatus.get(connectorId).acceptedStopTransactionRequests++;
+      } else {
+        this.connectorsStatus.get(connectorId).rejectedStopTransactionRequests++;
+      }
     } else {
       const transactionId = this.chargingStation.getConnectorStatus(connectorId).transactionId;
       logger.warn(
-        `${this.logPrefix(connectorId)} trying to stop a not started transaction${
+        `${this.logPrefix(connectorId)} stopping a not started transaction${
           transactionId ? ' ' + transactionId.toString() : ''
         }`
       );
@@ -372,4 +386,17 @@ export default class AutomaticTransactionGenerator {
       }:`
     );
   }
+
+  private handleStartTransactionResponse(
+    connectorId: number,
+    startResponse: StartTransactionResponse
+  ): void {
+    this.connectorsStatus.get(connectorId).startTransactionRequests++;
+    if (startResponse?.idTagInfo?.status === AuthorizationStatus.ACCEPTED) {
+      this.connectorsStatus.get(connectorId).acceptedStartTransactionRequests++;
+    } else {
+      logger.warn(this.logPrefix(connectorId) + ' start transaction rejected');
+      this.connectorsStatus.get(connectorId).rejectedStartTransactionRequests++;
+    }
+  }
 }