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