1 import Configuration from
'./utils/Configuration';
2 import Constants from
'./utils/Constants';
3 import Utils from
'./utils/Utils';
4 import WorkerData from
'./types/WorkerData';
5 import WorkerGroup from
'./charging-station/WorkerGroup';
6 import WorkerPool from
'./charging-station/WorkerPool';
11 let numStationsTotal
= 0;
12 let numConcurrentWorkers
= 0;
13 const chargingStationsPerWorker
= Configuration
.getChargingStationsPerWorker();
14 let chargingStationsPerWorkerCounter
= 0;
15 let workerImplementation
: WorkerGroup
| WorkerPool
;
16 if (Configuration
.useWorkerPool()) {
17 workerImplementation
= new WorkerPool('./dist/charging-station/StationWorker.js');
18 void workerImplementation
.start();
20 // Start each ChargingStation object in a worker thread
21 if (Configuration
.getStationTemplateURLs()) {
22 for (const stationURL
of Configuration
.getStationTemplateURLs()) {
24 const nbStations
= stationURL
.numberOfStations
? stationURL
.numberOfStations
: 0;
25 for (let index
= 1; index
<= nbStations
; index
++) {
26 const workerData
: WorkerData
= {
28 templateFile
: stationURL
.file
30 if (Configuration
.useWorkerPool()) {
31 void workerImplementation
.addElement(workerData
);
32 numConcurrentWorkers
= workerImplementation
.size
;
33 // Start worker sequentially to optimize memory at start time
34 await Utils
.sleep(Constants
.START_WORKER_DELAY
);
36 // eslint-disable-next-line no-lonely-if
37 if (chargingStationsPerWorkerCounter
=== 0 || chargingStationsPerWorkerCounter
>= chargingStationsPerWorker
) {
38 // Start new WorkerGroup with one charging station
39 workerImplementation
= new WorkerGroup('./dist/charging-station/StationWorker.js', workerData
, chargingStationsPerWorker
);
40 void workerImplementation
.start();
41 numConcurrentWorkers
++;
42 chargingStationsPerWorkerCounter
= 1;
43 // Start worker sequentially to optimize memory at start time
44 await Utils
.sleep(Constants
.START_WORKER_DELAY
);
46 // Add charging station to existing WorkerGroup
47 void workerImplementation
.addElement(workerData
);
48 chargingStationsPerWorkerCounter
++;
54 // eslint-disable-next-line no-console
55 console
.log('Charging station start with template file ' + stationURL
.file
+ ' error ' + JSON
.stringify(error
, null, ' '));
59 console
.log('No stationTemplateURLs defined in configuration, exiting');
61 if (numStationsTotal
=== 0) {
62 console
.log('No charging station template enabled in configuration, exiting');
63 } else if (Configuration
.useWorkerPool()) {
64 console
.log('Charging station simulator started with ' + numStationsTotal
.toString() + ' charging station(s) and ' + numConcurrentWorkers
.toString() + '/' + Configuration
.getWorkerPoolMaxSize().toString() + ' worker(s) concurrently running');
66 console
.log('Charging station simulator started with ' + numStationsTotal
.toString() + ' charging station(s) and ' + numConcurrentWorkers
.toString() + ' worker(s) concurrently running');
69 // eslint-disable-next-line no-console
70 console
.log('Bootstrap start error ' + JSON
.stringify(error
, null, ' '));
75 Bootstrap
.start().catch(