Commit | Line | Data |
---|---|---|
24d15716 | 1 | import _Ajv, { type ValidateFunction } from 'ajv' |
66a7748d | 2 | import _ajvFormats from 'ajv-formats' |
e3018bc4 | 3 | |
66a7748d JB |
4 | import { OCPPConstants } from './OCPPConstants.js' |
5 | import { OCPPServiceUtils } from './OCPPServiceUtils.js' | |
6 | import { type ChargingStation, getIdTagsFile } from '../../charging-station/index.js' | |
7 | import { OCPPError } from '../../exception/index.js' | |
268a74bb JB |
8 | import type { |
9 | ClearCacheResponse, | |
10 | HandleErrorParams, | |
11 | IncomingRequestCommand, | |
268a74bb | 12 | JsonType, |
66a7748d JB |
13 | OCPPVersion |
14 | } from '../../types/index.js' | |
15 | import { logger, setDefaultErrorParams } from '../../utils/index.js' | |
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 = 'OCPPIncomingRequestService' |
e3018bc4 | 22 | |
d1ff8599 | 23 | export abstract class OCPPIncomingRequestService { |
66a7748d JB |
24 | private static instance: OCPPIncomingRequestService | null = null |
25 | private readonly version: OCPPVersion | |
24d15716 JB |
26 | protected readonly ajv: Ajv |
27 | protected abstract jsonSchemasValidateFunction: Map< | |
28 | IncomingRequestCommand, | |
29 | ValidateFunction<JsonType> | |
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) | |
ba9a56a6 JB |
39 | this.incomingRequestHandler = this.incomingRequestHandler.bind(this) |
40 | this.validateIncomingRequestPayload = this.validateIncomingRequestPayload.bind(this) | |
c0560973 JB |
41 | } |
42 | ||
08f130a0 | 43 | public static getInstance<T extends OCPPIncomingRequestService>(this: new () => T): T { |
1ca780f9 | 44 | if (OCPPIncomingRequestService.instance === null) { |
66a7748d | 45 | OCPPIncomingRequestService.instance = new this() |
9f2e3130 | 46 | } |
66a7748d | 47 | return OCPPIncomingRequestService.instance as T |
9f2e3130 JB |
48 | } |
49 | ||
7b5dbe91 | 50 | protected handleIncomingRequestError<T extends JsonType>( |
08f130a0 | 51 | chargingStation: ChargingStation, |
e7aeea18 JB |
52 | commandName: IncomingRequestCommand, |
53 | error: Error, | |
66a7748d | 54 | params: HandleErrorParams<T> = { throwError: true, consoleOut: false } |
51581a20 | 55 | ): T | undefined { |
66a7748d | 56 | setDefaultErrorParams(params) |
e7aeea18 | 57 | logger.error( |
60ddad53 | 58 | `${chargingStation.logPrefix()} ${moduleName}.handleIncomingRequestError: Incoming request command '${commandName}' error:`, |
66a7748d JB |
59 | error |
60 | ) | |
5199f9fd JB |
61 | if (params.throwError === false && params.errorResponse != null) { |
62 | return params.errorResponse | |
e64c0923 | 63 | } |
5199f9fd | 64 | if (params.throwError === true && params.errorResponse == null) { |
66a7748d | 65 | throw error |
e0a50bcd | 66 | } |
5199f9fd JB |
67 | if (params.throwError === true && params.errorResponse != null) { |
68 | return params.errorResponse | |
717c1e56 | 69 | } |
47e22477 JB |
70 | } |
71 | ||
e3018bc4 JB |
72 | protected validateIncomingRequestPayload<T extends JsonType>( |
73 | chargingStation: ChargingStation, | |
74 | commandName: IncomingRequestCommand, | |
66a7748d | 75 | payload: T |
e3018bc4 | 76 | ): boolean { |
5398cecf | 77 | if (chargingStation.stationInfo?.ocppStrictCompliance === false) { |
66a7748d | 78 | return true |
e3018bc4 | 79 | } |
24d15716 JB |
80 | const validate = this.jsonSchemasValidateFunction.get(commandName) |
81 | if (validate?.(payload) === true) { | |
66a7748d | 82 | return true |
e3018bc4 JB |
83 | } |
84 | logger.error( | |
45988780 | 85 | `${chargingStation.logPrefix()} ${moduleName}.validateIncomingRequestPayload: Command '${commandName}' incoming request PDU is invalid: %j`, |
24d15716 | 86 | validate?.errors |
66a7748d | 87 | ) |
e3018bc4 | 88 | throw new OCPPError( |
24d15716 | 89 | OCPPServiceUtils.ajvErrorsToErrorType(validate?.errors), |
e3018bc4 JB |
90 | 'Incoming request PDU is invalid', |
91 | commandName, | |
24d15716 | 92 | JSON.stringify(validate?.errors, undefined, 2) |
66a7748d | 93 | ) |
e3018bc4 JB |
94 | } |
95 | ||
66a7748d JB |
96 | protected handleRequestClearCache (chargingStation: ChargingStation): ClearCacheResponse { |
97 | // eslint-disable-next-line @typescript-eslint/no-non-null-assertion | |
5199f9fd | 98 | if (chargingStation.idTagsCache.deleteIdTags(getIdTagsFile(chargingStation.stationInfo!)!)) { |
66a7748d | 99 | return OCPPConstants.OCPP_RESPONSE_ACCEPTED |
26a17d93 | 100 | } |
66a7748d | 101 | return OCPPConstants.OCPP_RESPONSE_REJECTED |
22e0d48e JB |
102 | } |
103 | ||
9429aa42 JB |
104 | // eslint-disable-next-line @typescript-eslint/no-unused-vars |
105 | public abstract incomingRequestHandler<ReqType extends JsonType, ResType extends JsonType>( | |
08f130a0 | 106 | chargingStation: ChargingStation, |
e7aeea18 JB |
107 | messageId: string, |
108 | commandName: IncomingRequestCommand, | |
66a7748d JB |
109 | commandPayload: ReqType |
110 | ): Promise<void> | |
c0560973 | 111 | } |