From: Jérôme Benoit Date: Sun, 3 Oct 2021 02:40:54 +0000 (+0200) Subject: Convert OCPP message buffer to a Set X-Git-Tag: v1.1.24~3 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=8e24227340572ec9f086d966c079ead58cc60ef5;p=e-mobility-charging-stations-simulator.git Convert OCPP message buffer to a Set Signed-off-by: Jérôme Benoit --- 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