From a745e4127ed71e21b50d0397cd8ef79bf59a7573 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Sat, 27 Aug 2022 11:26:40 +0200 Subject: [PATCH] UI server: logging and code refinements MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- ...charging-stations-simulator.code-workspace | 11 +++++++++++ .../ui-server/UIHttpServer.ts | 19 ++++++++++--------- src/ui/web/src/composable/UIClient.ts | 5 ++++- 3 files changed, 25 insertions(+), 10 deletions(-) create mode 100644 e-mobility-charging-stations-simulator.code-workspace diff --git a/e-mobility-charging-stations-simulator.code-workspace b/e-mobility-charging-stations-simulator.code-workspace new file mode 100644 index 00000000..e0dbb0cd --- /dev/null +++ b/e-mobility-charging-stations-simulator.code-workspace @@ -0,0 +1,11 @@ +{ + "folders": [ + { + "path": "." + }, + { + "path": "src/ui/web" + } + ], + "settings": {} +} \ No newline at end of file diff --git a/src/charging-station/ui-server/UIHttpServer.ts b/src/charging-station/ui-server/UIHttpServer.ts index 2d729bfc..a9b62063 100644 --- a/src/charging-station/ui-server/UIHttpServer.ts +++ b/src/charging-station/ui-server/UIHttpServer.ts @@ -7,6 +7,7 @@ import { ServerOptions } from '../../types/ConfigurationData'; import { ProcedureName, Protocol, + ProtocolRequest, ProtocolResponse, ProtocolVersion, RequestPayload, @@ -57,7 +58,7 @@ export default class UIHttpServer extends AbstractUIServer { this.responseHandlers.delete(uuid); } else { logger.error( - `${this.logPrefix()} ${moduleName}.sendResponse: Response received for unknown request: ${response}` + `${this.logPrefix()} ${moduleName}.sendResponse: Response for unknown request: ${response}` ); } } @@ -83,10 +84,7 @@ export default class UIHttpServer extends AbstractUIServer { } req.on('error', (error) => { logger.error( - `${this.logPrefix( - moduleName, - 'requestListener.req.onerror' - )} Error at incoming request handling:`, + `${this.logPrefix(moduleName, 'requestListener.req.onerror')} Error on HTTP request:`, error ); }); @@ -95,13 +93,12 @@ export default class UIHttpServer extends AbstractUIServer { } if (req.method === 'POST') { const bodyBuffer = []; - let body: RequestPayload; req .on('data', (chunk) => { bodyBuffer.push(chunk); }) .on('end', () => { - body = JSON.parse(Buffer.concat(bodyBuffer).toString()) as RequestPayload; + const body = JSON.parse(Buffer.concat(bodyBuffer).toString()) as RequestPayload; this.uiServices .get(version) .requestHandler(this.buildRequest(uuid, procedureName, body ?? {})) @@ -113,6 +110,10 @@ export default class UIHttpServer extends AbstractUIServer { throw new BaseError(`Unsupported HTTP method: '${req.method}'`); } } catch (error) { + logger.error( + `${this.logPrefix(moduleName, 'requestListener')} Handle HTTP request error:`, + error + ); this.sendResponse(this.buildResponse(uuid, { status: ResponseStatus.FAILURE })); } } @@ -122,11 +123,11 @@ export default class UIHttpServer extends AbstractUIServer { procedureName: ProcedureName, requestPayload: RequestPayload ): string { - return JSON.stringify([id, procedureName, requestPayload]); + return JSON.stringify([id, procedureName, requestPayload] as ProtocolRequest); } private buildResponse(id: string, responsePayload: ResponsePayload): string { - return JSON.stringify([id, responsePayload]); + return JSON.stringify([id, responsePayload] as ProtocolResponse); } private responseStatusToStatusCode(status: ResponseStatus): StatusCodes { diff --git a/src/ui/web/src/composable/UIClient.ts b/src/ui/web/src/composable/UIClient.ts index f699a837..2095b167 100644 --- a/src/ui/web/src/composable/UIClient.ts +++ b/src/ui/web/src/composable/UIClient.ts @@ -97,6 +97,9 @@ export default class UIClient { config.emobility.protocol ); this._ws.onmessage = this.responseHandler.bind(this); + this._ws.onerror = (error) => { + console.error('WebSocket error: ', error); + }; } private setResponseHandler( @@ -160,7 +163,7 @@ export default class UIClient { this.getResponseHandler(uuid)?.reject(response); break; default: - throw new Error(`Response status not supported: ${response.status}`); + console.error(`Response status not supported: ${response.status}`); } this.deleteResponseHandler(uuid); } else { -- 2.34.1