From 8e24227340572ec9f086d966c079ead58cc60ef5 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Sun, 3 Oct 2021 04:40:54 +0200 Subject: [PATCH] Convert OCPP message buffer to a Set MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- .../AutomaticTransactionGenerator.ts | 2 +- src/charging-station/ChargingStation.ts | 30 ++++++------------- .../ocpp/OCPPRequestService.ts | 2 +- 3 files changed, 11 insertions(+), 23 deletions(-) diff --git a/src/charging-station/AutomaticTransactionGenerator.ts b/src/charging-station/AutomaticTransactionGenerator.ts index f444018a..ccc30d0c 100644 --- a/src/charging-station/AutomaticTransactionGenerator.ts +++ b/src/charging-station/AutomaticTransactionGenerator.ts @@ -40,7 +40,7 @@ export default class AutomaticTransactionGenerator { } private startConnectors(): void { - if (this.connectorsStatus?.size !== 0 && this.connectorsStatus.size !== this.chargingStation.getNumberOfConnectors()) { + if (this.connectorsStatus?.size > 0 && this.connectorsStatus.size !== this.chargingStation.getNumberOfConnectors()) { this.connectorsStatus.clear(); } for (const connectorId of this.chargingStation.connectors.keys()) { diff --git a/src/charging-station/ChargingStation.ts b/src/charging-station/ChargingStation.ts index edcd312d..dde84562 100644 --- a/src/charging-station/ChargingStation.ts +++ b/src/charging-station/ChargingStation.ts @@ -52,7 +52,7 @@ export default class ChargingStation { private bootNotificationResponse!: BootNotificationResponse | null; private connectorsConfigurationHash!: string; private ocppIncomingRequestService!: OCPPIncomingRequestService; - private readonly messageQueue: string[]; + private readonly messageBuffer: Set; private wsConnectionUrl!: URL; private wsConnectionRestarted: boolean; private stopped: boolean; @@ -71,7 +71,7 @@ export default class ChargingStation { this.autoReconnectRetryCount = 0; this.requests = new Map(); - this.messageQueue = new Array(); + this.messageBuffer = new Set(); this.authorizedTags = this.getAuthorizedTags(); } @@ -411,28 +411,16 @@ export default class ChargingStation { this.stopMeterValues(connectorId); } - public addToMessageQueue(message: string): void { - let dups = false; - // Handle dups in message queue - for (const bufferedMessage of this.messageQueue) { - // Message already in the queue - if (message === bufferedMessage) { - dups = true; - break; - } - } - if (!dups) { - // Queue message - this.messageQueue.push(message); - } + public bufferMessage(message: string): void { + this.messageBuffer.add(message); } - private flushMessageQueue() { - if (!Utils.isEmptyArray(this.messageQueue)) { - this.messageQueue.forEach((message, index) => { - this.messageQueue.splice(index, 1); + private flushMessageBuffer() { + if (this.messageBuffer.size > 0) { + this.messageBuffer.forEach((message) => { // TODO: evaluate the need to track performance this.wsConnection.send(message); + this.messageBuffer.delete(message); }); } } @@ -627,7 +615,7 @@ export default class ChargingStation { await this.startMessageSequence(); this.stopped && (this.stopped = false); if (this.wsConnectionRestarted && this.isWebSocketConnectionOpened()) { - this.flushMessageQueue(); + this.flushMessageBuffer(); } } 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 b72b596c..4996b9e7 100644 --- a/src/charging-station/ocpp/OCPPRequestService.ts +++ b/src/charging-station/ocpp/OCPPRequestService.ts @@ -42,7 +42,7 @@ export default abstract class OCPPRequestService { PerformanceStatistics.endMeasure(commandName, beginId); } else if (!skipBufferingOnError) { // Buffer it - this.chargingStation.addToMessageQueue(messageToSend); + this.chargingStation.bufferMessage(messageToSend); const ocppError = new OCPPError(ErrorType.GENERIC_ERROR, `WebSocket closed for buffered message id '${messageId}' with content '${messageToSend}'`, commandParams?.details ?? {}); if (messageType === MessageType.CALL_MESSAGE) { // Reject it but keep the request in the cache -- 2.34.1