feat: support evses in more code path
authorJérôme Benoit <jerome.benoit@sap.com>
Fri, 28 Apr 2023 17:35:20 +0000 (19:35 +0200)
committerJérôme Benoit <jerome.benoit@sap.com>
Fri, 28 Apr 2023 17:35:20 +0000 (19:35 +0200)
Reference: https://github.com/SAP/e-mobility-charging-stations-simulator/issues/349

Signed-off-by: Jérôme Benoit <jerome.benoit@sap.com>
src/charging-station/AutomaticTransactionGenerator.ts
src/charging-station/ChargingStation.ts
src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts

index fb79ae259b9a0c56234553851e0f150b67e4ae9b..d928a623d27c3567b28c1b392cd49520caefa436 100644 (file)
@@ -132,17 +132,37 @@ export class AutomaticTransactionGenerator extends AsyncResource {
       this.connectorsStatus.clear();
       this.initializeConnectorsStatus();
     }
-    for (const connectorId of this.chargingStation.connectors.keys()) {
-      if (connectorId > 0) {
-        this.startConnector(connectorId);
+    if (this.chargingStation.hasEvses) {
+      for (const [evseId, evseStatus] of this.chargingStation.evses) {
+        if (evseId > 0) {
+          for (const connectorId of evseStatus.connectors.keys()) {
+            this.startConnector(connectorId);
+          }
+        }
+      }
+    } else {
+      for (const connectorId of this.chargingStation.connectors.keys()) {
+        if (connectorId > 0) {
+          this.startConnector(connectorId);
+        }
       }
     }
   }
 
   private stopConnectors(): void {
-    for (const connectorId of this.chargingStation.connectors.keys()) {
-      if (connectorId > 0) {
-        this.stopConnector(connectorId);
+    if (this.chargingStation.hasEvses) {
+      for (const [evseId, evseStatus] of this.chargingStation.evses) {
+        if (evseId > 0) {
+          for (const connectorId of evseStatus.connectors.keys()) {
+            this.stopConnector(connectorId);
+          }
+        }
+      }
+    } else {
+      for (const connectorId of this.chargingStation.connectors.keys()) {
+        if (connectorId > 0) {
+          this.stopConnector(connectorId);
+        }
       }
     }
   }
@@ -295,22 +315,45 @@ export class AutomaticTransactionGenerator extends AsyncResource {
   }
 
   private initializeConnectorsStatus(): void {
-    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,
-        });
+    if (this.chargingStation.hasEvses) {
+      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,
+            });
+          }
+        }
+      }
+    } 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,
+          });
+        }
       }
     }
   }
