Commit | Line | Data |
---|---|---|
f27eb751 JB |
1 | import type { JsonType } from '@/types/JsonType'; |
2 | import { ProcedureName, ResponseStatus } from '@/types/UIProtocol'; | |
3 | import type { ProtocolResponse, ResponsePayload } from '@/types/UIProtocol'; | |
6c1761d4 | 4 | |
32de5a57 LM |
5 | import Utils from './Utils'; |
6 | import config from '@/assets/config'; | |
7 | import { v4 as uuidv4 } from 'uuid'; | |
8 | ||
9 | type ResponseHandler = { | |
1f7fa4de | 10 | procedureName: ProcedureName; |
32de5a57 LM |
11 | resolve: (value: ResponsePayload | PromiseLike<ResponsePayload>) => void; |
12 | reject: (reason?: any) => void; | |
32de5a57 LM |
13 | }; |
14 | ||
15 | export default class UIClient { | |
16 | private static _instance: UIClient | null = null; | |
17 | ||
5a010bf0 | 18 | private _ws!: WebSocket; |
32de5a57 LM |
19 | private _responseHandlers: Map<string, ResponseHandler>; |
20 | ||
21 | private constructor() { | |
5a010bf0 | 22 | this.openWS(); |
32de5a57 | 23 | this._responseHandlers = new Map<string, ResponseHandler>(); |
32de5a57 LM |
24 | } |
25 | ||
f27eb751 | 26 | public static getInstance() { |
32de5a57 LM |
27 | if (UIClient._instance === null) { |
28 | UIClient._instance = new UIClient(); | |
29 | } | |
30 | return UIClient._instance; | |
31 | } | |
32 | ||
17bfa1b6 | 33 | public registerWSonOpenListener(listener: (event: Event) => void) { |
32de5a57 LM |
34 | this._ws.addEventListener('open', listener); |
35 | } | |
36 | ||
5a010bf0 JB |
37 | public async startSimulator(): Promise<ResponsePayload> { |
38 | return this.sendRequest(ProcedureName.START_SIMULATOR, {}); | |
39 | } | |
40 | ||
41 | public async stopSimulator(): Promise<ResponsePayload> { | |
42 | return this.sendRequest(ProcedureName.STOP_SIMULATOR, {}); | |
43 | } | |
32de5a57 | 44 | |
5a010bf0 | 45 | public async listChargingStations(): Promise<ResponsePayload> { |
32de5a57 LM |
46 | return this.sendRequest(ProcedureName.LIST_CHARGING_STATIONS, {}); |
47 | } | |
48 | ||
8fc2e5cc JB |
49 | public async startChargingStation(hashId: string): Promise<ResponsePayload> { |
50 | return this.sendRequest(ProcedureName.START_CHARGING_STATION, { hashId }); | |
51 | } | |
52 | ||
53 | public async stopChargingStation(hashId: string): Promise<ResponsePayload> { | |
54 | return this.sendRequest(ProcedureName.STOP_CHARGING_STATION, { hashId }); | |
55 | } | |
56 | ||
57 | public async openConnection(hashId: string): Promise<ResponsePayload> { | |
58 | return this.sendRequest(ProcedureName.OPEN_CONNECTION, { | |
59 | hashId, | |
60 | }); | |
61 | } | |
62 | ||
63 | public async closeConnection(hashId: string): Promise<ResponsePayload> { | |
64 | return this.sendRequest(ProcedureName.CLOSE_CONNECTION, { | |
65 | hashId, | |
66 | }); | |
67 | } | |
68 | ||
32de5a57 LM |
69 | public async startTransaction( |
70 | hashId: string, | |
71 | connectorId: number, | |
5a010bf0 | 72 | idTag: string | undefined |
32de5a57 | 73 | ): Promise<ResponsePayload> { |
32de5a57 LM |
74 | return this.sendRequest(ProcedureName.START_TRANSACTION, { |
75 | hashId, | |
76 | connectorId, | |
77 | idTag, | |
78 | }); | |
79 | } | |
80 | ||
5a010bf0 JB |
81 | public async stopTransaction( |
82 | hashId: string, | |
83 | transactionId: number | undefined | |
84 | ): Promise<ResponsePayload> { | |
32de5a57 LM |
85 | return this.sendRequest(ProcedureName.STOP_TRANSACTION, { |
86 | hashId, | |
87 | transactionId, | |
88 | }); | |
89 | } | |
90 | ||
5a010bf0 JB |
91 | private openWS(): void { |
92 | this._ws = new WebSocket( | |
6a619cce JB |
93 | `ws://${config.uiServer.host}:${config.uiServer.port}`, |
94 | config.uiServer.protocol | |
5a010bf0 JB |
95 | ); |
96 | this._ws.onmessage = this.responseHandler.bind(this); | |
6a619cce JB |
97 | this._ws.onerror = (errorEvent) => { |
98 | console.error('WebSocket error: ', errorEvent); | |
99 | }; | |
100 | this._ws.onclose = (closeEvent) => { | |
101 | console.info('WebSocket close: ', closeEvent); | |
a745e412 | 102 | }; |
5a010bf0 JB |
103 | } |
104 | ||
32de5a57 LM |
105 | private setResponseHandler( |
106 | id: string, | |
1f7fa4de | 107 | procedureName: ProcedureName, |
32de5a57 | 108 | resolve: (value: ResponsePayload | PromiseLike<ResponsePayload>) => void, |
1f7fa4de | 109 | reject: (reason?: any) => void |
32de5a57 | 110 | ): void { |
5cf9050d | 111 | this._responseHandlers.set(id, { procedureName, resolve, reject }); |
32de5a57 LM |
112 | } |
113 | ||
114 | private getResponseHandler(id: string): ResponseHandler | undefined { | |
115 | return this._responseHandlers.get(id); | |
116 | } | |
117 | ||
1f7fa4de JB |
118 | private deleteResponseHandler(id: string): boolean { |
119 | return this._responseHandlers.delete(id); | |
120 | } | |
121 | ||
32de5a57 LM |
122 | private async sendRequest(command: ProcedureName, data: JsonType): Promise<ResponsePayload> { |
123 | let uuid: string; | |
124 | return Utils.promiseWithTimeout( | |
125 | new Promise((resolve, reject) => { | |
126 | uuid = uuidv4(); | |
127 | const msg = JSON.stringify([uuid, command, data]); | |
128 | ||
5a010bf0 JB |
129 | if (this._ws.readyState !== WebSocket.OPEN) { |
130 | this.openWS(); | |
131 | } | |
132 | if (this._ws.readyState === WebSocket.OPEN) { | |
32de5a57 LM |
133 | this._ws.send(msg); |
134 | } else { | |
135 | throw new Error(`Send request ${command} message: connection not opened`); | |
136 | } | |
137 | ||
1f7fa4de | 138 | this.setResponseHandler(uuid, command, resolve, reject); |
32de5a57 LM |
139 | }), |
140 | 60 * 1000, | |
141 | Error(`Send request ${command} message timeout`), | |
142 | () => { | |
143 | this._responseHandlers.delete(uuid); | |
144 | } | |
145 | ); | |
146 | } | |
147 | ||
97f0a1a5 | 148 | private responseHandler(messageEvent: MessageEvent<string>): void { |
32de5a57 LM |
149 | const data = JSON.parse(messageEvent.data) as ProtocolResponse; |
150 | ||
53e5fd67 JB |
151 | if (Array.isArray(data) === false) { |
152 | throw new Error('Response not an array: ' + JSON.stringify(data, null, 2)); | |
32de5a57 LM |
153 | } |
154 | ||
155 | const [uuid, response] = data; | |
156 | ||
157 | if (this._responseHandlers.has(uuid) === true) { | |
158 | switch (response.status) { | |
159 | case ResponseStatus.SUCCESS: | |
160 | this.getResponseHandler(uuid)?.resolve(response); | |
161 | break; | |
162 | case ResponseStatus.FAILURE: | |
163 | this.getResponseHandler(uuid)?.reject(response); | |
164 | break; | |
165 | default: | |
a745e412 | 166 | console.error(`Response status not supported: ${response.status}`); |
32de5a57 | 167 | } |
1f7fa4de | 168 | this.deleteResponseHandler(uuid); |
32de5a57 LM |
169 | } else { |
170 | throw new Error('Not a response to a request: ' + JSON.stringify(data, null, 2)); | |
171 | } | |
172 | } | |
173 | } |