fix: ensure firmware update simulation always run as async resource
[e-mobility-charging-stations-simulator.git] / src / charging-station / ui-server / ui-services / AbstractUIService.ts
index 541966afbc98f2ad9317d126185ac15e5e4eace8..1ec6cf93b20e77363f08ecbb79cb9c10ed9cf656 100644 (file)
@@ -5,6 +5,7 @@ import {
   ProcedureName,
   type ProtocolRequest,
   type ProtocolRequestHandler,
+  type ProtocolResponse,
   type ProtocolVersion,
   type RequestPayload,
   type ResponsePayload,
@@ -65,11 +66,11 @@ export abstract class AbstractUIService {
     this.broadcastChannelRequests = new Map<string, number>();
   }
 
-  public async requestHandler(request: ProtocolRequest): Promise<void> {
+  public async requestHandler(request: ProtocolRequest): Promise<ProtocolResponse | undefined> {
     let messageId: string;
     let command: ProcedureName;
     let requestPayload: RequestPayload | undefined;
-    let responsePayload: ResponsePayload;
+    let responsePayload: ResponsePayload | undefined;
     try {
       [messageId, command, requestPayload] = request;
 
@@ -98,26 +99,26 @@ export abstract class AbstractUIService {
         errorStack: (error as Error).stack,
         errorDetails: (error as OCPPError).details,
       };
-    } finally {
-      // Send response for payload not forwarded to broadcast channel
-      if (!Utils.isNullOrUndefined(responsePayload)) {
-        this.sendResponse(messageId, responsePayload);
-      }
+    }
+    if (!Utils.isNullOrUndefined(responsePayload)) {
+      return this.uiServer.buildProtocolResponse(messageId, responsePayload);
     }
   }
 
-  public sendRequest(
-    messageId: string,
-    procedureName: ProcedureName,
-    requestPayload: RequestPayload
-  ): void {
-    this.uiServer.sendRequest(
-      this.uiServer.buildProtocolRequest(messageId, procedureName, requestPayload)
-    );
-  }
+  // public sendRequest(
+  //   messageId: string,
+  //   procedureName: ProcedureName,
+  //   requestPayload: RequestPayload
+  // ): void {
+  //   this.uiServer.sendRequest(
+  //     this.uiServer.buildProtocolRequest(messageId, procedureName, requestPayload)
+  //   );
+  // }
 
   public sendResponse(messageId: string, responsePayload: ResponsePayload): void {
-    this.uiServer.sendResponse(this.uiServer.buildProtocolResponse(messageId, responsePayload));
+    if (this.uiServer.hasResponseHandler(messageId)) {
+      this.uiServer.sendResponse(this.uiServer.buildProtocolResponse(messageId, responsePayload));
+    }
   }
 
   public logPrefix = (modName: string, methodName: string): string => {