From 1b821a640d1f793e969eed6d76e9c20e7cebb626 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Sun, 8 Jan 2023 20:26:55 +0100 Subject: [PATCH] Buffer OCPP message when an error occur at sending it MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- .../ocpp/OCPPRequestService.ts | 54 ++++++++++--------- 1 file changed, 30 insertions(+), 24 deletions(-) diff --git a/src/charging-station/ocpp/OCPPRequestService.ts b/src/charging-station/ocpp/OCPPRequestService.ts index d14fb570..0d95ffa5 100644 --- a/src/charging-station/ocpp/OCPPRequestService.ts +++ b/src/charging-station/ocpp/OCPPRequestService.ts @@ -240,47 +240,52 @@ export default abstract class OCPPRequestService { if (chargingStation.getEnableStatistics() === true) { chargingStation.performanceStatistics.addRequestStatistic(commandName, messageType); } + let sendError = false; // Check if wsConnection opened if (chargingStation.isWebSocketConnectionOpened() === true) { - // Yes: Send Message const beginId = PerformanceStatistics.beginMeasure(commandName as string); - // FIXME: Handle sending error - chargingStation.wsConnection.send(messageToSend); + try { + chargingStation.wsConnection.send(messageToSend); + } catch (error) { + sendError = true; + } PerformanceStatistics.endMeasure(commandName as string, beginId); logger.debug( `${chargingStation.logPrefix()} >> Command '${commandName}' sent ${this.getMessageTypeString( messageType )} payload: ${messageToSend}` ); - } else if (params.skipBufferingOnError === false) { - // Buffer it + } + const wsClosedOrErrored = + chargingStation.isWebSocketConnectionOpened() === false || sendError === true; + if (wsClosedOrErrored && params.skipBufferingOnError === false) { + // Buffer chargingStation.bufferMessage(messageToSend); + // Reject and keep request in the cache + return reject( + new OCPPError( + ErrorType.GENERIC_ERROR, + `WebSocket closed or errored for buffered message id '${messageId}' with content '${messageToSend}'`, + commandName, + (messagePayload as JsonObject)?.details ?? {} + ) + ); + } else if (wsClosedOrErrored) { const ocppError = new OCPPError( ErrorType.GENERIC_ERROR, - `WebSocket closed for buffered message id '${messageId}' with content '${messageToSend}'`, + `WebSocket closed or errored for non buffered message id '${messageId}' with content '${messageToSend}'`, commandName, (messagePayload as JsonObject)?.details ?? {} ); - if (messageType === MessageType.CALL_MESSAGE) { - // Reject it but keep the request in the cache + // Reject response + if (messageType !== MessageType.CALL_MESSAGE) { return reject(ocppError); } + // Reject and remove request from the cache return errorCallback(ocppError, false); - } else { - // Reject it - return errorCallback( - new OCPPError( - ErrorType.GENERIC_ERROR, - `WebSocket closed for non buffered message id '${messageId}' with content '${messageToSend}'`, - commandName, - (messagePayload as JsonObject)?.details ?? {} - ), - false - ); } - // Response? + // Resolve response if (messageType !== MessageType.CALL_MESSAGE) { - // Yes: send Ok return resolve(messagePayload); } @@ -330,9 +335,10 @@ export default abstract class OCPPRequestService { ); } logger.error( - `${chargingStation.logPrefix()} Error occurred when calling command ${commandName} with message data ${JSON.stringify( - messagePayload - )}:`, + `${chargingStation.logPrefix()} Error occurred at ${self.getMessageTypeString( + messageType + )} command ${commandName} with PDU %j:`, + messagePayload, error ); chargingStation.requests.delete(messageId); -- 2.34.1