Use a sensible naming in methods.
[e-mobility-charging-stations-simulator.git] / src / index.js
index 3591d11c91546bed3e7c8fcdc845c3563e680bae..d8ea947172d9dcdf268475b40a19176cb2782fbc 100644 (file)
@@ -1,47 +1,33 @@
 const Configuration = require('./utils/Configuration');
-const EventEmitter = require('events');
 const Utils = require('./utils/Utils');
 const Wrk = require('./charging-station/Worker');
-const fs = require('fs');
 const logger = require('./utils/Logger');
 
 class Bootstrap {
   static async start() {
     try {
-      logger.info('%s Configuration: %j', Utils.basicFormatLog(), Configuration.getConfig());
-      if (Configuration.useWorkerPool && Configuration.getWorkerPoolSize() > 10) {
-        EventEmitter.defaultMaxListeners = Configuration.getWorkerPoolSize() + 1;
-      }
+      logger.debug('%s Configuration: %j', Utils.logPrefix(), Configuration.getConfig());
       // Start each ChargingStation object in a worker thread
-      if (Configuration.getChargingStationTemplateURLs()) {
-        Configuration.getChargingStationTemplateURLs().forEach((stationURL) => {
+      if (Configuration.getStationTemplateURLs()) {
+        let numStationsTotal = 0;
+        Configuration.getStationTemplateURLs().forEach((stationURL) => {
           try {
-            // load file
-            const fileDescriptor = fs.openSync(stationURL.file, 'r');
-            const stationTemplate = JSON.parse(fs.readFileSync(fileDescriptor, 'utf8'));
-            fs.closeSync(fileDescriptor);
-            const nbStation = (stationURL.numberOfStation ? stationURL.numberOfStation : 0);
+            const nbStation = stationURL.numberOfStation ? stationURL.numberOfStation : 0;
+            numStationsTotal += nbStation;
             for (let index = 1; index <= nbStation; index++) {
               const worker = new Wrk('./src/charging-station/StationWorker.js', {
                 index,
-                template: JSON.parse(JSON.stringify(stationTemplate)),
-              });
+                templateFile: stationURL.file,
+              }, numStationsTotal);
               worker.start();
             }
           } catch (error) {
             // eslint-disable-next-line no-console
-            console.log('Template file' + stationURL.file + ' error' + error);
+            console.log('Charging station start with template file ' + stationURL.file + ' error ' + JSON.stringify(error, null, ' '));
           }
         });
       } else {
-        const nbStation = Configuration.getNumberofChargingStation();
-        for (let index = 1; index <= nbStation; index++) {
-          const worker = new Wrk('./src/charging-station/StationWorker.js', {
-            index,
-            template: JSON.parse(JSON.stringify(Configuration.getChargingStationTemplate())),
-          });
-          worker.start();
-        }
+        console.log('No stationTemplateURLs defined in configuration, exiting');
       }
     } catch (error) {
       // eslint-disable-next-line no-console