From: Jérôme Benoit Date: Wed, 29 Nov 2023 19:05:15 +0000 (+0100) Subject: refactor: cleanup buffered messages handling code X-Git-Tag: v1.2.28~9 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=2a2ad81b23c64c05c93a16e0e255ae1350a5c862;p=e-mobility-charging-stations-simulator.git refactor: cleanup buffered messages handling code Signed-off-by: Jérôme Benoit --- 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();