From 10d244c0aa14bb4839791eea27e1674d87ae4e20 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Sat, 27 Aug 2022 15:48:54 +0200 Subject: [PATCH] UI server: fix HTTP transport start/stop MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- src/charging-station/Bootstrap.ts | 2 +- .../ChargingStationWorkerBroadcastChannel.ts | 11 +++++++++-- .../UIServiceWorkerBroadcastChannel.ts | 2 ++ src/charging-station/ui-server/UIHttpServer.ts | 7 +++++-- src/types/WorkerBroadcastChannel.ts | 4 +++- 5 files changed, 20 insertions(+), 6 deletions(-) diff --git a/src/charging-station/Bootstrap.ts b/src/charging-station/Bootstrap.ts index 1fdb9eae..d6bf3f37 100644 --- a/src/charging-station/Bootstrap.ts +++ b/src/charging-station/Bootstrap.ts @@ -137,7 +137,7 @@ export class Bootstrap { } this.started = true; } catch (error) { - console.error(chalk.red('Bootstrap start error '), error); + console.error(chalk.red('Bootstrap start error: '), error); } } else { console.error(chalk.red('Cannot start an already started charging stations simulator')); diff --git a/src/charging-station/ChargingStationWorkerBroadcastChannel.ts b/src/charging-station/ChargingStationWorkerBroadcastChannel.ts index 42448275..35e3fb72 100644 --- a/src/charging-station/ChargingStationWorkerBroadcastChannel.ts +++ b/src/charging-station/ChargingStationWorkerBroadcastChannel.ts @@ -65,9 +65,15 @@ export default class ChargingStationWorkerBroadcastChannel extends WorkerBroadca try { commandResponse = await this.commandHandler(command, requestPayload); if (commandResponse === undefined) { - responsePayload = { status: ResponseStatus.SUCCESS }; + responsePayload = { + hashId: this.chargingStation.hashId, + status: ResponseStatus.SUCCESS, + }; } else { - responsePayload = { status: this.commandResponseToResponseStatus(commandResponse) }; + responsePayload = { + hashId: this.chargingStation.hashId, + status: this.commandResponseToResponseStatus(commandResponse), + }; } } catch (error) { logger.error( @@ -75,6 +81,7 @@ export default class ChargingStationWorkerBroadcastChannel extends WorkerBroadca error ); responsePayload = { + hashId: this.chargingStation.hashId, status: ResponseStatus.FAILURE, command, requestPayload, diff --git a/src/charging-station/UIServiceWorkerBroadcastChannel.ts b/src/charging-station/UIServiceWorkerBroadcastChannel.ts index 5397e690..8a85565e 100644 --- a/src/charging-station/UIServiceWorkerBroadcastChannel.ts +++ b/src/charging-station/UIServiceWorkerBroadcastChannel.ts @@ -21,6 +21,8 @@ export default class UIServiceWorkerBroadcastChannel extends WorkerBroadcastChan } this.validateMessageEvent(messageEvent); const [uuid, responsePayload] = messageEvent.data as BroadcastChannelResponse; + // TODO: handle multiple responses for the same uuid + delete responsePayload.hashId; this.uiService.sendResponse(uuid, responsePayload); } diff --git a/src/charging-station/ui-server/UIHttpServer.ts b/src/charging-station/ui-server/UIHttpServer.ts index dcf4cbc0..9f423ce4 100644 --- a/src/charging-station/ui-server/UIHttpServer.ts +++ b/src/charging-station/ui-server/UIHttpServer.ts @@ -33,11 +33,14 @@ export default class UIHttpServer extends AbstractUIServer { } public start(): void { - (this.server as Server).listen(this.options ?? Configuration.getUIServer().options); + if ((this.server as Server).listening === false) { + (this.server as Server).listen(this.options ?? Configuration.getUIServer().options); + } } public stop(): void { this.chargingStations.clear(); + this.responseHandlers.clear(); } // eslint-disable-next-line @typescript-eslint/no-unused-vars @@ -48,7 +51,7 @@ export default class UIHttpServer extends AbstractUIServer { public sendResponse(response: string): void { const [uuid, payload] = JSON.parse(response) as ProtocolResponse; const statusCode = this.responseStatusToStatusCode(payload.status); - if (this.responseHandlers.has(uuid)) { + if (this.responseHandlers.has(uuid) === true) { const { res } = this.responseHandlers.get(uuid); res.writeHead(statusCode, { 'Content-Type': 'application/json' }); res.write(JSON.stringify(payload)); diff --git a/src/types/WorkerBroadcastChannel.ts b/src/types/WorkerBroadcastChannel.ts index 45168b03..0ca44ada 100644 --- a/src/types/WorkerBroadcastChannel.ts +++ b/src/types/WorkerBroadcastChannel.ts @@ -34,6 +34,8 @@ export type BroadcastChannelRequestPayload = | HashIdBroadcastChannelRequestPayload | HashIdsBroadcastChannelRequestPayload; -export type BroadcastChannelResponsePayload = ResponsePayload; +export interface BroadcastChannelResponsePayload extends ResponsePayload { + hashId: string; +} export type MessageEvent = { data: BroadcastChannelRequest | BroadcastChannelResponse }; -- 2.34.1