X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fcharging-station%2Focpp%2FOCPPResponseService.ts;h=e838bd9fb1d4ae2280ca08d2e9516917ffac37fc;hb=dc6617020896c78ee5b3d4ef2513c98b4d61f06f;hp=0a1cd0c610a117ad9c7b0af8211c6ef2efe037c5;hpb=c0560973d259dbce64a24d10bab46246596fa1d5;p=e-mobility-charging-stations-simulator.git diff --git a/src/charging-station/ocpp/OCPPResponseService.ts b/src/charging-station/ocpp/OCPPResponseService.ts index 0a1cd0c6..e838bd9f 100644 --- a/src/charging-station/ocpp/OCPPResponseService.ts +++ b/src/charging-station/ocpp/OCPPResponseService.ts @@ -1,12 +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 = new Map< + string, + OCPPResponseService + >(); - constructor(chargingStation: ChargingStation) { + protected readonly chargingStation: ChargingStation; + + protected constructor(chargingStation: ChargingStation) { this.chargingStation = chargingStation; } - public abstract handleResponse(commandName: RequestCommand, payload: Record | string, requestPayload: Record): Promise; + public static getInstance( + this: new (chargingStation: ChargingStation) => T, + chargingStation: ChargingStation + ): T { + if (!OCPPResponseService.instances.has(chargingStation.hashId)) { + OCPPResponseService.instances.set(chargingStation.hashId, new this(chargingStation)); + } + return OCPPResponseService.instances.get(chargingStation.hashId) as T; + } + + public abstract responseHandler( + commandName: RequestCommand, + payload: JsonType, + requestPayload: JsonType + ): Promise; }