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