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