From: Jérôme Benoit Date: Thu, 25 Jan 2024 23:19:45 +0000 (+0100) Subject: fix: avoid duplicate payload validation function X-Git-Tag: v1.2.33~28 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=298be10c5c800e43b0b9dcd6c50f6d49e2cb786b;p=e-mobility-charging-stations-simulator.git fix: avoid duplicate payload validation function Signed-off-by: Jérôme Benoit --- diff --git a/src/charging-station/ocpp/1.6/OCPP16ResponseService.ts b/src/charging-station/ocpp/1.6/OCPP16ResponseService.ts index e65f8a8c..3b91fba1 100644 --- a/src/charging-station/ocpp/1.6/OCPP16ResponseService.ts +++ b/src/charging-station/ocpp/1.6/OCPP16ResponseService.ts @@ -225,7 +225,7 @@ export class OCPP16ResponseService extends OCPPResponseService { >([ [ OCPP16IncomingRequestCommand.RESET, - this.ajv + this.ajvIncomingRequest .compile( OCPP16ServiceUtils.parseJsonSchemaFile( 'assets/json-schemas/ocpp/1.6/ResetResponse.json', @@ -237,7 +237,7 @@ export class OCPP16ResponseService extends OCPPResponseService { ], [ OCPP16IncomingRequestCommand.CLEAR_CACHE, - this.ajv + this.ajvIncomingRequest .compile( OCPP16ServiceUtils.parseJsonSchemaFile( 'assets/json-schemas/ocpp/1.6/ClearCacheResponse.json', @@ -249,7 +249,7 @@ export class OCPP16ResponseService extends OCPPResponseService { ], [ OCPP16IncomingRequestCommand.CHANGE_AVAILABILITY, - this.ajv + this.ajvIncomingRequest .compile( OCPP16ServiceUtils.parseJsonSchemaFile( 'assets/json-schemas/ocpp/1.6/ChangeAvailabilityResponse.json', @@ -261,7 +261,7 @@ export class OCPP16ResponseService extends OCPPResponseService { ], [ OCPP16IncomingRequestCommand.UNLOCK_CONNECTOR, - this.ajv + this.ajvIncomingRequest .compile( OCPP16ServiceUtils.parseJsonSchemaFile( 'assets/json-schemas/ocpp/1.6/UnlockConnectorResponse.json', @@ -273,7 +273,7 @@ export class OCPP16ResponseService extends OCPPResponseService { ], [ OCPP16IncomingRequestCommand.GET_CONFIGURATION, - this.ajv + this.ajvIncomingRequest .compile( OCPP16ServiceUtils.parseJsonSchemaFile( 'assets/json-schemas/ocpp/1.6/GetConfigurationResponse.json', @@ -285,7 +285,7 @@ export class OCPP16ResponseService extends OCPPResponseService { ], [ OCPP16IncomingRequestCommand.CHANGE_CONFIGURATION, - this.ajv + this.ajvIncomingRequest .compile( OCPP16ServiceUtils.parseJsonSchemaFile( 'assets/json-schemas/ocpp/1.6/ChangeConfigurationResponse.json', @@ -297,7 +297,7 @@ export class OCPP16ResponseService extends OCPPResponseService { ], [ OCPP16IncomingRequestCommand.GET_COMPOSITE_SCHEDULE, - this.ajv + this.ajvIncomingRequest .compile( OCPP16ServiceUtils.parseJsonSchemaFile( 'assets/json-schemas/ocpp/1.6/GetCompositeScheduleResponse.json', @@ -309,7 +309,7 @@ export class OCPP16ResponseService extends OCPPResponseService { ], [ OCPP16IncomingRequestCommand.SET_CHARGING_PROFILE, - this.ajv + this.ajvIncomingRequest .compile( OCPP16ServiceUtils.parseJsonSchemaFile( 'assets/json-schemas/ocpp/1.6/SetChargingProfileResponse.json', @@ -321,7 +321,7 @@ export class OCPP16ResponseService extends OCPPResponseService { ], [ OCPP16IncomingRequestCommand.CLEAR_CHARGING_PROFILE, - this.ajv + this.ajvIncomingRequest .compile( OCPP16ServiceUtils.parseJsonSchemaFile( 'assets/json-schemas/ocpp/1.6/ClearChargingProfileResponse.json', @@ -333,7 +333,7 @@ export class OCPP16ResponseService extends OCPPResponseService { ], [ OCPP16IncomingRequestCommand.REMOTE_START_TRANSACTION, - this.ajv + this.ajvIncomingRequest .compile( OCPP16ServiceUtils.parseJsonSchemaFile( 'assets/json-schemas/ocpp/1.6/RemoteStartTransactionResponse.json', @@ -345,7 +345,7 @@ export class OCPP16ResponseService extends OCPPResponseService { ], [ OCPP16IncomingRequestCommand.REMOTE_STOP_TRANSACTION, - this.ajv + this.ajvIncomingRequest .compile( OCPP16ServiceUtils.parseJsonSchemaFile( 'assets/json-schemas/ocpp/1.6/RemoteStopTransactionResponse.json', @@ -357,7 +357,7 @@ export class OCPP16ResponseService extends OCPPResponseService { ], [ OCPP16IncomingRequestCommand.GET_DIAGNOSTICS, - this.ajv + this.ajvIncomingRequest .compile( OCPP16ServiceUtils.parseJsonSchemaFile( 'assets/json-schemas/ocpp/1.6/GetDiagnosticsResponse.json', @@ -369,7 +369,7 @@ export class OCPP16ResponseService extends OCPPResponseService { ], [ OCPP16IncomingRequestCommand.TRIGGER_MESSAGE, - this.ajv + this.ajvIncomingRequest .compile( OCPP16ServiceUtils.parseJsonSchemaFile( 'assets/json-schemas/ocpp/1.6/TriggerMessageResponse.json', @@ -381,7 +381,7 @@ export class OCPP16ResponseService extends OCPPResponseService { ], [ OCPP16IncomingRequestCommand.DATA_TRANSFER, - this.ajv + this.ajvIncomingRequest .compile( OCPP16ServiceUtils.parseJsonSchemaFile( 'assets/json-schemas/ocpp/1.6/DataTransferResponse.json', @@ -393,7 +393,7 @@ export class OCPP16ResponseService extends OCPPResponseService { ], [ OCPP16IncomingRequestCommand.UPDATE_FIRMWARE, - this.ajv + this.ajvIncomingRequest .compile( OCPP16ServiceUtils.parseJsonSchemaFile( 'assets/json-schemas/ocpp/1.6/UpdateFirmwareResponse.json', @@ -405,7 +405,7 @@ export class OCPP16ResponseService extends OCPPResponseService { ], [ OCPP16IncomingRequestCommand.RESERVE_NOW, - this.ajv + this.ajvIncomingRequest .compile( OCPP16ServiceUtils.parseJsonSchemaFile( 'assets/json-schemas/ocpp/1.6/ReserveNowResponse.json', @@ -417,7 +417,7 @@ export class OCPP16ResponseService extends OCPPResponseService { ], [ OCPP16IncomingRequestCommand.CANCEL_RESERVATION, - this.ajv + this.ajvIncomingRequest .compile( OCPP16ServiceUtils.parseJsonSchemaFile( 'assets/json-schemas/ocpp/1.6/CancelReservationResponse.json', diff --git a/src/charging-station/ocpp/2.0/OCPP20ResponseService.ts b/src/charging-station/ocpp/2.0/OCPP20ResponseService.ts index 99ca6566..d5b3e7d1 100644 --- a/src/charging-station/ocpp/2.0/OCPP20ResponseService.ts +++ b/src/charging-station/ocpp/2.0/OCPP20ResponseService.ts @@ -90,7 +90,7 @@ export class OCPP20ResponseService extends OCPPResponseService { >([ [ OCPP20IncomingRequestCommand.CLEAR_CACHE, - this.ajv + this.ajvIncomingRequest .compile( OCPP20ServiceUtils.parseJsonSchemaFile( 'assets/json-schemas/ocpp/2.0/ClearCacheResponse.json', diff --git a/src/charging-station/ocpp/OCPPResponseService.ts b/src/charging-station/ocpp/OCPPResponseService.ts index 5ee6e2d0..45a3e1f2 100644 --- a/src/charging-station/ocpp/OCPPResponseService.ts +++ b/src/charging-station/ocpp/OCPPResponseService.ts @@ -22,6 +22,7 @@ export abstract class OCPPResponseService { private static instance: OCPPResponseService | null = null private readonly version: OCPPVersion protected readonly ajv: Ajv + protected readonly ajvIncomingRequest: Ajv protected abstract jsonSchemasValidateFunction: Map> public abstract jsonSchemasIncomingRequestResponseValidateFunction: Map< IncomingRequestCommand, @@ -35,6 +36,11 @@ export abstract class OCPPResponseService { multipleOfPrecision: 2 }) ajvFormats(this.ajv) + this.ajvIncomingRequest = new Ajv({ + keywords: ['javaType'], + multipleOfPrecision: 2 + }) + ajvFormats(this.ajvIncomingRequest) this.responseHandler = this.responseHandler.bind(this) this.validateResponsePayload = this.validateResponsePayload.bind(this) }