Fix OCPP messages buffering handling
authorJérôme Benoit <jerome.benoit@sap.com>
Fri, 20 Jan 2023 14:46:52 +0000 (15:46 +0100)
committerJérôme Benoit <jerome.benoit@sap.com>
Fri, 20 Jan 2023 14:46:52 +0000 (15:46 +0100)
Signed-off-by: Jérôme Benoit <jerome.benoit@sap.com>
src/charging-station/ocpp/OCPPRequestService.ts
src/utils/Utils.ts

index 023d5f95174bc07df34f3f5dbc49e214f1191d1a..6ce5e3a7f6d27e9f5447073dfe36ccf7abbda686 100644 (file)
@@ -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);
index 6123215c4d8a107640b97de7b928847ec61c61c3..8f63eb681940925a075c0ae6f29fc66f7781e4a3 100644 (file)
@@ -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<unknown>): boolean {
+    return util.inspect(promise).includes('pending');
+  }
+
   public static async promiseWithTimeout<T>(
     promise: Promise<T>,
     timeoutMs: number,
@@ -252,7 +257,9 @@ export default class Utils {
     // Create a timeout promise that rejects in timeout milliseconds
     const timeoutPromise = new Promise<never>((_, reject) => {
       setTimeout(() => {
-        timeoutCallback();
+        if (Utils.isPromisePending(promise)) {
+          timeoutCallback();
+        }
         reject(timeoutError);
       }, timeoutMs);
     });