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