Improve OCPP incoming requests handling
authorJérôme Benoit <jerome.benoit@sap.com>
Sun, 1 May 2022 05:29:50 +0000 (07:29 +0200)
committerJérôme Benoit <jerome.benoit@sap.com>
Sun, 1 May 2022 05:29:50 +0000 (07:29 +0200)
Signed-off-by: Jérôme Benoit <jerome.benoit@piment-noir.org>
src/charging-station/ChargingStation.ts
src/charging-station/ocpp/OCPPRequestService.ts

index 1340e0a3e8cffa5d4047ab910d78ab86b2f78c5e..ba5dc1a6a8c419a7a9c3f3628f60dd95430a242e 100644 (file)
@@ -1577,7 +1577,7 @@ export default class ChargingStation {
     let errorMessage: string;
     let errorDetails: JsonType;
     let responseCallback: (payload: JsonType, requestPayload: JsonType) => void;
-    let rejectCallback: (error: OCPPError, requestStatistic?: boolean) => void;
+    let errorCallback: (error: OCPPError, requestStatistic?: boolean) => void;
     let requestCommandName: RequestCommand | IncomingRequestCommand;
     let requestPayload: JsonType;
     let cachedRequest: CachedRequest;
@@ -1609,6 +1609,15 @@ export default class ChargingStation {
           // Outcome Message
           case MessageType.CALL_RESULT_MESSAGE:
             [, , commandPayload] = request as Response;
+            if (!this.requests.has(messageId)) {
+              // Error
+              throw new OCPPError(
+                ErrorType.INTERNAL_ERROR,
+                `Response for unknown message id ${messageId}`,
+                null,
+                commandPayload
+              );
+            }
             // Respond
             cachedRequest = this.requests.get(messageId);
             if (Utils.isIterable(cachedRequest)) {
@@ -1626,23 +1635,23 @@ export default class ChargingStation {
                 requestCommandName ?? ''
               }' received response payload: ${JSON.stringify(request)}`
             );
-            if (!responseCallback) {
+            responseCallback(commandPayload, requestPayload);
+            break;
+          // Error Message
+          case MessageType.CALL_ERROR_MESSAGE:
+            [, , errorType, errorMessage, errorDetails] = request as ErrorResponse;
+            if (!this.requests.has(messageId)) {
               // Error
               throw new OCPPError(
                 ErrorType.INTERNAL_ERROR,
-                `Response for unknown message id ${messageId}`,
+                `Error response for unknown message id ${messageId}`,
                 null,
-                commandPayload
+                { errorType, errorMessage, errorDetails }
               );
             }
-            responseCallback(commandPayload, requestPayload);
-            break;
-          // Error Message
-          case MessageType.CALL_ERROR_MESSAGE:
-            [, , errorType, errorMessage, errorDetails] = request as ErrorResponse;
             cachedRequest = this.requests.get(messageId);
             if (Utils.isIterable(cachedRequest)) {
-              [, rejectCallback, requestCommandName] = cachedRequest;
+              [, errorCallback, requestCommandName] = cachedRequest;
             } else {
               throw new OCPPError(
                 ErrorType.PROTOCOL_ERROR,
@@ -1656,18 +1665,7 @@ export default class ChargingStation {
                 requestCommandName ?? ''
               }' received error payload: ${JSON.stringify(request)}`
             );
-            if (!rejectCallback) {
-              // Error
-              throw new OCPPError(
-                ErrorType.INTERNAL_ERROR,
-                `Error response for unknown message id ${messageId}`,
-                null,
-                { errorType, errorMessage, errorDetails }
-              );
-            }
-            rejectCallback(
-              new OCPPError(errorType, errorMessage, requestCommandName, errorDetails)
-            );
+            errorCallback(new OCPPError(errorType, errorMessage, requestCommandName, errorDetails));
             break;
           // Error
           default:
@@ -1695,7 +1693,7 @@ export default class ChargingStation {
         (await this.ocppRequestService.sendError(
           messageId,
           error as OCPPError,
-          Utils.isString(commandName) ? commandName : requestCommandName ?? null
+          commandName ?? requestCommandName ?? null
         ));
     }
   }
index e71409056552773921a8f45cf710454e6e354080..e906daf8a45169f485cebfe69532a8365160c7b4 100644 (file)
@@ -142,7 +142,7 @@ export default abstract class OCPPRequestService {
             messageType,
             commandName,
             responseCallback,
-            rejectCallback
+            errorCallback
           );
           if (this.chargingStation.getEnableStatistics()) {
             this.chargingStation.performanceStatistics.addRequestStatistic(
@@ -175,10 +175,10 @@ export default abstract class OCPPRequestService {
               // Reject it but keep the request in the cache
               return reject(ocppError);
             }
-            return rejectCallback(ocppError, false);
+            return errorCallback(ocppError, false);
           } else {
             // Reject it
-            return rejectCallback(
+            return errorCallback(
               new OCPPError(
                 ErrorType.GENERIC_ERROR,
                 `WebSocket closed for non buffered message id '${messageId}' with content '${messageToSend}'`,
@@ -231,7 +231,7 @@ export default abstract class OCPPRequestService {
            * @param error
            * @param requestStatistic
            */
-          function rejectCallback(error: OCPPError, requestStatistic = true): void {
+          function errorCallback(error: OCPPError, requestStatistic = true): void {
             if (requestStatistic && self.chargingStation.getEnableStatistics()) {
               self.chargingStation.performanceStatistics.addRequestStatistic(
                 commandName,
@@ -274,7 +274,7 @@ export default abstract class OCPPRequestService {
     messageType: MessageType,
     commandName?: RequestCommand | IncomingRequestCommand,
     responseCallback?: (payload: JsonType, requestPayload: JsonType) => Promise<void>,
-    rejectCallback?: (error: OCPPError, requestStatistic?: boolean) => void
+    errorCallback?: (error: OCPPError, requestStatistic?: boolean) => void
   ): string {
     let messageToSend: string;
     // Type of message
@@ -284,7 +284,7 @@ export default abstract class OCPPRequestService {
         // Build request
         this.chargingStation.requests.set(messageId, [
           responseCallback,
-          rejectCallback,
+          errorCallback,
           commandName,
           messagePayload as JsonType,
         ]);