fixe
[e-mobility-charging-stations-simulator.git] / src / charging-station / ui-websocket-services / AbstractUIService.ts
index 4d950d05ebb8c3abcef62cf123bd17267520d297..6755cd6e0fd3e1f8b2571cf43b7aad8f285304f7 100644 (file)
@@ -1,6 +1,7 @@
-import { ProtocolCommand, ProtocolRequestHandler } from '../../types/UiProtocol';
+import { ProtocolCommand, ProtocolRequestHandler } from '../../types/UIProtocol';
 
 import BaseError from '../../exception/BaseError';
+import { JsonType } from '../../types/JsonType';
 import UIWebSocketServer from '../UIWebSocketServer';
 import logger from '../../utils/Logger';
 
@@ -15,12 +16,12 @@ export default abstract class AbstractUIService {
     ]);
   }
 
-  public async handleMessage(command: ProtocolCommand, payload: Record<string, unknown>): Promise<void> {
-    let messageResponse: Record<string, unknown>;
+  public async messageHandler(command: ProtocolCommand, payload: JsonType): Promise<void> {
+    let messageResponse: JsonType;
     if (this.messageHandlers.has(command)) {
       try {
         // Call the method to build the message response
-        messageResponse = await this.messageHandlers.get(command)(payload) as Record<string, unknown>;
+        messageResponse = (await this.messageHandlers.get(command)(payload)) as JsonType;
       } catch (error) {
         // Log
         logger.error(this.uiWebSocketServer.logPrefix() + ' Handle message error: %j', error);
@@ -28,20 +29,25 @@ export default abstract class AbstractUIService {
       }
     } else {
       // Throw exception
-      throw new BaseError(`${command} is not implemented to handle message payload ${JSON.stringify(payload, null, 2)}`);
+      throw new BaseError(
+        `${command} is not implemented to handle message payload ${JSON.stringify(
+          payload,
+          null,
+          2
+        )}`
+      );
     }
     // Send the built message response
     this.uiWebSocketServer.broadcastToClients(this.buildProtocolMessage(command, messageResponse));
   }
 
-  protected buildProtocolMessage(
-      command: ProtocolCommand,
-      payload: Record<string, unknown>,
-  ): string {
+  protected buildProtocolMessage(command: ProtocolCommand, payload: JsonType): string {
+    console.log(JSON.stringify([command, payload])); // DEBUG
     return JSON.stringify([command, payload]);
   }
 
-  private handleListChargingStations(): Set<string> {
-    return this.uiWebSocketServer.chargingStations;
+  private handleListChargingStations(): string[] {
+    // FIXED
+    return Array.from(this.uiWebSocketServer.chargingStations);
   }
 }