Ensure 1:1 mapping between charging station instance and its OCPP services
[e-mobility-charging-stations-simulator.git] / src / charging-station / ocpp / OCPPResponseService.ts
CommitLineData
9f2e3130 1import type ChargingStation from '../ChargingStation';
d1888640 2import { JsonType } from '../../types/JsonType';
c0560973
JB
3import { RequestCommand } from '../../types/ocpp/Requests';
4
5export default abstract class OCPPResponseService {
9f2e3130
JB
6 private static readonly instances: Map<string, OCPPResponseService> = new Map<string, OCPPResponseService>();
7 protected readonly chargingStation: ChargingStation;
c0560973 8
9f2e3130 9 protected constructor(chargingStation: ChargingStation) {
c0560973
JB
10 this.chargingStation = chargingStation;
11 }
12
9f2e3130
JB
13 public static getInstance<T extends OCPPResponseService>(this: new (chargingStation: ChargingStation) => T, chargingStation: ChargingStation): T {
14 if (!OCPPResponseService.instances.has(chargingStation.id)) {
15 OCPPResponseService.instances.set(chargingStation.id, new this(chargingStation));
16 }
17 return OCPPResponseService.instances.get(chargingStation.id) as T;
18 }
19
d1888640 20 public abstract handleResponse(commandName: RequestCommand, payload: JsonType | string, requestPayload: JsonType): Promise<void>;
c0560973 21}