Add charging stations listing to WS server commands
[e-mobility-charging-stations-simulator.git] / src / charging-station / WebSocketServices / ui / 0.0.1 / UIService.ts
index 8ca0c6dc9495624c0d0f376b89547f35cf157b48..f2db451240135a182d0921cac1bcc28c9a19f121 100644 (file)
@@ -1,4 +1,4 @@
-import { ProtocolCommand, ProtocolRequestHandler } from '../../../../types/UIProtocol';
+import { ProtocolCommand, ProtocolRequestHandler, ProtocolVersion } from '../../../../types/UIProtocol';
 
 import AbstractUIService from '../AbstractUIService';
 import BaseError from '../../../../exception/BaseError';
@@ -11,12 +11,13 @@ export default class UIService extends AbstractUIService {
   constructor(webSocketServer: WebSocketServer) {
     super(webSocketServer);
     this.messageHandlers = new Map<ProtocolCommand, ProtocolRequestHandler>([
+      [ProtocolCommand.LIST_CHARGING_STATIONS, this.handleListChargingStations.bind(this)],
       [ProtocolCommand.START_TRANSACTION, this.handleStartTransaction.bind(this)],
       [ProtocolCommand.STOP_TRANSACTION, this.handleStopTransaction.bind(this)],
     ]);
   }
 
-  async handleMessage(command: ProtocolCommand, payload: Record<string, unknown>): Promise<void> {
+  async handleMessage(version: ProtocolVersion, command: ProtocolCommand, payload: Record<string, unknown>): Promise<void> {
     let messageResponse: Record<string, unknown>;
     if (this.messageHandlers.has(command) && command !== ProtocolCommand.UNKNOWN) {
       try {
@@ -32,7 +33,11 @@ export default class UIService extends AbstractUIService {
       throw new BaseError(`${command} is not implemented to handle message payload ${JSON.stringify(payload, null, 2)}`);
     }
     // Send the built response
-    this.webSocketServer.broadcastToClients(messageResponse);
+    this.webSocketServer.broadcastToClients(this.buildProtocolMessage(version, command, messageResponse));
+  }
+
+  private handleListChargingStations(payload: Record<string, unknown>) {
+    return this.chargingStations;
   }
 
   private handleStartTransaction(payload: Record<string, unknown>) { }