index b28c520ba17e0a4704e8b7dfa376f998ec988ced..9cd20129714e214efb905ff8c68a6ff012389ba8 100644 (file)
@@ -267,10 +267,9 @@ export class ChargingStation {
     if (this.hasEvses) {
       let numberOfConnectors = 0;
       for (const [evseId, evseStatus] of this.evses) {
-        if (evseId === 0) {
-          continue;
+        if (evseId > 0) {
+          numberOfConnectors += evseStatus.connectors.size;
         }
-        numberOfConnectors += evseStatus.connectors.size;
       }
       return numberOfConnectors;
     }
@@ -1994,30 +1993,32 @@ export class ChargingStation {
     // Initialize connectors status
     if (this.hasEvses) {
       for (const [evseId, evseStatus] of this.evses) {
-        if (evseId === 0) {
-          continue;
+        if (evseId > 0) {
+          for (const [connectorId, connectorStatus] of evseStatus.connectors) {
+            const connectorBootStatus = ChargingStationUtils.getBootConnectorStatus(
+              this,
+              connectorId,
+              connectorStatus
+            );
+            await OCPPServiceUtils.sendAndSetConnectorStatus(
+              this,
+              connectorId,
+              connectorBootStatus
+            );
+          }
         }
-        for (const [connectorId, connectorStatus] of evseStatus.connectors) {
+      }
+    } else {
+      for (const connectorId of this.connectors.keys()) {
+        if (connectorId > 0) {
           const connectorBootStatus = ChargingStationUtils.getBootConnectorStatus(
             this,
             connectorId,
-            connectorStatus
+            this.getConnectorStatus(connectorId)
           );
           await OCPPServiceUtils.sendAndSetConnectorStatus(this, connectorId, connectorBootStatus);
         }
       }
-    } else {
-      for (const connectorId of this.connectors.keys()) {
-        if (connectorId === 0) {
-          continue;
-        }
-        const connectorBootStatus = ChargingStationUtils.getBootConnectorStatus(
-          this,
-          connectorId,
-          this.getConnectorStatus(connectorId)
-        );
-        await OCPPServiceUtils.sendAndSetConnectorStatus(this, connectorId, connectorBootStatus);
-      }
     }
     if (this.stationInfo?.firmwareStatus === FirmwareStatus.Installing) {
       await this.ocppRequestService.requestHandler<
index 3cb96e192c2ddbb0e56280379911eb7af32e2af3..f715adc3cc563214158832835f3213b1f3b55f6e 100644 (file)
@@ -21,6 +21,7 @@ import {
   type ChangeConfigurationResponse,
   type ClearChargingProfileRequest,
   type ClearChargingProfileResponse,
+  type ConnectorStatus,
   ErrorType,
   type GenericResponse,
   GenericStatus,
@@ -655,12 +656,13 @@ export class OCPP16IncomingRequestService extends OCPPIncomingRequestService {
       );
       return OCPP16Constants.OCPP_CLEAR_CHARGING_PROFILE_RESPONSE_UNKNOWN;
     }
-    const connectorStatus = chargingStation.getConnectorStatus(commandPayload.connectorId);
     if (
       !Utils.isNullOrUndefined(commandPayload.connectorId) &&
-      Utils.isNotEmptyArray(connectorStatus?.chargingProfiles)
+      Utils.isNotEmptyArray(
+        chargingStation.getConnectorStatus(commandPayload.connectorId)?.chargingProfiles
+      )
     ) {
-      connectorStatus.chargingProfiles = [];
+      chargingStation.getConnectorStatus(commandPayload.connectorId).chargingProfiles = [];
       logger.debug(
         `${chargingStation.logPrefix()} Charging profile(s) cleared on connector id ${
           commandPayload.connectorId
@@ -670,13 +672,10 @@ export class OCPP16IncomingRequestService extends OCPPIncomingRequestService {
     }
     if (Utils.isNullOrUndefined(commandPayload.connectorId)) {
       let clearedCP = false;
-      for (const connectorId of chargingStation.connectors.keys()) {
-        if (
-          Utils.isNotEmptyArray(chargingStation.getConnectorStatus(connectorId)?.chargingProfiles)
-        ) {
-          chargingStation
-            .getConnectorStatus(connectorId)
-            ?.chargingProfiles?.forEach((chargingProfile: OCPP16ChargingProfile, index: number) => {
+      const clearChargingProfiles = (connectorStatus: ConnectorStatus) => {
+        if (Utils.isNotEmptyArray(connectorStatus?.chargingProfiles)) {
+          connectorStatus?.chargingProfiles?.forEach(
+            (chargingProfile: OCPP16ChargingProfile, index: number) => {
               let clearCurrentCP = false;
               if (chargingProfile.chargingProfileId === commandPayload.id) {
                 clearCurrentCP = true;
@@ -707,7 +706,19 @@ export class OCPP16IncomingRequestService extends OCPPIncomingRequestService {
                 );
                 clearedCP = true;
               }
-            });
+            }
+          );
+        }
+      };
+      if (chargingStation.hasEvses) {
+        for (const evseStatus of chargingStation.evses.values()) {
+          for (const connectorStatus of evseStatus.connectors.values()) {
+            clearChargingProfiles(connectorStatus);
+          }
+        }
+      } else {
+        for (const connectorId of chargingStation.connectors.keys()) {
+          clearChargingProfiles(chargingStation.getConnectorStatus(connectorId));
         }
       }
       if (clearedCP) {