From d42379d82506b8fe2bb750d81da5a45ac717c763 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Sun, 19 Nov 2023 16:45:41 +0100 Subject: [PATCH] fix: ensure error at WS message sending is handled 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 | 19 ++-- .../ocpp/OCPPRequestService.ts | 88 ++++++++++--------- 2 files changed, 59 insertions(+), 48 deletions(-) diff --git a/src/charging-station/ChargingStation.ts b/src/charging-station/ChargingStation.ts index 60d8f8b4..e12e6d17 100644 --- a/src/charging-station/ChargingStation.ts +++ b/src/charging-station/ChargingStation.ts @@ -1064,14 +1064,17 @@ export class ChargingStation extends EventEmitter { [, , commandName] = JSON.parse(message) as OutgoingRequest; beginId = PerformanceStatistics.beginMeasure(commandName); } - this.wsConnection?.send(message); - isRequest && PerformanceStatistics.endMeasure(commandName!, beginId!); - logger.debug( - `${this.logPrefix()} >> Buffered ${OCPPServiceUtils.getMessageTypeString( - messageType, - )} payload sent: ${message}`, - ); - this.messageBuffer.delete(message); + this.wsConnection?.send(message, (error?: Error) => { + isRequest && PerformanceStatistics.endMeasure(commandName!, beginId!); + if (isNullOrUndefined(error)) { + logger.debug( + `${this.logPrefix()} >> Buffered ${OCPPServiceUtils.getMessageTypeString( + messageType, + )} payload sent: ${message}`, + ); + this.messageBuffer.delete(message); + } + }); } } } diff --git a/src/charging-station/ocpp/OCPPRequestService.ts b/src/charging-station/ocpp/OCPPRequestService.ts index 01fd3d34..f1ea8711 100644 --- a/src/charging-station/ocpp/OCPPRequestService.ts +++ b/src/charging-station/ocpp/OCPPRequestService.ts @@ -23,7 +23,13 @@ import { type ResponseCallback, type ResponseType, } from '../../types'; -import { Constants, cloneObject, handleSendMessageError, logger } from '../../utils'; +import { + Constants, + cloneObject, + handleSendMessageError, + isNullOrUndefined, + logger, +} from '../../utils'; const moduleName = 'OCPPRequestService'; @@ -379,6 +385,23 @@ export abstract class OCPPRequestService { return errorCallback(ocppError, false); }; + const bufferAndRejectWithOcppError = (ocppError: OCPPError): void => { + // Buffer + chargingStation.bufferMessage(messageToSend); + if (messageType === MessageType.CALL_MESSAGE) { + this.cacheRequestPromise( + chargingStation, + messageId, + messagePayload as JsonType, + commandName, + responseCallback, + errorCallback, + ); + } + // Reject and keep request in the cache + return reject(ocppError); + }; + if (chargingStation.stationInfo?.enableStatistics === true) { chargingStation.performanceStatistics?.addRequestStatistic(commandName, messageType); } @@ -403,8 +426,29 @@ export abstract class OCPPRequestService { ); }, OCPPConstants.OCPP_WEBSOCKET_TIMEOUT); chargingStation.wsConnection?.send(messageToSend, (error?: Error) => { + PerformanceStatistics.endMeasure(commandName, beginId); clearTimeout(sendTimeout); - if (error) { + if (isNullOrUndefined(error)) { + logger.debug( + `${chargingStation.logPrefix()} >> Command '${commandName}' sent ${OCPPServiceUtils.getMessageTypeString( + messageType, + )} payload: ${messageToSend}`, + ); + if (messageType === MessageType.CALL_MESSAGE) { + this.cacheRequestPromise( + chargingStation, + messageId, + messagePayload as JsonType, + commandName, + responseCallback, + errorCallback, + ); + } + // Resolve response + if (messageType !== MessageType.CALL_MESSAGE) { + return resolve(messagePayload); + } + } else if (error) { const ocppError = new OCPPError( ErrorType.GENERIC_ERROR, `WebSocket errored for ${ @@ -414,30 +458,11 @@ export abstract class OCPPRequestService { { name: error.name, message: error.message, stack: error.stack }, ); if (params?.skipBufferingOnError === false) { - // Buffer - chargingStation.bufferMessage(messageToSend); - // Reject and keep request in the cache - return reject(ocppError); + return bufferAndRejectWithOcppError(ocppError); } return rejectWithOcppError(ocppError); } }); - if (messageType === MessageType.CALL_MESSAGE) { - this.cacheRequestPromise( - chargingStation, - messageId, - messagePayload as JsonType, - commandName, - responseCallback, - errorCallback, - ); - } - logger.debug( - `${chargingStation.logPrefix()} >> Command '${commandName}' sent ${OCPPServiceUtils.getMessageTypeString( - messageType, - )} payload: ${messageToSend}`, - ); - PerformanceStatistics.endMeasure(commandName, beginId); } else { const ocppError = new OCPPError( ErrorType.GENERIC_ERROR, @@ -448,27 +473,10 @@ export abstract class OCPPRequestService { (messagePayload as JsonObject)?.details ?? Constants.EMPTY_FROZEN_OBJECT, ); if (params?.skipBufferingOnError === false) { - // Buffer - chargingStation.bufferMessage(messageToSend); - if (messageType === MessageType.CALL_MESSAGE) { - this.cacheRequestPromise( - chargingStation, - messageId, - messagePayload as JsonType, - commandName, - responseCallback, - errorCallback, - ); - } - // Reject and keep request in the cache - return reject(ocppError); + return bufferAndRejectWithOcppError(ocppError); } return rejectWithOcppError(ocppError); } - // Resolve response - if (messageType !== MessageType.CALL_MESSAGE) { - return resolve(messagePayload); - } }); } throw new OCPPError( -- 2.34.1