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