b22d8932731356198cd64818b9b3b9cc77699e19
[e-mobility-charging-stations-simulator.git] / src / charging-station / StationWorker.ts
1 import { isMainThread, parentPort, workerData } from 'worker_threads';
2
3 import ChargingStation from './ChargingStation';
4 import Utils from '../utils/Utils';
5 import { WorkerEvents } from '../types/Worker';
6
7 if (!isMainThread) {
8 // Add listener to start charging station from main thread
9 addListener();
10 if (!Utils.isUndefined(workerData)) {
11 startChargingStation({ index: workerData.index as number, templateFile: workerData.templateFile as string });
12 }
13 }
14
15 function addListener() {
16 parentPort.on('message', (message) => {
17 if (message.id === WorkerEvents.START_WORKER_ELEMENT) {
18 startChargingStation(message.workerData);
19 }
20 });
21 }
22
23 function startChargingStation(data: any) {
24 const station = new ChargingStation(data.index as number, data.templateFile as string);
25 station.start();
26 }