X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fcharging-station%2Focpp%2FOCPPRequestService.ts;h=c9f100d48f2af32a9a4277e4671ea756efb045b3;hb=d270cc878c61c42098557a0e03cc1620f74112de;hp=6c241042e8f69b7ebbe836a6a54921acb4bfb06c;hpb=d900c8d7647a6119a2c8f9f1c283422a8398ef75;p=e-mobility-charging-stations-simulator.git diff --git a/src/charging-station/ocpp/OCPPRequestService.ts b/src/charging-station/ocpp/OCPPRequestService.ts index 6c241042..c9f100d4 100644 --- a/src/charging-station/ocpp/OCPPRequestService.ts +++ b/src/charging-station/ocpp/OCPPRequestService.ts @@ -1,5 +1,5 @@ -import type { JSONSchemaType } from 'ajv'; -import Ajv from 'ajv-draft-04'; +import Ajv, { type JSONSchemaType } from 'ajv'; +import AjvDraft04 from 'ajv-draft-04'; import ajvFormats from 'ajv-formats'; import OCPPError from '../../exception/OCPPError'; @@ -9,14 +9,15 @@ 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 { OCPPVersion } from '../../types/ocpp/OCPPVersion'; import { - ErrorCallback, - IncomingRequestCommand, - OutgoingRequest, + type ErrorCallback, + type IncomingRequestCommand, + type OutgoingRequest, RequestCommand, - RequestParams, - ResponseCallback, - ResponseType, + type RequestParams, + type ResponseCallback, + type ResponseType, } from '../../types/ocpp/Requests'; import type { ErrorResponse, Response } from '../../types/ocpp/Responses'; import Constants from '../../utils/Constants'; @@ -30,14 +31,24 @@ 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; + switch (this.version) { + case OCPPVersion.VERSION_16: + this.ajv = new AjvDraft04(); + break; + case OCPPVersion.VERSION_20: + case OCPPVersion.VERSION_201: + this.ajv = new Ajv(); + break; + } ajvFormats(this.ajv); + this.ocppResponseService = ocppResponseService; this.requestHandler.bind(this); this.sendMessage.bind(this); this.sendResponse.bind(this); @@ -189,10 +200,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 @@ -233,8 +244,8 @@ export default abstract class OCPPRequestService { /** * Function that will receive the request's response * - * @param payload - * @param requestPayload + * @param payload - + * @param requestPayload - */ function responseCallback(payload: JsonType, requestPayload: JsonType): void { if (chargingStation.getEnableStatistics() === true) { @@ -265,8 +276,8 @@ export default abstract class OCPPRequestService { /** * Function that will receive the request's error response * - * @param error - * @param requestStatistic + * @param error - + * @param requestStatistic - */ function errorCallback(error: OCPPError, requestStatistic = true): void { if (requestStatistic === true && chargingStation.getEnableStatistics() === true) { @@ -376,10 +387,10 @@ 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; }