From af4339e1cac037d86d6b10a14165e292a1feec2a Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Sat, 29 Jul 2023 23:39:01 +0200 Subject: [PATCH] fix: fix reservable connector detection MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- src/charging-station/ChargingStation.ts | 43 ++++++++----------- .../ocpp/1.6/OCPP16IncomingRequestService.ts | 7 ++- 2 files changed, 22 insertions(+), 28 deletions(-) diff --git a/src/charging-station/ChargingStation.ts b/src/charging-station/ChargingStation.ts index aad6a537..56adcc9c 100644 --- a/src/charging-station/ChargingStation.ts +++ b/src/charging-station/ChargingStation.ts @@ -943,12 +943,12 @@ export class ChargingStation { } public async addReservation(reservation: Reservation): Promise { - 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, - ): [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 { diff --git a/src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts b/src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts index 297b23b3..6e9e64cd 100644 --- a/src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts +++ b/src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts @@ -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`, -- 2.34.1