X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fcharging-station%2Focpp%2FOCPPRequestService.ts;h=1ae25a65329107bc31b0ade928d3507b54375822;hb=01a4dcbb168f3fb8c52b81a6ff5473b4259f390f;hp=9bf06e935d04c41f95e12ba30b1ad2d8fb7ffa25;hpb=e3018bc4b27b43106073e4c4cda031cc37715027;p=e-mobility-charging-stations-simulator.git diff --git a/src/charging-station/ocpp/OCPPRequestService.ts b/src/charging-station/ocpp/OCPPRequestService.ts index 9bf06e93..1ae25a65 100644 --- a/src/charging-station/ocpp/OCPPRequestService.ts +++ b/src/charging-station/ocpp/OCPPRequestService.ts @@ -1,3 +1,7 @@ +import { JSONSchemaType } from 'ajv'; +import Ajv from 'ajv-draft-04'; +import ajvFormats from 'ajv-formats'; + import OCPPError from '../../exception/OCPPError'; import PerformanceStatistics from '../../performance/PerformanceStatistics'; import { EmptyObject } from '../../types/EmptyObject'; @@ -18,11 +22,13 @@ import logger from '../../utils/Logger'; import Utils from '../../utils/Utils'; import type ChargingStation from '../ChargingStation'; import type OCPPResponseService from './OCPPResponseService'; +import { OCPPServiceUtils } from './OCPPServiceUtils'; const moduleName = 'OCPPRequestService'; export default abstract class OCPPRequestService { private static instance: OCPPRequestService | null = null; + private ajv: Ajv; private readonly ocppResponseService: OCPPResponseService; @@ -31,6 +37,8 @@ export default abstract class OCPPRequestService { this.requestHandler.bind(this); this.sendResponse.bind(this); this.sendError.bind(this); + this.ajv = new Ajv(); + ajvFormats(this.ajv); } public static getInstance( @@ -107,6 +115,31 @@ export default abstract class OCPPRequestService { } } + protected validateRequestPayload( + chargingStation: ChargingStation, + commandName: RequestCommand, + schema: JSONSchemaType, + payload: T + ): boolean { + if (!chargingStation.getPayloadSchemaValidation()) { + return true; + } + const validate = this.ajv.compile(schema); + if (validate(payload)) { + return true; + } + logger.error( + `${chargingStation.logPrefix()} ${moduleName}.validateRequestPayload: Request PDU is invalid: %j`, + validate.errors + ); + throw new OCPPError( + OCPPServiceUtils.ajvErrorsToErrorType(validate.errors), + 'Request PDU is invalid', + commandName, + JSON.stringify(validate.errors, null, 2) + ); + } + private async internalSendMessage( chargingStation: ChargingStation, messageId: string,