]>
Commit | Line | Data |
---|---|---|
24d15716 | 1 | import _Ajv, { type ValidateFunction } from 'ajv' |
66a7748d | 2 | import _ajvFormats from 'ajv-formats' |
844e496b | 3 | |
66a7748d | 4 | import type { ChargingStation } from '../../charging-station/index.js' |
a6ef1ece JB |
5 | import type { |
6 | IncomingRequestCommand, | |
7 | JsonType, | |
8 | OCPPVersion, | |
d1f5bfd8 | 9 | RequestCommand, |
66a7748d | 10 | } from '../../types/index.js' |
0749233f JB |
11 | |
12 | import { OCPPError } from '../../exception/index.js' | |
fc5529db | 13 | import { Constants, logger } from '../../utils/index.js' |
01b82de5 | 14 | import { ajvErrorsToErrorType } from './OCPPServiceUtils.js' |
0c58e0c2 | 15 | |
66a7748d JB |
16 | type Ajv = _Ajv.default |
17 | // eslint-disable-next-line @typescript-eslint/no-redeclare | |
18 | const Ajv = _Ajv.default | |
19 | const ajvFormats = _ajvFormats.default | |
c0560973 | 20 | |
66a7748d | 21 | const moduleName = 'OCPPResponseService' |
e3018bc4 | 22 | |
268a74bb | 23 | export abstract class OCPPResponseService { |
0749233f | 24 | private static instance: null | OCPPResponseService = null |
c4a89082 JB |
25 | public abstract incomingRequestResponsePayloadValidateFunctions: Map< |
26 | IncomingRequestCommand, | |
27 | ValidateFunction<JsonType> | |
28 | > | |
29 | ||
24d15716 | 30 | protected readonly ajv: Ajv |
298be10c | 31 | protected readonly ajvIncomingRequest: Ajv |
0749233f | 32 | protected emptyResponseHandler = Constants.EMPTY_FUNCTION |
d5490a13 | 33 | protected abstract payloadValidateFunctions: Map<RequestCommand, ValidateFunction<JsonType>> |
0749233f | 34 | private readonly version: OCPPVersion |
10068088 | 35 | |
66a7748d JB |
36 | protected constructor (version: OCPPVersion) { |
37 | this.version = version | |
45988780 | 38 | this.ajv = new Ajv({ |
98fc1389 | 39 | keywords: ['javaType'], |
d1f5bfd8 | 40 | multipleOfPrecision: 2, |
66a7748d JB |
41 | }) |
42 | ajvFormats(this.ajv) | |
298be10c JB |
43 | this.ajvIncomingRequest = new Ajv({ |
44 | keywords: ['javaType'], | |
d1f5bfd8 | 45 | multipleOfPrecision: 2, |
298be10c JB |
46 | }) |
47 | ajvFormats(this.ajvIncomingRequest) | |
ba9a56a6 JB |
48 | this.responseHandler = this.responseHandler.bind(this) |
49 | this.validateResponsePayload = this.validateResponsePayload.bind(this) | |
c0560973 JB |
50 | } |
51 | ||
08f130a0 | 52 | public static getInstance<T extends OCPPResponseService>(this: new () => T): T { |
678ffc93 | 53 | OCPPResponseService.instance ??= new this() |
66a7748d | 54 | return OCPPResponseService.instance as T |
9f2e3130 JB |
55 | } |
56 | ||
c4a89082 JB |
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 | ||
01841aad | 65 | // eslint-disable-next-line @typescript-eslint/no-unnecessary-type-parameters |
844e496b JB |
66 | protected validateResponsePayload<T extends JsonType>( |
67 | chargingStation: ChargingStation, | |
68 | commandName: RequestCommand, | |
66a7748d | 69 | payload: T |
844e496b | 70 | ): boolean { |
5398cecf | 71 | if (chargingStation.stationInfo?.ocppStrictCompliance === false) { |
66a7748d | 72 | return true |
844e496b | 73 | } |
d5490a13 | 74 | const validate = this.payloadValidateFunctions.get(commandName) |
24d15716 | 75 | if (validate?.(payload) === true) { |
66a7748d | 76 | return true |
844e496b JB |
77 | } |
78 | logger.error( | |
45988780 | 79 | `${chargingStation.logPrefix()} ${moduleName}.validateResponsePayload: Command '${commandName}' response PDU is invalid: %j`, |
24d15716 | 80 | validate?.errors |
66a7748d | 81 | ) |
844e496b | 82 | throw new OCPPError( |
01b82de5 | 83 | ajvErrorsToErrorType(validate?.errors), |
844e496b JB |
84 | 'Response PDU is invalid', |
85 | commandName, | |
24d15716 | 86 | JSON.stringify(validate?.errors, undefined, 2) |
66a7748d | 87 | ) |
844e496b | 88 | } |
c0560973 | 89 | } |