X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fcharging-station%2Focpp%2FOCPPIncomingRequestService.ts;h=4b27f9d82f8de91b38bd1a19c4a826ebe6800d6b;hb=1b2acf4e9c00cc7272ec7769b4e82113d61f64fb;hp=d364f7aa8df2866a59bf5f2da7d0059da475d569;hpb=5edd8ba0f8978cfb3ca9d80f299d9748c6c5970e;p=e-mobility-charging-stations-simulator.git diff --git a/src/charging-station/ocpp/OCPPIncomingRequestService.ts b/src/charging-station/ocpp/OCPPIncomingRequestService.ts index d364f7aa..4b27f9d8 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,10 @@ 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); + const validate = this.getJsonIncomingRequestValidateFunction(commandName, schema); if (validate(payload)) { return true; } @@ -99,21 +104,32 @@ export abstract class OCPPIncomingRequestService extends AsyncResource { OCPPServiceUtils.ajvErrorsToErrorType(validate.errors), 'Incoming request PDU is invalid', commandName, - JSON.stringify(validate.errors, null, 2), + JSON.stringify(validate.errors, undefined, 2), ); } protected handleRequestClearCache(chargingStation: ChargingStation): ClearCacheResponse { - if (chargingStation.idTagsCache.deleteIdTags(getIdTagsFile(chargingStation.stationInfo))) { + if (chargingStation.idTagsCache.deleteIdTags(getIdTagsFile(chargingStation.stationInfo)!)) { return OCPPConstants.OCPP_RESPONSE_ACCEPTED; } return OCPPConstants.OCPP_RESPONSE_REJECTED; } - public abstract incomingRequestHandler( + private getJsonIncomingRequestValidateFunction( + commandName: IncomingRequestCommand, + schema: JSONSchemaType, + ) { + if (this.jsonValidateFunctions.has(commandName) === false) { + this.jsonValidateFunctions.set(commandName, this.ajv.compile(schema).bind(this)); + } + return this.jsonValidateFunctions.get(commandName)!; + } + + // eslint-disable-next-line @typescript-eslint/no-unused-vars + public abstract incomingRequestHandler( chargingStation: ChargingStation, messageId: string, commandName: IncomingRequestCommand, - commandPayload: JsonType, + commandPayload: ReqType, ): Promise; }