X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;ds=sidebyside;f=src%2Fcharging-station%2Focpp%2FOCPPIncomingRequestService.ts;h=ef64350812b49f897087a832040d494bf652b8a3;hb=d270cc878c61c42098557a0e03cc1620f74112de;hp=06ccd312c0e970022c844f200fe3d0b631ef8c93;hpb=e6159ce8b782e3464a9a0dc377897bc4a4718121;p=e-mobility-charging-stations-simulator.git diff --git a/src/charging-station/ocpp/OCPPIncomingRequestService.ts b/src/charging-station/ocpp/OCPPIncomingRequestService.ts index 06ccd312..ef643508 100644 --- a/src/charging-station/ocpp/OCPPIncomingRequestService.ts +++ b/src/charging-station/ocpp/OCPPIncomingRequestService.ts @@ -1,12 +1,13 @@ import { AsyncResource } from 'async_hooks'; -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'; import type { HandleErrorParams } from '../../types/Error'; import type { JsonType } from '../../types/JsonType'; +import { OCPPVersion } from '../../types/ocpp/OCPPVersion'; import type { IncomingRequestCommand } from '../../types/ocpp/Requests'; import logger from '../../utils/Logger'; import type ChargingStation from '../ChargingStation'; @@ -17,12 +18,22 @@ const moduleName = 'OCPPIncomingRequestService'; export default abstract class OCPPIncomingRequestService { private static instance: OCPPIncomingRequestService | null = null; protected asyncResource: AsyncResource; - private ajv: Ajv; + private readonly version: OCPPVersion; + private readonly ajv: Ajv; - protected constructor() { - this.asyncResource = new AsyncResource(moduleName); - this.ajv = new Ajv(); + protected constructor(version: OCPPVersion) { + 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.asyncResource = new AsyncResource(moduleName); this.incomingRequestHandler.bind(this); this.validateIncomingRequestPayload.bind(this); } @@ -39,7 +50,7 @@ export default abstract class OCPPIncomingRequestService { commandName: IncomingRequestCommand, error: Error, params: HandleErrorParams = { throwError: true } - ): T { + ): T | undefined { logger.error( `${chargingStation.logPrefix()} ${moduleName}.handleIncomingRequestError: Incoming request command '${commandName}' error:`, error