fix: fix some undefined/null checks
authorJérôme Benoit <jerome.benoit@sap.com>
Sun, 30 Jul 2023 19:39:32 +0000 (21:39 +0200)
committerJérôme Benoit <jerome.benoit@sap.com>
Sun, 30 Jul 2023 19:39:32 +0000 (21:39 +0200)
Signed-off-by: Jérôme Benoit <jerome.benoit@sap.com>
src/charging-station/AutomaticTransactionGenerator.ts
src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts
src/charging-station/ocpp/1.6/OCPP16ResponseService.ts

index 20c8e81a992b1f6944dfe4f2136c1502a576e162..1dc0a80924e9ee08dad6506e354e168277332984 100644 (file)
@@ -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`,
index 46598b0b6e5aa7161827eec3f336b35981f9f0ba..f5b568dc41eb39ade5eeb28cbfb7fff9459c81c7 100644 (file)
@@ -826,10 +826,10 @@ export class OCPP16IncomingRequestService extends OCPPIncomingRequestService {
   ): Promise<GenericResponse> {
     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,
             )!,
index e5caf4bf3cfb9d23c41e1403125a23d784943fe2..2d4853f052db9ec1f44f7148d532d9218e9b86be 100644 (file)
@@ -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,
         );
       }