From: Jérôme Benoit Date: Sun, 19 Nov 2023 15:45:41 +0000 (+0100) Subject: fix: ensure error at WS message sending is handled X-Git-Tag: v1.2.25~4 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=d42379d82506b8fe2bb750d81da5a45ac717c763;p=e-mobility-charging-stations-simulator.git fix: ensure error at WS message sending is handled Signed-off-by: Jérôme Benoit --- 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(