UI protocol: update Insomnia requests collection
[e-mobility-charging-stations-simulator.git] / src / charging-station / ui-server / UIWebSocketServer.ts
index f789d3963e881762b58457e1fa5eed498633b5a4..c067fb362a8c8b717312bcff35a049135aac2a23 100644 (file)
@@ -1,4 +1,4 @@
-import { IncomingMessage, createServer } from 'http';
+import type { IncomingMessage } from 'http';
 import type internal from 'stream';
 
 import { StatusCodes } from 'http-status-codes';
@@ -20,7 +20,6 @@ export default class UIWebSocketServer extends AbstractUIServer {
 
   public constructor(protected readonly uiServerConfiguration: UIServerConfiguration) {
     super(uiServerConfiguration);
-    this.httpServer = createServer();
     this.webSocketServer = new WebSocketServer({
       handleProtocols: UIServiceUtils.handleProtocols,
       noServer: true,
@@ -46,10 +45,11 @@ export default class UIWebSocketServer extends AbstractUIServer {
           ws.close(WebSocketCloseEventStatusCode.CLOSE_INVALID_PAYLOAD);
           return;
         }
-        const [messageId, procedureName, payload] = request as ProtocolRequest;
+        const [requestId] = request as ProtocolRequest;
+        this.responseHandlers.set(requestId, ws);
         this.uiServices
           .get(version)
-          .requestHandler(this.buildProtocolRequest(messageId, procedureName, payload))
+          .requestHandler(request)
           .catch(() => {
             /* Error caught by AbstractUIService */
           });
@@ -88,17 +88,26 @@ export default class UIWebSocketServer extends AbstractUIServer {
     }
   }
 
-  public stop(): void {
-    this.chargingStations.clear();
-  }
-
   public sendRequest(request: ProtocolRequest): void {
     this.broadcastToClients(JSON.stringify(request));
   }
 
   public sendResponse(response: ProtocolResponse): void {
-    // TODO: send response only to the client that sent the request
-    this.broadcastToClients(JSON.stringify(response));
+    const responseId = response[0];
+    if (this.responseHandlers.has(responseId)) {
+      const ws = this.responseHandlers.get(responseId) as WebSocket;
+      if (ws?.readyState === WebSocket.OPEN) {
+        ws.send(JSON.stringify(response));
+      }
+      this.responseHandlers.delete(responseId);
+    } else {
+      logger.error(
+        `${this.logPrefix(
+          moduleName,
+          'sendResponse'
+        )} Response for unknown request id: ${responseId}`
+      );
+    }
   }
 
   public logPrefix(modName?: string, methodName?: string, prefixSuffix?: string): string {
@@ -118,18 +127,6 @@ export default class UIWebSocketServer extends AbstractUIServer {
     }
   }
 
-  private authenticate(req: IncomingMessage, next: (err?: Error) => void): void {
-    if (this.isBasicAuthEnabled() === true) {
-      if (this.isValidBasicAuth(req) === false) {
-        next(new Error('Unauthorized'));
-      } else {
-        next();
-      }
-    } else {
-      next();
-    }
-  }
-
   private validateRawDataRequest(rawData: RawData): ProtocolRequest | false {
     // logger.debug(
     //   `${this.logPrefix(