X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;ds=sidebyside;f=src%2Fcharging-station%2Focpp%2FOCPPIncomingRequestService.ts;h=7203569749e04d8ee88a238e4ac2f0ea754382da;hb=dd21af153ed64c6220b6b14a5165c11443032f4f;hp=ea3e7b7312c03027dcae25c43a85b20a9cd42a7d;hpb=5199f9fdf202eb534948f165a0994e1993675aa8;p=e-mobility-charging-stations-simulator.git diff --git a/src/charging-station/ocpp/OCPPIncomingRequestService.ts b/src/charging-station/ocpp/OCPPIncomingRequestService.ts index ea3e7b73..72035697 100644 --- a/src/charging-station/ocpp/OCPPIncomingRequestService.ts +++ b/src/charging-station/ocpp/OCPPIncomingRequestService.ts @@ -1,4 +1,4 @@ -import _Ajv, { type JSONSchemaType, type ValidateFunction } from 'ajv' +import _Ajv, { type ValidateFunction } from 'ajv' import _ajvFormats from 'ajv-formats' import { OCPPConstants } from './OCPPConstants.js' @@ -23,9 +23,11 @@ const moduleName = 'OCPPIncomingRequestService' export abstract class OCPPIncomingRequestService { private static instance: OCPPIncomingRequestService | null = null private readonly version: OCPPVersion - private readonly ajv: Ajv - private readonly jsonValidateFunctions: Map> - protected abstract jsonSchemas: Map> + protected readonly ajv: Ajv + protected abstract payloadValidateFunctions: Map< + IncomingRequestCommand, + ValidateFunction + > protected constructor (version: OCPPVersion) { this.version = version @@ -34,25 +36,8 @@ export abstract class OCPPIncomingRequestService { multipleOfPrecision: 2 }) ajvFormats(this.ajv) - this.jsonValidateFunctions = new Map>() - this.incomingRequestHandler = this.incomingRequestHandler.bind(this) as < - ReqType extends JsonType, - // eslint-disable-next-line @typescript-eslint/no-unused-vars - ResType extends JsonType, - >( - chargingStation: ChargingStation, - messageId: string, - commandName: IncomingRequestCommand, - commandPayload: ReqType - ) => Promise - this.validateIncomingRequestPayload = this.validateIncomingRequestPayload.bind(this) as < - T extends JsonType, - >( - chargingStation: ChargingStation, - commandName: IncomingRequestCommand, - schema: JSONSchemaType, - payload: T - ) => boolean + this.incomingRequestHandler = this.incomingRequestHandler.bind(this) + this.validateIncomingRequestPayload = this.validateIncomingRequestPayload.bind(this) } public static getInstance(this: new () => T): T { @@ -87,25 +72,24 @@ export abstract class OCPPIncomingRequestService { protected validateIncomingRequestPayload( chargingStation: ChargingStation, commandName: IncomingRequestCommand, - schema: JSONSchemaType, payload: T ): boolean { if (chargingStation.stationInfo?.ocppStrictCompliance === false) { return true } - const validate = this.getJsonIncomingRequestValidateFunction(commandName, schema) - if (validate(payload)) { + const validate = this.payloadValidateFunctions.get(commandName) + if (validate?.(payload) === true) { return true } logger.error( `${chargingStation.logPrefix()} ${moduleName}.validateIncomingRequestPayload: Command '${commandName}' incoming request PDU is invalid: %j`, - validate.errors + validate?.errors ) throw new OCPPError( - OCPPServiceUtils.ajvErrorsToErrorType(validate.errors), + OCPPServiceUtils.ajvErrorsToErrorType(validate?.errors), 'Incoming request PDU is invalid', commandName, - JSON.stringify(validate.errors, undefined, 2) + JSON.stringify(validate?.errors, undefined, 2) ) } @@ -117,17 +101,6 @@ export abstract class OCPPIncomingRequestService { return OCPPConstants.OCPP_RESPONSE_REJECTED } - private getJsonIncomingRequestValidateFunction( - commandName: IncomingRequestCommand, - schema: JSONSchemaType - ): ValidateFunction { - if (!this.jsonValidateFunctions.has(commandName)) { - this.jsonValidateFunctions.set(commandName, this.ajv.compile(schema).bind(this)) - } - // eslint-disable-next-line @typescript-eslint/no-non-null-assertion - return this.jsonValidateFunctions.get(commandName)! - } - // eslint-disable-next-line @typescript-eslint/no-unused-vars public abstract incomingRequestHandler( chargingStation: ChargingStation,