Optimize ATG run duration handling
authorJérôme Benoit <jerome.benoit@sap.com>
Sat, 18 Sep 2021 14:04:58 +0000 (16:04 +0200)
committerJérôme Benoit <jerome.benoit@sap.com>
Sat, 18 Sep 2021 14:04:58 +0000 (16:04 +0200)
Signed-off-by: Jérôme Benoit <jerome.benoit@piment-noir.org>
src/charging-station/AutomaticTransactionGenerator.ts
src/charging-station/ChargingStation.ts

index 6f77199ab850aaeaa991668d182d63001b799e26..5181beed404b0d8865f9c7baf307c24ed630d691 100644 (file)
@@ -9,23 +9,24 @@ import Utils from '../utils/Utils';
 import logger from '../utils/Logger';
 
 export default class AutomaticTransactionGenerator {
-  public timeToStop: boolean;
+  public started: boolean;
   private startDate!: Date;
+  private lastRunDate!: Date;
   private stopDate!: Date;
-  private runningDuration!: number;
   private chargingStation: ChargingStation;
 
   constructor(chargingStation: ChargingStation) {
     this.chargingStation = chargingStation;
-    this.timeToStop = true;
+    this.started = false;
   }
 
   public start(): void {
     this.startDate = new Date();
+    this.lastRunDate = this?.lastRunDate ?? this.startDate;
     this.stopDate = new Date(this.startDate.getTime()
       + (this.chargingStation.stationInfo?.AutomaticTransactionGenerator?.stopAfterHours ?? Constants.CHARGING_STATION_ATG_DEFAULT_STOP_AFTER_HOURS) * 3600 * 1000
-      - (this.runningDuration ?? 0));
-    this.timeToStop = false;
+      - (this.lastRunDate.getTime() - this.startDate.getTime()));
+    this.started = true;
     for (const connector in this.chargingStation.connectors) {
       if (Utils.convertToInt(connector) > 0) {
         // Avoid hogging the event loop with a busy loop
@@ -38,7 +39,11 @@ export default class AutomaticTransactionGenerator {
   }
 
   public async stop(reason: StopTransactionReason = StopTransactionReason.NONE): Promise<void> {
-    logger.info(`${this.logPrefix()} over and lasted for ${Utils.formatDurationMilliSeconds(this.runningDuration ?? 0)}. Stopping all transactions`);
+    if (!this.started) {
+      logger.error(`${this.logPrefix()} trying to stop while not started`);
+      return;
+    }
+    logger.info(`${this.logPrefix()} over and lasted for ${Utils.formatDurationMilliSeconds(this.lastRunDate.getTime() - this.startDate.getTime())}. Stopping all transactions`);
     for (const connector in this.chargingStation.connectors) {
       const transactionId = this.chargingStation.getConnector(Utils.convertToInt(connector)).transactionId;
       if (this.chargingStation.getConnector(Utils.convertToInt(connector)).transactionStarted) {
@@ -47,14 +52,14 @@ export default class AutomaticTransactionGenerator {
           this.chargingStation.getTransactionIdTag(transactionId), reason);
       }
     }
-    this.timeToStop = true;
+    this.started = false;
   }
 
   private async startOnConnector(connectorId: number): Promise<void> {
     logger.info(this.logPrefix(connectorId) + ' started on connector');
     let transactionSkip = 0;
     let totalTransactionSkip = 0;
-    while (!this.timeToStop) {
+    while (this.started) {
       if ((new Date()) > this.stopDate) {
         await this.stop();
         break;
@@ -107,7 +112,7 @@ export default class AutomaticTransactionGenerator {
         totalTransactionSkip++;
         logger.info(this.logPrefix(connectorId) + ' skipped transaction ' + transactionSkip.toString() + '/' + totalTransactionSkip.toString());
       }
-      this.runningDuration = (new Date()).getTime() - this.startDate.getTime();
+      this.lastRunDate = new Date();
     }
     logger.info(this.logPrefix(connectorId) + ' stopped on connector');
   }
index 260365400f77e32b5f81d817de1bc0327576913f..aa5d24de2741e75410b433879d03fab0a93c8409 100644 (file)
@@ -42,7 +42,7 @@ export default class ChargingStation {
   public stationInfo!: ChargingStationInfo;
   public connectors: Connectors;
   public configuration!: ChargingStationConfiguration;
-  public hasStopped: boolean;
+  public stopped: boolean;
   public wsConnection!: WebSocket;
   public requests: Map<string, Request>;
   public messageQueue: string[];
@@ -55,9 +55,9 @@ export default class ChargingStation {
   private bootNotificationResponse!: BootNotificationResponse | null;
   private connectorsConfigurationHash!: string;
   private wsConnectionUrl!: URL;
-  private hasSocketRestarted: boolean;
+  private wsConnectionRestarted: boolean;
   private autoReconnectRetryCount: number;
-  private automaticTransactionGeneration!: AutomaticTransactionGenerator;
+  private automaticTransactionGenerator!: AutomaticTransactionGenerator;
   private webSocketPingSetInterval!: NodeJS.Timeout;
 
   constructor(index: number, stationTemplateFile: string) {
@@ -66,8 +66,8 @@ export default class ChargingStation {
     this.connectors = {} as Connectors;
     this.initialize();
 
-    this.hasStopped = false;
-    this.hasSocketRestarted = false;
+    this.stopped = false;
+    this.wsConnectionRestarted = false;
     this.autoReconnectRetryCount = 0;
 
     this.requests = new Map<string, Request>();
@@ -345,7 +345,7 @@ export default class ChargingStation {
       this.performanceStatistics.stop();
     }
     this.bootNotificationResponse = null;
-    this.hasStopped = true;
+    this.stopped = true;
   }
 
   public getConfigurationKey(key: string | StandardParametersKey, caseInsensitive = false): ConfigurationKey | undefined {
@@ -619,15 +619,15 @@ export default class ChargingStation {
     }
     if (this.isRegistered()) {
       await this.startMessageSequence();
-      this.hasStopped && (this.hasStopped = false);
-      if (this.hasSocketRestarted && this.isWebSocketConnectionOpened()) {
+      this.stopped && (this.stopped = false);
+      if (this.wsConnectionRestarted && this.isWebSocketConnectionOpened()) {
         this.flushMessageQueue();
       }
     } else {
       logger.error(`${this.logPrefix()} Registration failure: max retries reached (${this.getRegistrationMaxRetries()}) or retry disabled (${this.getRegistrationMaxRetries()})`);
     }
     this.autoReconnectRetryCount = 0;
-    this.hasSocketRestarted = false;
+    this.wsConnectionRestarted = false;
   }
 
   private async onClose(code: number, reason: string): Promise<void> {
@@ -839,15 +839,15 @@ export default class ChargingStation {
     for (const connector in this.connectors) {
       if (Utils.convertToInt(connector) === 0) {
         continue;
-      } else if (!this.hasStopped && !this.getConnector(Utils.convertToInt(connector))?.status && this.getConnector(Utils.convertToInt(connector))?.bootStatus) {
+      } else if (!this.stopped && !this.getConnector(Utils.convertToInt(connector))?.status && this.getConnector(Utils.convertToInt(connector))?.bootStatus) {
         // Send status in template at startup
         await this.ocppRequestService.sendStatusNotification(Utils.convertToInt(connector), this.getConnector(Utils.convertToInt(connector)).bootStatus);
         this.getConnector(Utils.convertToInt(connector)).status = this.getConnector(Utils.convertToInt(connector)).bootStatus;
-      } else if (this.hasStopped && this.getConnector(Utils.convertToInt(connector))?.bootStatus) {
+      } else if (this.stopped && this.getConnector(Utils.convertToInt(connector))?.bootStatus) {
         // Send status in template after reset
         await this.ocppRequestService.sendStatusNotification(Utils.convertToInt(connector), this.getConnector(Utils.convertToInt(connector)).bootStatus);
         this.getConnector(Utils.convertToInt(connector)).status = this.getConnector(Utils.convertToInt(connector)).bootStatus;
-      } else if (!this.hasStopped && this.getConnector(Utils.convertToInt(connector))?.status) {
+      } else if (!this.stopped && this.getConnector(Utils.convertToInt(connector))?.status) {
         // Send previous status at template reload
         await this.ocppRequestService.sendStatusNotification(Utils.convertToInt(connector), this.getConnector(Utils.convertToInt(connector)).status);
       } else {
@@ -862,11 +862,11 @@ export default class ChargingStation {
 
   private startAutomaticTransactionGenerator() {
     if (this.stationInfo.AutomaticTransactionGenerator.enable) {
-      if (!this.automaticTransactionGeneration) {
-        this.automaticTransactionGeneration = new AutomaticTransactionGenerator(this);
+      if (!this.automaticTransactionGenerator) {
+        this.automaticTransactionGenerator = new AutomaticTransactionGenerator(this);
       }
-      if (this.automaticTransactionGeneration.timeToStop) {
-        this.automaticTransactionGeneration.start();
+      if (!this.automaticTransactionGenerator.started) {
+        this.automaticTransactionGenerator.start();
       }
     }
   }
@@ -878,9 +878,9 @@ export default class ChargingStation {
     this.stopHeartbeat();
     // Stop the ATG
     if (this.stationInfo.AutomaticTransactionGenerator.enable &&
-      this.automaticTransactionGeneration &&
-      !this.automaticTransactionGeneration.timeToStop) {
-      await this.automaticTransactionGeneration.stop(reason);
+      this.automaticTransactionGenerator &&
+      this.automaticTransactionGenerator.started) {
+      await this.automaticTransactionGenerator.stop(reason);
     } else {
       for (const connector in this.connectors) {
         if (Utils.convertToInt(connector) > 0 && this.getConnector(Utils.convertToInt(connector)).transactionStarted) {
@@ -1011,8 +1011,8 @@ export default class ChargingStation {
             this.initialize();
             // Restart the ATG
             if (!this.stationInfo.AutomaticTransactionGenerator.enable &&
-                this.automaticTransactionGeneration) {
-              await this.automaticTransactionGeneration.stop();
+                this.automaticTransactionGenerator) {
+              await this.automaticTransactionGenerator.stop();
             }
             this.startAutomaticTransactionGenerator();
             if (this.getEnableStatistics()) {
@@ -1043,9 +1043,9 @@ export default class ChargingStation {
     // Stop the ATG if needed
     if (this.stationInfo.AutomaticTransactionGenerator.enable &&
       this.stationInfo.AutomaticTransactionGenerator.stopOnConnectionFailure &&
-      this.automaticTransactionGeneration &&
-      !this.automaticTransactionGeneration.timeToStop) {
-      await this.automaticTransactionGeneration.stop();
+      this.automaticTransactionGenerator &&
+      this.automaticTransactionGenerator.started) {
+      await this.automaticTransactionGenerator.stop();
     }
     if (this.autoReconnectRetryCount < this.getAutoReconnectMaxRetries() || this.getAutoReconnectMaxRetries() === -1) {
       this.autoReconnectRetryCount++;
@@ -1055,7 +1055,7 @@ export default class ChargingStation {
       await Utils.sleep(reconnectDelay);
       logger.error(this.logPrefix() + ' Socket: reconnecting try #' + this.autoReconnectRetryCount.toString());
       this.openWSConnection({ handshakeTimeout: reconnectTimeout }, true);
-      this.hasSocketRestarted = true;
+      this.wsConnectionRestarted = true;
     } else if (this.getAutoReconnectMaxRetries() !== -1) {
       logger.error(`${this.logPrefix()} Socket reconnect failure: max retries reached (${this.autoReconnectRetryCount}) or retry disabled (${this.getAutoReconnectMaxRetries()})`);
     }