X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fcharging-station%2Focpp%2FOCPPIncomingRequestService.ts;h=28de507774a281aeeb0d32b4d17b1348b45d50c5;hb=f23be6aad227f8942bb140d286585423fe84f60b;hp=0ea55a63515f0f479a627299625c1703e5442c15;hpb=e1d9a0f4d6ff1a90048e9a694fd12b7031cc6961;p=e-mobility-charging-stations-simulator.git diff --git a/src/charging-station/ocpp/OCPPIncomingRequestService.ts b/src/charging-station/ocpp/OCPPIncomingRequestService.ts index 0ea55a63..28de5077 100644 --- a/src/charging-station/ocpp/OCPPIncomingRequestService.ts +++ b/src/charging-station/ocpp/OCPPIncomingRequestService.ts @@ -1,6 +1,6 @@ import { AsyncResource } from 'node:async_hooks'; -import Ajv, { type JSONSchemaType } from 'ajv'; +import Ajv, { type JSONSchemaType, type ValidateFunction } from 'ajv'; import ajvFormats from 'ajv-formats'; import { OCPPConstants } from './OCPPConstants'; @@ -11,7 +11,6 @@ import type { ClearCacheResponse, HandleErrorParams, IncomingRequestCommand, - JsonObject, JsonType, OCPPVersion, } from '../../types'; @@ -23,7 +22,8 @@ export abstract class OCPPIncomingRequestService extends AsyncResource { private static instance: OCPPIncomingRequestService | null = null; private readonly version: OCPPVersion; private readonly ajv: Ajv; - protected abstract jsonSchemas: Map>; + private jsonValidateFunctions: Map>; + protected abstract jsonSchemas: Map>; protected constructor(version: OCPPVersion) { super(moduleName); @@ -33,11 +33,16 @@ export abstract class OCPPIncomingRequestService extends AsyncResource { multipleOfPrecision: 2, }); ajvFormats(this.ajv); - this.incomingRequestHandler = this.incomingRequestHandler.bind(this) as ( + 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: JsonType, + commandPayload: ReqType, ) => Promise; this.validateIncomingRequestPayload = this.validateIncomingRequestPayload.bind(this) as < T extends JsonType, @@ -84,10 +89,13 @@ export abstract class OCPPIncomingRequestService extends AsyncResource { schema: JSONSchemaType, payload: T, ): boolean { - if (chargingStation.getOcppStrictCompliance() === false) { + if (chargingStation.stationInfo?.ocppStrictCompliance === false) { return true; } - const validate = this.ajv.compile(schema); + if (this.jsonValidateFunctions.has(commandName) === false) { + this.jsonValidateFunctions.set(commandName, this.ajv.compile(schema).bind(this)); + } + const validate = this.jsonValidateFunctions.get(commandName)!; if (validate(payload)) { return true; } @@ -96,10 +104,10 @@ export abstract class OCPPIncomingRequestService extends AsyncResource { validate.errors, ); throw new OCPPError( - OCPPServiceUtils.ajvErrorsToErrorType(validate.errors!), + OCPPServiceUtils.ajvErrorsToErrorType(validate.errors), 'Incoming request PDU is invalid', commandName, - JSON.stringify(validate.errors, null, 2), + JSON.stringify(validate.errors, undefined, 2), ); } @@ -110,10 +118,11 @@ export abstract class OCPPIncomingRequestService extends AsyncResource { return OCPPConstants.OCPP_RESPONSE_REJECTED; } - public abstract incomingRequestHandler( + // eslint-disable-next-line @typescript-eslint/no-unused-vars + public abstract incomingRequestHandler( chargingStation: ChargingStation, messageId: string, commandName: IncomingRequestCommand, - commandPayload: JsonType, + commandPayload: ReqType, ): Promise; }