From: Jérôme Benoit Date: Sat, 6 Feb 2021 12:47:37 +0000 (+0100) Subject: Cleanups to message queue handling code. X-Git-Tag: v1.0.1-0~104^2~16 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=77f00f84dff47dc35d2fca1afb3ca574dd3954b5;p=e-mobility-charging-stations-simulator.git Cleanups to message queue handling code. Signed-off-by: Jérôme Benoit --- 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 : {})); }