Cleanup workers handling classes.
[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 Constants from '../utils/Constants';
5
6 if (!isMainThread) {
7 startChargingStation({ index: workerData.index as number, templateFile: workerData.templateFile as string });
8
9 // Listener: start new charging station from main thread
10 addListener();
11 }
12
13 function addListener() {
14 parentPort.on('message', (e) => {
15 if (e.id === Constants.START_WORKER_ELEMENT) {
16 startChargingStation(e.workerData);
17 }
18 });
19 }
20
21 function startChargingStation(data: any) {
22 const station = new ChargingStation(data.index as number, data.templateFile as string);
23 station.start();
24 }