build(deps-dev): apply updates
[e-mobility-charging-stations-simulator.git] / src / start.ts
index 3b9fc75b287c7c8d9eff1296dd0eb720ddb09fe0..7c42c4d205682073c4d23c04d7d2a3eff15add97 100644 (file)
@@ -1,66 +1,11 @@
-import Configuration from './utils/Configuration';
-import Constants from './utils/Constants';
-import Utils from './utils/Utils';
-import WorkerData from './types/WorkerData';
-import Wrk from './charging-station/Worker';
+// Partial Copyright Jerome Benoit. 2021-2024. All Rights Reserved.
 
-class Bootstrap {
-  static async start() {
-    try {
-      let numStationsTotal = 0;
-      let numConcurrentWorkers = 0;
-      let worker: Wrk;
-      // Start each ChargingStation object in a worker thread
-      if (Configuration.getStationTemplateURLs()) {
-        for await (const stationURL of Configuration.getStationTemplateURLs()) {
-          try {
-            const nbStations = stationURL.numberOfStations ? stationURL.numberOfStations : 0;
-            numStationsTotal += nbStations;
-            for (let index = 1; index <= nbStations; index++) {
-              const workerData = {
-                index,
-                templateFile: stationURL.file
-              } as WorkerData;
-              if (Configuration.useWorkerPool()) {
-                worker = new Wrk('./dist/charging-station/StationWorker.js', workerData);
-                worker.start().catch(() => { });
-                numConcurrentWorkers = Configuration.getWorkerPoolSize();
-              } else {
-                const chargingStationsPerWorker = Configuration.getChargingStationsPerWorker();
-                let chargingStationsPerWorkerCounter = 0;
-                if (chargingStationsPerWorkerCounter === 0 || chargingStationsPerWorkerCounter === chargingStationsPerWorker) {
-                  // Start new Wrk with one charging station
-                  worker = new Wrk('./dist/charging-station/StationWorker.js', workerData, chargingStationsPerWorker);
-                  worker.start().catch(() => { });
-                  numConcurrentWorkers++;
-                  chargingStationsPerWorkerCounter = 1;
-                  // Start Wrk sequentially to optimize memory at start time
-                  await Utils.sleep(Constants.START_WORKER_DELAY);
-                } else {
-                  // Add charging station to existing Wrk
-                  worker.addWorkerElement(workerData);
-                  chargingStationsPerWorkerCounter++;
-                }
-              }
-            }
-          } catch (error) {
-            // eslint-disable-next-line no-console
-            console.log('Charging station start with template file ' + stationURL.file + ' error ' + JSON.stringify(error, null, ' '));
-          }
-        }
-      } else {
-        console.log('No stationTemplateURLs defined in configuration, exiting');
-      }
-      if (numStationsTotal === 0) {
-        console.log('No charging station template enabled in configuration, exiting');
-      } else {
-        console.log('Charging station simulator started with ' + numStationsTotal.toString() + ' charging station(s) of ' + numConcurrentWorkers.toString() + ' concurrently running');
-      }
-    } catch (error) {
-      // eslint-disable-next-line no-console
-      console.log('Bootstrap start error ' + JSON.stringify(error, null, ' '));
-    }
-  }
-}
+import chalk from 'chalk'
+
+import { Bootstrap } from './charging-station/index.js'
 
-Bootstrap.start();
+try {
+  await Bootstrap.getInstance().start()
+} catch (error) {
+  console.error(chalk.red('Startup error: '), error)
+}