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