package-lock.json: sync
[e-mobility-charging-stations-simulator.git] / src / charging-station / Bootstrap.ts
index fe344407dc03400970fc89fb911f45011ce422b5..ebb5619b44929c655bf5c275dcf73b7d6aebd2bb 100644 (file)
@@ -5,8 +5,9 @@ import { ChargingStationWorkerData, ChargingStationWorkerMessage, ChargingStatio
 import Configuration from '../utils/Configuration';
 import { Storage } from '../performance/storage/Storage';
 import { StorageFactory } from '../performance/storage/StorageFactory';
+import { UIServiceUtils } from './ui-websocket-services/UIServiceUtils';
+import UIWebSocketServer from './UIWebSocketServer';
 import Utils from '../utils/Utils';
-import WebSocketServer from './WebSocketServer';
 import WorkerAbstract from '../worker/WorkerAbstract';
 import WorkerFactory from '../worker/WorkerFactory';
 import chalk from 'chalk';
@@ -17,8 +18,8 @@ import { version } from '../../package.json';
 export default class Bootstrap {
   private static instance: Bootstrap | null = null;
   private workerImplementation: WorkerAbstract | null = null;
-  private readonly webSocketServer: WebSocketServer;
-  private readonly storage: Storage;
+  private readonly uiWebSocketServer!: UIWebSocketServer;
+  private readonly storage!: Storage;
   private numberOfChargingStations: number;
   private readonly version: string = version;
   private started: boolean;
@@ -28,8 +29,14 @@ export default class Bootstrap {
     this.started = false;
     this.workerScript = path.join(path.resolve(__dirname, '../'), 'charging-station', 'ChargingStationWorker.js');
     this.initWorkerImplementation();
-    this.webSocketServer = new WebSocketServer();
-    this.storage = StorageFactory.getStorage(Configuration.getPerformanceStorage().type, Configuration.getPerformanceStorage().URI, this.logPrefix());
+    Configuration.getUIWebSocketServer().enabled && (this.uiWebSocketServer = new UIWebSocketServer({
+      ...Configuration.getUIWebSocketServer().options, handleProtocols: UIServiceUtils.handleProtocols
+    }));
+    Configuration.getPerformanceStorage().enabled && (this.storage = StorageFactory.getStorage(
+      Configuration.getPerformanceStorage().type,
+      Configuration.getPerformanceStorage().URI,
+      this.logPrefix()
+    ));
     Configuration.setConfigurationChangeCallback(async () => Bootstrap.getInstance().restart());
   }
 
@@ -44,9 +51,9 @@ export default class Bootstrap {
     if (isMainThread && !this.started) {
       try {
         this.numberOfChargingStations = 0;
-        await this.storage.open();
+        await this.storage?.open();
         await this.workerImplementation.start();
-        this.webSocketServer.start();
+        this.uiWebSocketServer?.start();
         // Start ChargingStation object in worker thread
         if (Configuration.getStationTemplateURLs()) {
           for (const stationURL of Configuration.getStationTemplateURLs()) {
@@ -84,8 +91,8 @@ export default class Bootstrap {
   public async stop(): Promise<void> {
     if (isMainThread && this.started) {
       await this.workerImplementation.stop();
-      this.webSocketServer.stop();
-      await this.storage.close();
+      this.uiWebSocketServer?.stop();
+      await this.storage?.close();
     } else {
       console.error(chalk.red('Trying to stop the charging stations simulator while not started'));
     }
@@ -110,9 +117,9 @@ export default class Bootstrap {
         },
         messageHandler: async (msg: ChargingStationWorkerMessage) => {
           if (msg.id === ChargingStationWorkerMessageEvents.STARTED) {
-            this.webSocketServer.webSocketServerService.chargingStations.add(msg.data.id);
+            this.uiWebSocketServer.chargingStations.add(msg.data.id);
           } else if (msg.id === ChargingStationWorkerMessageEvents.STOPPED) {
-            this.webSocketServer.webSocketServerService.chargingStations.delete(msg.data.id);
+            this.uiWebSocketServer.chargingStations.delete(msg.data.id);
           } else if (msg.id === ChargingStationWorkerMessageEvents.PERFORMANCE_STATISTICS) {
             await this.storage.storePerformanceStatistics(msg.data);
           }