Add helper to get connector id for a transaction id
authorJérôme Benoit <jerome.benoit@sap.com>
Wed, 9 Mar 2022 20:38:05 +0000 (21:38 +0100)
committerJérôme Benoit <jerome.benoit@sap.com>
Wed, 9 Mar 2022 20:38:05 +0000 (21:38 +0100)
Signed-off-by: Jérôme Benoit <jerome.benoit@sap.com>
src/charging-station/ChargingStation.ts
src/charging-station/ocpp/1.6/OCPP16RequestService.ts
src/charging-station/ocpp/1.6/OCPP16ResponseService.ts

index 1be6e335d63c196f2d42d7a501db04de62770bb5..36c5b8d5227ef30ad7fb9c4195d9b805ff807404 100644 (file)
@@ -256,19 +256,22 @@ export default class ChargingStation {
   }
 
   public getEnergyActiveImportRegisterByTransactionId(transactionId: number): number | undefined {
+    const transactionConnectorStatus = this.getConnectorStatus(
+      this.getConnectorIdByTransactionId(transactionId)
+    );
     if (this.getMeteringPerTransaction()) {
-      for (const connectorId of this.connectors.keys()) {
-        if (
-          connectorId > 0 &&
-          this.getConnectorStatus(connectorId).transactionId === transactionId
-        ) {
-          return this.getConnectorStatus(connectorId).transactionEnergyActiveImportRegisterValue;
-        }
-      }
+      return transactionConnectorStatus.transactionEnergyActiveImportRegisterValue;
     }
+    return transactionConnectorStatus.energyActiveImportRegisterValue;
+  }
+
+  public getConnectorIdByTransactionId(transactionId: number): number | undefined {
     for (const connectorId of this.connectors.keys()) {
-      if (connectorId > 0 && this.getConnectorStatus(connectorId).transactionId === transactionId) {
-        return this.getConnectorStatus(connectorId).energyActiveImportRegisterValue;
+      if (
+        connectorId > 0 &&
+        this.getConnectorStatus(connectorId)?.transactionId === transactionId
+      ) {
+        return connectorId;
       }
     }
   }
index ab45df16b83261b5385c6328bf60806036fee1ab..571ac066fc5710159c7bc5f968b9554689eb26de 100644 (file)
@@ -122,16 +122,9 @@ export default class OCPP16RequestService extends OCPPRequestService {
           timestamp: new Date().toISOString(),
         } as StartTransactionRequest;
       case OCPP16RequestCommand.STOP_TRANSACTION:
-        for (const id of this.chargingStation.connectors.keys()) {
-          if (
-            id > 0 &&
-            this.chargingStation.getConnectorStatus(id)?.transactionId ===
-              commandParams?.transactionId
-          ) {
-            connectorId = id;
-            break;
-          }
-        }
+        connectorId = this.chargingStation.getConnectorIdByTransactionId(
+          commandParams?.transactionId as number
+        );
         return {
           transactionId: commandParams?.transactionId,
           ...(!Utils.isUndefined(commandParams?.idTag) && { idTag: commandParams.idTag }),
index 27f252e1fdf76b2bfe96806b7fd7e89e5f884b25..aa48501e2621d8e0c3e261baab2978493af12192 100644 (file)
@@ -389,17 +389,9 @@ export default class OCPP16ResponseService extends OCPPResponseService {
     payload: OCPP16StopTransactionResponse,
     requestPayload: StopTransactionRequest
   ): Promise<void> {
-    let transactionConnectorId: number;
-    for (const connectorId of this.chargingStation.connectors.keys()) {
-      if (
-        connectorId > 0 &&
-        this.chargingStation.getConnectorStatus(connectorId)?.transactionId ===
-          requestPayload.transactionId
-      ) {
-        transactionConnectorId = connectorId;
-        break;
-      }
-    }
+    const transactionConnectorId = this.chargingStation.getConnectorIdByTransactionId(
+      requestPayload.transactionId
+    );
     if (!transactionConnectorId) {
       logger.error(
         this.chargingStation.logPrefix() +