From 2a2ad81b23c64c05c93a16e0e255ae1350a5c862 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Wed, 29 Nov 2023 20:05:15 +0100 Subject: [PATCH] refactor: cleanup buffered messages handling code MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- src/charging-station/ChargingStation.ts | 40 ++++++++++++------- src/charging-station/ocpp/OCPPServiceUtils.ts | 2 +- 2 files changed, 26 insertions(+), 16 deletions(-) diff --git a/src/charging-station/ChargingStation.ts b/src/charging-station/ChargingStation.ts index ba430878..6acd4018 100644 --- a/src/charging-station/ChargingStation.ts +++ b/src/charging-station/ChargingStation.ts @@ -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(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; } diff --git a/src/charging-station/ocpp/OCPPServiceUtils.ts b/src/charging-station/ocpp/OCPPServiceUtils.ts index d9d71298..f6234463 100644 --- a/src/charging-station/ocpp/OCPPServiceUtils.ts +++ b/src/charging-station/ocpp/OCPPServiceUtils.ts @@ -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(); -- 2.34.1