Commit | Line | Data |
---|---|---|
0f71040c | 1 | import { |
217db058 | 2 | ApplicationProtocol, |
329eab0e | 3 | AuthenticationType, |
093ca832 | 4 | type ChargingStationOptions, |
0f71040c | 5 | ProcedureName, |
82a77234 | 6 | type ProtocolResponse, |
0f71040c JB |
7 | type RequestPayload, |
8 | type ResponsePayload, | |
f292861c JB |
9 | ResponseStatus, |
10 | type UIServerConfigurationSection | |
66a7748d | 11 | } from '@/types' |
32de5a57 LM |
12 | |
13 | type ResponseHandler = { | |
66a7748d JB |
14 | procedureName: ProcedureName |
15 | resolve: (value: ResponsePayload | PromiseLike<ResponsePayload>) => void | |
16 | reject: (reason?: unknown) => void | |
17 | } | |
32de5a57 | 18 | |
8137295e | 19 | export class UIClient { |
c25a4046 | 20 | private static instance: UIClient | null = null |
32de5a57 | 21 | |
66a7748d JB |
22 | private ws!: WebSocket |
23 | private responseHandlers: Map<string, ResponseHandler> | |
32de5a57 | 24 | |
f292861c | 25 | private constructor(private uiServerConfiguration: UIServerConfigurationSection) { |
66a7748d JB |
26 | this.openWS() |
27 | this.responseHandlers = new Map<string, ResponseHandler>() | |
32de5a57 LM |
28 | } |
29 | ||
c25a4046 JB |
30 | public static getInstance(uiServerConfiguration: UIServerConfigurationSection): UIClient { |
31 | if (UIClient.instance === null) { | |
32 | UIClient.instance = new UIClient(uiServerConfiguration) | |
32de5a57 | 33 | } |
c25a4046 | 34 | return UIClient.instance |
32de5a57 LM |
35 | } |
36 | ||
a4baab63 JB |
37 | public setConfiguration(uiServerConfiguration: UIServerConfigurationSection): void { |
38 | this.ws.close() | |
39 | this.uiServerConfiguration = uiServerConfiguration | |
40 | this.openWS() | |
41 | } | |
42 | ||
ca1e5439 JB |
43 | public registerWSEventListener<K extends keyof WebSocketEventMap>( |
44 | event: K, | |
45 | listener: (event: WebSocketEventMap[K]) => void | |
46 | ) { | |
47 | this.ws.addEventListener(event, listener) | |
32de5a57 LM |
48 | } |
49 | ||
5a010bf0 | 50 | public async startSimulator(): Promise<ResponsePayload> { |
66a7748d | 51 | return this.sendRequest(ProcedureName.START_SIMULATOR, {}) |
5a010bf0 JB |
52 | } |
53 | ||
54 | public async stopSimulator(): Promise<ResponsePayload> { | |
66a7748d | 55 | return this.sendRequest(ProcedureName.STOP_SIMULATOR, {}) |
5a010bf0 | 56 | } |
32de5a57 | 57 | |
c317ae3e JB |
58 | public async listTemplates(): Promise<ResponsePayload> { |
59 | return this.sendRequest(ProcedureName.LIST_TEMPLATES, {}) | |
a64b9a64 JB |
60 | } |
61 | ||
5a010bf0 | 62 | public async listChargingStations(): Promise<ResponsePayload> { |
66a7748d | 63 | return this.sendRequest(ProcedureName.LIST_CHARGING_STATIONS, {}) |
32de5a57 LM |
64 | } |
65 | ||
c317ae3e JB |
66 | public async addChargingStations( |
67 | template: string, | |
093ca832 JB |
68 | numberOfStations: number, |
69 | options?: ChargingStationOptions | |
c317ae3e | 70 | ): Promise<ResponsePayload> { |
093ca832 JB |
71 | return this.sendRequest(ProcedureName.ADD_CHARGING_STATIONS, { |
72 | template, | |
73 | numberOfStations, | |
74 | options | |
75 | }) | |
c317ae3e JB |
76 | } |
77 | ||
78 | public async deleteChargingStation(hashId: string): Promise<ResponsePayload> { | |
79 | return this.sendRequest(ProcedureName.DELETE_CHARGING_STATIONS, { hashIds: [hashId] }) | |
80 | } | |
81 | ||
f8696170 JB |
82 | public async setSupervisionUrl(hashId: string, supervisionUrl: string): Promise<ResponsePayload> { |
83 | return this.sendRequest(ProcedureName.SET_SUPERVISION_URL, { | |
84 | hashIds: [hashId], | |
85 | url: supervisionUrl | |
86 | }) | |
87 | } | |
88 | ||
8fc2e5cc | 89 | public async startChargingStation(hashId: string): Promise<ResponsePayload> { |
66a7748d | 90 | return this.sendRequest(ProcedureName.START_CHARGING_STATION, { hashIds: [hashId] }) |
8fc2e5cc JB |
91 | } |
92 | ||
93 | public async stopChargingStation(hashId: string): Promise<ResponsePayload> { | |
66a7748d | 94 | return this.sendRequest(ProcedureName.STOP_CHARGING_STATION, { hashIds: [hashId] }) |
8fc2e5cc JB |
95 | } |
96 | ||
97 | public async openConnection(hashId: string): Promise<ResponsePayload> { | |
98 | return this.sendRequest(ProcedureName.OPEN_CONNECTION, { | |
a974c8e4 | 99 | hashIds: [hashId] |
66a7748d | 100 | }) |
8fc2e5cc JB |
101 | } |
102 | ||
103 | public async closeConnection(hashId: string): Promise<ResponsePayload> { | |
104 | return this.sendRequest(ProcedureName.CLOSE_CONNECTION, { | |
a974c8e4 | 105 | hashIds: [hashId] |
66a7748d | 106 | }) |
8fc2e5cc JB |
107 | } |
108 | ||
32de5a57 LM |
109 | public async startTransaction( |
110 | hashId: string, | |
111 | connectorId: number, | |
66a7748d | 112 | idTag: string | undefined |
32de5a57 | 113 | ): Promise<ResponsePayload> { |
32de5a57 | 114 | return this.sendRequest(ProcedureName.START_TRANSACTION, { |
757b2ecf | 115 | hashIds: [hashId], |
32de5a57 | 116 | connectorId, |
a974c8e4 | 117 | idTag |
66a7748d | 118 | }) |
32de5a57 LM |
119 | } |
120 | ||
5a010bf0 JB |
121 | public async stopTransaction( |
122 | hashId: string, | |
66a7748d | 123 | transactionId: number | undefined |
5a010bf0 | 124 | ): Promise<ResponsePayload> { |
32de5a57 | 125 | return this.sendRequest(ProcedureName.STOP_TRANSACTION, { |
757b2ecf | 126 | hashIds: [hashId], |
a974c8e4 | 127 | transactionId |
66a7748d | 128 | }) |
32de5a57 LM |
129 | } |
130 | ||
757b2ecf JB |
131 | public async startAutomaticTransactionGenerator( |
132 | hashId: string, | |
66a7748d | 133 | connectorId: number |
757b2ecf JB |
134 | ): Promise<ResponsePayload> { |
135 | return this.sendRequest(ProcedureName.START_AUTOMATIC_TRANSACTION_GENERATOR, { | |
136 | hashIds: [hashId], | |
a974c8e4 | 137 | connectorIds: [connectorId] |
66a7748d | 138 | }) |
757b2ecf JB |
139 | } |
140 | ||
141 | public async stopAutomaticTransactionGenerator( | |
142 | hashId: string, | |
66a7748d | 143 | connectorId: number |
757b2ecf JB |
144 | ): Promise<ResponsePayload> { |
145 | return this.sendRequest(ProcedureName.STOP_AUTOMATIC_TRANSACTION_GENERATOR, { | |
146 | hashIds: [hashId], | |
a974c8e4 | 147 | connectorIds: [connectorId] |
66a7748d | 148 | }) |
757b2ecf JB |
149 | } |
150 | ||
9e1d6e03 | 151 | private openWS(): void { |
329eab0e | 152 | const protocols = |
f292861c JB |
153 | this.uiServerConfiguration.authentication?.enabled === true && |
154 | this.uiServerConfiguration.authentication?.type === AuthenticationType.PROTOCOL_BASIC_AUTH | |
329eab0e | 155 | ? [ |
f292861c JB |
156 | `${this.uiServerConfiguration.protocol}${this.uiServerConfiguration.version}`, |
157 | `authorization.basic.${btoa(`${this.uiServerConfiguration.authentication.username}:${this.uiServerConfiguration.authentication.password}`).replace(/={1,2}$/, '')}` | |
329eab0e | 158 | ] |
f292861c | 159 | : `${this.uiServerConfiguration.protocol}${this.uiServerConfiguration.version}` |
12f26d4a | 160 | this.ws = new WebSocket( |
f292861c | 161 | `${this.uiServerConfiguration.secure === true ? ApplicationProtocol.WSS : ApplicationProtocol.WS}://${this.uiServerConfiguration.host}:${this.uiServerConfiguration.port}`, |
329eab0e | 162 | protocols |
66a7748d | 163 | ) |
e2372e58 JB |
164 | this.ws.onopen = openEvent => { |
165 | console.info('WebSocket opened', openEvent) | |
166 | } | |
66a7748d | 167 | this.ws.onmessage = this.responseHandler.bind(this) |
a974c8e4 | 168 | this.ws.onerror = errorEvent => { |
66a7748d JB |
169 | console.error('WebSocket error: ', errorEvent) |
170 | } | |
a974c8e4 | 171 | this.ws.onclose = closeEvent => { |
66a7748d JB |
172 | console.info('WebSocket closed: ', closeEvent) |
173 | } | |
5a010bf0 JB |
174 | } |
175 | ||
757b2ecf | 176 | private async sendRequest( |
8d6f4790 JB |
177 | procedureName: ProcedureName, |
178 | payload: RequestPayload | |
757b2ecf | 179 | ): Promise<ResponsePayload> { |
5b2721db | 180 | return new Promise<ResponsePayload>((resolve, reject) => { |
1b2acf4e | 181 | if (this.ws.readyState === WebSocket.OPEN) { |
66a7748d | 182 | const uuid = crypto.randomUUID() |
8d6f4790 | 183 | const msg = JSON.stringify([uuid, procedureName, payload]) |
1a32c36b | 184 | const sendTimeout = setTimeout(() => { |
8d6f4790 | 185 | this.responseHandlers.delete(uuid) |
e2372e58 | 186 | return reject(new Error(`Send request '${procedureName}' message: connection timeout`)) |
9d76f5ec | 187 | }, 60000) |
1a32c36b | 188 | try { |
66a7748d | 189 | this.ws.send(msg) |
8d6f4790 | 190 | this.responseHandlers.set(uuid, { procedureName, resolve, reject }) |
1a32c36b | 191 | } catch (error) { |
8d6f4790 | 192 | this.responseHandlers.delete(uuid) |
66a7748d | 193 | reject(error) |
1a32c36b | 194 | } finally { |
66a7748d | 195 | clearTimeout(sendTimeout) |
1a32c36b | 196 | } |
1b2acf4e | 197 | } else { |
e2372e58 | 198 | reject(new Error(`Send request '${procedureName}' message: connection closed`)) |
1b2acf4e | 199 | } |
66a7748d | 200 | }) |
32de5a57 LM |
201 | } |
202 | ||
97f0a1a5 | 203 | private responseHandler(messageEvent: MessageEvent<string>): void { |
66a7748d | 204 | const response = JSON.parse(messageEvent.data) as ProtocolResponse |
32de5a57 | 205 | |
5e3cb728 | 206 | if (Array.isArray(response) === false) { |
66a7748d | 207 | throw new Error(`Response not an array: ${JSON.stringify(response, undefined, 2)}`) |
32de5a57 LM |
208 | } |
209 | ||
66a7748d | 210 | const [uuid, responsePayload] = response |
32de5a57 | 211 | |
12f26d4a | 212 | if (this.responseHandlers.has(uuid) === true) { |
8d6f4790 | 213 | const { procedureName, resolve, reject } = this.responseHandlers.get(uuid)! |
5e3cb728 | 214 | switch (responsePayload.status) { |
32de5a57 | 215 | case ResponseStatus.SUCCESS: |
66a7748d JB |
216 | resolve(responsePayload) |
217 | break | |
32de5a57 | 218 | case ResponseStatus.FAILURE: |
66a7748d JB |
219 | reject(responsePayload) |
220 | break | |
32de5a57 | 221 | default: |
7c1d037f JB |
222 | reject( |
223 | new Error( | |
224 | `Response status for procedure '${procedureName}' not supported: '${responsePayload.status}'` | |
225 | ) | |
66a7748d | 226 | ) |
32de5a57 | 227 | } |
8d6f4790 | 228 | this.responseHandlers.delete(uuid) |
32de5a57 | 229 | } else { |
66a7748d | 230 | throw new Error(`Not a response to a request: ${JSON.stringify(response, undefined, 2)}`) |
32de5a57 LM |
231 | } |
232 | } | |
233 | } |