X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fcharging-station%2Focpp%2FOCPPRequestService.ts;h=e00e954c9bfebe5be9e6dd7c6200182559248d90;hb=e8a92d57004dcddba34fa4a510226b56e11a8cdb;hp=e58a1d43f71f97509c0d8d4a3edd37b00b780bb8;hpb=2896e06dc8d72adf7150b23c941079f622f6f37c;p=e-mobility-charging-stations-simulator.git diff --git a/src/charging-station/ocpp/OCPPRequestService.ts b/src/charging-station/ocpp/OCPPRequestService.ts index e58a1d43..e00e954c 100644 --- a/src/charging-station/ocpp/OCPPRequestService.ts +++ b/src/charging-station/ocpp/OCPPRequestService.ts @@ -4,8 +4,7 @@ import ajvFormats from 'ajv-formats'; import { type OCPPResponseService, OCPPServiceUtils } from './internal'; import type { ChargingStation } from '../../charging-station'; import { OCPPError } from '../../exception'; -// import { PerformanceStatistics } from '../../performance'; -import { PerformanceStatistics } from '../../performance/PerformanceStatistics'; +import { PerformanceStatistics } from '../../performance'; import { type EmptyObject, type ErrorCallback, @@ -24,9 +23,7 @@ import { type ResponseCallback, type ResponseType, } from '../../types'; -import { Constants } from '../../utils/Constants'; -import { logger } from '../../utils/Logger'; -import { Utils } from '../../utils/Utils'; +import { Constants, Utils, logger } from '../../utils'; const moduleName = 'OCPPRequestService'; @@ -233,6 +230,62 @@ export abstract class OCPPRequestService { // Send a message through wsConnection return Utils.promiseWithTimeout( new Promise((resolve, reject) => { + /** + * Function that will receive the request's response + * + * @param payload - + * @param requestPayload - + */ + const responseCallback = (payload: JsonType, requestPayload: JsonType): void => { + if (chargingStation.getEnableStatistics() === true) { + chargingStation.performanceStatistics?.addRequestStatistic( + commandName, + MessageType.CALL_RESULT_MESSAGE + ); + } + // Handle the request's response + self.ocppResponseService + .responseHandler( + chargingStation, + commandName as RequestCommand, + payload, + requestPayload + ) + .then(() => { + resolve(payload); + }) + .catch((error) => { + reject(error); + }) + .finally(() => { + chargingStation.requests.delete(messageId); + }); + }; + + /** + * Function that will receive the request's error response + * + * @param error - + * @param requestStatistic - + */ + const errorCallback = (error: OCPPError, requestStatistic = true): void => { + if (requestStatistic === true && chargingStation.getEnableStatistics() === true) { + chargingStation.performanceStatistics?.addRequestStatistic( + commandName, + MessageType.CALL_ERROR_MESSAGE + ); + } + logger.error( + `${chargingStation.logPrefix()} Error occurred at ${OCPPServiceUtils.getMessageTypeString( + messageType + )} command ${commandName} with PDU %j:`, + messagePayload, + error + ); + chargingStation.requests.delete(messageId); + reject(error); + }; + if (chargingStation.getEnableStatistics() === true) { chargingStation.performanceStatistics?.addRequestStatistic(commandName, messageType); } @@ -278,7 +331,7 @@ export abstract class OCPPRequestService { ErrorType.GENERIC_ERROR, `WebSocket closed or errored for buffered message id '${messageId}' with content '${messageToSend}'`, commandName, - (messagePayload as JsonObject)?.details ?? {} + (messagePayload as JsonObject)?.details ?? Constants.EMPTY_FREEZED_OBJECT ) ); } else if (wsClosedOrErrored) { @@ -286,7 +339,7 @@ export abstract class OCPPRequestService { ErrorType.GENERIC_ERROR, `WebSocket closed or errored for non buffered message id '${messageId}' with content '${messageToSend}'`, commandName, - (messagePayload as JsonObject)?.details ?? {} + (messagePayload as JsonObject)?.details ?? Constants.EMPTY_FREEZED_OBJECT ); // Reject response if (messageType !== MessageType.CALL_MESSAGE) { @@ -299,69 +352,13 @@ export abstract class OCPPRequestService { if (messageType !== MessageType.CALL_MESSAGE) { return resolve(messagePayload); } - - /** - * Function that will receive the request's response - * - * @param payload - - * @param requestPayload - - */ - function responseCallback(payload: JsonType, requestPayload: JsonType): void { - if (chargingStation.getEnableStatistics() === true) { - chargingStation.performanceStatistics?.addRequestStatistic( - commandName, - MessageType.CALL_RESULT_MESSAGE - ); - } - // Handle the request's response - self.ocppResponseService - .responseHandler( - chargingStation, - commandName as RequestCommand, - payload, - requestPayload - ) - .then(() => { - resolve(payload); - }) - .catch((error) => { - reject(error); - }) - .finally(() => { - chargingStation.requests.delete(messageId); - }); - } - - /** - * Function that will receive the request's error response - * - * @param error - - * @param requestStatistic - - */ - function errorCallback(error: OCPPError, requestStatistic = true): void { - if (requestStatistic === true && chargingStation.getEnableStatistics() === true) { - chargingStation.performanceStatistics?.addRequestStatistic( - commandName, - MessageType.CALL_ERROR_MESSAGE - ); - } - logger.error( - `${chargingStation.logPrefix()} Error occurred at ${OCPPServiceUtils.getMessageTypeString( - messageType - )} command ${commandName} with PDU %j:`, - messagePayload, - error - ); - chargingStation.requests.delete(messageId); - reject(error); - } }), Constants.OCPP_WEBSOCKET_TIMEOUT, new OCPPError( ErrorType.GENERIC_ERROR, `Timeout for message id '${messageId}'`, commandName, - (messagePayload as JsonObject)?.details ?? {} + (messagePayload as JsonObject)?.details ?? Constants.EMPTY_FREEZED_OBJECT ), () => { messageType === MessageType.CALL_MESSAGE && chargingStation.requests.delete(messageId);