Commit | Line | Data |
---|---|---|
66a7748d JB |
1 | import _Ajv, { type JSONSchemaType, type ValidateFunction } from 'ajv' |
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 | |
26 | private readonly ajv: Ajv | |
27 | private readonly jsonValidateFunctions: Map<IncomingRequestCommand, ValidateFunction<JsonType>> | |
28 | protected abstract jsonSchemas: Map<IncomingRequestCommand, JSONSchemaType<JsonType>> | |
10068088 | 29 | |
66a7748d JB |
30 | protected constructor (version: OCPPVersion) { |
31 | this.version = version | |
45988780 | 32 | this.ajv = new Ajv({ |
98fc1389 | 33 | keywords: ['javaType'], |
66a7748d JB |
34 | multipleOfPrecision: 2 |
35 | }) | |
36 | ajvFormats(this.ajv) | |
37 | this.jsonValidateFunctions = new Map<IncomingRequestCommand, ValidateFunction<JsonType>>() | |
ba9a56a6 JB |
38 | this.incomingRequestHandler = this.incomingRequestHandler.bind(this) |
39 | this.validateIncomingRequestPayload = this.validateIncomingRequestPayload.bind(this) | |
c0560973 JB |
40 | } |
41 | ||
08f130a0 | 42 | public static getInstance<T extends OCPPIncomingRequestService>(this: new () => T): T { |
1ca780f9 | 43 | if (OCPPIncomingRequestService.instance === null) { |
66a7748d | 44 | OCPPIncomingRequestService.instance = new this() |
9f2e3130 | 45 | } |
66a7748d | 46 | return OCPPIncomingRequestService.instance as T |
9f2e3130 JB |
47 | } |
48 | ||
7b5dbe91 | 49 | protected handleIncomingRequestError<T extends JsonType>( |
08f130a0 | 50 | chargingStation: ChargingStation, |
e7aeea18 JB |
51 | commandName: IncomingRequestCommand, |
52 | error: Error, | |
66a7748d | 53 | params: HandleErrorParams<T> = { throwError: true, consoleOut: false } |
51581a20 | 54 | ): T | undefined { |
66a7748d | 55 | setDefaultErrorParams(params) |
e7aeea18 | 56 | logger.error( |
60ddad53 | 57 | `${chargingStation.logPrefix()} ${moduleName}.handleIncomingRequestError: Incoming request command '${commandName}' error:`, |
66a7748d JB |
58 | error |
59 | ) | |
5199f9fd JB |
60 | if (params.throwError === false && params.errorResponse != null) { |
61 | return params.errorResponse | |
e64c0923 | 62 | } |
5199f9fd | 63 | if (params.throwError === true && params.errorResponse == null) { |
66a7748d | 64 | throw error |
e0a50bcd | 65 | } |
5199f9fd JB |
66 | if (params.throwError === true && params.errorResponse != null) { |
67 | return params.errorResponse | |
717c1e56 | 68 | } |
47e22477 JB |
69 | } |
70 | ||
e3018bc4 JB |
71 | protected validateIncomingRequestPayload<T extends JsonType>( |
72 | chargingStation: ChargingStation, | |
73 | commandName: IncomingRequestCommand, | |
74 | schema: JSONSchemaType<T>, | |
66a7748d | 75 | payload: T |
e3018bc4 | 76 | ): boolean { |
5398cecf | 77 | if (chargingStation.stationInfo?.ocppStrictCompliance === false) { |
66a7748d | 78 | return true |
e3018bc4 | 79 | } |
66a7748d | 80 | const validate = this.getJsonIncomingRequestValidateFunction<T>(commandName, schema) |
e3018bc4 | 81 | if (validate(payload)) { |
66a7748d | 82 | return true |
e3018bc4 JB |
83 | } |
84 | logger.error( | |
45988780 | 85 | `${chargingStation.logPrefix()} ${moduleName}.validateIncomingRequestPayload: Command '${commandName}' incoming request PDU is invalid: %j`, |
66a7748d JB |
86 | validate.errors |
87 | ) | |
e3018bc4 | 88 | throw new OCPPError( |
9ff486f4 | 89 | OCPPServiceUtils.ajvErrorsToErrorType(validate.errors), |
e3018bc4 JB |
90 | 'Incoming request PDU is invalid', |
91 | commandName, | |
66a7748d JB |
92 | JSON.stringify(validate.errors, undefined, 2) |
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 | ||
0b0ca54f JB |
104 | private getJsonIncomingRequestValidateFunction<T extends JsonType>( |
105 | commandName: IncomingRequestCommand, | |
66a7748d JB |
106 | schema: JSONSchemaType<T> |
107 | ): ValidateFunction<JsonType> { | |
108 | if (!this.jsonValidateFunctions.has(commandName)) { | |
109 | this.jsonValidateFunctions.set(commandName, this.ajv.compile<T>(schema).bind(this)) | |
0b0ca54f | 110 | } |
66a7748d JB |
111 | // eslint-disable-next-line @typescript-eslint/no-non-null-assertion |
112 | return this.jsonValidateFunctions.get(commandName)! | |
0b0ca54f JB |
113 | } |
114 | ||
9429aa42 JB |
115 | // eslint-disable-next-line @typescript-eslint/no-unused-vars |
116 | public abstract incomingRequestHandler<ReqType extends JsonType, ResType extends JsonType>( | |
08f130a0 | 117 | chargingStation: ChargingStation, |
e7aeea18 JB |
118 | messageId: string, |
119 | commandName: IncomingRequestCommand, | |
66a7748d JB |
120 | commandPayload: ReqType |
121 | ): Promise<void> | |
c0560973 | 122 | } |