X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fcharging-station%2Focpp%2FOCPPResponseService.ts;h=311d62e393034cb449b79910363abfb332a51af9;hb=a95873d8d308a20a7151346ac70d9a551f1a06f5;hp=17bf80650c1e04e615e7b7ddc017f31d43822dc8;hpb=d18886407cdb8b8148c87492f2c953075e708401;p=e-mobility-charging-stations-simulator.git diff --git a/src/charging-station/ocpp/OCPPResponseService.ts b/src/charging-station/ocpp/OCPPResponseService.ts index 17bf8065..311d62e3 100644 --- a/src/charging-station/ocpp/OCPPResponseService.ts +++ b/src/charging-station/ocpp/OCPPResponseService.ts @@ -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 = 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; + public static getInstance( + 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; }