Version 1.1.57
[e-mobility-charging-stations-simulator.git] / src / charging-station / UIWebSocketServer.ts
index f941b138d505540f3fa3c12bee916ffda4e28ec8..5ffc9e0c640320a36f22fb3658afe7478747f32b 100644 (file)
@@ -1,24 +1,31 @@
 import { Protocol, ProtocolCommand, ProtocolRequest, ProtocolVersion } from '../types/UIProtocol';
+import WebSocket, { OPEN, Server, ServerOptions } from 'ws';
 
-import AbstractUIService from './UIWebSocketServices/AbstractUIService';
+import AbstractUIService from './ui-websocket-services/AbstractUIService';
 import BaseError from '../exception/BaseError';
+import Configuration from '../utils/Configuration';
 import { IncomingMessage } from 'http';
-import UIServiceFactory from './UIWebSocketServices/UIServiceFactory';
+import UIServiceFactory from './ui-websocket-services/UIServiceFactory';
 import Utils from '../utils/Utils';
-import WebSocket from 'ws';
 import logger from '../utils/Logger';
 
-export default class UIWebSocketServer extends WebSocket.Server {
-  public uiService: AbstractUIService;
+export default class UIWebSocketServer extends Server {
+  public readonly chargingStations: Set<string>;
+  public readonly uiServices: Map<ProtocolVersion, AbstractUIService>;
 
-  public constructor(options?: WebSocket.ServerOptions, callback?: () => void) {
+  public constructor(options?: ServerOptions, callback?: () => void) {
     // Create the WebSocket Server
-    super(options ?? { port: 80 }, callback);
+    super(options ?? Configuration.getUIWebSocketServer().options, callback);
+    this.chargingStations = new Set<string>();
+    this.uiServices = new Map<ProtocolVersion, AbstractUIService>();
+    for (const version of Object.values(ProtocolVersion)) {
+      this.uiServices.set(version, UIServiceFactory.getUIServiceImplementation(version, this));
+    }
   }
 
-  public broadcastToClients(message: string | Record<string, unknown>): void {
+  public broadcastToClients(message: string): void {
     for (const client of this.clients) {
-      if (client?.readyState === WebSocket.OPEN) {
+      if (client?.readyState === OPEN) {
         client.send(message);
       }
     }
@@ -27,10 +34,13 @@ export default class UIWebSocketServer extends WebSocket.Server {
   public start(): void {
     this.on('connection', (socket: WebSocket, request: IncomingMessage): void => {
       const protocolIndex = socket.protocol.indexOf(Protocol.UI);
-      const version = socket.protocol.substring(protocolIndex + Protocol.UI.length) as ProtocolVersion;
-      this.uiService = UIServiceFactory.getUIServiceImplementation(version, this);
-      if (!this.uiService) {
-        throw new BaseError(`Could not find a UI service implementation for UI protocol version ${version}`);
+      const version = socket.protocol.substring(
+        protocolIndex + Protocol.UI.length
+      ) as ProtocolVersion;
+      if (!this.uiServices.has(version)) {
+        throw new BaseError(
+          `Could not find a UI service implementation for UI protocol version ${version}`
+        );
       }
       // FIXME: check connection validity
       socket.on('message', (messageData) => {
@@ -41,9 +51,16 @@ export default class UIWebSocketServer extends WebSocket.Server {
         } else {
           throw new BaseError('UI protocol request is not iterable');
         }
-        this.uiService.handleMessage(command, payload).catch(() => {
-          logger.error(`${this.logPrefix()} Error while handling command %s message: %j`, command, payload);
-        });
+        this.uiServices
+          .get(version)
+          .handleMessage(command, payload)
+          .catch(() => {
+            logger.error(
+              `${this.logPrefix()} Error while handling command %s message: %j`,
+              command,
+              payload
+            );
+          });
       });
       socket.on('error', (error) => {
         logger.error(`${this.logPrefix()} Error on WebSocket: %j`, error);