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