- if (isMainThread && this.started === true) {
- await this.uiServer?.sendRequestOnBroadcastChannel(
- this.uiServer.buildProtocolRequest(
- Utils.generateUUID(),
- ProcedureName.STOP_CHARGING_STATION,
- Constants.EMPTY_FREEZED_OBJECT
- )
- );
- await this.waitForChargingStationsStopped();
- await this.workerImplementation?.stop();
- this.workerImplementation = null;
- this.uiServer?.stop();
- await this.storage?.close();
- this.initializedCounters = false;
- this.started = false;
+ if (!isMainThread) {
+ throw new Error('Cannot stop charging stations simulator from worker thread');
+ }
+ if (this.started === true) {
+ if (this.stopping === false) {
+ this.stopping = true;
+ await this.uiServer?.sendInternalRequest(
+ this.uiServer.buildProtocolRequest(
+ Utils.generateUUID(),
+ ProcedureName.STOP_CHARGING_STATION,
+ Constants.EMPTY_FREEZED_OBJECT
+ )
+ );
+ await ChargingStationUtils.waitForChargingStationEvents(
+ this,
+ ChargingStationWorkerMessageEvents.stopped,
+ this.numberOfChargingStations
+ );
+ await this.workerImplementation?.stop();
+ this.workerImplementation = null;
+ this.uiServer?.stop();
+ await this.storage?.close();
+ this.resetCounters();
+ this.initializedCounters = false;
+ this.started = false;
+ this.stopping = false;
+ } else {
+ console.error(chalk.red('Cannot stop an already stopping charging stations simulator'));
+ }