X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fcharging-station%2Focpp%2F2.0%2FOCPP20IncomingRequestService.ts;h=b45345a6faf316febc7c0b283901d708802f8462;hb=55ae7b758f478a2beb4557bbc96363fb913dcc73;hp=cfc58323ed8ad4cf597c1a2b3b9227b86f8ba2cd;hpb=24d15716245469249b57e5aa790147d703d60a4e;p=e-mobility-charging-stations-simulator.git diff --git a/src/charging-station/ocpp/2.0/OCPP20IncomingRequestService.ts b/src/charging-station/ocpp/2.0/OCPP20IncomingRequestService.ts index cfc58323..b45345a6 100644 --- a/src/charging-station/ocpp/2.0/OCPP20IncomingRequestService.ts +++ b/src/charging-station/ocpp/2.0/OCPP20IncomingRequestService.ts @@ -13,16 +13,13 @@ import { OCPP20IncomingRequestCommand, OCPPVersion } from '../../../types/index.js' -import { logger } from '../../../utils/index.js' +import { isAsyncFunction, logger } from '../../../utils/index.js' import { OCPPIncomingRequestService } from '../OCPPIncomingRequestService.js' const moduleName = 'OCPP20IncomingRequestService' export class OCPP20IncomingRequestService extends OCPPIncomingRequestService { - protected jsonSchemasValidateFunction: Map< - OCPP20IncomingRequestCommand, - ValidateFunction - > + protected payloadValidateFunctions: Map> private readonly incomingRequestHandlers: Map< OCPP20IncomingRequestCommand, @@ -37,7 +34,7 @@ export class OCPP20IncomingRequestService extends OCPPIncomingRequestService { this.incomingRequestHandlers = new Map([ [OCPP20IncomingRequestCommand.CLEAR_CACHE, this.handleRequestClearCache.bind(this)] ]) - this.jsonSchemasValidateFunction = new Map< + this.payloadValidateFunctions = new Map< OCPP20IncomingRequestCommand, ValidateFunction >([ @@ -94,10 +91,12 @@ export class OCPP20IncomingRequestService extends OCPPIncomingRequestService { this.validatePayload(chargingStation, commandName, commandPayload) // Call the method to build the response // eslint-disable-next-line @typescript-eslint/no-non-null-assertion - response = (await this.incomingRequestHandlers.get(commandName)!( - chargingStation, - commandPayload - )) as ResType + const incomingRequestHandler = this.incomingRequestHandlers.get(commandName)! + if (isAsyncFunction(incomingRequestHandler)) { + response = (await incomingRequestHandler(chargingStation, commandPayload)) as ResType + } else { + response = incomingRequestHandler(chargingStation, commandPayload) as ResType + } } catch (error) { // Log logger.error( @@ -126,7 +125,7 @@ export class OCPP20IncomingRequestService extends OCPPIncomingRequestService { commandPayload, undefined, 2 - )} while the charging station is not registered on the central server.`, + )} while the charging station is not registered on the central server`, commandName, commandPayload ) @@ -138,6 +137,8 @@ export class OCPP20IncomingRequestService extends OCPPIncomingRequestService { response, commandName ) + // Emit command name event to allow delayed handling + this.emit(commandName, chargingStation, commandPayload, response) } private validatePayload ( @@ -145,7 +146,7 @@ export class OCPP20IncomingRequestService extends OCPPIncomingRequestService { commandName: OCPP20IncomingRequestCommand, commandPayload: JsonType ): boolean { - if (this.jsonSchemasValidateFunction.has(commandName)) { + if (this.payloadValidateFunctions.has(commandName)) { return this.validateIncomingRequestPayload(chargingStation, commandName, commandPayload) } logger.warn(