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