From: Jérôme Benoit Date: Sun, 30 Jul 2023 11:22:32 +0000 (+0200) Subject: refactor: use keyof to build the reservation filter key X-Git-Tag: v1.2.20~91 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=2ca0ea909c319b763257cedb8ba421c3f22823fd;p=e-mobility-charging-stations-simulator.git refactor: use keyof to build the reservation filter key Signed-off-by: Jérôme Benoit --- diff --git a/src/charging-station/ChargingStation.ts b/src/charging-station/ChargingStation.ts index e7288851..ccc95263 100644 --- a/src/charging-station/ChargingStation.ts +++ b/src/charging-station/ChargingStation.ts @@ -104,7 +104,7 @@ import { RegistrationStatusEnumType, RequestCommand, type Reservation, - ReservationFilterKey, + type ReservationFilterKey, ReservationTerminationReason, type Response, StandardParametersKey, @@ -943,10 +943,7 @@ export class ChargingStation { } public async addReservation(reservation: Reservation): Promise { - const reservationFound = this.getReservationBy( - ReservationFilterKey.RESERVATION_ID, - reservation.reservationId, - ); + const reservationFound = this.getReservationBy('reservationId', reservation.reservationId); if (!isUndefined(reservationFound)) { await this.removeReservation( reservationFound!, @@ -997,14 +994,14 @@ export class ChargingStation { if (this.hasEvses) { for (const evseStatus of this.evses.values()) { for (const connectorStatus of evseStatus.connectors.values()) { - if (connectorStatus?.reservation?.[filterKey as keyof Reservation] === value) { + if (connectorStatus?.reservation?.[filterKey] === value) { return connectorStatus.reservation; } } } } else { for (const connectorStatus of this.connectors.values()) { - if (connectorStatus?.reservation?.[filterKey as keyof Reservation] === value) { + if (connectorStatus?.reservation?.[filterKey] === value) { return connectorStatus.reservation; } } @@ -1059,7 +1056,7 @@ export class ChargingStation { } public validateIncomingRequestWithReservation(connectorId: number, idTag: string): boolean { - return this.getReservationBy(ReservationFilterKey.CONNECTOR_ID, connectorId)?.idTag === idTag; + return this.getReservationBy('connectorId', connectorId)?.idTag === idTag; } public isConnectorReservable( @@ -1067,13 +1064,9 @@ export class ChargingStation { idTag?: string, connectorId?: number, ): boolean { - const reservationExists = !isUndefined( - this.getReservationBy(ReservationFilterKey.RESERVATION_ID, reservationId), - ); + const reservationExists = !isUndefined(this.getReservationBy('reservationId', reservationId)); const userReservationExists = - !isUndefined(idTag) && isUndefined(this.getReservationBy(ReservationFilterKey.ID_TAG, idTag!)) - ? false - : true; + !isUndefined(idTag) && isUndefined(this.getReservationBy('idTag', idTag!)) ? false : true; const notConnectorZero = isUndefined(connectorId) ? true : connectorId! > 0; const freeConnectorsAvailable = this.getNumberOfReservableConnectors() > 0; return ( diff --git a/src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts b/src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts index 6e9e64cd..2659458f 100644 --- a/src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts +++ b/src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts @@ -80,7 +80,6 @@ import { OCPPVersion, type RemoteStartTransactionRequest, type RemoteStopTransactionRequest, - ReservationFilterKey, ReservationTerminationReason, type ResetRequest, type SetChargingProfileRequest, @@ -886,7 +885,7 @@ export class OCPP16IncomingRequestService extends OCPPIncomingRequestService { }; if (reserved || reservedOnConnectorZero) { const reservation = chargingStation.getReservationBy( - ReservationFilterKey.CONNECTOR_ID, + 'connectorId', reservedOnConnectorZero ? 0 : transactionConnectorId, )!; startTransactionPayload.reservationId = reservation.reservationId; @@ -1615,10 +1614,7 @@ export class OCPP16IncomingRequestService extends OCPPIncomingRequestService { } try { const { reservationId } = commandPayload; - const reservation = chargingStation.getReservationBy( - ReservationFilterKey.RESERVATION_ID, - reservationId, - ); + const reservation = chargingStation.getReservationBy('reservationId', reservationId); if (isUndefined(reservation)) { logger.error( `${chargingStation.logPrefix()} Reservation with ID ${reservationId} diff --git a/src/types/index.ts b/src/types/index.ts index f65e4cae..04608aab 100644 --- a/src/types/index.ts +++ b/src/types/index.ts @@ -256,6 +256,6 @@ export { } from './WebSocket'; export { type Reservation, - ReservationFilterKey, + type ReservationFilterKey, ReservationTerminationReason, } from './ocpp/Reservation'; diff --git a/src/types/ocpp/Reservation.ts b/src/types/ocpp/Reservation.ts index 0bcf101c..0d79375e 100644 --- a/src/types/ocpp/Reservation.ts +++ b/src/types/ocpp/Reservation.ts @@ -10,10 +10,4 @@ export enum ReservationTerminationReason { REPLACE_EXISTING = 'ReplaceExisting', } -export enum ReservationFilterKey { - RESERVATION_ID = 'reservationId', - ID_TAG = 'idTag', - PARENT_ID_TAG = 'parentIdTag', - CONNECTOR_ID = 'connectorId', - EVSE_ID = 'evseId', -} +export type ReservationFilterKey = keyof OCPP16ReserveNowRequest;