From 3ba2381e6a5415267d9146fbac658a6c187ecb2b Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Tue, 2 Feb 2021 09:10:19 +0100 Subject: [PATCH] Move message buffer code to charging station. 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 | 16 ++++++++++++++++ src/charging-station/ocpp/OCPPRequestService.ts | 15 ++------------- 2 files changed, 18 insertions(+), 13 deletions(-) diff --git a/src/charging-station/ChargingStation.ts b/src/charging-station/ChargingStation.ts index efedd101..4f19f2d9 100644 --- a/src/charging-station/ChargingStation.ts +++ b/src/charging-station/ChargingStation.ts @@ -319,6 +319,22 @@ export default class ChargingStation { } } + public addMessageToBuffer(message: string): void { + let dups = false; + // Handle dups in buffer + for (const bufferedMessage of this.messageQueue) { + // Same message + if (message === bufferedMessage) { + dups = true; + break; + } + } + if (!dups) { + // Buffer message + this.messageQueue.push(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; diff --git a/src/charging-station/ocpp/OCPPRequestService.ts b/src/charging-station/ocpp/OCPPRequestService.ts index 89248822..6b19d061 100644 --- a/src/charging-station/ocpp/OCPPRequestService.ts +++ b/src/charging-station/ocpp/OCPPRequestService.ts @@ -55,19 +55,8 @@ export default abstract class OCPPRequestService { // Yes: Send Message this.chargingStation.wsConnection.send(messageToSend); } else if (commandName !== RequestCommand.BOOT_NOTIFICATION) { - let dups = false; - // Handle dups in buffer - for (const message of this.chargingStation.messageQueue) { - // Same message - if (messageToSend === message) { - dups = true; - break; - } - } - if (!dups) { - // Buffer message - this.chargingStation.messageQueue.push(messageToSend); - } + // Buffer it + this.chargingStation.addMessageToBuffer(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