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