X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fcharging-station%2Focpp%2FOCPPResponseService.ts;h=5adcf6fb9007ae2ac52e80770f17b10d9d4dba00;hb=411894569d4a0333a4e38e911a178520a69448cd;hp=cfb1aa08f45e75a42e88c31295daa0ad6b1389e4;hpb=f7f98c68f78566039b7d6f53391e874d79a8b022;p=e-mobility-charging-stations-simulator.git diff --git a/src/charging-station/ocpp/OCPPResponseService.ts b/src/charging-station/ocpp/OCPPResponseService.ts index cfb1aa08..5adcf6fb 100644 --- a/src/charging-station/ocpp/OCPPResponseService.ts +++ b/src/charging-station/ocpp/OCPPResponseService.ts @@ -1,32 +1,80 @@ -import type ChargingStation from '../ChargingStation'; -import { JsonType } from '../../types/JsonType'; -import { RequestCommand } from '../../types/ocpp/Requests'; +import Ajv, { type JSONSchemaType } from 'ajv'; +import ajvFormats from 'ajv-formats'; -export default abstract class OCPPResponseService { - private static readonly instances: Map = new Map< - string, - OCPPResponseService - >(); +import { OCPPServiceUtils } from './internal'; +import type { ChargingStation } from '../../charging-station'; +import { OCPPError } from '../../exception'; +import type { + IncomingRequestCommand, + JsonObject, + JsonType, + OCPPVersion, + RequestCommand, +} from '../../types'; +import { logger } from '../../utils'; - protected readonly chargingStation: ChargingStation; +const moduleName = 'OCPPResponseService'; - protected constructor(chargingStation: ChargingStation) { - this.chargingStation = chargingStation; +export abstract class OCPPResponseService { + private static instance: OCPPResponseService | null = null; + private readonly version: OCPPVersion; + private readonly ajv: Ajv; + public abstract jsonIncomingRequestResponseSchemas: Map< + IncomingRequestCommand, + JSONSchemaType + >; + + protected constructor(version: OCPPVersion) { + this.version = version; + this.ajv = new Ajv({ + keywords: ['javaType'], + multipleOfPrecision: 2, + }); + ajvFormats(this.ajv); + this.responseHandler.bind(this); + this.validateResponsePayload.bind(this); + } + + public static getInstance(this: new () => T): T { + if (OCPPResponseService.instance === null) { + OCPPResponseService.instance = new this(); + } + return OCPPResponseService.instance as T; } - public static getInstance( - this: new (chargingStation: ChargingStation) => T, - chargingStation: ChargingStation - ): T { - if (!OCPPResponseService.instances.has(chargingStation.hashId)) { - OCPPResponseService.instances.set(chargingStation.hashId, new this(chargingStation)); + protected validateResponsePayload( + chargingStation: ChargingStation, + commandName: RequestCommand, + schema: JSONSchemaType, + payload: T + ): boolean { + if (chargingStation.getPayloadSchemaValidation() === false) { + return true; + } + const validate = this.ajv.compile(schema); + if (validate(payload)) { + return true; } - return OCPPResponseService.instances.get(chargingStation.hashId) as T; + logger.error( + `${chargingStation.logPrefix()} ${moduleName}.validateResponsePayload: Command '${commandName}' response PDU is invalid: %j`, + validate.errors + ); + throw new OCPPError( + OCPPServiceUtils.ajvErrorsToErrorType(validate.errors), + 'Response PDU is invalid', + commandName, + JSON.stringify(validate.errors, null, 2) + ); + } + + protected emptyResponseHandler() { + /* This is intentional */ } public abstract responseHandler( + chargingStation: ChargingStation, commandName: RequestCommand, - payload: JsonType | string, + payload: JsonType, requestPayload: JsonType ): Promise; }