From: Jérôme Benoit Date: Sun, 4 Jun 2023 18:42:30 +0000 (+0200) Subject: fix: avoid concurrent ATG startup X-Git-Tag: v1.2.16~14 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=1135386576395b90768d5e03a16fb44d340361e0;p=e-mobility-charging-stations-simulator.git fix: avoid concurrent ATG startup Signed-off-by: Jérôme Benoit --- diff --git a/src/charging-station/AutomaticTransactionGenerator.ts b/src/charging-station/AutomaticTransactionGenerator.ts index 477a8d9c..0389526f 100644 --- a/src/charging-station/AutomaticTransactionGenerator.ts +++ b/src/charging-station/AutomaticTransactionGenerator.ts @@ -31,11 +31,15 @@ export class AutomaticTransactionGenerator extends AsyncResource { public readonly connectorsStatus: Map; public started: boolean; + private starting: boolean; + private stopping: boolean; private readonly chargingStation: ChargingStation; private constructor(chargingStation: ChargingStation) { super(moduleName); this.started = false; + this.starting = false; + this.stopping = false; this.chargingStation = chargingStation; this.connectorsStatus = new Map(); this.initializeConnectorsStatus(); @@ -63,8 +67,14 @@ export class AutomaticTransactionGenerator extends AsyncResource { logger.warn(`${this.logPrefix()} is already started`); return; } + if (this.starting === true) { + logger.warn(`${this.logPrefix()} is already starting`); + return; + } + this.starting = true; this.startConnectors(); this.started = true; + this.starting = false; } public stop(): void { @@ -72,8 +82,14 @@ export class AutomaticTransactionGenerator extends AsyncResource { logger.warn(`${this.logPrefix()} is already stopped`); return; } + if (this.stopping === true) { + logger.warn(`${this.logPrefix()} is already stopping`); + return; + } + this.stopping = true; this.stopConnectors(); this.started = false; + this.stopping = false; } public startConnector(connectorId: number): void { diff --git a/src/charging-station/ChargingStationWorker.ts b/src/charging-station/ChargingStationWorker.ts index de0b7a9f..741449e3 100644 --- a/src/charging-station/ChargingStationWorker.ts +++ b/src/charging-station/ChargingStationWorker.ts @@ -10,6 +10,8 @@ import type { ChargingStationWorkerData } from '../types'; import { Configuration } from '../utils'; import { WorkerConstants, type WorkerMessage, WorkerMessageEvents } from '../worker'; +const moduleName = 'ChargingStationWorker'; + /** * Create and start a charging station instance * @@ -21,7 +23,7 @@ const startChargingStation = (data: ChargingStationWorkerData): void => { class ChargingStationWorker extends AsyncResource { constructor() { - super('ChargingStationWorker'); + super(moduleName); // Add message listener to create and start charging station from the main thread parentPort?.on('message', (message: WorkerMessage) => { if (message.id === WorkerMessageEvents.startWorkerElement) {