Move the UI WS protocol version handling is protocol header
[e-mobility-charging-stations-simulator.git] / src / charging-station / Bootstrap.ts
index 06bc50e391695090eb25d6e469a98c5e5be7ce3d..74fb74b7cb6e195524f0440b3eb831e85f25804d 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 './UIWebSocketServices/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,7 +18,7 @@ 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 uiWebSocketServer: UIWebSocketServer;
   private readonly storage: Storage;
   private numberOfChargingStations: number;
   private readonly version: string = version;
@@ -28,7 +29,7 @@ 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.uiWebSocketServer = new UIWebSocketServer({ port: 80, handleProtocols: UIServiceUtils.handleProtocols });
     this.storage = StorageFactory.getStorage(Configuration.getPerformanceStorage().type, Configuration.getPerformanceStorage().URI, this.logPrefix());
     Configuration.setConfigurationChangeCallback(async () => Bootstrap.getInstance().restart());
   }
@@ -46,7 +47,7 @@ export default class Bootstrap {
         this.numberOfChargingStations = 0;
         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,7 +85,7 @@ export default class Bootstrap {
   public async stop(): Promise<void> {
     if (isMainThread && this.started) {
       await this.workerImplementation.stop();
-      this.webSocketServer.stop();
+      this.uiWebSocketServer.stop();
       await this.storage.close();
     } else {
       console.error(chalk.red('Trying to stop the charging stations simulator while not started'));
@@ -109,7 +110,11 @@ export default class Bootstrap {
           workerChoiceStrategy: Configuration.getWorkerPoolStrategy()
         },
         messageHandler: async (msg: ChargingStationWorkerMessage) => {
-          if (msg.id === ChargingStationWorkerMessageEvents.PERFORMANCE_STATISTICS) {
+          if (msg.id === ChargingStationWorkerMessageEvents.STARTED) {
+            this.uiWebSocketServer.uiService.chargingStations.add(msg.data.id);
+          } else if (msg.id === ChargingStationWorkerMessageEvents.STOPPED) {
+            this.uiWebSocketServer.uiService.chargingStations.delete(msg.data.id);
+          } else if (msg.id === ChargingStationWorkerMessageEvents.PERFORMANCE_STATISTICS) {
             await this.storage.storePerformanceStatistics(msg.data);
           }
         }