fix(ui): move v-show to the UI server container component
[e-mobility-charging-stations-simulator.git] / ui / web / src / composables / UIClient.ts
index 81a322d31c73d1d2a0222f2086fa6d498e796a22..51d61f263f7788adcd45e930ef79103dbd92d883 100644 (file)
@@ -1,6 +1,8 @@
+import { useToast } from 'vue-toast-notification'
 import {
   ApplicationProtocol,
   AuthenticationType,
+  type ChargingStationOptions,
   ProcedureName,
   type ProtocolResponse,
   type RequestPayload,
@@ -16,7 +18,7 @@ type ResponseHandler = {
 }
 
 export class UIClient {
-  private static readonly instances: Map<number, UIClient> = new Map<number, UIClient>()
+  private static instance: UIClient | null = null
 
   private ws!: WebSocket
   private responseHandlers: Map<string, ResponseHandler>
@@ -26,16 +28,17 @@ export class UIClient {
     this.responseHandlers = new Map<string, ResponseHandler>()
   }
 
-  public static getInstance(
-    serverId: number,
-    uiServerConfiguration?: UIServerConfigurationSection
-  ): UIClient {
-    if (!UIClient.instances.has(serverId) && uiServerConfiguration != null) {
-      UIClient.instances.set(serverId, new UIClient(uiServerConfiguration))
-    } else if (!UIClient.instances.has(serverId)) {
-      throw new Error(`UI client instance not found for server id: ${serverId}`)
+  public static getInstance(uiServerConfiguration: UIServerConfigurationSection): UIClient {
+    if (UIClient.instance === null) {
+      UIClient.instance = new UIClient(uiServerConfiguration)
     }
-    return UIClient.instances.get(serverId)!
+    return UIClient.instance
+  }
+
+  public setConfiguration(uiServerConfiguration: UIServerConfigurationSection): void {
+    this.ws.close()
+    this.uiServerConfiguration = uiServerConfiguration
+    this.openWS()
   }
 
   public registerWSEventListener<K extends keyof WebSocketEventMap>(
@@ -63,9 +66,14 @@ export class UIClient {
 
   public async addChargingStations(
     template: string,
-    numberOfStations: number
+    numberOfStations: number,
+    options?: ChargingStationOptions
   ): Promise<ResponsePayload> {
-    return this.sendRequest(ProcedureName.ADD_CHARGING_STATIONS, { template, numberOfStations })
+    return this.sendRequest(ProcedureName.ADD_CHARGING_STATIONS, {
+      template,
+      numberOfStations,
+      options
+    })
   }
 
   public async deleteChargingStation(hashId: string): Promise<ResponsePayload> {
@@ -154,15 +162,21 @@ export class UIClient {
       `${this.uiServerConfiguration.secure === true ? ApplicationProtocol.WSS : ApplicationProtocol.WS}://${this.uiServerConfiguration.host}:${this.uiServerConfiguration.port}`,
       protocols
     )
-    this.ws.onopen = openEvent => {
-      console.info('WebSocket opened', openEvent)
+    this.ws.onopen = () => {
+      useToast().success(
+        `WebSocket to UI server '${this.uiServerConfiguration.host}' successfully opened`
+      )
     }
     this.ws.onmessage = this.responseHandler.bind(this)
     this.ws.onerror = errorEvent => {
-      console.error('WebSocket error: ', errorEvent)
+      useToast().error(`Error in WebSocket to UI server '${this.uiServerConfiguration.host}'`)
+      console.error(
+        `Error in WebSocket to UI server '${this.uiServerConfiguration.host}'`,
+        errorEvent
+      )
     }
-    this.ws.onclose = closeEvent => {
-      console.info('WebSocket closed: ', closeEvent)
+    this.ws.onclose = () => {
+      useToast().info(`WebSocket to UI server '${this.uiServerConfiguration.host}' closed`)
     }
   }
 
@@ -212,8 +226,10 @@ export class UIClient {
           reject(responsePayload)
           break
         default:
-          console.error(
-            `Response status for procedure '${procedureName}' not supported: '${responsePayload.status}'`
+          reject(
+            new Error(
+              `Response status for procedure '${procedureName}' not supported: '${responsePayload.status}'`
+            )
           )
       }
       this.responseHandlers.delete(uuid)