fix: fix control flow at remote start
authorJérôme Benoit <jerome.benoit@sap.com>
Mon, 29 Jan 2024 13:29:04 +0000 (14:29 +0100)
committerJérôme Benoit <jerome.benoit@sap.com>
Mon, 29 Jan 2024 13:29:04 +0000 (14:29 +0100)
closes #959

Signed-off-by: Jérôme Benoit <jerome.benoit@sap.com>
src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts

index 289f03be33c430aee9adf510db8744bee10237cc..9e0fed9d087a1ac26c89f52c7348bb8259c6fc84 100644 (file)
@@ -981,90 +981,55 @@ export class OCPP16IncomingRequestService extends OCPPIncomingRequestService {
         idTag
       )
     }
-    const remoteStartTransactionLogMsg = `
-      ${chargingStation.logPrefix()} Transaction remotely STARTED on ${
-        chargingStation.stationInfo?.chargingStationId
-      }#${transactionConnectorId} for idTag '${idTag}'`
-    await OCPP16ServiceUtils.sendAndSetConnectorStatus(
-      chargingStation,
-      transactionConnectorId,
-      OCPP16ChargePointStatus.Preparing
-    )
-    // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
-    const connectorStatus = chargingStation.getConnectorStatus(transactionConnectorId)!
-    // Authorization check required
+    // idTag authorization check required
     if (
       chargingStation.getAuthorizeRemoteTxRequests() &&
-      (await OCPP16ServiceUtils.isIdTagAuthorized(chargingStation, transactionConnectorId, idTag))
+      !(await OCPP16ServiceUtils.isIdTagAuthorized(chargingStation, transactionConnectorId, idTag))
     ) {
-      // Authorization successful, start transaction
-      if (
-        (chargingProfile != null &&
-          this.setRemoteStartTransactionChargingProfile(
-            chargingStation,
-            transactionConnectorId,
-            chargingProfile
-          )) ||
-        chargingProfile == null
-      ) {
-        connectorStatus.transactionRemoteStarted = true
-        if (
-          (
-            await chargingStation.ocppRequestService.requestHandler<
-            OCPP16StartTransactionRequest,
-            OCPP16StartTransactionResponse
-            >(chargingStation, OCPP16RequestCommand.START_TRANSACTION, {
-              connectorId: transactionConnectorId,
-              idTag
-            })
-          ).idTagInfo.status === OCPP16AuthorizationStatus.ACCEPTED
-        ) {
-          logger.debug(remoteStartTransactionLogMsg)
-          return OCPP16Constants.OCPP_RESPONSE_ACCEPTED
-        }
-        return await this.notifyRemoteStartTransactionRejected(
-          chargingStation,
-          transactionConnectorId,
-          idTag
-        )
-      }
       return await this.notifyRemoteStartTransactionRejected(
         chargingStation,
         transactionConnectorId,
         idTag
       )
     }
-    // No authorization check required, start transaction
+    await OCPP16ServiceUtils.sendAndSetConnectorStatus(
+      chargingStation,
+      transactionConnectorId,
+      OCPP16ChargePointStatus.Preparing
+    )
     if (
-      (chargingProfile != null &&
-        this.setRemoteStartTransactionChargingProfile(
-          chargingStation,
-          transactionConnectorId,
-          chargingProfile
-        )) ||
-      chargingProfile == null
+      chargingProfile != null &&
+      !this.setRemoteStartTransactionChargingProfile(
+        chargingStation,
+        transactionConnectorId,
+        chargingProfile
+      )
     ) {
-      connectorStatus.transactionRemoteStarted = true
-      if (
-        (
-          await chargingStation.ocppRequestService.requestHandler<
-          OCPP16StartTransactionRequest,
-          OCPP16StartTransactionResponse
-          >(chargingStation, OCPP16RequestCommand.START_TRANSACTION, {
-            connectorId: transactionConnectorId,
-            idTag
-          })
-        ).idTagInfo.status === OCPP16AuthorizationStatus.ACCEPTED
-      ) {
-        logger.debug(remoteStartTransactionLogMsg)
-        return OCPP16Constants.OCPP_RESPONSE_ACCEPTED
-      }
       return await this.notifyRemoteStartTransactionRejected(
         chargingStation,
         transactionConnectorId,
         idTag
       )
     }
+    // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
+    chargingStation.getConnectorStatus(transactionConnectorId)!.transactionRemoteStarted = true
+    if (
+      (
+        await chargingStation.ocppRequestService.requestHandler<
+        OCPP16StartTransactionRequest,
+        OCPP16StartTransactionResponse
+        >(chargingStation, OCPP16RequestCommand.START_TRANSACTION, {
+          connectorId: transactionConnectorId,
+          idTag
+        })
+      ).idTagInfo.status === OCPP16AuthorizationStatus.ACCEPTED
+    ) {
+      logger.debug(`
+      ${chargingStation.logPrefix()} Transaction remotely STARTED on ${
+        chargingStation.stationInfo?.chargingStationId
+      }#${transactionConnectorId} for idTag '${idTag}'`)
+      return OCPP16Constants.OCPP_RESPONSE_ACCEPTED
+    }
     return await this.notifyRemoteStartTransactionRejected(
       chargingStation,
       transactionConnectorId,