Cleanups.
[e-mobility-charging-stations-simulator.git] / src / start.ts
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 Wrk from './charging-station/Worker';
6
7 class Bootstrap {
8 static async start() {
9 try {
10 let numStationsTotal = 0;
11 let numConcurrentWorkers = 0;
12 let worker: Wrk;
13 const chargingStationsPerWorker = Configuration.getChargingStationsPerWorker();
14 let counter = 0;
15 // Start each ChargingStation object in a worker thread
16 if (Configuration.getStationTemplateURLs()) {
17 for await (const stationURL of Configuration.getStationTemplateURLs()) {
18 try {
19 const nbStations = stationURL.numberOfStations ? stationURL.numberOfStations : 0;
20 numStationsTotal += nbStations;
21 for (let index = 1; index <= nbStations; index++) {
22 const workerData = {
23 index,
24 templateFile: stationURL.file
25 } as WorkerData;
26 if (counter === 0 || counter === chargingStationsPerWorker) {
27 // Start new worker with one charging station
28 worker = new Wrk('./dist/charging-station/StationWorker.js', workerData, numStationsTotal);
29 worker.start().catch(() => { });
30 counter = 0;
31 // Start workers sequentially to optimize memory at start time
32 await Utils.sleep(Constants.START_WORKER_DELAY);
33 } else {
34 // Add charging station to existing Worker
35 worker.addChargingStation(workerData, numStationsTotal);
36 }
37 counter++;
38 numConcurrentWorkers = worker.concurrentWorkers;
39 }
40 } catch (error) {
41 // eslint-disable-next-line no-console
42 console.log('Charging station start with template file ' + stationURL.file + ' error ' + JSON.stringify(error, null, ' '));
43 }
44 }
45 } else {
46 console.log('No stationTemplateURLs defined in configuration, exiting');
47 }
48 if (numStationsTotal === 0) {
49 console.log('No charging station template enabled in configuration, exiting');
50 } else {
51 console.log('Charging station simulator started with ' + numStationsTotal.toString() + ' charging station(s) of ' + numConcurrentWorkers.toString() + ' concurrently running');
52 }
53 } catch (error) {
54 // eslint-disable-next-line no-console
55 console.log('Bootstrap start error ' + JSON.stringify(error, null, ' '));
56 }
57 }
58 }
59
60 Bootstrap.start();