Commit | Line | Data |
---|---|---|
3d2ff9e4 | 1 | import { isMainThread, parentPort, workerData } from 'worker_threads'; |
3f40bc9c | 2 | |
6af9012e | 3 | import ChargingStation from './ChargingStation'; |
6013bc53 | 4 | import Utils from '../utils/Utils'; |
3e1416d8 | 5 | import { WorkerEvents } from '../types/WorkerEvents'; |
7dde0b73 JB |
6 | |
7 | if (!isMainThread) { | |
6013bc53 | 8 | // Add listener to start charging station from main thread |
3d2ff9e4 | 9 | addListener(); |
6013bc53 JB |
10 | if (!Utils.isUndefined(workerData)) { |
11 | startChargingStation({ index: workerData.index as number, templateFile: workerData.templateFile as string }); | |
12 | } | |
3d2ff9e4 J |
13 | } |
14 | ||
15 | function addListener() { | |
3e1416d8 JB |
16 | parentPort.on('message', (message) => { |
17 | if (message.id === WorkerEvents.START_WORKER_ELEMENT) { | |
18 | startChargingStation(message.workerData); | |
3d2ff9e4 J |
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(); | |
7dde0b73 | 26 | } |