X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fcharging-station%2Focpp%2FOCPPResponseService.ts;h=45a3e1f2d5822f5236e0266492935effd38f165d;hb=298be10c5c800e43b0b9dcd6c50f6d49e2cb786b;hp=e47450890076f3b200420bd014718e2c7b88f9af;hpb=ba9a56a613727d96757690a8b52af6731f3fd8a8;p=e-mobility-charging-stations-simulator.git diff --git a/src/charging-station/ocpp/OCPPResponseService.ts b/src/charging-station/ocpp/OCPPResponseService.ts index e4745089..45a3e1f2 100644 --- a/src/charging-station/ocpp/OCPPResponseService.ts +++ b/src/charging-station/ocpp/OCPPResponseService.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 { OCPPServiceUtils } from './OCPPServiceUtils.js' @@ -20,19 +20,13 @@ 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 readonly jsonRequestValidateFunctions: Map> - - public abstract jsonIncomingRequestResponseSchemas: Map< + protected readonly ajv: Ajv + protected readonly ajvIncomingRequest: Ajv + protected abstract jsonSchemasValidateFunction: Map> + public abstract jsonSchemasIncomingRequestResponseValidateFunction: Map< IncomingRequestCommand, - JSONSchemaType + ValidateFunction > protected constructor (version: OCPPVersion) { @@ -42,11 +36,11 @@ export abstract class OCPPResponseService { multipleOfPrecision: 2 }) ajvFormats(this.ajv) - this.jsonRequestValidateFunctions = new Map>() - this.jsonIncomingRequestResponseValidateFunctions = new Map< - IncomingRequestCommand, - ValidateFunction - >() + this.ajvIncomingRequest = new Ajv({ + keywords: ['javaType'], + multipleOfPrecision: 2 + }) + ajvFormats(this.ajvIncomingRequest) this.responseHandler = this.responseHandler.bind(this) this.validateResponsePayload = this.validateResponsePayload.bind(this) } @@ -61,41 +55,29 @@ export abstract class OCPPResponseService { protected validateResponsePayload( chargingStation: ChargingStation, commandName: RequestCommand, - schema: JSONSchemaType, payload: T ): boolean { if (chargingStation.stationInfo?.ocppStrictCompliance === false) { return true } - const validate = this.getJsonRequestValidateFunction(commandName, schema) - if (validate(payload)) { + const validate = this.jsonSchemasValidateFunction.get(commandName) + if (validate?.(payload) === true) { return true } logger.error( `${chargingStation.logPrefix()} ${moduleName}.validateResponsePayload: Command '${commandName}' response PDU is invalid: %j`, - validate.errors + validate?.errors ) throw new OCPPError( - OCPPServiceUtils.ajvErrorsToErrorType(validate.errors), + OCPPServiceUtils.ajvErrorsToErrorType(validate?.errors), 'Response PDU is invalid', commandName, - JSON.stringify(validate.errors, undefined, 2) + JSON.stringify(validate?.errors, undefined, 2) ) } protected emptyResponseHandler = Constants.EMPTY_FUNCTION - private getJsonRequestValidateFunction( - commandName: RequestCommand, - schema: JSONSchemaType - ): ValidateFunction { - if (!this.jsonRequestValidateFunctions.has(commandName)) { - this.jsonRequestValidateFunctions.set(commandName, this.ajv.compile(schema).bind(this)) - } - // eslint-disable-next-line @typescript-eslint/no-non-null-assertion - return this.jsonRequestValidateFunctions.get(commandName)! - } - public abstract responseHandler( chargingStation: ChargingStation, commandName: RequestCommand,