Commit | Line | Data |
---|---|---|
d270cc87 JB |
1 | import Ajv, { type JSONSchemaType } from 'ajv'; |
2 | import AjvDraft04 from 'ajv-draft-04'; | |
844e496b JB |
3 | import ajvFormats from 'ajv-formats'; |
4 | ||
5 | import OCPPError from '../../exception/OCPPError'; | |
6c1761d4 | 6 | import type { JsonType } from '../../types/JsonType'; |
d270cc87 | 7 | import { OCPPVersion } from '../../types/ocpp/OCPPVersion'; |
6c1761d4 | 8 | import type { RequestCommand } from '../../types/ocpp/Requests'; |
844e496b | 9 | import logger from '../../utils/Logger'; |
8114d10e | 10 | import type ChargingStation from '../ChargingStation'; |
cbb3711f | 11 | import { OCPPServiceUtils } from './OCPPServiceUtils'; |
c0560973 | 12 | |
e3018bc4 JB |
13 | const moduleName = 'OCPPResponseService'; |
14 | ||
c0560973 | 15 | export default abstract class OCPPResponseService { |
08f130a0 | 16 | private static instance: OCPPResponseService | null = null; |
d270cc87 | 17 | private readonly version: OCPPVersion; |
012ae1a9 | 18 | private readonly ajv: Ajv; |
10068088 | 19 | |
d270cc87 JB |
20 | protected constructor(version: OCPPVersion) { |
21 | this.version = version; | |
22 | switch (this.version) { | |
23 | case OCPPVersion.VERSION_16: | |
24 | this.ajv = new AjvDraft04(); | |
25 | break; | |
26 | case OCPPVersion.VERSION_20: | |
27 | case OCPPVersion.VERSION_201: | |
28 | this.ajv = new Ajv(); | |
29 | break; | |
30 | } | |
844e496b | 31 | ajvFormats(this.ajv); |
9952c548 JB |
32 | this.responseHandler.bind(this); |
33 | this.validateResponsePayload.bind(this); | |
c0560973 JB |
34 | } |
35 | ||
08f130a0 | 36 | public static getInstance<T extends OCPPResponseService>(this: new () => T): T { |
1ca780f9 | 37 | if (OCPPResponseService.instance === null) { |
08f130a0 | 38 | OCPPResponseService.instance = new this(); |
9f2e3130 | 39 | } |
08f130a0 | 40 | return OCPPResponseService.instance as T; |
9f2e3130 JB |
41 | } |
42 | ||
844e496b JB |
43 | protected validateResponsePayload<T extends JsonType>( |
44 | chargingStation: ChargingStation, | |
45 | commandName: RequestCommand, | |
46 | schema: JSONSchemaType<T>, | |
47 | payload: T | |
48 | ): boolean { | |
0638ddd2 | 49 | if (chargingStation.getPayloadSchemaValidation() === false) { |
844e496b JB |
50 | return true; |
51 | } | |
52 | const validate = this.ajv.compile(schema); | |
53 | if (validate(payload)) { | |
54 | return true; | |
55 | } | |
56 | logger.error( | |
57 | `${chargingStation.logPrefix()} ${moduleName}.validateResponsePayload: Response PDU is invalid: %j`, | |
58 | validate.errors | |
59 | ); | |
60 | throw new OCPPError( | |
01a4dcbb | 61 | OCPPServiceUtils.ajvErrorsToErrorType(validate.errors), |
844e496b JB |
62 | 'Response PDU is invalid', |
63 | commandName, | |
64 | JSON.stringify(validate.errors, null, 2) | |
65 | ); | |
66 | } | |
67 | ||
81e3cc38 | 68 | protected emptyResponseHandler() { |
bd770dde | 69 | /* This is intentional */ |
81e3cc38 | 70 | } |
b52c969d | 71 | |
f7f98c68 | 72 | public abstract responseHandler( |
08f130a0 | 73 | chargingStation: ChargingStation, |
e7aeea18 | 74 | commandName: RequestCommand, |
5cc4b63b JB |
75 | payload: JsonType, |
76 | requestPayload: JsonType | |
e7aeea18 | 77 | ): Promise<void>; |
c0560973 | 78 | } |