fix: fix reservable connector detection
authorJérôme Benoit <jerome.benoit@sap.com>
Sat, 29 Jul 2023 21:39:01 +0000 (23:39 +0200)
committerJérôme Benoit <jerome.benoit@sap.com>
Sat, 29 Jul 2023 21:39:01 +0000 (23:39 +0200)
Signed-off-by: Jérôme Benoit <jerome.benoit@sap.com>
src/charging-station/ChargingStation.ts
src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts

index aad6a5370617841e4f7480e2c90bd9daebd52e9c..56adcc9cc13d4e351087b2e25895ed78cf716d40 100644 (file)
@@ -943,12 +943,12 @@ export class ChargingStation {
   }
 
   public async addReservation(reservation: Reservation): Promise<void> {
-    const [exists, reservationFound] = this.doesReservationExists(reservation);
-    if (exists) {
-      await this.removeReservation(
-        reservationFound!,
-        ReservationTerminationReason.REPLACE_EXISTING,
-      );
+    const reservationFound = this.getReservationBy(
+      ReservationFilterKey.RESERVATION_ID,
+      reservation.reservationId,
+    );
+    if (reservationFound) {
+      await this.removeReservation(reservationFound, ReservationTerminationReason.REPLACE_EXISTING);
     }
     this.getConnectorStatus(reservation.connectorId)!.reservation = reservation;
     await OCPPServiceUtils.sendAndSetConnectorStatus(
@@ -1008,16 +1008,6 @@ export class ChargingStation {
     }
   }
 
-  public doesReservationExists(
-    reservation: Partial<Reservation>,
-  ): [boolean, Reservation | undefined] {
-    const foundReservation = this.getReservationBy(
-      ReservationFilterKey.RESERVATION_ID,
-      reservation.reservationId!,
-    );
-    return isUndefined(foundReservation) ? [false, undefined] : [true, foundReservation];
-  }
-
   public startReservationExpirationSetInterval(customInterval?: number): void {
     const interval =
       customInterval ?? Constants.DEFAULT_RESERVATION_EXPIRATION_OBSERVATION_INTERVAL;
@@ -1074,18 +1064,19 @@ export class ChargingStation {
     idTag?: string,
     connectorId?: number,
   ): boolean {
-    const [alreadyExists] = this.doesReservationExists({ id: reservationId });
-    if (alreadyExists) {
-      return alreadyExists;
-    }
-    const userReservedAlready = isUndefined(
-      this.getReservationBy(ReservationFilterKey.ID_TAG, idTag!),
-    )
-      ? false
-      : true;
+    const reservation = this.getReservationBy(ReservationFilterKey.RESERVATION_ID, reservationId);
+    const userReservedAlready =
+      !isUndefined(idTag) && isUndefined(this.getReservationBy(ReservationFilterKey.ID_TAG, idTag!))
+        ? false
+        : true;
     const notConnectorZero = isUndefined(connectorId) ? true : connectorId! > 0;
     const freeConnectorsAvailable = this.getNumberOfReservableConnectors() > 0;
-    return !alreadyExists && !userReservedAlready && notConnectorZero && freeConnectorsAvailable;
+    return (
+      isUndefined(reservation) &&
+      !userReservedAlready &&
+      notConnectorZero &&
+      freeConnectorsAvailable
+    );
   }
 
   private getNumberOfReservableConnectors(): number {
index 297b23b3ecbcc294ba4e1d4be7cbc566934bccf3..6e9e64cde55fd07c0ced291b86a75c424a9375a1 100644 (file)
@@ -1615,8 +1615,11 @@ export class OCPP16IncomingRequestService extends OCPPIncomingRequestService {
     }
     try {
       const { reservationId } = commandPayload;
-      const [exists, reservation] = chargingStation.doesReservationExists({ id: reservationId });
-      if (!exists) {
+      const reservation = chargingStation.getReservationBy(
+        ReservationFilterKey.RESERVATION_ID,
+        reservationId,
+      );
+      if (isUndefined(reservation)) {
         logger.error(
           `${chargingStation.logPrefix()} Reservation with ID ${reservationId}
             does not exist on charging station`,