Ensure 1:1 mapping between charging station and ATG instances
authorJérôme Benoit <jerome.benoit@sap.com>
Tue, 22 Feb 2022 05:52:09 +0000 (06:52 +0100)
committerJérôme Benoit <jerome.benoit@sap.com>
Tue, 22 Feb 2022 05:52:09 +0000 (06:52 +0100)
Signed-off-by: Jérôme Benoit <jerome.benoit@sap.com>
src/charging-station/AutomaticTransactionGenerator.ts
src/charging-station/ChargingStation.ts
src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts
src/charging-station/ocpp/1.6/OCPP16RequestService.ts
src/charging-station/ocpp/1.6/OCPP16ResponseService.ts
src/charging-station/ocpp/1.6/OCPP16ServiceUtils.ts
src/worker/WorkerFactory.ts

index dddfc045c9a80aba7e56ab3e985727188bbd6f87..0e0982a201dd7165f39212dc3acaba7b8e893002 100644 (file)
@@ -2,7 +2,7 @@
 
 import { AuthorizationStatus, AuthorizeResponse, StartTransactionResponse, StopTransactionReason, StopTransactionResponse } from '../types/ocpp/Transaction';
 
-import ChargingStation from './ChargingStation';
+import type ChargingStation from './ChargingStation';
 import Constants from '../utils/Constants';
 import PerformanceStatistics from '../performance/PerformanceStatistics';
 import { Status } from '../types/AutomaticTransactionGenerator';
