From 764d2c911026befed9b15af0087eafcf7a3afc5e Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Fri, 20 Jan 2023 15:46:52 +0100 Subject: [PATCH] Fix OCPP messages buffering handling MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- src/charging-station/ocpp/OCPPRequestService.ts | 12 ++++++------ src/utils/Utils.ts | 9 ++++++++- 2 files changed, 14 insertions(+), 7 deletions(-) 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); }); -- 2.34.1