Add OCPP params file monitoring
[e-mobility-charging-stations-simulator.git] / src / charging-station / ocpp / OCPPResponseService.ts
index 17bf80650c1e04e615e7b7ddc017f31d43822dc8..311d62e393034cb449b79910363abfb332a51af9 100644 (file)
@@ -1,13 +1,32 @@
-import ChargingStation from '../ChargingStation';
+import type ChargingStation from '../ChargingStation';
 import { JsonType } from '../../types/JsonType';
 import { RequestCommand } from '../../types/ocpp/Requests';
 
 export default abstract class OCPPResponseService {
-  protected chargingStation: ChargingStation;
+  private static readonly instances: Map<string, OCPPResponseService> = new Map<
+    string,
+    OCPPResponseService
+  >();
 
-  constructor(chargingStation: ChargingStation) {
+  protected readonly chargingStation: ChargingStation;
+
+  protected constructor(chargingStation: ChargingStation) {
     this.chargingStation = chargingStation;
   }
 
-  public abstract handleResponse(commandName: RequestCommand, payload: JsonType | string, requestPayload: JsonType): Promise<void>;
+  public static getInstance<T extends OCPPResponseService>(
+    this: new (chargingStation: ChargingStation) => T,
+    chargingStation: ChargingStation
+  ): T {
+    if (!OCPPResponseService.instances.has(chargingStation.id)) {
+      OCPPResponseService.instances.set(chargingStation.id, new this(chargingStation));
+    }
+    return OCPPResponseService.instances.get(chargingStation.id) as T;
+  }
+
+  public abstract handleResponse(
+    commandName: RequestCommand,
+    payload: JsonType | string,
+    requestPayload: JsonType
+  ): Promise<void>;
 }