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 | ||
4c3c0d59 JB |
6 | import { OCPPConstants } from './OCPPConstants'; |
7 | import { OCPPServiceUtils } from './OCPPServiceUtils'; | |
2896e06d | 8 | import { type ChargingStation, ChargingStationUtils } 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'; | |
60a74391 | 18 | import { logger } 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; |
b3fc3ff5 | 26 | protected abstract jsonSchemas: Map<IncomingRequestCommand, JSONSchemaType<JsonObject>>; |
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); |
31f59c6d JB |
36 | this.incomingRequestHandler = this.incomingRequestHandler.bind(this) as ( |
37 | chargingStation: ChargingStation, | |
38 | messageId: string, | |
39 | commandName: IncomingRequestCommand, | |
40 | commandPayload: JsonType | |
41 | ) => Promise<void>; | |
42 | this.validateIncomingRequestPayload = this.validateIncomingRequestPayload.bind(this) as < | |
43 | T extends JsonType | |
44 | >( | |
45 | chargingStation: ChargingStation, | |
46 | commandName: IncomingRequestCommand, | |
47 | schema: JSONSchemaType<T>, | |
48 | payload: T | |
49 | ) => boolean; | |
c0560973 JB |
50 | } |
51 | ||
08f130a0 | 52 | public static getInstance<T extends OCPPIncomingRequestService>(this: new () => T): T { |
1ca780f9 | 53 | if (OCPPIncomingRequestService.instance === null) { |
08f130a0 | 54 | OCPPIncomingRequestService.instance = new this(); |
9f2e3130 | 55 | } |
08f130a0 | 56 | return OCPPIncomingRequestService.instance as T; |
9f2e3130 JB |
57 | } |
58 | ||
e7aeea18 | 59 | protected handleIncomingRequestError<T>( |
08f130a0 | 60 | chargingStation: ChargingStation, |
e7aeea18 JB |
61 | commandName: IncomingRequestCommand, |
62 | error: Error, | |
63 | params: HandleErrorParams<T> = { throwError: true } | |
51581a20 | 64 | ): T | undefined { |
e7aeea18 | 65 | logger.error( |
60ddad53 | 66 | `${chargingStation.logPrefix()} ${moduleName}.handleIncomingRequestError: Incoming request command '${commandName}' error:`, |
e7aeea18 JB |
67 | error |
68 | ); | |
717c1e56 JB |
69 | if (!params?.throwError && params?.errorResponse) { |
70 | return params?.errorResponse; | |
e64c0923 | 71 | } |
717c1e56 | 72 | if (params?.throwError && !params?.errorResponse) { |
e0a50bcd JB |
73 | throw error; |
74 | } | |
717c1e56 JB |
75 | if (params?.throwError && params?.errorResponse) { |
76 | return params?.errorResponse; | |
77 | } | |
47e22477 JB |
78 | } |
79 | ||
e3018bc4 JB |
80 | protected validateIncomingRequestPayload<T extends JsonType>( |
81 | chargingStation: ChargingStation, | |
82 | commandName: IncomingRequestCommand, | |
83 | schema: JSONSchemaType<T>, | |
84 | payload: T | |
85 | ): boolean { | |
0638ddd2 | 86 | if (chargingStation.getPayloadSchemaValidation() === false) { |
e3018bc4 JB |
87 | return true; |
88 | } | |
89 | const validate = this.ajv.compile(schema); | |
90 | if (validate(payload)) { | |
91 | return true; | |
92 | } | |
93 | logger.error( | |
45988780 | 94 | `${chargingStation.logPrefix()} ${moduleName}.validateIncomingRequestPayload: Command '${commandName}' incoming request PDU is invalid: %j`, |
e3018bc4 JB |
95 | validate.errors |
96 | ); | |
97 | throw new OCPPError( | |
01a4dcbb | 98 | OCPPServiceUtils.ajvErrorsToErrorType(validate.errors), |
e3018bc4 JB |
99 | 'Incoming request PDU is invalid', |
100 | commandName, | |
101 | JSON.stringify(validate.errors, null, 2) | |
102 | ); | |
103 | } | |
104 | ||
22e0d48e | 105 | protected handleRequestClearCache(chargingStation: ChargingStation): ClearCacheResponse { |
f911a4af | 106 | chargingStation.idTagsCache.deleteIdTags( |
e302df1d | 107 | ChargingStationUtils.getIdTagsFile(chargingStation.stationInfo) |
22e0d48e JB |
108 | ); |
109 | return OCPPConstants.OCPP_RESPONSE_ACCEPTED; | |
110 | } | |
111 | ||
f7f98c68 | 112 | public abstract incomingRequestHandler( |
08f130a0 | 113 | chargingStation: ChargingStation, |
e7aeea18 JB |
114 | messageId: string, |
115 | commandName: IncomingRequestCommand, | |
5cc4b63b | 116 | commandPayload: JsonType |
e7aeea18 | 117 | ): Promise<void>; |
c0560973 | 118 | } |