fix: fix off-by-one in ATG statuses handling
authorJérôme Benoit <jerome.benoit@sap.com>
Sun, 7 Jan 2024 22:09:35 +0000 (23:09 +0100)
committerJérôme Benoit <jerome.benoit@sap.com>
Sun, 7 Jan 2024 22:09:35 +0000 (23:09 +0100)
Signed-off-by: Jérôme Benoit <jerome.benoit@sap.com>
src/charging-station/AutomaticTransactionGenerator.ts
src/types/AutomaticTransactionGenerator.ts

index 38efbf51a22ce5c19390a0219702ddb81c9dfd2c..d056d8ec866ee1c948740e24ca03ed2ae4f525d4 100644 (file)
@@ -22,6 +22,7 @@ import {
   cloneObject,
   formatDurationMilliSeconds,
   getRandomInteger,
+  isValidTime,
   logPrefix,
   logger,
   secureRandom,
@@ -226,9 +227,9 @@ export class AutomaticTransactionGenerator {
         }
       } else {
         // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
-        ++this.connectorsStatus.get(connectorId)!.skippedConsecutiveTransactions!
+        ++this.connectorsStatus.get(connectorId)!.skippedConsecutiveTransactions
         // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
-        ++this.connectorsStatus.get(connectorId)!.skippedTransactions!
+        ++this.connectorsStatus.get(connectorId)!.skippedTransactions
         logger.info(
           `${this.logPrefix(connectorId)} skipped consecutively ${this.connectorsStatus.get(
             connectorId
@@ -258,11 +259,9 @@ export class AutomaticTransactionGenerator {
   }
 
   private setStartConnectorStatus (connectorId: number): void {
-    // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
-    this.connectorsStatus.get(connectorId)!.skippedConsecutiveTransactions = 0
     const previousRunDuration =
-      this.connectorsStatus.get(connectorId)?.startDate != null &&
-      this.connectorsStatus.get(connectorId)?.lastRunDate != null
+      isValidTime(this.connectorsStatus.get(connectorId)?.startDate) &&
+      isValidTime(this.connectorsStatus.get(connectorId)?.lastRunDate)
         ? // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
         this.connectorsStatus.get(connectorId)!.lastRunDate!.getTime() -
           // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
@@ -280,6 +279,9 @@ export class AutomaticTransactionGenerator {
         ) -
         previousRunDuration
     )
+    delete this.connectorsStatus.get(connectorId)?.stoppedDate
+    // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
+    this.connectorsStatus.get(connectorId)!.skippedConsecutiveTransactions = 0
     // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
     this.connectorsStatus.get(connectorId)!.start = true
   }
@@ -287,6 +289,11 @@ export class AutomaticTransactionGenerator {
   private canStartConnector (connectorId: number): boolean {
     // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
     if (new Date() > this.connectorsStatus.get(connectorId)!.stopDate!) {
+      logger.info(
+        `${this.logPrefix(
+          connectorId
+        )} entered in transaction loop while the ATG stop date has been reached`
+      )
       return false
     }
     if (!this.chargingStation.inAcceptedState()) {
@@ -366,10 +373,10 @@ export class AutomaticTransactionGenerator {
 
   private getConnectorStatus (connectorId: number): Status {
     const connectorStatus =
-      this.chargingStation.getAutomaticTransactionGeneratorStatuses()?.[connectorId] != null
+      this.chargingStation.getAutomaticTransactionGeneratorStatuses()?.[connectorId - 1] != null
         ? cloneObject<Status>(
           // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
-          this.chargingStation.getAutomaticTransactionGeneratorStatuses()![connectorId]
+          this.chargingStation.getAutomaticTransactionGeneratorStatuses()![connectorId - 1]
         )
         : undefined
     this.resetConnectorStatus(connectorStatus)
@@ -395,10 +402,6 @@ export class AutomaticTransactionGenerator {
     if (connectorStatus == null) {
       return
     }
-    delete connectorStatus.startDate
-    delete connectorStatus.lastRunDate
-    delete connectorStatus.stopDate
-    delete connectorStatus.stoppedDate
     if (
       !this.started &&
       (connectorStatus.start ||
@@ -426,10 +429,10 @@ export class AutomaticTransactionGenerator {
       )} start transaction with an idTag '${idTag}'`
       if (this.getRequireAuthorize()) {
         // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
-        ++this.connectorsStatus.get(connectorId)!.authorizeRequests!
+        ++this.connectorsStatus.get(connectorId)!.authorizeRequests
         if (await isIdTagAuthorized(this.chargingStation, connectorId, idTag)) {
           // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
-          ++this.connectorsStatus.get(connectorId)!.acceptedAuthorizeRequests!
+          ++this.connectorsStatus.get(connectorId)!.acceptedAuthorizeRequests
           logger.info(startTransactionLogMsg)
           // Start transaction
           startResponse = await this.chargingStation.ocppRequestService.requestHandler<
@@ -444,7 +447,7 @@ export class AutomaticTransactionGenerator {
           return startResponse
         }
         // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
-        ++this.connectorsStatus.get(connectorId)!.rejectedAuthorizeRequests!
+        ++this.connectorsStatus.get(connectorId)!.rejectedAuthorizeRequests
         PerformanceStatistics.endMeasure(measureId, beginId)
         return startResponse
       }
@@ -487,13 +490,13 @@ export class AutomaticTransactionGenerator {
       )
       stopResponse = await this.chargingStation.stopTransactionOnConnector(connectorId, reason)
       // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
-      ++this.connectorsStatus.get(connectorId)!.stopTransactionRequests!
+      ++this.connectorsStatus.get(connectorId)!.stopTransactionRequests
       if (stopResponse.idTagInfo?.status === AuthorizationStatus.ACCEPTED) {
         // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
-        ++this.connectorsStatus.get(connectorId)!.acceptedStopTransactionRequests!
+        ++this.connectorsStatus.get(connectorId)!.acceptedStopTransactionRequests
       } else {
         // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
-        ++this.connectorsStatus.get(connectorId)!.rejectedStopTransactionRequests!
+        ++this.connectorsStatus.get(connectorId)!.rejectedStopTransactionRequests
       }
     } else {
       const transactionId = this.chargingStation.getConnectorStatus(connectorId)?.transactionId
@@ -526,14 +529,14 @@ export class AutomaticTransactionGenerator {
     startResponse: StartTransactionResponse
   ): void {
     // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
-    ++this.connectorsStatus.get(connectorId)!.startTransactionRequests!
+    ++this.connectorsStatus.get(connectorId)!.startTransactionRequests
     if (startResponse.idTagInfo.status === AuthorizationStatus.ACCEPTED) {
       // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
-      ++this.connectorsStatus.get(connectorId)!.acceptedStartTransactionRequests!
+      ++this.connectorsStatus.get(connectorId)!.acceptedStartTransactionRequests
     } else {
       logger.warn(`${this.logPrefix(connectorId)} start transaction rejected`)
       // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
-      ++this.connectorsStatus.get(connectorId)!.rejectedStartTransactionRequests!
+      ++this.connectorsStatus.get(connectorId)!.rejectedStartTransactionRequests
     }
   }
 }
index 68fd2fe3800b16b5dd14c7497ab5b99223400a74..670590d491307aeb97f03bcd3d1bb2019ceb1773 100644 (file)
@@ -23,17 +23,17 @@ export interface Status {
   lastRunDate?: Date
   stopDate?: Date
   stoppedDate?: Date
-  authorizeRequests?: number
-  acceptedAuthorizeRequests?: number
-  rejectedAuthorizeRequests?: number
-  startTransactionRequests?: number
-  acceptedStartTransactionRequests?: number
-  rejectedStartTransactionRequests?: number
-  stopTransactionRequests?: number
-  acceptedStopTransactionRequests?: number
-  rejectedStopTransactionRequests?: number
-  skippedConsecutiveTransactions?: number
-  skippedTransactions?: number
+  authorizeRequests: number
+  acceptedAuthorizeRequests: number
+  rejectedAuthorizeRequests: number
+  startTransactionRequests: number
+  acceptedStartTransactionRequests: number
+  rejectedStartTransactionRequests: number
+  stopTransactionRequests: number
+  acceptedStopTransactionRequests: number
+  rejectedStopTransactionRequests: number
+  skippedConsecutiveTransactions: number
+  skippedTransactions: number
 }
 
 export interface ChargingStationAutomaticTransactionGeneratorConfiguration {