fix: properly handle template relative file path within a directory
[e-mobility-charging-stations-simulator.git] / src / charging-station / AutomaticTransactionGenerator.ts
index 2380c967beada92d179262a2c71abc30640758f6..95483b8ec54dd80072f9fe48171e45fe08761de0 100644 (file)
@@ -66,6 +66,11 @@ export class AutomaticTransactionGenerator {
     return AutomaticTransactionGenerator.instances.get(chargingStation.stationInfo!.hashId)
   }
 
+  public static deleteInstance (chargingStation: ChargingStation): boolean {
+    // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
+    return AutomaticTransactionGenerator.instances.delete(chargingStation.stationInfo!.hashId)
+  }
+
   public start (stopAbsoluteDuration?: boolean): void {
     if (!checkChargingStation(this.chargingStation, this.logPrefix())) {
       return
@@ -188,6 +193,7 @@ export class AutomaticTransactionGenerator {
     while (this.connectorsStatus.get(connectorId)?.start === true) {
       await this.waitChargingStationAvailable(connectorId)
       await this.waitConnectorAvailable(connectorId)
+      await this.waitRunningTransactionStopped(connectorId)
       if (!this.canStartConnector(connectorId)) {
         this.stopConnector(connectorId)
         break
@@ -323,6 +329,15 @@ export class AutomaticTransactionGenerator {
       )
       return false
     }
+    const connectorStatus = this.chargingStation.getConnectorStatus(connectorId)
+    if (connectorStatus?.transactionStarted === true) {
+      logger.info(
+        `${this.logPrefix(
+          connectorId
+        )} entered in transaction loop while a transaction ${connectorStatus.transactionId} is already started on connector ${connectorId}`
+      )
+      return false
+    }
     return true
   }
 
@@ -337,7 +352,7 @@ export class AutomaticTransactionGenerator {
         )
         logged = true
       }
-      await sleep(Constants.CHARGING_STATION_ATG_AVAILABILITY_TIME)
+      await sleep(Constants.DEFAULT_ATG_WAIT_TIME)
     }
   }
 
@@ -352,7 +367,23 @@ export class AutomaticTransactionGenerator {
         )
         logged = true
       }
-      await sleep(Constants.CHARGING_STATION_ATG_AVAILABILITY_TIME)
+      await sleep(Constants.DEFAULT_ATG_WAIT_TIME)
+    }
+  }
+
+  private async waitRunningTransactionStopped (connectorId: number): Promise<void> {
+    const connectorStatus = this.chargingStation.getConnectorStatus(connectorId)
+    let logged = false
+    while (connectorStatus?.transactionStarted === true) {
+      if (!logged) {
+        logger.info(
+          `${this.logPrefix(
+            connectorId
+          )} transaction loop waiting for started transaction ${connectorStatus.transactionId} on connector ${connectorId} to be stopped`
+        )
+        logged = true
+      }
+      await sleep(Constants.DEFAULT_ATG_WAIT_TIME)
     }
   }