Separate OCPP command support check implementation per type
[e-mobility-charging-stations-simulator.git] / src / charging-station / ocpp / OCPPResponseService.ts
index 0a1cd0c610a117ad9c7b0af8211c6ef2efe037c5..ffcb888fe422bf35bb24549205ff4dfbc3ba09f5 100644 (file)
@@ -1,12 +1,25 @@
-import ChargingStation from '../ChargingStation';
+import { JsonType } from '../../types/JsonType';
 import { RequestCommand } from '../../types/ocpp/Requests';
+import type ChargingStation from '../ChargingStation';
 
 export default abstract class OCPPResponseService {
-  protected chargingStation: ChargingStation;
+  private static instance: OCPPResponseService | null = null;
 
-  constructor(chargingStation: ChargingStation) {
-    this.chargingStation = chargingStation;
+  protected constructor() {
+    // This is intentional
   }
 
-  public abstract handleResponse(commandName: RequestCommand, payload: Record<string, unknown> | string, requestPayload: Record<string, unknown>): Promise<void>;
+  public static getInstance<T extends OCPPResponseService>(this: new () => T): T {
+    if (!OCPPResponseService.instance) {
+      OCPPResponseService.instance = new this();
+    }
+    return OCPPResponseService.instance as T;
+  }
+
+  public abstract responseHandler(
+    chargingStation: ChargingStation,
+    commandName: RequestCommand,
+    payload: JsonType,
+    requestPayload: JsonType
+  ): Promise<void>;
 }