X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fcharging-station%2Focpp%2FOCPPRequestService.ts;h=e4890a91d8a6a88e2c8d282fcd603c2b2c31baef;hb=bfbda738d80c83b76e2d38f221bfe305a4a89c2b;hp=839e39955f9ec59d724f2615374d8c0e1cee52e8;hpb=27782dbc3512349e7ff5e9fab9180dd31bf68ffa;p=e-mobility-charging-stations-simulator.git diff --git a/src/charging-station/ocpp/OCPPRequestService.ts b/src/charging-station/ocpp/OCPPRequestService.ts index 839e3995..e4890a91 100644 --- a/src/charging-station/ocpp/OCPPRequestService.ts +++ b/src/charging-station/ocpp/OCPPRequestService.ts @@ -1,5 +1,4 @@ -import type { JSONSchemaType } from 'ajv'; -import Ajv from 'ajv-draft-04'; +import Ajv, { type JSONSchemaType } from 'ajv'; import ajvFormats from 'ajv-formats'; import OCPPError from '../../exception/OCPPError'; @@ -9,6 +8,7 @@ import type { HandleErrorParams } from '../../types/Error'; import type { JsonObject, JsonType } from '../../types/JsonType'; import { ErrorType } from '../../types/ocpp/ErrorType'; import { MessageType } from '../../types/ocpp/MessageType'; +import type { OCPPVersion } from '../../types/ocpp/OCPPVersion'; import { type ErrorCallback, type IncomingRequestCommand, @@ -16,7 +16,7 @@ import { RequestCommand, type RequestParams, type ResponseCallback, - ResponseType, + type ResponseType, } from '../../types/ocpp/Requests'; import type { ErrorResponse, Response } from '../../types/ocpp/Responses'; import Constants from '../../utils/Constants'; @@ -30,14 +30,19 @@ const moduleName = 'OCPPRequestService'; export default abstract class OCPPRequestService { private static instance: OCPPRequestService | null = null; + private readonly version: OCPPVersion; private readonly ajv: Ajv; private readonly ocppResponseService: OCPPResponseService; - protected constructor(ocppResponseService: OCPPResponseService) { - this.ocppResponseService = ocppResponseService; - this.ajv = new Ajv(); + protected constructor(version: OCPPVersion, ocppResponseService: OCPPResponseService) { + this.version = version; + this.ajv = new Ajv({ + keywords: ['javaType'], + multipleOfPrecision: 2, + }); ajvFormats(this.ajv); + this.ocppResponseService = ocppResponseService; this.requestHandler.bind(this); this.sendMessage.bind(this); this.sendResponse.bind(this); @@ -125,19 +130,23 @@ export default abstract class OCPPRequestService { protected validateRequestPayload( chargingStation: ChargingStation, - commandName: RequestCommand, - schema: JSONSchemaType, + commandName: RequestCommand | IncomingRequestCommand, payload: T ): boolean { if (chargingStation.getPayloadSchemaValidation() === false) { return true; } + const schema = this.getRequestPayloadValidationSchema(chargingStation, commandName); + if (schema === false) { + return true; + } const validate = this.ajv.compile(schema); + OCPPServiceUtils.convertDateToISOString(payload); if (validate(payload)) { return true; } logger.error( - `${chargingStation.logPrefix()} ${moduleName}.validateRequestPayload: Request PDU is invalid: %j`, + `${chargingStation.logPrefix()} ${moduleName}.validateRequestPayload: Command '${commandName}' request PDU is invalid: %j`, validate.errors ); // OCPPError usage here is debatable: it's an error in the OCPP stack but not targeted to sendError(). @@ -189,10 +198,10 @@ export default abstract class OCPPRequestService { // Check if wsConnection opened if (chargingStation.isWebSocketConnectionOpened() === true) { // Yes: Send Message - const beginId = PerformanceStatistics.beginMeasure(commandName); + const beginId = PerformanceStatistics.beginMeasure(commandName as string); // FIXME: Handle sending error chargingStation.wsConnection.send(messageToSend); - PerformanceStatistics.endMeasure(commandName, beginId); + PerformanceStatistics.endMeasure(commandName as string, beginId); logger.debug( `${chargingStation.logPrefix()} >> Command '${commandName}' sent ${this.getMessageTypeString( messageType @@ -325,6 +334,7 @@ export default abstract class OCPPRequestService { commandName, messagePayload as JsonType, ]); + this.validateRequestPayload(chargingStation, commandName, messagePayload as JsonType); messageToSend = JSON.stringify([ messageType, messageId, @@ -335,6 +345,7 @@ export default abstract class OCPPRequestService { // Response case MessageType.CALL_RESULT_MESSAGE: // Build response + // FIXME: Validate response payload messageToSend = JSON.stringify([messageType, messageId, messagePayload] as Response); break; // Error Message @@ -376,10 +387,15 @@ export default abstract class OCPPRequestService { } // eslint-disable-next-line @typescript-eslint/no-unused-vars - public abstract requestHandler( + public abstract requestHandler( chargingStation: ChargingStation, commandName: RequestCommand, commandParams?: JsonType, params?: RequestParams - ): Promise; + ): Promise; + + protected abstract getRequestPayloadValidationSchema( + chargingStation: ChargingStation, + commandName: RequestCommand | IncomingRequestCommand + ): JSONSchemaType | false; }