X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fcharging-station%2Focpp%2FOCPPRequestService.ts;h=a3b78ca63ec8e40d44a07c7a12f9689ed38eee63;hb=933e253b07b60f598e94a40cb817e334fb0f1995;hp=a50500dc24047f7a7eb3276a03d725cedf69fb44;hpb=d8b1fab113850cb6c18747bd39d482d39a611b3f;p=e-mobility-charging-stations-simulator.git diff --git a/src/charging-station/ocpp/OCPPRequestService.ts b/src/charging-station/ocpp/OCPPRequestService.ts index a50500dc..a3b78ca6 100644 --- a/src/charging-station/ocpp/OCPPRequestService.ts +++ b/src/charging-station/ocpp/OCPPRequestService.ts @@ -1,16 +1,16 @@ import Ajv, { type JSONSchemaType } from 'ajv'; import ajvFormats from 'ajv-formats'; -import { OCPPConstants, type OCPPResponseService, OCPPServiceUtils } from './internal'; +import { OCPPConstants } from './OCPPConstants'; +import type { OCPPResponseService } from './OCPPResponseService'; +import { OCPPServiceUtils } from './OCPPServiceUtils'; import type { ChargingStation } from '../../charging-station'; import { OCPPError } from '../../exception'; import { PerformanceStatistics } from '../../performance'; import { - type EmptyObject, type ErrorCallback, type ErrorResponse, ErrorType, - type HandleErrorParams, type IncomingRequestCommand, type JsonObject, type JsonType, @@ -23,10 +23,16 @@ import { type ResponseCallback, type ResponseType, } from '../../types'; -import { Constants, Utils, logger } from '../../utils'; +import { Constants, Utils, handleSendMessageError, logger } from '../../utils'; const moduleName = 'OCPPRequestService'; +const defaultRequestParams: RequestParams = { + skipBufferingOnError: false, + triggerMessage: false, + throwError: false, +}; + export abstract class OCPPRequestService { private static instance: OCPPRequestService | null = null; private readonly version: OCPPVersion; @@ -127,7 +133,7 @@ export abstract class OCPPRequestService { commandName ); } catch (error) { - this.handleSendMessageError(chargingStation, commandName, error as Error, { + handleSendMessageError(chargingStation, commandName, error as Error, { throwError: true, }); } @@ -149,7 +155,7 @@ export abstract class OCPPRequestService { commandName ); } catch (error) { - this.handleSendMessageError(chargingStation, commandName, error as Error); + handleSendMessageError(chargingStation, commandName, error as Error); } } @@ -158,12 +164,12 @@ export abstract class OCPPRequestService { messageId: string, messagePayload: JsonType, commandName: RequestCommand, - params: RequestParams = { - skipBufferingOnError: false, - triggerMessage: false, - throwError: false, - } + params: RequestParams = defaultRequestParams ): Promise { + params = { + ...defaultRequestParams, + ...params, + }; try { return await this.internalSendMessage( chargingStation, @@ -174,7 +180,7 @@ export abstract class OCPPRequestService { params ); } catch (error) { - this.handleSendMessageError(chargingStation, commandName, error as Error, { + handleSendMessageError(chargingStation, commandName, error as Error, { throwError: params.throwError, }); } @@ -260,18 +266,19 @@ export abstract class OCPPRequestService { messagePayload: JsonType | OCPPError, messageType: MessageType, commandName: RequestCommand | IncomingRequestCommand, - params: RequestParams = { - skipBufferingOnError: false, - triggerMessage: false, - } + params: RequestParams = defaultRequestParams ): Promise { + params = { + ...defaultRequestParams, + ...params, + }; if ( - (chargingStation.isInUnknownState() === true && + (chargingStation.inUnknownState() === true && commandName === RequestCommand.BOOT_NOTIFICATION) || (chargingStation.getOcppStrictCompliance() === false && - chargingStation.isInUnknownState() === true) || - chargingStation.isInAcceptedState() === true || - (chargingStation.isInPendingState() === true && + chargingStation.inUnknownState() === true) || + chargingStation.inAcceptedState() === true || + (chargingStation.inPendingState() === true && (params.triggerMessage === true || messageType === MessageType.CALL_RESULT_MESSAGE)) ) { // eslint-disable-next-line @typescript-eslint/no-this-alias @@ -475,18 +482,6 @@ export abstract class OCPPRequestService { return messageToSend; } - private handleSendMessageError( - chargingStation: ChargingStation, - commandName: RequestCommand | IncomingRequestCommand, - error: Error, - params: HandleErrorParams = { throwError: false } - ): void { - logger.error(`${chargingStation.logPrefix()} Request command '${commandName}' error:`, error); - if (params?.throwError === true) { - throw error; - } - } - // eslint-disable-next-line @typescript-eslint/no-unused-vars public abstract requestHandler( chargingStation: ChargingStation,