X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fcharging-station%2Focpp%2FOCPPIncomingRequestService.ts;h=28de507774a281aeeb0d32b4d17b1348b45d50c5;hb=f23be6aad227f8942bb140d286585423fe84f60b;hp=b7ab8ef66382b1ac0e799294eccca7f676480855;hpb=9429aa42d725c4b42c30e0134d75b363aed412f0;p=e-mobility-charging-stations-simulator.git diff --git a/src/charging-station/ocpp/OCPPIncomingRequestService.ts b/src/charging-station/ocpp/OCPPIncomingRequestService.ts index b7ab8ef6..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,6 +33,7 @@ export abstract class OCPPIncomingRequestService extends AsyncResource { 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 @@ -88,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; } @@ -100,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), ); }