]>
Commit | Line | Data |
---|---|---|
1 | import _Ajv, { type ValidateFunction } from 'ajv' | |
2 | import _ajvFormats from 'ajv-formats' | |
3 | ||
4 | import type { ChargingStation } from '../../charging-station/index.js' | |
5 | import type { | |
6 | IncomingRequestCommand, | |
7 | JsonType, | |
8 | OCPPVersion, | |
9 | RequestCommand, | |
10 | } from '../../types/index.js' | |
11 | ||
12 | import { OCPPError } from '../../exception/index.js' | |
13 | import { Constants, logger } from '../../utils/index.js' | |
14 | import { ajvErrorsToErrorType } from './OCPPServiceUtils.js' | |
15 | ||
16 | type Ajv = _Ajv.default | |
17 | // eslint-disable-next-line @typescript-eslint/no-redeclare | |
18 | const Ajv = _Ajv.default | |
19 | const ajvFormats = _ajvFormats.default | |
20 | ||
21 | const moduleName = 'OCPPResponseService' | |
22 | ||
23 | export abstract class OCPPResponseService { | |
24 | private static instance: null | OCPPResponseService = null | |
25 | public abstract incomingRequestResponsePayloadValidateFunctions: Map< | |
26 | IncomingRequestCommand, | |
27 | ValidateFunction<JsonType> | |
28 | > | |
29 | ||
30 | protected readonly ajv: Ajv | |
31 | protected readonly ajvIncomingRequest: Ajv | |
32 | protected emptyResponseHandler = Constants.EMPTY_FUNCTION | |
33 | protected abstract payloadValidateFunctions: Map<RequestCommand, ValidateFunction<JsonType>> | |
34 | private readonly version: OCPPVersion | |
35 | ||
36 | protected constructor (version: OCPPVersion) { | |
37 | this.version = version | |
38 | this.ajv = new Ajv({ | |
39 | keywords: ['javaType'], | |
40 | multipleOfPrecision: 2, | |
41 | }) | |
42 | ajvFormats(this.ajv) | |
43 | this.ajvIncomingRequest = new Ajv({ | |
44 | keywords: ['javaType'], | |
45 | multipleOfPrecision: 2, | |
46 | }) | |
47 | ajvFormats(this.ajvIncomingRequest) | |
48 | this.responseHandler = this.responseHandler.bind(this) | |
49 | this.validateResponsePayload = this.validateResponsePayload.bind(this) | |
50 | } | |
51 | ||
52 | public static getInstance<T extends OCPPResponseService>(this: new () => T): T { | |
53 | OCPPResponseService.instance ??= new this() | |
54 | return OCPPResponseService.instance as T | |
55 | } | |
56 | ||
57 | // eslint-disable-next-line @typescript-eslint/no-unnecessary-type-parameters | |
58 | public abstract responseHandler<ReqType extends JsonType, ResType extends JsonType>( | |
59 | chargingStation: ChargingStation, | |
60 | commandName: RequestCommand, | |
61 | payload: ResType, | |
62 | requestPayload: ReqType | |
63 | ): Promise<void> | |
64 | ||
65 | // eslint-disable-next-line @typescript-eslint/no-unnecessary-type-parameters | |
66 | protected validateResponsePayload<T extends JsonType>( | |
67 | chargingStation: ChargingStation, | |
68 | commandName: RequestCommand, | |
69 | payload: T | |
70 | ): boolean { | |
71 | if (chargingStation.stationInfo?.ocppStrictCompliance === false) { | |
72 | return true | |
73 | } | |
74 | const validate = this.payloadValidateFunctions.get(commandName) | |
75 | if (validate?.(payload) === true) { | |
76 | return true | |
77 | } | |
78 | logger.error( | |
79 | `${chargingStation.logPrefix()} ${moduleName}.validateResponsePayload: Command '${commandName}' response PDU is invalid: %j`, | |
80 | validate?.errors | |
81 | ) | |
82 | throw new OCPPError( | |
83 | ajvErrorsToErrorType(validate?.errors), | |
84 | 'Response PDU is invalid', | |
85 | commandName, | |
86 | JSON.stringify(validate?.errors, undefined, 2) | |
87 | ) | |
88 | } | |
89 | } |