From: Jérôme Benoit Date: Fri, 20 Jan 2023 14:46:52 +0000 (+0100) Subject: Fix OCPP messages buffering handling X-Git-Tag: v1.1.91~3 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=764d2c911026befed9b15af0087eafcf7a3afc5e;p=e-mobility-charging-stations-simulator.git Fix OCPP messages buffering handling Signed-off-by: Jérôme Benoit --- diff --git a/src/charging-station/ocpp/OCPPRequestService.ts b/src/charging-station/ocpp/OCPPRequestService.ts index 023d5f95..6ce5e3a7 100644 --- a/src/charging-station/ocpp/OCPPRequestService.ts +++ b/src/charging-station/ocpp/OCPPRequestService.ts @@ -231,6 +231,9 @@ export default abstract class OCPPRequestService { // Send a message through wsConnection return Utils.promiseWithTimeout( new Promise((resolve, reject) => { + if (chargingStation.getEnableStatistics() === true) { + chargingStation.performanceStatistics.addRequestStatistic(commandName, messageType); + } const messageToSend = this.buildMessageToSend( chargingStation, messageId, @@ -240,12 +243,10 @@ export default abstract class OCPPRequestService { responseCallback, errorCallback ); - if (chargingStation.getEnableStatistics() === true) { - chargingStation.performanceStatistics.addRequestStatistic(commandName, messageType); - } let sendError = false; // Check if wsConnection opened - if (chargingStation.isWebSocketConnectionOpened() === true) { + const wsOpened = chargingStation.isWebSocketConnectionOpened() === true; + if (wsOpened) { const beginId = PerformanceStatistics.beginMeasure(commandName as string); try { chargingStation.wsConnection.send(messageToSend); @@ -265,8 +266,7 @@ export default abstract class OCPPRequestService { } PerformanceStatistics.endMeasure(commandName as string, beginId); } - const wsClosedOrErrored = - chargingStation.isWebSocketConnectionOpened() === false || sendError === true; + const wsClosedOrErrored = !wsOpened || sendError === true; if (wsClosedOrErrored && params.skipBufferingOnError === false) { // Buffer chargingStation.bufferMessage(messageToSend); diff --git a/src/utils/Utils.ts b/src/utils/Utils.ts index 6123215c..8f63eb68 100644 --- a/src/utils/Utils.ts +++ b/src/utils/Utils.ts @@ -1,4 +1,5 @@ import crypto from 'node:crypto'; +import util from 'node:util'; import clone from 'just-clone'; @@ -241,6 +242,10 @@ export default class Utils { return delay + randomSum; } + public static isPromisePending(promise: Promise): boolean { + return util.inspect(promise).includes('pending'); + } + public static async promiseWithTimeout( promise: Promise, timeoutMs: number, @@ -252,7 +257,9 @@ export default class Utils { // Create a timeout promise that rejects in timeout milliseconds const timeoutPromise = new Promise((_, reject) => { setTimeout(() => { - timeoutCallback(); + if (Utils.isPromisePending(promise)) { + timeoutCallback(); + } reject(timeoutError); }, timeoutMs); });