chore: version 1.1.94
[e-mobility-charging-stations-simulator.git] / src / charging-station / ChargingStationWorker.ts
index de0a47fe5d5745fd29b477ac072c046024922686..2204f308f699aff9179ab9c0096e11722df909ab 100644 (file)
@@ -1,20 +1,19 @@
-// Partial Copyright Jerome Benoit. 2021. All Rights Reserved.
+// Partial Copyright Jerome Benoit. 2021-2023. All Rights Reserved.
 
-import {
-  ChargingStationWorkerData,
-  ChargingStationWorkerMessage,
-  ChargingStationWorkerMessageEvents,
-} from '../types/ChargingStationWorker';
 import { parentPort, workerData } from 'worker_threads';
 
-import ChargingStation from './ChargingStation';
 import { ThreadWorker } from 'poolifier';
+
+import ChargingStation from './ChargingStation';
+import { ChargingStationUtils } from './ChargingStationUtils';
+import type { ChargingStationWorkerData } from '../types/ChargingStationWorker';
+import { type WorkerMessage, WorkerMessageEvents } from '../types/Worker';
 import Utils from '../utils/Utils';
 import WorkerConstants from '../worker/WorkerConstants';
 
 // Conditionally export ThreadWorker instance for pool usage
 export let threadWorker: ThreadWorker;
-if (Utils.workerPoolInUse()) {
+if (ChargingStationUtils.workerPoolInUse()) {
   threadWorker = new ThreadWorker<ChargingStationWorkerData>(startChargingStation, {
     maxInactiveTime: WorkerConstants.POOL_MAX_INACTIVE_TIME,
     async: false,
@@ -22,13 +21,8 @@ if (Utils.workerPoolInUse()) {
 } else {
   // Add message listener to start charging station from main thread
   addMessageListener();
-  if (!Utils.isUndefined(workerData)) {
-    startChargingStation({
-      // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
-      index: workerData.index as number,
-      // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
-      templateFile: workerData.templateFile as string,
-    });
+  if (Utils.isUndefined(workerData) === false) {
+    startChargingStation(workerData as ChargingStationWorkerData);
   }
 }
 
@@ -36,8 +30,8 @@ if (Utils.workerPoolInUse()) {
  * Listen messages send by the main thread
  */
 function addMessageListener(): void {
-  parentPort?.on('message', (message: ChargingStationWorkerMessage) => {
-    if (message.id === ChargingStationWorkerMessageEvents.START_WORKER_ELEMENT) {
+  parentPort?.on('message', (message: WorkerMessage<ChargingStationWorkerData>) => {
+    if (message.id === WorkerMessageEvents.START_WORKER_ELEMENT) {
       startChargingStation(message.data);
     }
   });
@@ -46,7 +40,7 @@ function addMessageListener(): void {
 /**
  * Create and start a charging station instance
  *
- * @param data workerData
+ * @param data workerData
  */
 function startChargingStation(data: ChargingStationWorkerData): void {
   const station = new ChargingStation(data.index, data.templateFile);