Make the station worker self contained.
[e-mobility-charging-stations-simulator.git] / src / index.js
index 3f47f1fe0b1806b8088014a59c46a05a01edcd1a..fb54b6c06f1045cce9b41f2caa73d914182a8817 100644 (file)
@@ -1,7 +1,6 @@
 const Configuration = require('./utils/Configuration');
 const Utils = require('./utils/Utils');
 const Wrk = require('./charging-station/Worker');
-const fs = require('fs');
 const logger = require('./utils/Logger');
 
 class Bootstrap {
@@ -9,37 +8,26 @@ class Bootstrap {
     try {
       logger.info('%s Configuration: %j', Utils.basicFormatLog(), Configuration.getConfig());
       // Start each ChargingStation object in a worker thread
-      if (Configuration.getChargingStationTemplateURLs()) {
+      if (Configuration.getStationTemplateURLs()) {
         let numStationsTotal = 0;
-        Configuration.getChargingStationTemplateURLs().forEach((stationURL) => {
+        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())),
-          }, nbStation);
-          worker.start();
-        }
+        console.log('No stationURLS defined in configuration, exiting');
       }
     } catch (error) {
       // eslint-disable-next-line no-console