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