8 import config from
'@/assets/config';
10 type ResponseHandler
= {
11 procedureName
: ProcedureName
;
12 resolve
: (value
: ResponsePayload
| PromiseLike
<ResponsePayload
>) => void;
13 reject
: (reason
?: unknown
) => void;
16 export class UIClient
{
17 private static instance
: UIClient
| null = null;
19 private ws
!: WebSocket
;
20 private responseHandlers
: Map
<string, ResponseHandler
>;
22 private constructor() {
24 this.responseHandlers
= new Map
<string, ResponseHandler
>();
27 public static getInstance() {
28 if (UIClient
.instance
=== null) {
29 UIClient
.instance
= new UIClient();
31 return UIClient
.instance
;
34 public registerWSonOpenListener(listener
: (event
: Event
) => void) {
35 this.ws
.addEventListener('open', listener
);
38 public async startSimulator(): Promise
<ResponsePayload
> {
39 return this.sendRequest(ProcedureName
.START_SIMULATOR
, {});
42 public async stopSimulator(): Promise
<ResponsePayload
> {
43 return this.sendRequest(ProcedureName
.STOP_SIMULATOR
, {});
46 public async listChargingStations(): Promise
<ResponsePayload
> {
47 return this.sendRequest(ProcedureName
.LIST_CHARGING_STATIONS
, {});
50 public async startChargingStation(hashId
: string): Promise
<ResponsePayload
> {
51 return this.sendRequest(ProcedureName
.START_CHARGING_STATION
, { hashIds
: [hashId
] });
54 public async stopChargingStation(hashId
: string): Promise
<ResponsePayload
> {
55 return this.sendRequest(ProcedureName
.STOP_CHARGING_STATION
, { hashIds
: [hashId
] });
58 public async openConnection(hashId
: string): Promise
<ResponsePayload
> {
59 return this.sendRequest(ProcedureName
.OPEN_CONNECTION
, {
64 public async closeConnection(hashId
: string): Promise
<ResponsePayload
> {
65 return this.sendRequest(ProcedureName
.CLOSE_CONNECTION
, {
70 public async startTransaction(
73 idTag
: string | undefined,
74 ): Promise
<ResponsePayload
> {
75 return this.sendRequest(ProcedureName
.START_TRANSACTION
, {
82 public async stopTransaction(
84 transactionId
: number | undefined,
85 ): Promise
<ResponsePayload
> {
86 return this.sendRequest(ProcedureName
.STOP_TRANSACTION
, {
92 public async startAutomaticTransactionGenerator(
95 ): Promise
<ResponsePayload
> {
96 return this.sendRequest(ProcedureName
.START_AUTOMATIC_TRANSACTION_GENERATOR
, {
98 connectorIds
: [connectorId
],
102 public async stopAutomaticTransactionGenerator(
105 ): Promise
<ResponsePayload
> {
106 return this.sendRequest(ProcedureName
.STOP_AUTOMATIC_TRANSACTION_GENERATOR
, {
108 connectorIds
: [connectorId
],
112 private openWS(): void {
113 this.ws
= new WebSocket(
114 `ws://${config.uiServer.host}:${config.uiServer.port}`,
115 config
.uiServer
.protocol
,
117 this.ws
.onmessage
= this.responseHandler
.bind(this);
118 this.ws
.onerror
= (errorEvent
) => {
119 console
.error('WebSocket error: ', errorEvent
);
121 this.ws
.onclose
= (closeEvent
) => {
122 console
.info('WebSocket closed: ', closeEvent
);
126 private setResponseHandler(
128 procedureName
: ProcedureName
,
129 resolve
: (value
: ResponsePayload
| PromiseLike
<ResponsePayload
>) => void,
130 reject
: (reason
?: unknown
) => void,
132 this.responseHandlers
.set(id
, { procedureName
, resolve
, reject
});
135 private getResponseHandler(id
: string): ResponseHandler
| undefined {
136 return this.responseHandlers
.get(id
);
139 private deleteResponseHandler(id
: string): boolean {
140 return this.responseHandlers
.delete(id
);
143 private async sendRequest(
144 command
: ProcedureName
,
145 data
: RequestPayload
,
146 ): Promise
<ResponsePayload
> {
148 return await new Promise
<ResponsePayload
>((resolve
, reject
) => {
149 uuid
= crypto
.randomUUID();
150 const msg
= JSON
.stringify([uuid
, command
, data
]);
152 if (this.ws
.readyState
!== WebSocket
.OPEN
) {
155 if (this.ws
.readyState
=== WebSocket
.OPEN
) {
157 this.deleteResponseHandler(uuid
);
158 return reject(new Error(`Send request '${command}' message timeout`));
162 throw new Error(`Send request '${command}' message: connection not opened`);
165 this.setResponseHandler(uuid
, command
, resolve
, reject
);
169 private responseHandler(messageEvent
: MessageEvent
<string>): void {
170 const response
= JSON
.parse(messageEvent
.data
) as ProtocolResponse
;
172 if (Array.isArray(response
) === false) {
173 throw new Error(`Response not an array: ${JSON.stringify(response, undefined, 2)}`);
176 const [uuid
, responsePayload
] = response
;
178 if (this.responseHandlers
.has(uuid
) === true) {
179 switch (responsePayload
.status) {
180 case ResponseStatus
.SUCCESS
:
181 this.getResponseHandler(uuid
)?.resolve(responsePayload
);
183 case ResponseStatus
.FAILURE
:
184 this.getResponseHandler(uuid
)?.reject(responsePayload
);
187 console
.error(`Response status not supported: ${responsePayload.status}`);
189 this.deleteResponseHandler(uuid
);
191 throw new Error(`Not a response to a request: ${JSON.stringify(response, undefined, 2)}`);