+ };
+
+ if (chargingStation.stationInfo?.enableStatistics === true) {
+ chargingStation.performanceStatistics?.addRequestStatistic(commandName, messageType);
+ }
+ const messageToSend = this.buildMessageToSend(
+ chargingStation,
+ messageId,
+ messagePayload,
+ messageType,
+ commandName,
+ );
+ // Check if wsConnection opened
+ if (chargingStation.isWebSocketConnectionOpened() === true) {
+ const beginId = PerformanceStatistics.beginMeasure(commandName);
+ const sendTimeout = setTimeout(() => {
+ return errorCallback(
+ new OCPPError(
+ ErrorType.GENERIC_ERROR,
+ `Timeout for message id '${messageId}'`,
+ commandName,
+ (messagePayload as JsonObject)?.details ?? Constants.EMPTY_FROZEN_OBJECT,
+ ),
+ false,
+ );
+ }, OCPPConstants.OCPP_WEBSOCKET_TIMEOUT);
+ chargingStation.wsConnection?.send(messageToSend, (error?: Error) => {
+ clearTimeout(sendTimeout);
+ if (error) {
+ const ocppError = new OCPPError(
+ ErrorType.GENERIC_ERROR,
+ `WebSocket errored for ${
+ params?.skipBufferingOnError === false ? '' : 'non '
+ }buffered message id '${messageId}' with content '${messageToSend}'`,
+ commandName,
+ { name: error.name, message: error.message, stack: error.stack } ??
+ Constants.EMPTY_FROZEN_OBJECT,
+ );
+ if (params?.skipBufferingOnError === false) {
+ // Buffer
+ chargingStation.bufferMessage(messageToSend);
+ // Reject and keep request in the cache
+ return reject(ocppError);
+ }
+ // Reject response
+ if (messageType !== MessageType.CALL_MESSAGE) {
+ return reject(ocppError);
+ }
+ // Reject and remove request from the cache
+ return errorCallback(ocppError, false);
+ }
+ });
+ 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 {
+ 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(
+ new OCPPError(
+ ErrorType.GENERIC_ERROR,
+ `WebSocket closed for ${
+ params?.skipBufferingOnError === false ? '' : 'non '
+ }buffered message id '${messageId}' with content '${messageToSend}'`,
+ commandName,
+ (messagePayload as JsonObject)?.details ?? Constants.EMPTY_FROZEN_OBJECT,
+ ),
+ );
+ }
+ // Resolve response
+ if (messageType !== MessageType.CALL_MESSAGE) {
+ return resolve(messagePayload);