X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fcharging-station%2Focpp%2FOCPPResponseService.ts;h=90f92169cd4c76f8a49f37fc3635a4778689cd86;hb=291b5ec8b20005de53c4de3eba0e4cd1b068006c;hp=7934500ed3ebd7612cfb05ad58b0c68699396dc1;hpb=9429aa42d725c4b42c30e0134d75b363aed412f0;p=e-mobility-charging-stations-simulator.git diff --git a/src/charging-station/ocpp/OCPPResponseService.ts b/src/charging-station/ocpp/OCPPResponseService.ts index 7934500e..90f92169 100644 --- a/src/charging-station/ocpp/OCPPResponseService.ts +++ b/src/charging-station/ocpp/OCPPResponseService.ts @@ -1,27 +1,29 @@ -import Ajv, { type JSONSchemaType } from 'ajv'; +import Ajv, { type JSONSchemaType, type ValidateFunction } from 'ajv'; import ajvFormats from 'ajv-formats'; import { OCPPServiceUtils } from './OCPPServiceUtils'; import type { ChargingStation } from '../../charging-station'; import { OCPPError } from '../../exception'; -import type { - IncomingRequestCommand, - JsonObject, - JsonType, - OCPPVersion, - RequestCommand, -} from '../../types'; +import type { IncomingRequestCommand, JsonType, OCPPVersion, RequestCommand } from '../../types'; import { logger } from '../../utils'; const moduleName = 'OCPPResponseService'; export abstract class OCPPResponseService { private static instance: OCPPResponseService | null = null; + + public jsonIncomingRequestResponseValidateFunctions: Map< + IncomingRequestCommand, + ValidateFunction + >; + private readonly version: OCPPVersion; private readonly ajv: Ajv; + private jsonRequestValidateFunctions: Map>; + public abstract jsonIncomingRequestResponseSchemas: Map< IncomingRequestCommand, - JSONSchemaType + JSONSchemaType >; protected constructor(version: OCPPVersion) { @@ -31,6 +33,11 @@ export abstract class OCPPResponseService { multipleOfPrecision: 2, }); ajvFormats(this.ajv); + this.jsonRequestValidateFunctions = new Map>(); + this.jsonIncomingRequestResponseValidateFunctions = new Map< + IncomingRequestCommand, + ValidateFunction + >(); this.responseHandler = this.responseHandler.bind(this) as < ReqType extends JsonType, ResType extends JsonType, @@ -64,7 +71,13 @@ export abstract class OCPPResponseService { if (chargingStation.getOcppStrictCompliance() === false) { return true; } - const validate = this.ajv.compile(schema); + if (this.jsonRequestValidateFunctions.has(commandName) === false) { + this.jsonRequestValidateFunctions.set( + commandName, + this.ajv.compile(schema).bind(this), + ); + } + const validate = this.jsonRequestValidateFunctions.get(commandName)!; if (validate(payload)) { return true; } @@ -76,7 +89,7 @@ export abstract class OCPPResponseService { OCPPServiceUtils.ajvErrorsToErrorType(validate.errors!), 'Response PDU is invalid', commandName, - JSON.stringify(validate.errors, null, 2), + JSON.stringify(validate.errors, undefined, 2), ); }