From 611d5cd2c94317acc7959c930bf962847b059e86 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Sun, 30 Jul 2023 21:39:32 +0200 Subject: [PATCH] fix: fix some undefined/null checks MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- .../AutomaticTransactionGenerator.ts | 2 +- .../ocpp/1.6/OCPP16IncomingRequestService.ts | 8 ++++---- .../ocpp/1.6/OCPP16ResponseService.ts | 13 ++++++------- 3 files changed, 11 insertions(+), 12 deletions(-) diff --git a/src/charging-station/AutomaticTransactionGenerator.ts b/src/charging-station/AutomaticTransactionGenerator.ts index 20c8e81a..1dc0a809 100644 --- a/src/charging-station/AutomaticTransactionGenerator.ts +++ b/src/charging-station/AutomaticTransactionGenerator.ts @@ -408,7 +408,7 @@ export class AutomaticTransactionGenerator extends AsyncResource { ++this.connectorsStatus.get(connectorId)!.authorizeRequests!; if (authorizeResponse?.idTagInfo?.status === AuthorizationStatus.ACCEPTED) { if ( - isNullOrUndefined(this.chargingStation.getConnectorStatus(connectorId)!.authorizeIdTag) + isNullOrUndefined(this.chargingStation.getConnectorStatus(connectorId)?.authorizeIdTag) ) { logger.warn( `${this.chargingStation.logPrefix()} IdTag ${idTag} is not set as authorized remotely, applying deferred initialization`, diff --git a/src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts b/src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts index 46598b0b..f5b568dc 100644 --- a/src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts +++ b/src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts @@ -826,10 +826,10 @@ export class OCPP16IncomingRequestService extends OCPPIncomingRequestService { ): Promise { const { connectorId: transactionConnectorId, idTag, chargingProfile } = commandPayload; if ( - (chargingStation.getConnectorStatus(transactionConnectorId)!.status === + (chargingStation.getConnectorStatus(transactionConnectorId)?.status === OCPP16ChargePointStatus.Reserved && chargingStation.getReservationBy('connectorId', transactionConnectorId)?.idTag !== idTag) || - (chargingStation.getConnectorStatus(0)!.status === OCPP16ChargePointStatus.Reserved && + (chargingStation.getConnectorStatus(0)?.status === OCPP16ChargePointStatus.Reserved && chargingStation.getReservationBy('connectorId', 0)?.idTag !== idTag) ) { return OCPP16Constants.OCPP_RESPONSE_REJECTED; @@ -896,7 +896,7 @@ export class OCPP16IncomingRequestService extends OCPPIncomingRequestService { idTag, reservationId: chargingStation.getReservationBy( 'connectorId', - chargingStation.getConnectorStatus(0)!.status === OCPP16ChargePointStatus.Reserved + chargingStation.getConnectorStatus(0)?.status === OCPP16ChargePointStatus.Reserved ? 0 : transactionConnectorId, )!, @@ -937,7 +937,7 @@ export class OCPP16IncomingRequestService extends OCPPIncomingRequestService { idTag, reservationId: chargingStation.getReservationBy( 'connectorId', - chargingStation.getConnectorStatus(0)!.status === OCPP16ChargePointStatus.Reserved + chargingStation.getConnectorStatus(0)?.status === OCPP16ChargePointStatus.Reserved ? 0 : transactionConnectorId, )!, diff --git a/src/charging-station/ocpp/1.6/OCPP16ResponseService.ts b/src/charging-station/ocpp/1.6/OCPP16ResponseService.ts index e5caf4bf..2d4853f0 100644 --- a/src/charging-station/ocpp/1.6/OCPP16ResponseService.ts +++ b/src/charging-station/ocpp/1.6/OCPP16ResponseService.ts @@ -640,18 +640,17 @@ export class OCPP16ResponseService extends OCPPResponseService { requestPayload.meterStart, ); const reservedOnConnectorZero = - chargingStation.getConnectorStatus(0)!.status === OCPP16ChargePointStatus.Reserved; + chargingStation.getConnectorStatus(0)?.status === OCPP16ChargePointStatus.Reserved; if ( - chargingStation.getConnectorStatus(transactionConnectorId)!.status === + chargingStation.getConnectorStatus(transactionConnectorId)?.status === OCPP16ChargePointStatus.Reserved || reservedOnConnectorZero ) { - const reservation = chargingStation.getReservationBy( - 'connectorId', - reservedOnConnectorZero ? 0 : transactionConnectorId, - )!; await chargingStation.removeReservation( - reservation, + chargingStation.getReservationBy( + 'connectorId', + reservedOnConnectorZero ? 0 : transactionConnectorId, + )!, ReservationTerminationReason.TRANSACTION_STARTED, ); } -- 2.34.1