Commit | Line | Data |
---|---|---|
01f4001e | 1 | import { AsyncResource } from 'node:async_hooks'; |
e6159ce8 | 2 | |
d270cc87 | 3 | import Ajv, { type JSONSchemaType } from 'ajv'; |
e3018bc4 JB |
4 | import ajvFormats from 'ajv-formats'; |
5 | ||
2896e06d JB |
6 | import { OCPPConstants, OCPPServiceUtils } from './internal'; |
7 | import { type ChargingStation, ChargingStationUtils } from '../../charging-station'; | |
268a74bb JB |
8 | import { OCPPError } from '../../exception'; |
9 | import type { | |
10 | ClearCacheResponse, | |
11 | HandleErrorParams, | |
12 | IncomingRequestCommand, | |
13 | JsonObject, | |
14 | JsonType, | |
15 | OCPPVersion, | |
16 | } from '../../types'; | |
60a74391 | 17 | import { logger } 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; |
b3fc3ff5 | 25 | protected abstract jsonSchemas: Map<IncomingRequestCommand, JSONSchemaType<JsonObject>>; |
10068088 | 26 | |
d270cc87 | 27 | protected constructor(version: OCPPVersion) { |
27f08ad3 | 28 | super(moduleName); |
d270cc87 | 29 | this.version = version; |
45988780 | 30 | this.ajv = new Ajv({ |
98fc1389 | 31 | keywords: ['javaType'], |
45988780 JB |
32 | multipleOfPrecision: 2, |
33 | }); | |
e3018bc4 | 34 | ajvFormats(this.ajv); |
9952c548 JB |
35 | this.incomingRequestHandler.bind(this); |
36 | this.validateIncomingRequestPayload.bind(this); | |
c0560973 JB |
37 | } |
38 | ||
08f130a0 | 39 | public static getInstance<T extends OCPPIncomingRequestService>(this: new () => T): T { |
1ca780f9 | 40 | if (OCPPIncomingRequestService.instance === null) { |
08f130a0 | 41 | OCPPIncomingRequestService.instance = new this(); |
9f2e3130 | 42 | } |
08f130a0 | 43 | return OCPPIncomingRequestService.instance as T; |
9f2e3130 JB |
44 | } |
45 | ||
e7aeea18 | 46 | protected handleIncomingRequestError<T>( |
08f130a0 | 47 | chargingStation: ChargingStation, |
e7aeea18 JB |
48 | commandName: IncomingRequestCommand, |
49 | error: Error, | |
50 | params: HandleErrorParams<T> = { throwError: true } | |
51581a20 | 51 | ): T | undefined { |
e7aeea18 | 52 | logger.error( |
60ddad53 | 53 | `${chargingStation.logPrefix()} ${moduleName}.handleIncomingRequestError: Incoming request command '${commandName}' error:`, |
e7aeea18 JB |
54 | error |
55 | ); | |
717c1e56 JB |
56 | if (!params?.throwError && params?.errorResponse) { |
57 | return params?.errorResponse; | |
e64c0923 | 58 | } |
717c1e56 | 59 | if (params?.throwError && !params?.errorResponse) { |
e0a50bcd JB |
60 | throw error; |
61 | } | |
717c1e56 JB |
62 | if (params?.throwError && params?.errorResponse) { |
63 | return params?.errorResponse; | |
64 | } | |
47e22477 JB |
65 | } |
66 | ||
e3018bc4 JB |
67 | protected validateIncomingRequestPayload<T extends JsonType>( |
68 | chargingStation: ChargingStation, | |
69 | commandName: IncomingRequestCommand, | |
70 | schema: JSONSchemaType<T>, | |
71 | payload: T | |
72 | ): boolean { | |
0638ddd2 | 73 | if (chargingStation.getPayloadSchemaValidation() === false) { |
e3018bc4 JB |
74 | return true; |
75 | } | |
76 | const validate = this.ajv.compile(schema); | |
77 | if (validate(payload)) { | |
78 | return true; | |
79 | } | |
80 | logger.error( | |
45988780 | 81 | `${chargingStation.logPrefix()} ${moduleName}.validateIncomingRequestPayload: Command '${commandName}' incoming request PDU is invalid: %j`, |
e3018bc4 JB |
82 | validate.errors |
83 | ); | |
84 | throw new OCPPError( | |
01a4dcbb | 85 | OCPPServiceUtils.ajvErrorsToErrorType(validate.errors), |
e3018bc4 JB |
86 | 'Incoming request PDU is invalid', |
87 | commandName, | |
88 | JSON.stringify(validate.errors, null, 2) | |
89 | ); | |
90 | } | |
91 | ||
22e0d48e | 92 | protected handleRequestClearCache(chargingStation: ChargingStation): ClearCacheResponse { |
f911a4af | 93 | chargingStation.idTagsCache.deleteIdTags( |
e302df1d | 94 | ChargingStationUtils.getIdTagsFile(chargingStation.stationInfo) |
22e0d48e JB |
95 | ); |
96 | return OCPPConstants.OCPP_RESPONSE_ACCEPTED; | |
97 | } | |
98 | ||
f7f98c68 | 99 | public abstract incomingRequestHandler( |
08f130a0 | 100 | chargingStation: ChargingStation, |
e7aeea18 JB |
101 | messageId: string, |
102 | commandName: IncomingRequestCommand, | |
5cc4b63b | 103 | commandPayload: JsonType |
e7aeea18 | 104 | ): Promise<void>; |
c0560973 | 105 | } |