Commit | Line | Data |
---|---|---|
e6159ce8 JB |
1 | import { AsyncResource } from 'async_hooks'; |
2 | ||
6c1761d4 | 3 | import type { JSONSchemaType } from 'ajv'; |
e3018bc4 JB |
4 | import Ajv from 'ajv-draft-04'; |
5 | import ajvFormats from 'ajv-formats'; | |
6 | ||
7 | import OCPPError from '../../exception/OCPPError'; | |
6c1761d4 JB |
8 | import type { HandleErrorParams } from '../../types/Error'; |
9 | import type { JsonType } from '../../types/JsonType'; | |
10 | import type { IncomingRequestCommand } from '../../types/ocpp/Requests'; | |
9f2e3130 | 11 | import logger from '../../utils/Logger'; |
8114d10e | 12 | import type ChargingStation from '../ChargingStation'; |
cbb3711f | 13 | import { OCPPServiceUtils } from './OCPPServiceUtils'; |
c0560973 | 14 | |
e3018bc4 JB |
15 | const moduleName = 'OCPPIncomingRequestService'; |
16 | ||
c0560973 | 17 | export default abstract class OCPPIncomingRequestService { |
08f130a0 | 18 | private static instance: OCPPIncomingRequestService | null = null; |
e6159ce8 | 19 | protected asyncResource: AsyncResource; |
e3018bc4 | 20 | private ajv: Ajv; |
10068088 | 21 | |
08f130a0 | 22 | protected constructor() { |
e6159ce8 | 23 | this.asyncResource = new AsyncResource(moduleName); |
e3018bc4 JB |
24 | this.ajv = new Ajv(); |
25 | ajvFormats(this.ajv); | |
9952c548 JB |
26 | this.incomingRequestHandler.bind(this); |
27 | this.validateIncomingRequestPayload.bind(this); | |
c0560973 JB |
28 | } |
29 | ||
08f130a0 | 30 | public static getInstance<T extends OCPPIncomingRequestService>(this: new () => T): T { |
1ca780f9 | 31 | if (OCPPIncomingRequestService.instance === null) { |
08f130a0 | 32 | OCPPIncomingRequestService.instance = new this(); |
9f2e3130 | 33 | } |
08f130a0 | 34 | return OCPPIncomingRequestService.instance as T; |
9f2e3130 JB |
35 | } |
36 | ||
e7aeea18 | 37 | protected handleIncomingRequestError<T>( |
08f130a0 | 38 | chargingStation: ChargingStation, |
e7aeea18 JB |
39 | commandName: IncomingRequestCommand, |
40 | error: Error, | |
41 | params: HandleErrorParams<T> = { throwError: true } | |
42 | ): T { | |
43 | logger.error( | |
60ddad53 | 44 | `${chargingStation.logPrefix()} ${moduleName}.handleIncomingRequestError: Incoming request command '${commandName}' error:`, |
e7aeea18 JB |
45 | error |
46 | ); | |
717c1e56 JB |
47 | if (!params?.throwError && params?.errorResponse) { |
48 | return params?.errorResponse; | |
e64c0923 | 49 | } |
717c1e56 | 50 | if (params?.throwError && !params?.errorResponse) { |
e0a50bcd JB |
51 | throw error; |
52 | } | |
717c1e56 JB |
53 | if (params?.throwError && params?.errorResponse) { |
54 | return params?.errorResponse; | |
55 | } | |
47e22477 JB |
56 | } |
57 | ||
e3018bc4 JB |
58 | protected validateIncomingRequestPayload<T extends JsonType>( |
59 | chargingStation: ChargingStation, | |
60 | commandName: IncomingRequestCommand, | |
61 | schema: JSONSchemaType<T>, | |
62 | payload: T | |
63 | ): boolean { | |
0638ddd2 | 64 | if (chargingStation.getPayloadSchemaValidation() === false) { |
e3018bc4 JB |
65 | return true; |
66 | } | |
67 | const validate = this.ajv.compile(schema); | |
68 | if (validate(payload)) { | |
69 | return true; | |
70 | } | |
71 | logger.error( | |
72 | `${chargingStation.logPrefix()} ${moduleName}.validateIncomingRequestPayload: Incoming request PDU is invalid: %j`, | |
73 | validate.errors | |
74 | ); | |
75 | throw new OCPPError( | |
01a4dcbb | 76 | OCPPServiceUtils.ajvErrorsToErrorType(validate.errors), |
e3018bc4 JB |
77 | 'Incoming request PDU is invalid', |
78 | commandName, | |
79 | JSON.stringify(validate.errors, null, 2) | |
80 | ); | |
81 | } | |
82 | ||
f7f98c68 | 83 | public abstract incomingRequestHandler( |
08f130a0 | 84 | chargingStation: ChargingStation, |
e7aeea18 JB |
85 | messageId: string, |
86 | commandName: IncomingRequestCommand, | |
5cc4b63b | 87 | commandPayload: JsonType |
e7aeea18 | 88 | ): Promise<void>; |
c0560973 | 89 | } |