Use ConnectionTineOut OCPP parameter as WS timeout
[e-mobility-charging-stations-simulator.git] / src / charging-station / StationWorker.ts
... / ...
CommitLineData
1import { StationWorkerData, WorkerEvents } from '../types/Worker';
2import { parentPort, workerData } from 'worker_threads';
3
4import ChargingStation from './ChargingStation';
5import Constants from '../utils/Constants';
6import { ThreadWorker } from 'poolifier';
7import Utils from '../utils/Utils';
8
9// Conditionally export ThreadWorker instance for pool usage
10export let threadWorker: ThreadWorker;
11if (Utils.workerPoolInUse()) {
12 threadWorker = new ThreadWorker<StationWorkerData>(startChargingStation, { maxInactiveTime: Constants.WORKER_POOL_MAX_INACTIVE_TIME, async: false });
13} else {
14 // Add message listener to start charging station from main thread
15 addMessageListener();
16 if (!Utils.isUndefined(workerData)) {
17 startChargingStation({ index: workerData.index as number, templateFile: workerData.templateFile as string });
18 }
19}
20
21/**
22 * Listen messages send by the main thread
23 */
24function addMessageListener(): void {
25 parentPort?.on('message', (message) => {
26 if (message.id === WorkerEvents.START_WORKER_ELEMENT) {
27 startChargingStation(message.workerData);
28 }
29 });
30}
31
32/**
33 * Create and start a charging station instance
34 *
35 * @param {StationWorkerData} data workerData
36 */
37function startChargingStation(data: StationWorkerData): void {
38 const station = new ChargingStation(data.index, data.templateFile);
39 station.start();
40}