Refine sonar-project.properties
[e-mobility-charging-stations-simulator.git] / src / ui / web / src / composables / UIClient.ts
index 275e30e9f8519111cc726e6e171558e900d6048b..cf9894009d8844b5f4879d44d899404052f0db1a 100644 (file)
@@ -1,10 +1,8 @@
-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';
 import config from '@/assets/config';
-import { v4 as uuidv4 } from 'uuid';
 
 type ResponseHandler = {
   procedureName: ProcedureName;
@@ -47,22 +45,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 +70,7 @@ export default class UIClient {
     idTag: string | undefined
   ): Promise<ResponsePayload> {
     return this.sendRequest(ProcedureName.START_TRANSACTION, {
-      hashId,
+      hashIds: [hashId],
       connectorId,
       idTag,
     });
@@ -83,11 +81,31 @@ 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.uiServer.host}:${config.uiServer.port}`,
@@ -98,7 +116,7 @@ export default class UIClient {
       console.error('WebSocket error: ', errorEvent);
     };
     this._ws.onclose = (closeEvent) => {
-      console.info('WebSocket close: ', closeEvent);
+      console.info('WebSocket closed: ', closeEvent);
     };
   }
 
@@ -119,11 +137,14 @@ 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) => {
-        uuid = uuidv4();
+        uuid = crypto.randomUUID();
         const msg = JSON.stringify([uuid, command, data]);
 
         if (this._ws.readyState !== WebSocket.OPEN) {
@@ -132,13 +153,13 @@ export default class UIClient {
         if (this._ws.readyState === WebSocket.OPEN) {
           this._ws.send(msg);
         } else {
-          throw new Error(`Send request ${command} message: connection not opened`);
+          throw new Error(`Send request '${command}' message: connection not opened`);
         }
 
         this.setResponseHandler(uuid, command, resolve, reject);
       }),
-      60 * 1000,
-      Error(`Send request ${command} message timeout`),
+      120 * 1000,
+      Error(`Send request '${command}' message timeout`),
       () => {
         this._responseHandlers.delete(uuid);
       }
@@ -146,28 +167,28 @@ export default class UIClient {
   }
 
   private responseHandler(messageEvent: MessageEvent<string>): void {
-    const data = JSON.parse(messageEvent.data) as ProtocolResponse;
+    const response = JSON.parse(messageEvent.data) as ProtocolResponse;
 
-    if (Array.isArray(data) === false) {
-      throw new Error('Response not an array: ' + JSON.stringify(data, null, 2));
+    if (Array.isArray(response) === false) {
+      throw new Error('Response not an array: ' + JSON.stringify(response, null, 2));
     }
 
-    const [uuid, response] = data;
+    const [uuid, responsePayload] = response;
 
     if (this._responseHandlers.has(uuid) === true) {
-      switch (response.status) {
+      switch (responsePayload.status) {
         case ResponseStatus.SUCCESS:
-          this.getResponseHandler(uuid)?.resolve(response);
+          this.getResponseHandler(uuid)?.resolve(responsePayload);
           break;
         case ResponseStatus.FAILURE:
-          this.getResponseHandler(uuid)?.reject(response);
+          this.getResponseHandler(uuid)?.reject(responsePayload);
           break;
         default:
-          console.error(`Response status not supported: ${response.status}`);
+          console.error(`Response status not supported: ${responsePayload.status}`);
       }
       this.deleteResponseHandler(uuid);
     } else {
-      throw new Error('Not a response to a request: ' + JSON.stringify(data, null, 2));
+      throw new Error('Not a response to a request: ' + JSON.stringify(response, null, 2));
     }
   }
 }