UI server: fix HTTP transport start/stop
authorJérôme Benoit <jerome.benoit@sap.com>
Sat, 27 Aug 2022 13:48:54 +0000 (15:48 +0200)
committerJérôme Benoit <jerome.benoit@sap.com>
Sat, 27 Aug 2022 13:48:54 +0000 (15:48 +0200)
Signed-off-by: Jérôme Benoit <jerome.benoit@sap.com>
src/charging-station/Bootstrap.ts
src/charging-station/ChargingStationWorkerBroadcastChannel.ts
src/charging-station/UIServiceWorkerBroadcastChannel.ts
src/charging-station/ui-server/UIHttpServer.ts
src/types/WorkerBroadcastChannel.ts

index 1fdb9eae79d8b0bb0364da7709bf24514f1036f2..d6bf3f37da35a767ad70c3ae38d1c783c1daa73b 100644 (file)
@@ -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'));
index 42448275432337772fe80a691a5364840680f068..35e3fb7205940e0525570bc3d40e6e2703383425 100644 (file)
@@ -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,
index 5397e6906a076c28b1202dc9d59869fb640651aa..8a85565ee88469d20e959f6b531c73a0d4db94cb 100644 (file)
@@ -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);
   }
index dcf4cbc032db72af3731de3cb9105acbe203332a..9f423ce4157a61d5aa89f25e417dbf34765ad930 100644 (file)
@@ -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));
index 45168b03834471fad4fa41cdc2c62ad8b117b7ff..0ca44adac903f816f6917a60e1587762235b26e9 100644 (file)
@@ -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 };