1 // Partial Copyright Jerome Benoit. 2021. All Rights Reserved.
4 ChargingStationWorkerData
,
5 ChargingStationWorkerMessage
,
6 ChargingStationWorkerMessageEvents
,
7 } from
'../types/ChargingStationWorker';
8 import { parentPort
, workerData
} from
'worker_threads';
10 import ChargingStation from
'./ChargingStation';
11 import Constants from
'../utils/Constants';
12 import { ThreadWorker
} from
'poolifier';
13 import Utils from
'../utils/Utils';
15 // Conditionally export ThreadWorker instance for pool usage
16 export let threadWorker
: ThreadWorker
;
17 if (Utils
.workerPoolInUse()) {
18 threadWorker
= new ThreadWorker
<ChargingStationWorkerData
>(startChargingStation
, {
19 maxInactiveTime
: Constants
.WORKER_POOL_MAX_INACTIVE_TIME
,
23 // Add message listener to start charging station from main thread
25 if (!Utils
.isUndefined(workerData
)) {
26 startChargingStation({
27 index
: workerData
.index
as number,
28 templateFile
: workerData
.templateFile
as string,
34 * Listen messages send by the main thread
36 function addMessageListener(): void {
37 parentPort
?.on('message', (message
: ChargingStationWorkerMessage
) => {
38 if (message
.id
=== ChargingStationWorkerMessageEvents
.START_WORKER_ELEMENT
) {
39 startChargingStation(message
.data
);
45 * Create and start a charging station instance
47 * @param data workerData
49 function startChargingStation(data
: ChargingStationWorkerData
): void {
50 const station
= new ChargingStation(data
.index
, data
.templateFile
);