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