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 | >() | |
ba9a56a6 JB |
50 | this.responseHandler = this.responseHandler.bind(this) |
51 | this.validateResponsePayload = this.validateResponsePayload.bind(this) | |
c0560973 JB |
52 | } |
53 | ||
08f130a0 | 54 | public static getInstance<T extends OCPPResponseService>(this: new () => T): T { |
1ca780f9 | 55 | if (OCPPResponseService.instance === null) { |
66a7748d | 56 | OCPPResponseService.instance = new this() |
9f2e3130 | 57 | } |
66a7748d | 58 | return OCPPResponseService.instance as T |
9f2e3130 JB |
59 | } |
60 | ||
844e496b JB |
61 | protected validateResponsePayload<T extends JsonType>( |
62 | chargingStation: ChargingStation, | |
63 | commandName: RequestCommand, | |
64 | schema: JSONSchemaType<T>, | |
66a7748d | 65 | payload: T |
844e496b | 66 | ): boolean { |
5398cecf | 67 | if (chargingStation.stationInfo?.ocppStrictCompliance === false) { |
66a7748d | 68 | return true |
844e496b | 69 | } |
66a7748d | 70 | const validate = this.getJsonRequestValidateFunction<T>(commandName, schema) |
844e496b | 71 | if (validate(payload)) { |
66a7748d | 72 | return true |
844e496b JB |
73 | } |
74 | logger.error( | |
45988780 | 75 | `${chargingStation.logPrefix()} ${moduleName}.validateResponsePayload: Command '${commandName}' response PDU is invalid: %j`, |
66a7748d JB |
76 | validate.errors |
77 | ) | |
844e496b | 78 | throw new OCPPError( |
9ff486f4 | 79 | OCPPServiceUtils.ajvErrorsToErrorType(validate.errors), |
844e496b JB |
80 | 'Response PDU is invalid', |
81 | commandName, | |
66a7748d JB |
82 | JSON.stringify(validate.errors, undefined, 2) |
83 | ) | |
844e496b JB |
84 | } |
85 | ||
fc5529db | 86 | protected emptyResponseHandler = Constants.EMPTY_FUNCTION |
b52c969d | 87 | |
0b0ca54f JB |
88 | private getJsonRequestValidateFunction<T extends JsonType>( |
89 | commandName: RequestCommand, | |
66a7748d JB |
90 | schema: JSONSchemaType<T> |
91 | ): ValidateFunction<JsonType> { | |
92 | if (!this.jsonRequestValidateFunctions.has(commandName)) { | |
93 | this.jsonRequestValidateFunctions.set(commandName, this.ajv.compile<T>(schema).bind(this)) | |
0b0ca54f | 94 | } |
66a7748d JB |
95 | // eslint-disable-next-line @typescript-eslint/no-non-null-assertion |
96 | return this.jsonRequestValidateFunctions.get(commandName)! | |
0b0ca54f JB |
97 | } |
98 | ||
9429aa42 | 99 | public abstract responseHandler<ReqType extends JsonType, ResType extends JsonType>( |
08f130a0 | 100 | chargingStation: ChargingStation, |
e7aeea18 | 101 | commandName: RequestCommand, |
9429aa42 | 102 | payload: ResType, |
66a7748d JB |
103 | requestPayload: ReqType |
104 | ): Promise<void> | |
c0560973 | 105 | } |