refactor: cleanup buffered messages handling code
authorJérôme Benoit <jerome.benoit@sap.com>
Wed, 29 Nov 2023 19:05:15 +0000 (20:05 +0100)
committerJérôme Benoit <jerome.benoit@sap.com>
Wed, 29 Nov 2023 19:05:15 +0000 (20:05 +0100)
Signed-off-by: Jérôme Benoit <jerome.benoit@sap.com>
src/charging-station/ChargingStation.ts
src/charging-station/ocpp/OCPPServiceUtils.ts

index ba430878d4947641da61c72548fc58e437e75914..6acd40182fee333100b9a3985726a93d0e3c811e 100644 (file)
@@ -497,7 +497,7 @@ export class ChargingStation extends EventEmitter {
   }
 
   public startHeartbeat(): void {
-    if (this.getHeartbeatInterval() > 0 && !this.heartbeatSetInterval) {
+    if (this.getHeartbeatInterval() > 0 && this.heartbeatSetInterval === undefined) {
       this.heartbeatSetInterval = setInterval(() => {
         this.ocppRequestService
           .requestHandler<HeartbeatRequest, HeartbeatResponse>(this, RequestCommand.HEARTBEAT)
@@ -513,7 +513,7 @@ export class ChargingStation extends EventEmitter {
           this.getHeartbeatInterval(),
         )}`,
       );
-    } else if (this.heartbeatSetInterval) {
+    } else if (this.heartbeatSetInterval !== undefined) {
       logger.info(
         `${this.logPrefix()} Heartbeat already started every ${formatDurationMilliSeconds(
           this.getHeartbeatInterval(),
@@ -604,7 +604,7 @@ export class ChargingStation extends EventEmitter {
   }
 
   public stopMeterValues(connectorId: number) {
-    if (this.getConnectorStatus(connectorId)?.transactionSetInterval) {
+    if (this.getConnectorStatus(connectorId)?.transactionSetInterval !== undefined) {
       clearInterval(this.getConnectorStatus(connectorId)?.transactionSetInterval);
     }
   }
@@ -707,17 +707,7 @@ export class ChargingStation extends EventEmitter {
 
   public bufferMessage(message: string): void {
     this.messageBuffer.add(message);
-    if (this.flushMessageBufferSetInterval === undefined) {
-      this.flushMessageBufferSetInterval = setInterval(() => {
-        if (this.isWebSocketConnectionOpened() === true && this.inAcceptedState() === true) {
-          this.flushMessageBuffer();
-        }
-        if (this.flushMessageBufferSetInterval !== undefined && this.messageBuffer.size === 0) {
-          clearInterval(this.flushMessageBufferSetInterval);
-          delete this.flushMessageBufferSetInterval;
-        }
-      }, Constants.DEFAULT_MESSAGE_BUFFER_FLUSH_INTERVAL);
-    }
+    this.setIntervalFlushMessageBuffer();
   }
 
   public openWSConnection(
@@ -978,6 +968,26 @@ export class ChargingStation extends EventEmitter {
     return false;
   }
 
+  private setIntervalFlushMessageBuffer(): void {
+    if (this.flushMessageBufferSetInterval === undefined) {
+      this.flushMessageBufferSetInterval = setInterval(() => {
+        if (this.isWebSocketConnectionOpened() === true && this.inAcceptedState() === true) {
+          this.flushMessageBuffer();
+        }
+        if (this.messageBuffer.size === 0) {
+          this.clearIntervalFlushMessageBuffer();
+        }
+      }, Constants.DEFAULT_MESSAGE_BUFFER_FLUSH_INTERVAL);
+    }
+  }
+
+  private clearIntervalFlushMessageBuffer() {
+    if (this.flushMessageBufferSetInterval !== undefined) {
+      clearInterval(this.flushMessageBufferSetInterval);
+      delete this.flushMessageBufferSetInterval;
+    }
+  }
+
   private getNumberOfReservableConnectors(): number {
     let numberOfReservableConnectors = 0;
     if (this.hasEvses) {
@@ -2262,7 +2272,7 @@ export class ChargingStation extends EventEmitter {
   }
 
   private stopHeartbeat(): void {
-    if (this.heartbeatSetInterval) {
+    if (this.heartbeatSetInterval !== undefined) {
       clearInterval(this.heartbeatSetInterval);
       delete this.heartbeatSetInterval;
     }
index d9d712981c816790cb4e2b6f1c09fa6cd10e7dac..f62344639c91266c2b2855d2b4fb8d4c6075072d 100644 (file)
@@ -196,7 +196,7 @@ export class OCPPServiceUtils {
   }
 
   public static startHeartbeatInterval(chargingStation: ChargingStation, interval: number): void {
-    if (!chargingStation.heartbeatSetInterval) {
+    if (chargingStation.heartbeatSetInterval === undefined) {
       chargingStation.startHeartbeat();
     } else if (chargingStation.getHeartbeatInterval() !== interval) {
       chargingStation.restartHeartbeat();