From a2d1c0f1f3008198a96d932bab7c933c084ffeca Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Sun, 1 May 2022 07:29:50 +0200 Subject: [PATCH] Improve OCPP incoming requests 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/ChargingStation.ts | 44 +++++++++---------- .../ocpp/OCPPRequestService.ts | 12 ++--- 2 files changed, 27 insertions(+), 29 deletions(-) diff --git a/src/charging-station/ChargingStation.ts b/src/charging-station/ChargingStation.ts index 1340e0a3..ba5dc1a6 100644 --- a/src/charging-station/ChargingStation.ts +++ b/src/charging-station/ChargingStation.ts @@ -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 )); } } diff --git a/src/charging-station/ocpp/OCPPRequestService.ts b/src/charging-station/ocpp/OCPPRequestService.ts index e7140905..e906daf8 100644 --- a/src/charging-station/ocpp/OCPPRequestService.ts +++ b/src/charging-station/ocpp/OCPPRequestService.ts @@ -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, - 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, ]); -- 2.34.1