Code cleanups
authorJérôme Benoit <jerome.benoit@sap.com>
Thu, 27 May 2021 23:01:14 +0000 (01:01 +0200)
committerJérôme Benoit <jerome.benoit@sap.com>
Thu, 27 May 2021 23:01:14 +0000 (01:01 +0200)
Signed-off-by: Jérôme Benoit <jerome.benoit@sap.com>
src/charging-station/ChargingStation.ts

index c20ed239492b2a0bc6c2181857a9819aeb6ac124..164157377d7a03c0d5273a662003867046786fe8 100644 (file)
@@ -315,9 +315,7 @@ export default class ChargingStation {
 
   public resetTransactionOnConnector(connectorId: number): void {
     this.initTransactionOnConnector(connectorId);
-    if (this.getConnector(connectorId)?.transactionSetInterval) {
-      clearInterval(this.getConnector(connectorId).transactionSetInterval);
-    }
+    this.stopMeterValues(connectorId);
   }
 
   public addToMessageQueue(message: string): void {
@@ -723,6 +721,13 @@ export default class ChargingStation {
       }
     }
     // Start the ATG
+    this.startAutomaticTransactionGenerator();
+    if (this.getEnableStatistics()) {
+      this.performanceStatistics.start();
+    }
+  }
+
+  private startAutomaticTransactionGenerator() {
     if (this.stationInfo.AutomaticTransactionGenerator.enable) {
       if (!this.automaticTransactionGeneration) {
         this.automaticTransactionGeneration = new AutomaticTransactionGenerator(this);
@@ -732,9 +737,6 @@ export default class ChargingStation {
         void this.automaticTransactionGeneration.start();
       }
     }
-    if (this.getEnableStatistics()) {
-      this.performanceStatistics.start();
-    }
   }
 
   private async stopMessageSequence(reason: StopTransactionReason = StopTransactionReason.NONE): Promise<void> {
@@ -836,6 +838,12 @@ export default class ChargingStation {
     logger.info(this.logPrefix() + ' Will communicate through URL ' + this.supervisionUrl);
   }
 
+  private stopMeterValues(connectorId: number) {
+    if (this.getConnector(connectorId)?.transactionSetInterval) {
+      clearInterval(this.getConnector(connectorId).transactionSetInterval);
+    }
+  }
+
   private startAuthorizationFileMonitoring(): void {
     fs.watch(this.getAuthorizationFile()).on('change', (e) => {
       try {
@@ -861,15 +869,7 @@ export default class ChargingStation {
           await this.automaticTransactionGeneration.stop();
         }
         // Start the ATG
-        if (this.stationInfo.AutomaticTransactionGenerator.enable) {
-          if (!this.automaticTransactionGeneration) {
-            this.automaticTransactionGeneration = new AutomaticTransactionGenerator(this);
-          }
-          if (this.automaticTransactionGeneration.timeToStop) {
-            // The ATG might sleep
-            void this.automaticTransactionGeneration.start();
-          }
-        }
+        this.startAutomaticTransactionGenerator();
         // FIXME?: restart heartbeat and WebSocket ping when their interval values have changed
       } catch (error) {
         logger.error(this.logPrefix() + ' Charging station template file monitoring error: %j', error);
@@ -889,7 +889,7 @@ export default class ChargingStation {
       this.stationInfo.AutomaticTransactionGenerator.stopOnConnectionFailure &&
       this.automaticTransactionGeneration &&
       !this.automaticTransactionGeneration.timeToStop) {
-      this.automaticTransactionGeneration.stop().catch(() => { });
+      await this.automaticTransactionGeneration.stop();
     }
     if (this.autoReconnectRetryCount < this.getAutoReconnectMaxRetries() || this.getAutoReconnectMaxRetries() === -1) {
       this.autoReconnectRetryCount++;