Cleanups to message queue handling code.
[e-mobility-charging-stations-simulator.git] / src / charging-station / ChargingStation.ts
index 2d9e9a63ce2c2bf4bbca8cce9673f4b80ad229ba..0c39613beefea49afdb1a3701457daba98fa12ee 100644 (file)
@@ -223,7 +223,7 @@ export default class ChargingStation {
         }
       }, interval);
     } else {
-      logger.error(`${this.logPrefix()} Charging station ${StandardParametersKey.MeterValueSampleInterval} configuration set to ${Utils.milliSecondsToHHMMSS(interval)}, not sending MeterValues`);
+      logger.error(`${this.logPrefix()} Charging station ${StandardParametersKey.MeterValueSampleInterval} configuration set to ${interval ? Utils.milliSecondsToHHMMSS(interval) : interval}, not sending MeterValues`);
     }
   }
 
@@ -319,7 +319,7 @@ export default class ChargingStation {
     }
   }
 
-  public addMessageToBuffer(message: string): void {
+  public addToMessageQueue(message: string): void {
     let dups = false;
     // Handle dups in buffer
     for (const bufferedMessage of this.messageQueue) {
@@ -335,6 +335,15 @@ export default class ChargingStation {
     }
   }
 
+  private flushMessageQueue() {
+    if (!Utils.isEmptyArray(this.messageQueue)) {
+      this.messageQueue.forEach((message, index) => {
+        this.messageQueue.splice(index, 1);
+        this.wsConnection.send(message);
+      });
+    }
+  }
+
   private getChargingStationId(stationTemplate: ChargingStationTemplate): string {
     // In case of multiple instances: add instance index to charging station id
     let instanceIndex = process.env.CF_INSTANCE_INDEX ? process.env.CF_INSTANCE_INDEX : 0;
@@ -481,12 +490,7 @@ export default class ChargingStation {
       await this.startMessageSequence();
       this.hasStopped && (this.hasStopped = false);
       if (this.hasSocketRestarted && this.isWebSocketOpen()) {
-        if (!Utils.isEmptyArray(this.messageQueue)) {
-          this.messageQueue.forEach((message, index) => {
-            this.messageQueue.splice(index, 1);
-            this.wsConnection.send(message);
-          });
-        }
+        this.flushMessageQueue();
       }
     } else {
       logger.error(`${this.logPrefix()} Registration failure: max retries reached (${this.getRegistrationMaxRetries()}) or retry disabled (${this.getRegistrationMaxRetries()})`);