fix: various fixes to the reservation code
authorJérôme Benoit <jerome.benoit@sap.com>
Wed, 28 Jun 2023 22:23:12 +0000 (00:23 +0200)
committerJérôme Benoit <jerome.benoit@sap.com>
Wed, 28 Jun 2023 22:23:12 +0000 (00:23 +0200)
Signed-off-by: Jérôme Benoit <jerome.benoit@sap.com>
src/charging-station/ChargingStation.ts
src/charging-station/ChargingStationUtils.ts

index c88c76e7dfe76f9d8c9ed165f0c2bb69a7962acf..fe818a2721be342e8418bac7871b83ea77a21aae 100644 (file)
@@ -378,10 +378,7 @@ export class ChargingStation {
       }
     } else {
       for (const connectorId of this.connectors.keys()) {
-        if (
-          connectorId > 0 &&
-          this.getConnectorStatus(connectorId)?.transactionId === transactionId
-        ) {
+        if (this.getConnectorStatus(connectorId)?.transactionId === transactionId) {
           return this.getConnectorStatus(connectorId)?.transactionIdTag;
         }
       }
@@ -391,7 +388,10 @@ export class ChargingStation {
   public getNumberOfRunningTransactions(): number {
     let trxCount = 0;
     if (this.hasEvses) {
-      for (const evseStatus of this.evses.values()) {
+      for (const [evseId, evseStatus] of this.evses) {
+        if (evseId === 0) {
+          continue;
+        }
         for (const connectorStatus of evseStatus.connectors.values()) {
           if (connectorStatus.transactionStarted === true) {
             ++trxCount;
@@ -447,10 +447,7 @@ export class ChargingStation {
       }
     } else {
       for (const connectorId of this.connectors.keys()) {
-        if (
-          connectorId > 0 &&
-          this.getConnectorStatus(connectorId)?.transactionId === transactionId
-        ) {
+        if (this.getConnectorStatus(connectorId)?.transactionId === transactionId) {
           return connectorId;
         }
       }
@@ -975,19 +972,22 @@ export class ChargingStation {
     }
   }
 
-  public getReservationBy(filterKey: ReservationFilterKey, value: number | string): Reservation {
+  public getReservationBy(
+    filterKey: ReservationFilterKey,
+    value: number | string
+  ): Reservation | undefined {
     if (this.hasEvses) {
-      for (const evse of this.evses.values()) {
-        for (const connector of evse.connectors.values()) {
-          if (connector?.reservation?.[filterKey] === value) {
-            return connector.reservation;
+      for (const evseStatus of this.evses.values()) {
+        for (const connectorStatus of evseStatus.connectors.values()) {
+          if (connectorStatus?.reservation?.[filterKey] === value) {
+            return connectorStatus.reservation;
           }
         }
       }
     } else {
-      for (const connector of this.connectors.values()) {
-        if (connector?.reservation?.[filterKey] === value) {
-          return connector.reservation;
+      for (const connectorStatus of this.connectors.values()) {
+        if (connectorStatus?.reservation?.[filterKey] === value) {
+          return connectorStatus.reservation;
         }
       }
     }
@@ -1010,22 +1010,23 @@ export class ChargingStation {
     );
     // eslint-disable-next-line @typescript-eslint/no-misused-promises
     this.reservationExpirationSetInterval = setInterval(async (): Promise<void> => {
+      const now = new Date();
       if (this.hasEvses) {
-        for (const evse of this.evses.values()) {
-          for (const connector of evse.connectors.values()) {
-            if (connector?.reservation?.expiryDate.toString() < new Date().toISOString()) {
+        for (const evseStatus of this.evses.values()) {
+          for (const connectorStatus of evseStatus.connectors.values()) {
+            if (connectorStatus?.reservation?.expiryDate < now) {
               await this.removeReservation(
-                connector.reservation,
+                connectorStatus.reservation,
                 ReservationTerminationReason.EXPIRED
               );
             }
           }
         }
       } else {
-        for (const connector of this.connectors.values()) {
-          if (connector?.reservation?.expiryDate.toString() < new Date().toISOString()) {
+        for (const connectorStatus of this.connectors.values()) {
+          if (connectorStatus?.reservation?.expiryDate < now) {
             await this.removeReservation(
-              connector.reservation,
+              connectorStatus.reservation,
               ReservationTerminationReason.EXPIRED
             );
           }
@@ -1040,8 +1041,7 @@ export class ChargingStation {
   }
 
   public validateIncomingRequestWithReservation(connectorId: number, idTag: string): boolean {
-    const reservation = this.getReservationBy(ReservationFilterKey.CONNECTOR_ID, connectorId);
-    return !Utils.isUndefined(reservation) && reservation.idTag === idTag;
+    return this.getReservationBy(ReservationFilterKey.CONNECTOR_ID, connectorId)?.idTag === idTag;
   }
 
   public isConnectorReservable(
@@ -1066,36 +1066,21 @@ export class ChargingStation {
   private getNumberOfReservableConnectors(): number {
     let reservableConnectors = 0;
     if (this.hasEvses) {
-      for (const evse of this.evses.values()) {
-        reservableConnectors = this.countReservableConnectors(evse.connectors);
+      for (const evseStatus of this.evses.values()) {
+        reservableConnectors += ChargingStationUtils.countReservableConnectors(
+          evseStatus.connectors
+        );
       }
     } else {
-      reservableConnectors = this.countReservableConnectors(this.connectors);
+      reservableConnectors = ChargingStationUtils.countReservableConnectors(this.connectors);
     }
     return reservableConnectors - this.getNumberOfReservationsOnConnectorZero();
   }
 
-  private countReservableConnectors(connectors: Map<number, ConnectorStatus>) {
-    let reservableConnectors = 0;
-    for (const [connectorId, connector] of connectors) {
-      if (connectorId === 0) {
-        continue;
-      }
-      if (connector.status === ConnectorStatusEnum.Available) {
-        ++reservableConnectors;
-      }
-    }
-    return reservableConnectors;
-  }
-
   private getNumberOfReservationsOnConnectorZero(): number {
     let numberOfReservations = 0;
-    if (this.hasEvses) {
-      for (const evse of this.evses.values()) {
-        if (evse.connectors.get(0)?.reservation) {
-          ++numberOfReservations;
-        }
-      }
+    if (this.hasEvses && this.evses.get(0)?.connectors.get(0)?.reservation) {
+      ++numberOfReservations;
     } else if (this.connectors.get(0)?.reservation) {
       ++numberOfReservations;
     }
@@ -2148,7 +2133,10 @@ export class ChargingStation {
 
   private async stopRunningTransactions(reason = StopTransactionReason.NONE): Promise<void> {
     if (this.hasEvses) {
-      for (const evseStatus of this.evses.values()) {
+      for (const [evseId, evseStatus] of this.evses) {
+        if (evseId === 0) {
+          continue;
+        }
         for (const [connectorId, connectorStatus] of evseStatus.connectors) {
           if (connectorStatus.transactionStarted === true) {
             await this.stopTransactionOnConnector(connectorId, reason);
index 033fc4b36f79647746106169fe462834d241e463..4d51b0ea348329f803f2506351370784e5df8981 100644 (file)
@@ -55,6 +55,19 @@ export class ChargingStationUtils {
         )}${idSuffix}`;
   }
 
+  public static countReservableConnectors(connectors: Map<number, ConnectorStatus>) {
+    let reservableConnectors = 0;
+    for (const [connectorId, connectorStatus] of connectors) {
+      if (connectorId === 0) {
+        continue;
+      }
+      if (connectorStatus.status === ConnectorStatusEnum.Available) {
+        ++reservableConnectors;
+      }
+    }
+    return reservableConnectors;
+  }
+
   public static getHashId(index: number, stationTemplate: ChargingStationTemplate): string {
     const chargingStationInfo = {
       chargePointModel: stationTemplate.chargePointModel,