@@ -10,17 +10,25 @@ import Utils from '../utils/Utils';
 import logger from '../utils/Logger';
 
 export default class AutomaticTransactionGenerator {
+  private static readonly instances: Map<string, AutomaticTransactionGenerator> = new Map<string, AutomaticTransactionGenerator>();
   public started: boolean;
   private readonly chargingStation: ChargingStation;
   private readonly connectorsStatus: Map<number, Status>;
 
-  constructor(chargingStation: ChargingStation) {
+  private constructor(chargingStation: ChargingStation) {
     this.chargingStation = chargingStation;
     this.connectorsStatus = new Map<number, Status>();
     this.stopConnectors();
     this.started = false;
   }
 
+  public static getInstance(chargingStation: ChargingStation): AutomaticTransactionGenerator {
+    if (!AutomaticTransactionGenerator.instances.has(chargingStation.id)) {
+      AutomaticTransactionGenerator.instances.set(chargingStation.id, new AutomaticTransactionGenerator(chargingStation));
+    }
+    return AutomaticTransactionGenerator.instances.get(chargingStation.id);
+  }
+
   public start(): void {
     if (this.started) {
       logger.error(`${this.logPrefix()} trying to start while already started`);
index 028de5918c9f38cc7d9dd4281b589611d19b19a4..3d29e47219c0924ad44345e926213b6fd53101de 100644 (file)
@@ -909,7 +909,7 @@ export default class ChargingStation {
   private startAutomaticTransactionGenerator() {
     if (this.stationInfo.AutomaticTransactionGenerator.enable) {
       if (!this.automaticTransactionGenerator) {
-        this.automaticTransactionGenerator = new AutomaticTransactionGenerator(this);
+        this.automaticTransactionGenerator = AutomaticTransactionGenerator.getInstance(this);
       }
       if (!this.automaticTransactionGenerator.started) {
         this.automaticTransactionGenerator.start();
index 9377c605cb94a966c201361311f5eae4e0394fc5..6cf078059f0d8ec6e3dab8012cedb25ec343c2fc 100644 (file)
@@ -6,7 +6,7 @@ import { ChargingProfilePurposeType, OCPP16ChargingProfile } from '../../../type
 import { Client, FTPResponse } from 'basic-ftp';
 import { OCPP16AuthorizationStatus, OCPP16StopTransactionReason } from '../../../types/ocpp/1.6/Transaction';
 
-import ChargingStation from '../../ChargingStation';
+import type ChargingStation from '../../ChargingStation';
 import Constants from '../../../utils/Constants';
 import { DefaultResponse } from '../../../types/ocpp/Responses';
 import { ErrorType } from '../../../types/ocpp/ErrorType';
index 9d0a90a60cd59816dddab5edd2f4e7807b51d43d..3b671d6b809a0464b74da9e271a9cba57e193785 100644 (file)
@@ -3,10 +3,10 @@
 import { ACElectricUtils, DCElectricUtils } from '../../../utils/ElectricUtils';
 import { AuthorizeRequest, OCPP16AuthorizeResponse, OCPP16StartTransactionResponse, OCPP16StopTransactionReason, OCPP16StopTransactionResponse, StartTransactionRequest, StopTransactionRequest } from '../../../types/ocpp/1.6/Transaction';
 import { CurrentType, Voltage } from '../../../types/ChargingStationTemplate';
-import { DiagnosticsStatusNotificationRequest, HeartbeatRequest, OCPP16BootNotificationRequest, OCPP16IncomingRequestCommand, OCPP16RequestCommand, StatusNotificationRequest } from '../../../types/ocpp/1.6/Requests';
+import { DiagnosticsStatusNotificationRequest, HeartbeatRequest, OCPP16BootNotificationRequest, OCPP16RequestCommand, StatusNotificationRequest } from '../../../types/ocpp/1.6/Requests';
 import { MeterValueUnit, MeterValuesRequest, OCPP16MeterValue, OCPP16MeterValueMeasurand, OCPP16MeterValuePhase } from '../../../types/ocpp/1.6/MeterValues';
 
-import ChargingStation from '../../ChargingStation';
+import type ChargingStation from '../../ChargingStation';
 import Constants from '../../../utils/Constants';
 import { ErrorType } from '../../../types/ocpp/ErrorType';
 import MeasurandPerPhaseSampledValueTemplates from '../../../types/MeasurandPerPhaseSampledValueTemplates';
@@ -18,7 +18,7 @@ import { OCPP16DiagnosticsStatus } from '../../../types/ocpp/1.6/DiagnosticsStat
 import { OCPP16ServiceUtils } from './OCPP16ServiceUtils';
 import OCPPError from '../../../exception/OCPPError';
 import OCPPRequestService from '../OCPPRequestService';
-import OCPPResponseService from '../OCPPResponseService';
+import type OCPPResponseService from '../OCPPResponseService';
 import { SendParams } from '../../../types/ocpp/Requests';
 import Utils from '../../../utils/Utils';
 import logger from '../../../utils/Logger';
index 20255d7d49fdee0e2c95c5b2caed4d043b4e1f61..54815bd6b4eee8c46c1351cd4c0c658834a63155 100644 (file)
@@ -5,7 +5,7 @@ import { HeartbeatRequest, OCPP16RequestCommand, StatusNotificationRequest } fro
 import { HeartbeatResponse, OCPP16BootNotificationResponse, OCPP16RegistrationStatus, StatusNotificationResponse } from '../../../types/ocpp/1.6/Responses';
 import { MeterValuesRequest, MeterValuesResponse } from '../../../types/ocpp/1.6/MeterValues';
 
-import ChargingStation from '../../ChargingStation';
+import type ChargingStation from '../../ChargingStation';
 import { ErrorType } from '../../../types/ocpp/ErrorType';
 import { JsonType } from '../../../types/JsonType';
 import { OCPP16ChargePointStatus } from '../../../types/ocpp/1.6/ChargePointStatus';
index f55c74290efb3e773348c2456b36551314001f12..cacbe38914654e903e7e38f494499ae9e0da8fd0 100644 (file)
@@ -2,7 +2,7 @@
 
 import { MeterValueContext, MeterValueLocation, MeterValueUnit, OCPP16MeterValue, OCPP16MeterValueMeasurand, OCPP16MeterValuePhase, OCPP16SampledValue } from '../../../types/ocpp/1.6/MeterValues';
 
-import ChargingStation from '../../ChargingStation';
+import type ChargingStation from '../../ChargingStation';
 import { ErrorType } from '../../../types/ocpp/ErrorType';
 import OCPPError from '../../../exception/OCPPError';
 import { RequestCommand } from '../../../types/ocpp/Requests';
index 76da7a20fc453d561218814be001e53a43a7c546..929bf6199b94c91fdbc28014daf74fe80d9ddd52 100644 (file)
@@ -3,7 +3,7 @@ import { WorkerData, WorkerOptions, WorkerProcessType } from '../types/Worker';
 
 import Constants from '../utils/Constants';
 import { PoolOptions } from 'poolifier';
-import WorkerAbstract from './WorkerAbstract';
+import type WorkerAbstract from './WorkerAbstract';
 import WorkerDynamicPool from './WorkerDynamicPool';
 import WorkerSet from './WorkerSet';
 import WorkerStaticPool from './WorkerStaticPool';