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