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