X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=ui%2Fweb%2Fsrc%2Fcomposables%2FUIClient.ts;h=bbb4a4499b233a6aead4703eaf1b3de93004e32e;hb=3d20f4db7cad24703c7acac80f01ab4c6908ce58;hp=7c0c06c121be0da21190687cc2ea752feb197fd3;hpb=12f26d4a81773cf8ede8bad4255e36aadf10bce0;p=e-mobility-charging-stations-simulator.git diff --git a/ui/web/src/composables/UIClient.ts b/ui/web/src/composables/UIClient.ts index 7c0c06c1..bbb4a449 100644 --- a/ui/web/src/composables/UIClient.ts +++ b/ui/web/src/composables/UIClient.ts @@ -1,21 +1,21 @@ -import Utils from './Utils'; +import { promiseWithTimeout } from './Utils'; import { ProcedureName, type ProtocolResponse, type RequestPayload, type ResponsePayload, ResponseStatus, -} from '@/types/UIProtocol'; +} from '@/types'; import config from '@/assets/config'; type ResponseHandler = { procedureName: ProcedureName; resolve: (value: ResponsePayload | PromiseLike) => void; - reject: (reason?: any) => void; + reject: (reason?: unknown) => void; }; -export default class UIClient { - private static _instance: UIClient | null = null; +export class UIClient { + private static instance: UIClient | null = null; private ws!: WebSocket; private responseHandlers: Map; @@ -26,10 +26,10 @@ export default class UIClient { } public static getInstance() { - if (UIClient._instance === null) { - UIClient._instance = new UIClient(); + if (UIClient.instance === null) { + UIClient.instance = new UIClient(); } - return UIClient._instance; + return UIClient.instance; } public registerWSonOpenListener(listener: (event: Event) => void) { @@ -71,7 +71,7 @@ export default class UIClient { public async startTransaction( hashId: string, connectorId: number, - idTag: string | undefined + idTag: string | undefined, ): Promise { return this.sendRequest(ProcedureName.START_TRANSACTION, { hashIds: [hashId], @@ -82,7 +82,7 @@ export default class UIClient { public async stopTransaction( hashId: string, - transactionId: number | undefined + transactionId: number | undefined, ): Promise { return this.sendRequest(ProcedureName.STOP_TRANSACTION, { hashIds: [hashId], @@ -92,7 +92,7 @@ export default class UIClient { public async startAutomaticTransactionGenerator( hashId: string, - connectorId: number + connectorId: number, ): Promise { return this.sendRequest(ProcedureName.START_AUTOMATIC_TRANSACTION_GENERATOR, { hashIds: [hashId], @@ -102,7 +102,7 @@ export default class UIClient { public async stopAutomaticTransactionGenerator( hashId: string, - connectorId: number + connectorId: number, ): Promise { return this.sendRequest(ProcedureName.STOP_AUTOMATIC_TRANSACTION_GENERATOR, { hashIds: [hashId], @@ -113,7 +113,7 @@ export default class UIClient { private openWS(): void { this.ws = new WebSocket( `ws://${config.uiServer.host}:${config.uiServer.port}`, - config.uiServer.protocol + config.uiServer.protocol, ); this.ws.onmessage = this.responseHandler.bind(this); this.ws.onerror = (errorEvent) => { @@ -128,7 +128,7 @@ export default class UIClient { id: string, procedureName: ProcedureName, resolve: (value: ResponsePayload | PromiseLike) => void, - reject: (reason?: any) => void + reject: (reason?: unknown) => void, ): void { this.responseHandlers.set(id, { procedureName, resolve, reject }); } @@ -143,11 +143,11 @@ export default class UIClient { private async sendRequest( command: ProcedureName, - data: RequestPayload + data: RequestPayload, ): Promise { let uuid: string; - return Utils.promiseWithTimeout( - new Promise((resolve, reject) => { + return promiseWithTimeout( + new Promise((resolve, reject) => { uuid = crypto.randomUUID(); const msg = JSON.stringify([uuid, command, data]); @@ -166,7 +166,7 @@ export default class UIClient { Error(`Send request '${command}' message timeout`), () => { this.responseHandlers.delete(uuid); - } + }, ); } @@ -174,7 +174,7 @@ export default class UIClient { const response = JSON.parse(messageEvent.data) as ProtocolResponse; if (Array.isArray(response) === false) { - throw new Error(`Response not an array: ${JSON.stringify(response, null, 2)}`); + throw new Error(`Response not an array: ${JSON.stringify(response, undefined, 2)}`); } const [uuid, responsePayload] = response; @@ -192,7 +192,7 @@ export default class UIClient { } this.deleteResponseHandler(uuid); } else { - throw new Error(`Not a response to a request: ${JSON.stringify(response, null, 2)}`); + throw new Error(`Not a response to a request: ${JSON.stringify(response, undefined, 2)}`); } } }