X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fcharging-station%2Focpp%2FOCPPResponseService.ts;h=91b1499676b01f3b1e7583c0c385490c70e78ef8;hb=1789ba2c82f32832d577617a615d79c889eea1e6;hp=2fa0112c70c6d42fc29958da64007576824b6285;hpb=e7aeea18e189dd087c8f951cf77a253e2818ae90;p=e-mobility-charging-stations-simulator.git diff --git a/src/charging-station/ocpp/OCPPResponseService.ts b/src/charging-station/ocpp/OCPPResponseService.ts index 2fa0112c..91b14996 100644 --- a/src/charging-station/ocpp/OCPPResponseService.ts +++ b/src/charging-station/ocpp/OCPPResponseService.ts @@ -1,31 +1,66 @@ +import type { JSONSchemaType } from 'ajv'; +import Ajv from 'ajv-draft-04'; +import ajvFormats from 'ajv-formats'; + +import OCPPError from '../../exception/OCPPError'; +import type { JsonType } from '../../types/JsonType'; +import type { RequestCommand } from '../../types/ocpp/Requests'; +import logger from '../../utils/Logger'; import type ChargingStation from '../ChargingStation'; -import { JsonType } from '../../types/JsonType'; -import { RequestCommand } from '../../types/ocpp/Requests'; +import { OCPPServiceUtils } from './OCPPServiceUtils'; + +const moduleName = 'OCPPResponseService'; export default abstract class OCPPResponseService { - private static readonly instances: Map = new Map< - string, - OCPPResponseService - >(); - protected readonly chargingStation: ChargingStation; - - protected constructor(chargingStation: ChargingStation) { - this.chargingStation = chargingStation; + private static instance: OCPPResponseService | null = null; + private ajv: Ajv; + + protected constructor() { + this.ajv = new Ajv(); + ajvFormats(this.ajv); + this.responseHandler.bind(this); + this.validateResponsePayload.bind(this); } - public static getInstance( - this: new (chargingStation: ChargingStation) => T, - chargingStation: ChargingStation - ): T { - if (!OCPPResponseService.instances.has(chargingStation.id)) { - OCPPResponseService.instances.set(chargingStation.id, new this(chargingStation)); + public static getInstance(this: new () => T): T { + if (OCPPResponseService.instance === null) { + OCPPResponseService.instance = new this(); } - return OCPPResponseService.instances.get(chargingStation.id) as T; + return OCPPResponseService.instance as T; } - public abstract handleResponse( + 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; + } + logger.error( + `${chargingStation.logPrefix()} ${moduleName}.validateResponsePayload: 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) + ); + } + + // eslint-disable-next-line @typescript-eslint/no-empty-function + protected emptyResponseHandler() {} + + public abstract responseHandler( + chargingStation: ChargingStation, commandName: RequestCommand, - payload: JsonType | string, + payload: JsonType, requestPayload: JsonType ): Promise; }