refactor: improve error logging if ATG connector status is not found
authorJérôme Benoit <jerome.benoit@sap.com>
Sun, 21 Jan 2024 10:19:27 +0000 (11:19 +0100)
committerJérôme Benoit <jerome.benoit@sap.com>
Sun, 21 Jan 2024 10:19:27 +0000 (11:19 +0100)
Signed-off-by: Jérôme Benoit <jerome.benoit@sap.com>
src/charging-station/AutomaticTransactionGenerator.ts

index f0d20739b57970477e367a04c560268e42e3e88d..85d7f43a4a9be6890e5f9066eb7285b0b28c982d 100644 (file)
@@ -369,13 +369,16 @@ export class AutomaticTransactionGenerator {
   }
 
   private getConnectorStatus (connectorId: number): Status {
-    const connectorStatus =
-      this.chargingStation.getAutomaticTransactionGeneratorStatuses()?.[connectorId - 1] != null
-        ? clone<Status>(
-          // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
-          this.chargingStation.getAutomaticTransactionGeneratorStatuses()![connectorId - 1]
-        )
-        : undefined
+    const statusIndex = connectorId - 1
+    let connectorStatus: Status | undefined
+    if (this.chargingStation.getAutomaticTransactionGeneratorStatuses()?.[statusIndex] != null) {
+      connectorStatus = clone<Status>(
+        // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
+        this.chargingStation.getAutomaticTransactionGeneratorStatuses()![statusIndex]
+      )
+    } else if (this.chargingStation.getAutomaticTransactionGeneratorStatuses() != null) {
+      logger.error(`${this.logPrefix(connectorId)} no status found for connector #${connectorId}`)
+    }
     if (connectorStatus != null) {
       connectorStatus.startDate = convertToDate(connectorStatus.startDate)
       connectorStatus.lastRunDate = convertToDate(connectorStatus.lastRunDate)