Ensure 1:1 mapping between charging station instance and its OCPP services
[e-mobility-charging-stations-simulator.git] / src / charging-station / ocpp / OCPPIncomingRequestService.ts
CommitLineData
9f2e3130 1import type ChargingStation from '../ChargingStation';
c0560973 2import { IncomingRequestCommand } from '../../types/ocpp/Requests';
d1888640 3import { JsonType } from '../../types/JsonType';
9f2e3130 4import logger from '../../utils/Logger';
c0560973
JB
5
6export default abstract class OCPPIncomingRequestService {
9f2e3130 7 private static readonly instances: Map<string, OCPPIncomingRequestService> = new Map<string, OCPPIncomingRequestService>();
c0560973
JB
8 protected chargingStation: ChargingStation;
9
9f2e3130 10 protected constructor(chargingStation: ChargingStation) {
c0560973
JB
11 this.chargingStation = chargingStation;
12 }
13
9f2e3130
JB
14 public static getInstance<T extends OCPPIncomingRequestService>(this: new (chargingStation: ChargingStation) => T, chargingStation: ChargingStation): T {
15 if (!OCPPIncomingRequestService.instances.has(chargingStation.id)) {
16 OCPPIncomingRequestService.instances.set(chargingStation.id, new this(chargingStation));
17 }
18 return OCPPIncomingRequestService.instances.get(chargingStation.id) as T;
19 }
20
d4bc21e0 21 protected handleIncomingRequestError<T>(commandName: IncomingRequestCommand, error: Error, errorOcppResponse?: T): T {
9f2e3130 22 logger.error(this.chargingStation.logPrefix() + ' Incoming request command %s error: %j', commandName, error);
6c5fc6c3
JB
23 if (errorOcppResponse) {
24 return errorOcppResponse;
e64c0923 25 }
47e22477
JB
26 throw error;
27 }
28
d1888640 29 public abstract handleRequest(messageId: string, commandName: IncomingRequestCommand, commandPayload: JsonType): Promise<void>;
c0560973 30}