Commit | Line | Data |
---|---|---|
01f4001e | 1 | import { AsyncResource } from 'node:async_hooks'; |
e6159ce8 | 2 | |
ec0eebcc | 3 | import Ajv, { type JSONSchemaType, type ValidateFunction } from 'ajv'; |
e3018bc4 JB |
4 | import ajvFormats from 'ajv-formats'; |
5 | ||
4c3c0d59 JB |
6 | import { OCPPConstants } from './OCPPConstants'; |
7 | import { OCPPServiceUtils } from './OCPPServiceUtils'; | |
fba11dc6 | 8 | import { type ChargingStation, getIdTagsFile } from '../../charging-station'; |
268a74bb JB |
9 | import { OCPPError } from '../../exception'; |
10 | import type { | |
11 | ClearCacheResponse, | |
12 | HandleErrorParams, | |
13 | IncomingRequestCommand, | |
268a74bb JB |
14 | JsonType, |
15 | OCPPVersion, | |
16 | } from '../../types'; | |
fa5995d6 | 17 | import { logger, setDefaultErrorParams } from '../../utils'; |
c0560973 | 18 | |
e3018bc4 JB |
19 | const moduleName = 'OCPPIncomingRequestService'; |
20 | ||
268a74bb | 21 | export abstract class OCPPIncomingRequestService extends AsyncResource { |
08f130a0 | 22 | private static instance: OCPPIncomingRequestService | null = null; |
d270cc87 | 23 | private readonly version: OCPPVersion; |
012ae1a9 | 24 | private readonly ajv: Ajv; |
291b5ec8 JB |
25 | private jsonValidateFunctions: Map<IncomingRequestCommand, ValidateFunction<JsonType>>; |
26 | protected abstract jsonSchemas: Map<IncomingRequestCommand, JSONSchemaType<JsonType>>; | |
10068088 | 27 | |
d270cc87 | 28 | protected constructor(version: OCPPVersion) { |
27f08ad3 | 29 | super(moduleName); |
d270cc87 | 30 | this.version = version; |
45988780 | 31 | this.ajv = new Ajv({ |
98fc1389 | 32 | keywords: ['javaType'], |
45988780 JB |
33 | multipleOfPrecision: 2, |
34 | }); | |
e3018bc4 | 35 | ajvFormats(this.ajv); |
291b5ec8 | 36 | this.jsonValidateFunctions = new Map<IncomingRequestCommand, ValidateFunction<JsonType>>(); |
9429aa42 JB |
37 | this.incomingRequestHandler = this.incomingRequestHandler.bind(this) as < |
38 | ReqType extends JsonType, | |
39 | // eslint-disable-next-line @typescript-eslint/no-unused-vars | |
40 | ResType extends JsonType, | |
41 | >( | |
31f59c6d JB |
42 | chargingStation: ChargingStation, |
43 | messageId: string, | |
44 | commandName: IncomingRequestCommand, | |
9429aa42 | 45 | commandPayload: ReqType, |
31f59c6d JB |
46 | ) => Promise<void>; |
47 | this.validateIncomingRequestPayload = this.validateIncomingRequestPayload.bind(this) as < | |
5edd8ba0 | 48 | T extends JsonType, |
31f59c6d JB |
49 | >( |
50 | chargingStation: ChargingStation, | |
51 | commandName: IncomingRequestCommand, | |
52 | schema: JSONSchemaType<T>, | |
5edd8ba0 | 53 | payload: T, |
31f59c6d | 54 | ) => boolean; |
c0560973 JB |
55 | } |
56 | ||
08f130a0 | 57 | public static getInstance<T extends OCPPIncomingRequestService>(this: new () => T): T { |
1ca780f9 | 58 | if (OCPPIncomingRequestService.instance === null) { |
08f130a0 | 59 | OCPPIncomingRequestService.instance = new this(); |
9f2e3130 | 60 | } |
08f130a0 | 61 | return OCPPIncomingRequestService.instance as T; |
9f2e3130 JB |
62 | } |
63 | ||
7b5dbe91 | 64 | protected handleIncomingRequestError<T extends JsonType>( |
08f130a0 | 65 | chargingStation: ChargingStation, |
e7aeea18 JB |
66 | commandName: IncomingRequestCommand, |
67 | error: Error, | |
5edd8ba0 | 68 | params: HandleErrorParams<T> = { throwError: true, consoleOut: false }, |
51581a20 | 69 | ): T | undefined { |
fa5995d6 | 70 | setDefaultErrorParams(params); |
e7aeea18 | 71 | logger.error( |
60ddad53 | 72 | `${chargingStation.logPrefix()} ${moduleName}.handleIncomingRequestError: Incoming request command '${commandName}' error:`, |
5edd8ba0 | 73 | error, |
e7aeea18 | 74 | ); |
717c1e56 JB |
75 | if (!params?.throwError && params?.errorResponse) { |
76 | return params?.errorResponse; | |
e64c0923 | 77 | } |
717c1e56 | 78 | if (params?.throwError && !params?.errorResponse) { |
e0a50bcd JB |
79 | throw error; |
80 | } | |
717c1e56 JB |
81 | if (params?.throwError && params?.errorResponse) { |
82 | return params?.errorResponse; | |
83 | } | |
47e22477 JB |
84 | } |
85 | ||
e3018bc4 JB |
86 | protected validateIncomingRequestPayload<T extends JsonType>( |
87 | chargingStation: ChargingStation, | |
88 | commandName: IncomingRequestCommand, | |
89 | schema: JSONSchemaType<T>, | |
5edd8ba0 | 90 | payload: T, |
e3018bc4 | 91 | ): boolean { |
0282b7c0 | 92 | if (chargingStation.getOcppStrictCompliance() === false) { |
e3018bc4 JB |
93 | return true; |
94 | } | |
ec0eebcc | 95 | if (this.jsonValidateFunctions.has(commandName) === false) { |
291b5ec8 | 96 | this.jsonValidateFunctions.set(commandName, this.ajv.compile<T>(schema).bind(this)); |
ec0eebcc JB |
97 | } |
98 | const validate = this.jsonValidateFunctions.get(commandName)!; | |
e3018bc4 JB |
99 | if (validate(payload)) { |
100 | return true; | |
101 | } | |
102 | logger.error( | |
45988780 | 103 | `${chargingStation.logPrefix()} ${moduleName}.validateIncomingRequestPayload: Command '${commandName}' incoming request PDU is invalid: %j`, |
5edd8ba0 | 104 | validate.errors, |
e3018bc4 JB |
105 | ); |
106 | throw new OCPPError( | |
9ff486f4 | 107 | OCPPServiceUtils.ajvErrorsToErrorType(validate.errors), |
e3018bc4 JB |
108 | 'Incoming request PDU is invalid', |
109 | commandName, | |
4ed03b6e | 110 | JSON.stringify(validate.errors, undefined, 2), |
e3018bc4 JB |
111 | ); |
112 | } | |
113 | ||
22e0d48e | 114 | protected handleRequestClearCache(chargingStation: ChargingStation): ClearCacheResponse { |
e1d9a0f4 | 115 | if (chargingStation.idTagsCache.deleteIdTags(getIdTagsFile(chargingStation.stationInfo)!)) { |
26a17d93 JB |
116 | return OCPPConstants.OCPP_RESPONSE_ACCEPTED; |
117 | } | |
118 | return OCPPConstants.OCPP_RESPONSE_REJECTED; | |
22e0d48e JB |
119 | } |
120 | ||
9429aa42 JB |
121 | // eslint-disable-next-line @typescript-eslint/no-unused-vars |
122 | public abstract incomingRequestHandler<ReqType extends JsonType, ResType extends JsonType>( | |
08f130a0 | 123 | chargingStation: ChargingStation, |
e7aeea18 JB |
124 | messageId: string, |
125 | commandName: IncomingRequestCommand, | |
9429aa42 | 126 | commandPayload: ReqType, |
e7aeea18 | 127 | ): Promise<void>; |
c0560973 | 128 | } |