[, , 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);
+ }
+ });
}
}
}
type ResponseCallback,
type ResponseType,
} from '../../types';
-import { Constants, cloneObject, handleSendMessageError, logger } from '../../utils';
+import {
+ Constants,
+ cloneObject,
+ handleSendMessageError,
+ isNullOrUndefined,
+ logger,
+} from '../../utils';
const moduleName = '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);
}
);
}, 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 ${
{ 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,
(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(