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