X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fcharging-station%2Focpp%2FOCPPRequestService.ts;h=e78ca0c6dfa4d8fcec3049d05b67a11d62df82de;hb=0afed85fd7e6cb8f4b5ea0d18800a8d7b3bd78a7;hp=d03e64ec067759a7ed0fefe8f0ce7fc40bdb48eb;hpb=f3a490e6bd184709de4377d81a459bf49242cae1;p=e-mobility-charging-stations-simulator.git diff --git a/src/charging-station/ocpp/OCPPRequestService.ts b/src/charging-station/ocpp/OCPPRequestService.ts index d03e64ec..e78ca0c6 100644 --- a/src/charging-station/ocpp/OCPPRequestService.ts +++ b/src/charging-station/ocpp/OCPPRequestService.ts @@ -1,4 +1,14 @@ -import { ErrorResponse, Response } from '../../types/ocpp/Responses'; +import type { 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 type { EmptyObject } from '../../types/EmptyObject'; +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 { IncomingRequestCommand, OutgoingRequest, @@ -6,37 +16,39 @@ import { RequestParams, ResponseType, } from '../../types/ocpp/Requests'; -import { JsonObject, JsonType } from '../../types/JsonType'; - -import type ChargingStation from '../ChargingStation'; +import type { ErrorResponse, Response } from '../../types/ocpp/Responses'; import Constants from '../../utils/Constants'; -import { EmptyObject } from '../../types/EmptyObject'; -import { ErrorType } from '../../types/ocpp/ErrorType'; -import { HandleErrorParams } from '../../types/Error'; -import { MessageType } from '../../types/ocpp/MessageType'; -import OCPPError from '../../exception/OCPPError'; -import type OCPPResponseService from './OCPPResponseService'; -import PerformanceStatistics from '../../performance/PerformanceStatistics'; -import Utils from '../../utils/Utils'; 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; protected constructor(ocppResponseService: OCPPResponseService) { this.ocppResponseService = ocppResponseService; + this.ajv = new Ajv(); + ajvFormats(this.ajv); this.requestHandler.bind(this); this.sendResponse.bind(this); this.sendError.bind(this); + this.internalSendMessage.bind(this); + this.buildMessageToSend.bind(this); + this.validateRequestPayload.bind(this); } public static getInstance( this: new (ocppResponseService: OCPPResponseService) => T, ocppResponseService: OCPPResponseService ): T { - if (!OCPPRequestService.instance) { + if (OCPPRequestService.instance === null) { OCPPRequestService.instance = new this(ocppResponseService); } return OCPPRequestService.instance as T; @@ -106,6 +118,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, @@ -224,17 +261,17 @@ export default abstract class OCPPRequestService { * @param requestStatistic */ function errorCallback(error: OCPPError, requestStatistic = true): void { - if (requestStatistic && chargingStation.getEnableStatistics()) { + if (requestStatistic === true && chargingStation.getEnableStatistics() === true) { chargingStation.performanceStatistics.addRequestStatistic( commandName, MessageType.CALL_ERROR_MESSAGE ); } logger.error( - `${chargingStation.logPrefix()} Error %j occurred when calling command %s with message data %j`, - error, - commandName, - messagePayload + `${chargingStation.logPrefix()} Error occurred when calling command ${commandName} with message data ${JSON.stringify( + messagePayload + )}:`, + error ); chargingStation.requests.delete(messageId); reject(error); @@ -254,7 +291,7 @@ export default abstract class OCPPRequestService { } throw new OCPPError( ErrorType.SECURITY_ERROR, - `Cannot send command ${commandName} payload when the charging station is in ${chargingStation.getRegistrationStatus()} state on the central server`, + `Cannot send command ${commandName} PDU when the charging station is in ${chargingStation.getRegistrationStatus()} state on the central server`, commandName ); } @@ -324,17 +361,17 @@ export default abstract class OCPPRequestService { error: Error, params: HandleErrorParams = { throwError: true } ): void { - logger.error(chargingStation.logPrefix() + ' Request command %s error: %j', commandName, error); + logger.error(`${chargingStation.logPrefix()} Request command '${commandName}' error:`, error); if (params?.throwError) { throw error; } } // 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; }