| 1 | import { isMainThread, parentPort, workerData } from 'worker_threads'; |
| 2 | import Constants from '../utils/Constants'; |
| 3 | |
| 4 | import ChargingStation from './ChargingStation'; |
| 5 | |
| 6 | if (!isMainThread) { |
| 7 | const station = new ChargingStation(workerData.index as number, workerData.templateFile as string); |
| 8 | station.start(); |
| 9 | |
| 10 | // Listener: start new charging station from main thread |
| 11 | addListener(); |
| 12 | } |
| 13 | |
| 14 | function addListener() { |
| 15 | parentPort.setMaxListeners(1000); |
| 16 | parentPort.on("message", e => { |
| 17 | if (e.id === Constants.START_NEW_CHARGING_STATION) { |
| 18 | startChargingStation(e.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 | } |