Ensure connector status is defined before getting its attributes
authorJérôme Benoit <jerome.benoit@sap.com>
Wed, 9 Mar 2022 22:06:39 +0000 (23:06 +0100)
committerJérôme Benoit <jerome.benoit@sap.com>
Wed, 9 Mar 2022 22:06:39 +0000 (23:06 +0100)
Signed-off-by: Jérôme Benoit <jerome.benoit@sap.com>
src/charging-station/ChargingStation.ts

index 36c5b8d5227ef30ad7fb9c4195d9b805ff807404..a451c5ee4668b31cbad728fa4881cf2356a812a8 100644 (file)
@@ -255,16 +255,6 @@ export default class ChargingStation {
     return this.stationInfo.phaseLineToLineVoltageMeterValues ?? false;
   }
 
-  public getEnergyActiveImportRegisterByTransactionId(transactionId: number): number | undefined {
-    const transactionConnectorStatus = this.getConnectorStatus(
-      this.getConnectorIdByTransactionId(transactionId)
-    );
-    if (this.getMeteringPerTransaction()) {
-      return transactionConnectorStatus.transactionEnergyActiveImportRegisterValue;
-    }
-    return transactionConnectorStatus.energyActiveImportRegisterValue;
-  }
-
   public getConnectorIdByTransactionId(transactionId: number): number | undefined {
     for (const connectorId of this.connectors.keys()) {
       if (
@@ -276,11 +266,22 @@ export default class ChargingStation {
     }
   }
 
+  public getEnergyActiveImportRegisterByTransactionId(transactionId: number): number | undefined {
+    const transactionConnectorStatus = this.getConnectorStatus(
+      this.getConnectorIdByTransactionId(transactionId)
+    );
+    if (this.getMeteringPerTransaction()) {
+      return transactionConnectorStatus?.transactionEnergyActiveImportRegisterValue;
+    }
+    return transactionConnectorStatus?.energyActiveImportRegisterValue;
+  }
+
   public getEnergyActiveImportRegisterByConnectorId(connectorId: number): number | undefined {
+    const connectorStatus = this.getConnectorStatus(connectorId);
     if (this.getMeteringPerTransaction()) {
-      return this.getConnectorStatus(connectorId).transactionEnergyActiveImportRegisterValue;
+      return connectorStatus?.transactionEnergyActiveImportRegisterValue;
     }
-    return this.getConnectorStatus(connectorId).energyActiveImportRegisterValue;
+    return connectorStatus?.energyActiveImportRegisterValue;
   }
 
   public getAuthorizeRemoteTxRequests(): boolean {