refactor(simulator): simplify ATG default configuration usage
authorJérôme Benoit <jerome.benoit@sap.com>
Wed, 24 May 2023 21:21:27 +0000 (23:21 +0200)
committerJérôme Benoit <jerome.benoit@sap.com>
Wed, 24 May 2023 21:21:27 +0000 (23:21 +0200)
Signed-off-by: Jérôme Benoit <jerome.benoit@sap.com>
src/charging-station/AutomaticTransactionGenerator.ts
src/charging-station/ChargingStation.ts
src/utils/Constants.ts

index 1499f8fb09e1936848738854c732b9aae2285009..c5076adce7ca9a24f7c25ea50a45a1c916a355cf 100644 (file)
@@ -225,11 +225,9 @@ export class AutomaticTransactionGenerator extends AsyncResource {
       const wait =
         Utils.getRandomInteger(
           this.chargingStation.getAutomaticTransactionGeneratorConfiguration()
-            .maxDelayBetweenTwoTransactions ??
-            Constants.DEFAULT_ATG_MAX_DELAY_BETWEEN_TWO_TRANSACTIONS,
+            .maxDelayBetweenTwoTransactions,
           this.chargingStation.getAutomaticTransactionGeneratorConfiguration()
-            .minDelayBetweenTwoTransactions ??
-            Constants.DEFAULT_ATG_MIN_DELAY_BETWEEN_TWO_TRANSACTIONS
+            .minDelayBetweenTwoTransactions
         ) * 1000;
       logger.info(
         `${this.logPrefix(connectorId)} waiting for ${Utils.formatDurationMilliSeconds(wait)}`
@@ -247,10 +245,8 @@ export class AutomaticTransactionGenerator extends AsyncResource {
           // Wait until end of transaction
           const waitTrxEnd =
             Utils.getRandomInteger(
-              this.chargingStation.getAutomaticTransactionGeneratorConfiguration().maxDuration ??
-                Constants.DEFAULT_ATG_MAX_DURATION,
-              this.chargingStation.getAutomaticTransactionGeneratorConfiguration().minDuration ??
-                Constants.DEFAULT_ATG_MIN_DURATION
+              this.chargingStation.getAutomaticTransactionGeneratorConfiguration().maxDuration,
+              this.chargingStation.getAutomaticTransactionGeneratorConfiguration().minDuration
             ) * 1000;
           logger.info(
             `${this.logPrefix(connectorId)} transaction started with id ${this.chargingStation
@@ -307,8 +303,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.DEFAULT_ATG_STOP_AFTER_HOURS) *
+        this.chargingStation.getAutomaticTransactionGeneratorConfiguration().stopAfterHours *
           3600 *
           1000 -
         previousRunDuration
index a8d96e139d784b55696ac63b362654f974849967..9a0d0f86749c1ad1d00bc029e356427ae6917de6 100644 (file)
@@ -818,12 +818,22 @@ export class ChargingStation {
   public getAutomaticTransactionGeneratorConfiguration():
     | AutomaticTransactionGeneratorConfiguration
     | undefined {
+    let automaticTransactionGeneratorConfiguration:
+      | AutomaticTransactionGeneratorConfiguration
+      | undefined;
     const automaticTransactionGeneratorConfigurationFromFile =
       this.getConfigurationFromFile()?.automaticTransactionGenerator;
     if (automaticTransactionGeneratorConfigurationFromFile) {
-      return automaticTransactionGeneratorConfigurationFromFile;
+      automaticTransactionGeneratorConfiguration =
+        automaticTransactionGeneratorConfigurationFromFile;
+    } else {
+      automaticTransactionGeneratorConfiguration =
+        this.getTemplateFromFile()?.AutomaticTransactionGenerator;
     }
-    return this.getTemplateFromFile()?.AutomaticTransactionGenerator;
+    return {
+      ...automaticTransactionGeneratorConfiguration,
+      ...Constants.DEFAULT_ATG_CONFIGURATION,
+    };
   }
 
   public startAutomaticTransactionGenerator(connectorIds?: number[]): void {
index 6d2a6bf05b269a5da031268819705d88eab584ec..a5cd81502d230e447a96efb4084b66ab59e2af1c 100644 (file)
@@ -8,20 +8,15 @@ export class Constants {
   static readonly CHARGING_STATION_DEFAULT_RESET_TIME = 60000; // Ms
   static readonly CHARGING_STATION_ATG_INITIALIZATION_TIME = 1000; // Ms
 
-  static readonly DEFAULT_ATG_MIN_DURATION = 60;
-  static readonly DEFAULT_ATG_MAX_DURATION = 120;
-  static readonly DEFAULT_ATG_MIN_DELAY_BETWEEN_TWO_TRANSACTIONS = 15;
-  static readonly DEFAULT_ATG_MAX_DELAY_BETWEEN_TWO_TRANSACTIONS = 30;
-  static readonly DEFAULT_ATG_STOP_AFTER_HOURS = 0.25; // Hours
   static readonly DEFAULT_ATG_CONFIGURATION: AutomaticTransactionGeneratorConfiguration =
     Object.freeze({
       enable: false,
-      minDuration: Constants.DEFAULT_ATG_MIN_DURATION,
-      maxDuration: Constants.DEFAULT_ATG_MAX_DURATION,
-      minDelayBetweenTwoTransactions: Constants.DEFAULT_ATG_MIN_DELAY_BETWEEN_TWO_TRANSACTIONS,
-      maxDelayBetweenTwoTransactions: Constants.DEFAULT_ATG_MAX_DELAY_BETWEEN_TWO_TRANSACTIONS,
+      minDuration: 60,
+      maxDuration: 120,
+      minDelayBetweenTwoTransactions: 15,
+      maxDelayBetweenTwoTransactions: 30,
       probabilityOfStart: 1,
-      stopAfterHours: Constants.DEFAULT_ATG_STOP_AFTER_HOURS,
+      stopAfterHours: 0.25,
       stopOnConnectionFailure: true,
     });