3f47f1fe0b1806b8088014a59c46a05a01edcd1a
[e-mobility-charging-stations-simulator.git] / src / index.js
1 const Configuration = require('./utils/Configuration');
2 const Utils = require('./utils/Utils');
3 const Wrk = require('./charging-station/Worker');
4 const fs = require('fs');
5 const logger = require('./utils/Logger');
6
7 class Bootstrap {
8 static async start() {
9 try {
10 logger.info('%s Configuration: %j', Utils.basicFormatLog(), Configuration.getConfig());
11 // Start each ChargingStation object in a worker thread
12 if (Configuration.getChargingStationTemplateURLs()) {
13 let numStationsTotal = 0;
14 Configuration.getChargingStationTemplateURLs().forEach((stationURL) => {
15 try {
16 // Load file
17 const fileDescriptor = fs.openSync(stationURL.file, 'r');
18 const stationTemplate = JSON.parse(fs.readFileSync(fileDescriptor, 'utf8'));
19 fs.closeSync(fileDescriptor);
20 const nbStation = (stationURL.numberOfStation ? stationURL.numberOfStation : 0);
21 numStationsTotal += nbStation;
22 for (let index = 1; index <= nbStation; index++) {
23 const worker = new Wrk('./src/charging-station/StationWorker.js', {
24 index,
25 template: JSON.parse(JSON.stringify(stationTemplate)),
26 }, numStationsTotal);
27 worker.start();
28 }
29 } catch (error) {
30 // eslint-disable-next-line no-console
31 console.log('Template file' + stationURL.file + ' error' + error);
32 }
33 });
34 } else {
35 const nbStation = Configuration.getNumberofChargingStation();
36 for (let index = 1; index <= nbStation; index++) {
37 const worker = new Wrk('./src/charging-station/StationWorker.js', {
38 index,
39 template: JSON.parse(JSON.stringify(Configuration.getChargingStationTemplate())),
40 }, nbStation);
41 worker.start();
42 }
43 }
44 } catch (error) {
45 // eslint-disable-next-line no-console
46 console.log('Bootstrap start error ' + JSON.stringify(error, null, ' '));
47 }
48 }
49 }
50
51 Bootstrap.start();