X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;ds=inline;f=src%2Fcharging-station%2Focpp%2FOCPPRequestService.ts;h=c9f100d48f2af32a9a4277e4671ea756efb045b3;hb=d270cc878c61c42098557a0e03cc1620f74112de;hp=da39fb9e4367f6dfa7a0ad93388c987d7a88190d;hpb=0e4fa348298d36900560f7466f21f44090d2fe1b;p=e-mobility-charging-stations-simulator.git diff --git a/src/charging-station/ocpp/OCPPRequestService.ts b/src/charging-station/ocpp/OCPPRequestService.ts index da39fb9e..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 @@ -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; }