README.md: document added UI protocol features
[e-mobility-charging-stations-simulator.git] / src / charging-station / AutomaticTransactionGenerator.ts
index c1674069bad7c188a9462865ee85ed6f205e3c95..bf28e55bfdcbc9e4894996f1b383d47a3e0af0b8 100644 (file)
@@ -62,8 +62,8 @@ export default class AutomaticTransactionGenerator {
   }
 
   public start(): void {
-    if (this.started) {
-      logger.error(`${this.logPrefix()} trying to start while already started`);
+    if (this.started === true) {
+      logger.warn(`${this.logPrefix()} trying to start while already started`);
       return;
     }
     this.startConnectors();
@@ -71,14 +71,38 @@ export default class AutomaticTransactionGenerator {
   }
 
   public stop(): void {
-    if (!this.started) {
-      logger.error(`${this.logPrefix()} trying to stop while not started`);
+    if (this.started === false) {
+      logger.warn(`${this.logPrefix()} trying to stop while not started`);
       return;
     }
     this.stopConnectors();
     this.started = false;
   }
 
+  public startConnector(connectorId: number): void {
+    if (this.chargingStation.connectors.has(connectorId) === false) {
+      logger.warn(`${this.logPrefix(connectorId)} trying to start on non existing connector`);
+      return;
+    }
+    if (this.connectorsStatus.get(connectorId)?.start === false) {
+      // Avoid hogging the event loop with a busy loop
+      setImmediate(() => {
+        this.internalStartConnector(connectorId).catch(() => {
+          /* This is intentional */
+        });
+      });
+    } else if (this.connectorsStatus.get(connectorId)?.start === true) {
+      logger.warn(`${this.logPrefix(connectorId)} already started on connector`);
+    }
+  }
+
+  public stopConnector(connectorId: number): void {
+    this.connectorsStatus.set(connectorId, {
+      ...this.connectorsStatus.get(connectorId),
+      start: false,
+    });
+  }
+
   private startConnectors(): void {
     if (
       this.connectorsStatus?.size > 0 &&
@@ -111,7 +135,7 @@ export default class AutomaticTransactionGenerator {
             this.connectorsStatus.get(connectorId).startDate.getTime()
         )
     );
-    while (this.connectorsStatus.get(connectorId).start) {
+    while (this.connectorsStatus.get(connectorId).start === true) {
       if (new Date() > this.connectorsStatus.get(connectorId).stopDate) {
         this.stopConnector(connectorId);
         break;
@@ -221,22 +245,6 @@ export default class AutomaticTransactionGenerator {
     );
   }
 
-  private startConnector(connectorId: number): void {
-    // Avoid hogging the event loop with a busy loop
-    setImmediate(() => {
-      this.internalStartConnector(connectorId).catch(() => {
-        /* This is intentional */
-      });
-    });
-  }
-
-  private stopConnector(connectorId: number): void {
-    this.connectorsStatus.set(connectorId, {
-      ...this.connectorsStatus.get(connectorId),
-      start: false,
-    });
-  }
-
   private initializeConnectorStatus(connectorId: number): void {
     this.connectorsStatus.get(connectorId).authorizeRequests =
       this?.connectorsStatus.get(connectorId)?.authorizeRequests ?? 0;
@@ -395,7 +403,7 @@ export default class AutomaticTransactionGenerator {
   private logPrefix(connectorId?: number): string {
     return Utils.logPrefix(
       ` ${this.chargingStation.stationInfo.chargingStationId} | ATG${
-        connectorId && ` on connector #${connectorId.toString()}`
+        connectorId !== undefined ? ` on connector #${connectorId.toString()}` : ''
       }:`
     );
   }