Align 'started' attribute usage to all classes
[e-mobility-charging-stations-simulator.git] / src / ui / web / src / composables / UIClient.ts
index 0c30494dd95ff8f2bdec8dcd61d13d1746cc2017..556e9bb7c4e4b458ecfaaa4499a6f32d9f9e713c 100644 (file)
@@ -1,5 +1,4 @@
-import type { JsonType } from '@/types/JsonType';
-import { ProcedureName, ResponseStatus } from '@/types/UIProtocol';
+import { ProcedureName, ResponseStatus, type RequestPayload } from '@/types/UIProtocol';
 import type { ProtocolResponse, ResponsePayload } from '@/types/UIProtocol';
 
 import Utils from './Utils';
@@ -30,7 +29,7 @@ export default class UIClient {
     return UIClient._instance;
   }
 
-  public registerWSonOpenListener(listener: (this: WebSocket, ev: Event) => void) {
+  public registerWSonOpenListener(listener: (event: Event) => void) {
     this._ws.addEventListener('open', listener);
   }
 
@@ -47,22 +46,22 @@ export default class UIClient {
   }
 
   public async startChargingStation(hashId: string): Promise<ResponsePayload> {
-    return this.sendRequest(ProcedureName.START_CHARGING_STATION, { hashId });
+    return this.sendRequest(ProcedureName.START_CHARGING_STATION, { hashIds: [hashId] });
   }
 
   public async stopChargingStation(hashId: string): Promise<ResponsePayload> {
-    return this.sendRequest(ProcedureName.STOP_CHARGING_STATION, { hashId });
+    return this.sendRequest(ProcedureName.STOP_CHARGING_STATION, { hashIds: [hashId] });
   }
 
   public async openConnection(hashId: string): Promise<ResponsePayload> {
     return this.sendRequest(ProcedureName.OPEN_CONNECTION, {
-      hashId,
+      hashIds: [hashId],
     });
   }
 
   public async closeConnection(hashId: string): Promise<ResponsePayload> {
     return this.sendRequest(ProcedureName.CLOSE_CONNECTION, {
-      hashId,
+      hashIds: [hashId],
     });
   }
 
@@ -72,7 +71,7 @@ export default class UIClient {
     idTag: string | undefined
   ): Promise<ResponsePayload> {
     return this.sendRequest(ProcedureName.START_TRANSACTION, {
-      hashId,
+      hashIds: [hashId],
       connectorId,
       idTag,
     });
@@ -83,19 +82,42 @@ export default class UIClient {
     transactionId: number | undefined
   ): Promise<ResponsePayload> {
     return this.sendRequest(ProcedureName.STOP_TRANSACTION, {
-      hashId,
+      hashIds: [hashId],
       transactionId,
     });
   }
 
+  public async startAutomaticTransactionGenerator(
+    hashId: string,
+    connectorId: number
+  ): Promise<ResponsePayload> {
+    return this.sendRequest(ProcedureName.START_AUTOMATIC_TRANSACTION_GENERATOR, {
+      hashIds: [hashId],
+      connectorIds: [connectorId],
+    });
+  }
+
+  public async stopAutomaticTransactionGenerator(
+    hashId: string,
+    connectorId: number
+  ): Promise<ResponsePayload> {
+    return this.sendRequest(ProcedureName.STOP_AUTOMATIC_TRANSACTION_GENERATOR, {
+      hashIds: [hashId],
+      connectorIds: [connectorId],
+    });
+  }
+
   private openWS(): void {
     this._ws = new WebSocket(
-      `ws://${config.emobility.host}:${config.emobility.port}`,
-      config.emobility.protocol
+      `ws://${config.uiServer.host}:${config.uiServer.port}`,
+      config.uiServer.protocol
     );
     this._ws.onmessage = this.responseHandler.bind(this);
-    this._ws.onerror = (error) => {
-      console.error('WebSocket error: ', error);
+    this._ws.onerror = (errorEvent) => {
+      console.error('WebSocket error: ', errorEvent);
+    };
+    this._ws.onclose = (closeEvent) => {
+      console.info('WebSocket close: ', closeEvent);
     };
   }
 
@@ -105,7 +127,7 @@ export default class UIClient {
     resolve: (value: ResponsePayload | PromiseLike<ResponsePayload>) => void,
     reject: (reason?: any) => void
   ): void {
-    this._responseHandlers.set(id, { resolve, reject, procedureName });
+    this._responseHandlers.set(id, { procedureName, resolve, reject });
   }
 
   private getResponseHandler(id: string): ResponseHandler | undefined {
@@ -116,7 +138,10 @@ export default class UIClient {
     return this._responseHandlers.delete(id);
   }
 
-  private async sendRequest(command: ProcedureName, data: JsonType): Promise<ResponsePayload> {
+  private async sendRequest(
+    command: ProcedureName,
+    data: RequestPayload
+  ): Promise<ResponsePayload> {
     let uuid: string;
     return Utils.promiseWithTimeout(
       new Promise((resolve, reject) => {