From 73b9adecc7a50c329b3f83bbaf95a62ba1f01412 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Tue, 22 Feb 2022 06:52:09 +0100 Subject: [PATCH] Ensure 1:1 mapping between charging station and ATG instances MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- .../AutomaticTransactionGenerator.ts | 12 ++++++++++-- src/charging-station/ChargingStation.ts | 2 +- .../ocpp/1.6/OCPP16IncomingRequestService.ts | 2 +- .../ocpp/1.6/OCPP16RequestService.ts | 6 +++--- .../ocpp/1.6/OCPP16ResponseService.ts | 2 +- src/charging-station/ocpp/1.6/OCPP16ServiceUtils.ts | 2 +- src/worker/WorkerFactory.ts | 2 +- 7 files changed, 18 insertions(+), 10 deletions(-) diff --git a/src/charging-station/AutomaticTransactionGenerator.ts b/src/charging-station/AutomaticTransactionGenerator.ts index dddfc045..0e0982a2 100644 --- a/src/charging-station/AutomaticTransactionGenerator.ts +++ b/src/charging-station/AutomaticTransactionGenerator.ts @@ -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 = new Map(); public started: boolean; private readonly chargingStation: ChargingStation; private readonly connectorsStatus: Map; - constructor(chargingStation: ChargingStation) { + private constructor(chargingStation: ChargingStation) { this.chargingStation = chargingStation; this.connectorsStatus = new Map(); 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`); diff --git a/src/charging-station/ChargingStation.ts b/src/charging-station/ChargingStation.ts index 028de591..3d29e472 100644 --- a/src/charging-station/ChargingStation.ts +++ b/src/charging-station/ChargingStation.ts @@ -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(); diff --git a/src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts b/src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts index 9377c605..6cf07805 100644 --- a/src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts +++ b/src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts @@ -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'; diff --git a/src/charging-station/ocpp/1.6/OCPP16RequestService.ts b/src/charging-station/ocpp/1.6/OCPP16RequestService.ts index 9d0a90a6..3b671d6b 100644 --- a/src/charging-station/ocpp/1.6/OCPP16RequestService.ts +++ b/src/charging-station/ocpp/1.6/OCPP16RequestService.ts @@ -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'; diff --git a/src/charging-station/ocpp/1.6/OCPP16ResponseService.ts b/src/charging-station/ocpp/1.6/OCPP16ResponseService.ts index 20255d7d..54815bd6 100644 --- a/src/charging-station/ocpp/1.6/OCPP16ResponseService.ts +++ b/src/charging-station/ocpp/1.6/OCPP16ResponseService.ts @@ -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'; diff --git a/src/charging-station/ocpp/1.6/OCPP16ServiceUtils.ts b/src/charging-station/ocpp/1.6/OCPP16ServiceUtils.ts index f55c7429..cacbe389 100644 --- a/src/charging-station/ocpp/1.6/OCPP16ServiceUtils.ts +++ b/src/charging-station/ocpp/1.6/OCPP16ServiceUtils.ts @@ -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'; diff --git a/src/worker/WorkerFactory.ts b/src/worker/WorkerFactory.ts index 76da7a20..929bf619 100644 --- a/src/worker/WorkerFactory.ts +++ b/src/worker/WorkerFactory.ts @@ -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'; -- 2.34.1