Commit | Line | Data |
---|---|---|
e0a50bcd | 1 | import { HandleErrorParams } from '../../types/Error'; |
5cc4b63b | 2 | import { JsonType } from '../../types/JsonType'; |
8114d10e | 3 | import { IncomingRequestCommand } from '../../types/ocpp/Requests'; |
9f2e3130 | 4 | import logger from '../../utils/Logger'; |
8114d10e | 5 | import type ChargingStation from '../ChargingStation'; |
c0560973 JB |
6 | |
7 | export default abstract class OCPPIncomingRequestService { | |
08f130a0 | 8 | private static instance: OCPPIncomingRequestService | null = null; |
10068088 | 9 | |
08f130a0 JB |
10 | protected constructor() { |
11 | // This is intentional | |
c0560973 JB |
12 | } |
13 | ||
08f130a0 JB |
14 | public static getInstance<T extends OCPPIncomingRequestService>(this: new () => T): T { |
15 | if (!OCPPIncomingRequestService.instance) { | |
16 | OCPPIncomingRequestService.instance = new this(); | |
9f2e3130 | 17 | } |
08f130a0 | 18 | return OCPPIncomingRequestService.instance as T; |
9f2e3130 JB |
19 | } |
20 | ||
e7aeea18 | 21 | protected handleIncomingRequestError<T>( |
08f130a0 | 22 | chargingStation: ChargingStation, |
e7aeea18 JB |
23 | commandName: IncomingRequestCommand, |
24 | error: Error, | |
25 | params: HandleErrorParams<T> = { throwError: true } | |
26 | ): T { | |
27 | logger.error( | |
08f130a0 | 28 | chargingStation.logPrefix() + ' Incoming request command %s error: %j', |
e7aeea18 JB |
29 | commandName, |
30 | error | |
31 | ); | |
717c1e56 JB |
32 | if (!params?.throwError && params?.errorResponse) { |
33 | return params?.errorResponse; | |
e64c0923 | 34 | } |
717c1e56 | 35 | if (params?.throwError && !params?.errorResponse) { |
e0a50bcd JB |
36 | throw error; |
37 | } | |
717c1e56 JB |
38 | if (params?.throwError && params?.errorResponse) { |
39 | return params?.errorResponse; | |
40 | } | |
47e22477 JB |
41 | } |
42 | ||
f7f98c68 | 43 | public abstract incomingRequestHandler( |
08f130a0 | 44 | chargingStation: ChargingStation, |
e7aeea18 JB |
45 | messageId: string, |
46 | commandName: IncomingRequestCommand, | |
5cc4b63b | 47 | commandPayload: JsonType |
e7aeea18 | 48 | ): Promise<void>; |
c0560973 | 49 | } |