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>>() | |
9429aa42 JB |
38 | this.incomingRequestHandler = this.incomingRequestHandler.bind(this) as < |
39 | ReqType extends JsonType, | |
40 | // eslint-disable-next-line @typescript-eslint/no-unused-vars | |
a807045b | 41 | ResType extends JsonType, |
9429aa42 | 42 | >( |
31f59c6d JB |
43 | chargingStation: ChargingStation, |
44 | messageId: string, | |
45 | commandName: IncomingRequestCommand, | |
66a7748d JB |
46 | commandPayload: ReqType |
47 | ) => Promise<void> | |
31f59c6d | 48 | this.validateIncomingRequestPayload = this.validateIncomingRequestPayload.bind(this) as < |
a807045b | 49 | T extends JsonType, |
31f59c6d JB |
50 | >( |
51 | chargingStation: ChargingStation, | |
52 | commandName: IncomingRequestCommand, | |
53 | schema: JSONSchemaType<T>, | |
66a7748d JB |
54 | payload: T |
55 | ) => boolean | |
c0560973 JB |
56 | } |
57 | ||
08f130a0 | 58 | public static getInstance<T extends OCPPIncomingRequestService>(this: new () => T): T { |
1ca780f9 | 59 | if (OCPPIncomingRequestService.instance === null) { |
66a7748d | 60 | OCPPIncomingRequestService.instance = new this() |
9f2e3130 | 61 | } |
66a7748d | 62 | return OCPPIncomingRequestService.instance as T |
9f2e3130 JB |
63 | } |
64 | ||
7b5dbe91 | 65 | protected handleIncomingRequestError<T extends JsonType>( |
08f130a0 | 66 | chargingStation: ChargingStation, |
e7aeea18 JB |
67 | commandName: IncomingRequestCommand, |
68 | error: Error, | |
66a7748d | 69 | params: HandleErrorParams<T> = { throwError: true, consoleOut: false } |
51581a20 | 70 | ): T | undefined { |
66a7748d | 71 | setDefaultErrorParams(params) |
e7aeea18 | 72 | logger.error( |
60ddad53 | 73 | `${chargingStation.logPrefix()} ${moduleName}.handleIncomingRequestError: Incoming request command '${commandName}' error:`, |
66a7748d JB |
74 | error |
75 | ) | |
5199f9fd JB |
76 | if (params.throwError === false && params.errorResponse != null) { |
77 | return params.errorResponse | |
e64c0923 | 78 | } |
5199f9fd | 79 | if (params.throwError === true && params.errorResponse == null) { |
66a7748d | 80 | throw error |
e0a50bcd | 81 | } |
5199f9fd JB |
82 | if (params.throwError === true && params.errorResponse != null) { |
83 | return params.errorResponse | |
717c1e56 | 84 | } |
47e22477 JB |
85 | } |
86 | ||
e3018bc4 JB |
87 | protected validateIncomingRequestPayload<T extends JsonType>( |
88 | chargingStation: ChargingStation, | |
89 | commandName: IncomingRequestCommand, | |
90 | schema: JSONSchemaType<T>, | |
66a7748d | 91 | payload: T |
e3018bc4 | 92 | ): boolean { |
5398cecf | 93 | if (chargingStation.stationInfo?.ocppStrictCompliance === false) { |
66a7748d | 94 | return true |
e3018bc4 | 95 | } |
66a7748d | 96 | const validate = this.getJsonIncomingRequestValidateFunction<T>(commandName, schema) |
e3018bc4 | 97 | if (validate(payload)) { |
66a7748d | 98 | return true |
e3018bc4 JB |
99 | } |
100 | logger.error( | |
45988780 | 101 | `${chargingStation.logPrefix()} ${moduleName}.validateIncomingRequestPayload: Command '${commandName}' incoming request PDU is invalid: %j`, |
66a7748d JB |
102 | validate.errors |
103 | ) | |
e3018bc4 | 104 | throw new OCPPError( |
9ff486f4 | 105 | OCPPServiceUtils.ajvErrorsToErrorType(validate.errors), |
e3018bc4 JB |
106 | 'Incoming request PDU is invalid', |
107 | commandName, | |
66a7748d JB |
108 | JSON.stringify(validate.errors, undefined, 2) |
109 | ) | |
e3018bc4 JB |
110 | } |
111 | ||
66a7748d JB |
112 | protected handleRequestClearCache (chargingStation: ChargingStation): ClearCacheResponse { |
113 | // eslint-disable-next-line @typescript-eslint/no-non-null-assertion | |
5199f9fd | 114 | if (chargingStation.idTagsCache.deleteIdTags(getIdTagsFile(chargingStation.stationInfo!)!)) { |
66a7748d | 115 | return OCPPConstants.OCPP_RESPONSE_ACCEPTED |
26a17d93 | 116 | } |
66a7748d | 117 | return OCPPConstants.OCPP_RESPONSE_REJECTED |
22e0d48e JB |
118 | } |
119 | ||
0b0ca54f JB |
120 | private getJsonIncomingRequestValidateFunction<T extends JsonType>( |
121 | commandName: IncomingRequestCommand, | |
66a7748d JB |
122 | schema: JSONSchemaType<T> |
123 | ): ValidateFunction<JsonType> { | |
124 | if (!this.jsonValidateFunctions.has(commandName)) { | |
125 | this.jsonValidateFunctions.set(commandName, this.ajv.compile<T>(schema).bind(this)) | |
0b0ca54f | 126 | } |
66a7748d JB |
127 | // eslint-disable-next-line @typescript-eslint/no-non-null-assertion |
128 | return this.jsonValidateFunctions.get(commandName)! | |
0b0ca54f JB |
129 | } |
130 | ||
9429aa42 JB |
131 | // eslint-disable-next-line @typescript-eslint/no-unused-vars |
132 | public abstract incomingRequestHandler<ReqType extends JsonType, ResType extends JsonType>( | |
08f130a0 | 133 | chargingStation: ChargingStation, |
e7aeea18 JB |
134 | messageId: string, |
135 | commandName: IncomingRequestCommand, | |
66a7748d JB |
136 | commandPayload: ReqType |
137 | ): Promise<void> | |
c0560973 | 138 | } |