Commit | Line | Data |
---|---|---|
d270cc87 | 1 | import Ajv, { type JSONSchemaType } 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; |
d270cc87 | 20 | private readonly version: OCPPVersion; |
012ae1a9 | 21 | private readonly ajv: Ajv; |
b3fc3ff5 JB |
22 | public abstract jsonIncomingRequestResponseSchemas: Map< |
23 | IncomingRequestCommand, | |
24 | JSONSchemaType<JsonObject> | |
25 | >; | |
10068088 | 26 | |
d270cc87 JB |
27 | protected constructor(version: OCPPVersion) { |
28 | this.version = version; | |
45988780 | 29 | this.ajv = new Ajv({ |
98fc1389 | 30 | keywords: ['javaType'], |
45988780 JB |
31 | multipleOfPrecision: 2, |
32 | }); | |
844e496b | 33 | ajvFormats(this.ajv); |
31f59c6d JB |
34 | this.responseHandler = this.responseHandler.bind(this) as ( |
35 | chargingStation: ChargingStation, | |
36 | commandName: RequestCommand, | |
37 | payload: JsonType, | |
38 | requestPayload: JsonType | |
39 | ) => Promise<void>; | |
40 | this.validateResponsePayload = this.validateResponsePayload.bind(this) as <T extends JsonType>( | |
41 | chargingStation: ChargingStation, | |
42 | commandName: RequestCommand, | |
43 | schema: JSONSchemaType<T>, | |
44 | payload: T | |
45 | ) => boolean; | |
c0560973 JB |
46 | } |
47 | ||
08f130a0 | 48 | public static getInstance<T extends OCPPResponseService>(this: new () => T): T { |
1ca780f9 | 49 | if (OCPPResponseService.instance === null) { |
08f130a0 | 50 | OCPPResponseService.instance = new this(); |
9f2e3130 | 51 | } |
08f130a0 | 52 | return OCPPResponseService.instance as T; |
9f2e3130 JB |
53 | } |
54 | ||
844e496b JB |
55 | protected validateResponsePayload<T extends JsonType>( |
56 | chargingStation: ChargingStation, | |
57 | commandName: RequestCommand, | |
58 | schema: JSONSchemaType<T>, | |
59 | payload: T | |
60 | ): boolean { | |
0282b7c0 | 61 | if (chargingStation.getOcppStrictCompliance() === false) { |
844e496b JB |
62 | return true; |
63 | } | |
64 | const validate = this.ajv.compile(schema); | |
65 | if (validate(payload)) { | |
66 | return true; | |
67 | } | |
68 | logger.error( | |
45988780 | 69 | `${chargingStation.logPrefix()} ${moduleName}.validateResponsePayload: Command '${commandName}' response PDU is invalid: %j`, |
844e496b JB |
70 | validate.errors |
71 | ); | |
72 | throw new OCPPError( | |
01a4dcbb | 73 | OCPPServiceUtils.ajvErrorsToErrorType(validate.errors), |
844e496b JB |
74 | 'Response PDU is invalid', |
75 | commandName, | |
76 | JSON.stringify(validate.errors, null, 2) | |
77 | ); | |
78 | } | |
79 | ||
81e3cc38 | 80 | protected emptyResponseHandler() { |
bd770dde | 81 | /* This is intentional */ |
81e3cc38 | 82 | } |
b52c969d | 83 | |
f7f98c68 | 84 | public abstract responseHandler( |
08f130a0 | 85 | chargingStation: ChargingStation, |
e7aeea18 | 86 | commandName: RequestCommand, |
5cc4b63b JB |
87 | payload: JsonType, |
88 | requestPayload: JsonType | |
e7aeea18 | 89 | ): Promise<void>; |
c0560973 | 90 | } |