X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fcharging-station%2Focpp%2F2.0%2FOCPP20IncomingRequestService.ts;h=4ae587d43f7de90a119341843b8682985c95dccc;hb=9388120364d5a2f3b9103f05802e1756d2dad04f;hp=94401593b6d0f5ae772bdb9098d60162914e377d;hpb=d5490a13c341153af159d4b2ed745d32dc38452f;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 94401593..4ae587d4 100644 --- a/src/charging-station/ocpp/2.0/OCPP20IncomingRequestService.ts +++ b/src/charging-station/ocpp/2.0/OCPP20IncomingRequestService.ts @@ -2,7 +2,6 @@ import type { ValidateFunction } from 'ajv' -import { OCPP20ServiceUtils } from './OCPP20ServiceUtils.js' import type { ChargingStation } from '../../../charging-station/index.js' import { OCPPError } from '../../../exception/index.js' import { @@ -13,8 +12,9 @@ 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' +import { OCPP20ServiceUtils } from './OCPP20ServiceUtils.js' const moduleName = 'OCPP20IncomingRequestService' @@ -30,7 +30,7 @@ export class OCPP20IncomingRequestService extends OCPPIncomingRequestService { // if (new.target.name === moduleName) { // throw new TypeError(`Cannot construct ${new.target.name} instances directly`) // } - super(OCPPVersion.VERSION_20) + super(OCPPVersion.VERSION_201) this.incomingRequestHandlers = new Map([ [OCPP20IncomingRequestCommand.CLEAR_CACHE, this.handleRequestClearCache.bind(this)] ]) @@ -91,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( @@ -123,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 ) @@ -135,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 (