X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fcharging-station%2Focpp%2FOCPPResponseService.ts;h=97a130341efe1f669ef2a242325f5d76cdc2205e;hb=98fc1389a2464ce8738047f8990731ae31938ee5;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..97a13034 100644 --- a/src/charging-station/ocpp/OCPPResponseService.ts +++ b/src/charging-station/ocpp/OCPPResponseService.ts @@ -1,31 +1,72 @@ +import Ajv, { type JSONSchemaType } from 'ajv'; +import ajvFormats from 'ajv-formats'; + +import OCPPError from '../../exception/OCPPError'; +import type { JsonType } from '../../types/JsonType'; +import type { OCPPVersion } from '../../types/ocpp/OCPPVersion'; +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 readonly version: OCPPVersion; + private readonly ajv: Ajv; + + 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.id)) { - OCPPResponseService.instances.set(chargingStation.id, 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.id) 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 handleResponse( + public abstract responseHandler( + chargingStation: ChargingStation, commandName: RequestCommand, - payload: JsonType | string, + payload: JsonType, requestPayload: JsonType ): Promise; }