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