| 1 | import _Ajv, { type JSONSchemaType, type ValidateFunction } from 'ajv' |
| 2 | import _ajvFormats from 'ajv-formats' |
| 3 | |
| 4 | import { OCPPServiceUtils } from './OCPPServiceUtils.js' |
| 5 | import type { ChargingStation } from '../../charging-station/index.js' |
| 6 | import { OCPPError } from '../../exception/index.js' |
| 7 | import type { |
| 8 | IncomingRequestCommand, |
| 9 | JsonType, |
| 10 | OCPPVersion, |
| 11 | RequestCommand |
| 12 | } from '../../types/index.js' |
| 13 | import { logger } from '../../utils/index.js' |
| 14 | type Ajv = _Ajv.default |
| 15 | // eslint-disable-next-line @typescript-eslint/no-redeclare |
| 16 | const Ajv = _Ajv.default |
| 17 | const ajvFormats = _ajvFormats.default |
| 18 | |
| 19 | const moduleName = 'OCPPResponseService' |
| 20 | |
| 21 | export abstract class OCPPResponseService { |
| 22 | private static instance: OCPPResponseService | null = null |
| 23 | |
| 24 | public jsonIncomingRequestResponseValidateFunctions: Map< |
| 25 | IncomingRequestCommand, |
| 26 | ValidateFunction<JsonType> |
| 27 | > |
| 28 | |
| 29 | private readonly version: OCPPVersion |
| 30 | private readonly ajv: Ajv |
| 31 | private readonly jsonRequestValidateFunctions: Map<RequestCommand, ValidateFunction<JsonType>> |
| 32 | |
| 33 | public abstract jsonIncomingRequestResponseSchemas: Map< |
| 34 | IncomingRequestCommand, |
| 35 | JSONSchemaType<JsonType> |
| 36 | > |
| 37 | |
| 38 | protected constructor (version: OCPPVersion) { |
| 39 | this.version = version |
| 40 | this.ajv = new Ajv({ |
| 41 | keywords: ['javaType'], |
| 42 | multipleOfPrecision: 2 |
| 43 | }) |
| 44 | ajvFormats(this.ajv) |
| 45 | this.jsonRequestValidateFunctions = new Map<RequestCommand, ValidateFunction<JsonType>>() |
| 46 | this.jsonIncomingRequestResponseValidateFunctions = new Map< |
| 47 | IncomingRequestCommand, |
| 48 | ValidateFunction<JsonType> |
| 49 | >() |
| 50 | this.responseHandler = this.responseHandler.bind(this) as < |
| 51 | ReqType extends JsonType, |
| 52 | ResType extends JsonType, |
| 53 | >( |
| 54 | chargingStation: ChargingStation, |
| 55 | commandName: RequestCommand, |
| 56 | payload: ResType, |
| 57 | requestPayload: ReqType |
| 58 | ) => Promise<void> |
| 59 | this.validateResponsePayload = this.validateResponsePayload.bind(this) as <T extends JsonType>( |
| 60 | chargingStation: ChargingStation, |
| 61 | commandName: RequestCommand, |
| 62 | schema: JSONSchemaType<T>, |
| 63 | payload: T |
| 64 | ) => boolean |
| 65 | } |
| 66 | |
| 67 | public static getInstance<T extends OCPPResponseService>(this: new () => T): T { |
| 68 | if (OCPPResponseService.instance === null) { |
| 69 | OCPPResponseService.instance = new this() |
| 70 | } |
| 71 | return OCPPResponseService.instance as T |
| 72 | } |
| 73 | |
| 74 | protected validateResponsePayload<T extends JsonType>( |
| 75 | chargingStation: ChargingStation, |
| 76 | commandName: RequestCommand, |
| 77 | schema: JSONSchemaType<T>, |
| 78 | payload: T |
| 79 | ): boolean { |
| 80 | if (chargingStation.stationInfo?.ocppStrictCompliance === false) { |
| 81 | return true |
| 82 | } |
| 83 | const validate = this.getJsonRequestValidateFunction<T>(commandName, schema) |
| 84 | if (validate(payload)) { |
| 85 | return true |
| 86 | } |
| 87 | logger.error( |
| 88 | `${chargingStation.logPrefix()} ${moduleName}.validateResponsePayload: Command '${commandName}' response PDU is invalid: %j`, |
| 89 | validate.errors |
| 90 | ) |
| 91 | throw new OCPPError( |
| 92 | OCPPServiceUtils.ajvErrorsToErrorType(validate.errors), |
| 93 | 'Response PDU is invalid', |
| 94 | commandName, |
| 95 | JSON.stringify(validate.errors, undefined, 2) |
| 96 | ) |
| 97 | } |
| 98 | |
| 99 | protected emptyResponseHandler (): void { |
| 100 | /* This is intentional */ |
| 101 | } |
| 102 | |
| 103 | private getJsonRequestValidateFunction<T extends JsonType>( |
| 104 | commandName: RequestCommand, |
| 105 | schema: JSONSchemaType<T> |
| 106 | ): ValidateFunction<JsonType> { |
| 107 | if (!this.jsonRequestValidateFunctions.has(commandName)) { |
| 108 | this.jsonRequestValidateFunctions.set(commandName, this.ajv.compile<T>(schema).bind(this)) |
| 109 | } |
| 110 | // eslint-disable-next-line @typescript-eslint/no-non-null-assertion |
| 111 | return this.jsonRequestValidateFunctions.get(commandName)! |
| 112 | } |
| 113 | |
| 114 | public abstract responseHandler<ReqType extends JsonType, ResType extends JsonType>( |
| 115 | chargingStation: ChargingStation, |
| 116 | commandName: RequestCommand, |
| 117 | payload: ResType, |
| 118 | requestPayload: ReqType |
| 119 | ): Promise<void> |
| 120 | } |