import OCPPError from './OCPPError';
import OCPPResponseService from './OCPPResponseService';
import PerformanceStatistics from '../../performance/PerformanceStatistics';
+import Utils from '../../utils/Utils';
import logger from '../../utils/Logger';
export default abstract class OCPPRequestService {
// eslint-disable-next-line @typescript-eslint/no-this-alias
const self = this;
// Send a message through wsConnection
- return new Promise((resolve, reject) => {
+ return Utils.promiseWithTimeout(new Promise((resolve, reject) => {
const messageToSend = this.buildMessageToSend(messageId, commandParams, messageType, commandName, responseCallback, rejectCallback);
if (this.chargingStation.getEnableStatistics()) {
this.chargingStation.performanceStatistics.addRequestStatistic(commandName, messageType);
// Yes: send Ok
return resolve(commandParams);
}
- // Send timeout
- setTimeout(() => rejectCallback(new OCPPError(ErrorType.GENERIC_ERROR, `Timeout for message id '${messageId}' with content '${messageToSend}'`, commandParams?.details ?? {}), false), Constants.OCPP_SOCKET_TIMEOUT);
/**
* Function that will receive the request's response
if (self.chargingStation.getEnableStatistics()) {
self.chargingStation.performanceStatistics.addRequestStatistic(commandName, MessageType.CALL_RESULT_MESSAGE);
}
- // Send the response
- await self.ocppResponseService.handleResponse(commandName as RequestCommand, payload, requestPayload);
- self.chargingStation.requests.delete(messageId);
- resolve(payload);
+ // Handle the request's response
+ try {
+ await self.ocppResponseService.handleResponse(commandName as RequestCommand, payload, requestPayload);
+ resolve(payload);
+ } catch (error) {
+ reject(error);
+ throw error;
+ } finally {
+ self.chargingStation.requests.delete(messageId);
+ }
}
/**
self.chargingStation.requests.delete(messageId);
reject(error);
}
+ }), Constants.OCPP_WEBSOCKET_TIMEOUT, new OCPPError(ErrorType.GENERIC_ERROR, `Timeout for message id '${messageId}'`, commandParams?.details ?? {}), () => {
+ messageType === MessageType.CALL_MESSAGE && this.chargingStation.requests.delete(messageId);
});
}