Fix ATG connectors status initialization
authorJérôme Benoit <jerome.benoit@sap.com>
Mon, 12 Sep 2022 12:22:31 +0000 (14:22 +0200)
committerJérôme Benoit <jerome.benoit@sap.com>
Mon, 12 Sep 2022 12:24:36 +0000 (14:24 +0200)
Signed-off-by: Jérôme Benoit <jerome.benoit@sap.com>
src/charging-station/AutomaticTransactionGenerator.ts
src/charging-station/ChargingStation.ts
src/types/AutomaticTransactionGenerator.ts

index 03297e4a8c99dafdd375f8714db9717d1e78ebbc..ba5b3e652e09867be8afa9473c8109b5d1670211 100644 (file)
@@ -38,15 +38,14 @@ export default class AutomaticTransactionGenerator {
     this.started = false;
     this.configuration = automaticTransactionGeneratorConfiguration;
     this.chargingStation = chargingStation;
-    this.connectorsStatus = new Map<number, Status>();
-    this.stopConnectors();
+    this.connectorsStatus = new Map<number, Status>(this.buildInitialConnectorsStatus());
   }
 
   public static getInstance(
     automaticTransactionGeneratorConfiguration: AutomaticTransactionGeneratorConfiguration,
     chargingStation: ChargingStation
   ): AutomaticTransactionGenerator {
-    if (!AutomaticTransactionGenerator.instances.has(chargingStation.stationInfo.hashId)) {
+    if (AutomaticTransactionGenerator.instances.has(chargingStation.stationInfo.hashId) === false) {
       AutomaticTransactionGenerator.instances.set(
         chargingStation.stationInfo.hashId,
         new AutomaticTransactionGenerator(
@@ -132,6 +131,7 @@ export default class AutomaticTransactionGenerator {
 
   private async internalStartConnector(connectorId: number): Promise<void> {
     this.initializeConnectorStatus(connectorId);
+    this.connectorsStatus.get(connectorId).start = true;
     logger.info(
       this.logPrefix(connectorId) +
         ' started on connector and will run for ' +
@@ -281,7 +281,34 @@ export default class AutomaticTransactionGenerator {
           1000 -
         previousRunDuration
     );
-    this.connectorsStatus.get(connectorId).start = true;
+    this.connectorsStatus.get(connectorId).start =
+      this?.connectorsStatus.get(connectorId)?.start ?? false;
+  }
+
+  private buildInitialConnectorsStatus(): [number, Status][] {
+    const connectorsStatus: [number, Status][] = [];
+    for (const connectorId of this.chargingStation.connectors.keys()) {
+      if (connectorId > 0) {
+        connectorsStatus.push([
+          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,
+          },
+        ]);
+      }
+    }
+    return connectorsStatus;
   }
 
   private async startTransaction(
index 767ea76d913cae9dfe51e2bc4cc17af9afab7b07..a6694066adb1b5cb57a63a01e63ca5454f2581bb 100644 (file)
@@ -737,6 +737,7 @@ export default class ChargingStation {
     if (!Utils.isEmptyArray(connectorIds)) {
       for (const connectorId of connectorIds) {
         this.automaticTransactionGenerator?.stopConnector(connectorId);
+        // FIXME: check if the number of connectors match the CS number of connectors
       }
     } else {
       this.automaticTransactionGenerator?.stop();
index de26ce62bdc2d4770641715a951051f0fbc9209e..f88659bff630ce8025e6f9f6082a9a2cee2e3e8e 100644 (file)
@@ -11,7 +11,7 @@ export type AutomaticTransactionGeneratorConfiguration = {
 };
 
 export type Status = {
-  start?: boolean;
+  start: boolean;
   startDate?: Date;
   lastRunDate?: Date;
   stopDate?: Date;