From 77f00f84dff47dc35d2fca1afb3ca574dd3954b5 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Sat, 6 Feb 2021 13:47:37 +0100 Subject: [PATCH] Cleanups to message queue 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 | 18 +++++++++++------- .../ocpp/OCPPRequestService.ts | 2 +- 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/src/charging-station/ChargingStation.ts b/src/charging-station/ChargingStation.ts index f60fc79e..0c39613b 100644 --- a/src/charging-station/ChargingStation.ts +++ b/src/charging-station/ChargingStation.ts @@ -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()})`); diff --git a/src/charging-station/ocpp/OCPPRequestService.ts b/src/charging-station/ocpp/OCPPRequestService.ts index 6ebb4e38..a81974ad 100644 --- a/src/charging-station/ocpp/OCPPRequestService.ts +++ b/src/charging-station/ocpp/OCPPRequestService.ts @@ -55,7 +55,7 @@ export default abstract class OCPPRequestService { this.chargingStation.wsConnection.send(messageToSend); } else if (commandName !== RequestCommand.BOOT_NOTIFICATION) { // Buffer it - this.chargingStation.addMessageToBuffer(messageToSend); + this.chargingStation.addToMessageQueue(messageToSend); // Reject it return rejectCallback(new OCPPError(commandParams.code ? commandParams.code : ErrorType.GENERIC_ERROR, commandParams.message ? commandParams.message : `WebSocket closed for message id '${messageId}' with content '${messageToSend}', message buffered`, commandParams.details ? commandParams.details : {})); } -- 2.34.1