docs: fix README.md formatting
[e-mobility-charging-stations-simulator.git] / src / charging-station / ocpp / 2.0 / OCPP20IncomingRequestService.ts
index a11026d8c0068c49fcecbe5b348f92c2c7bec1a9..b45345a6faf316febc7c0b283901d708802f8462 100644 (file)
@@ -13,7 +13,7 @@ import {
   OCPP20IncomingRequestCommand,
   OCPPVersion
 } from '../../../types/index.js'
-import { logger } from '../../../utils/index.js'
+import { isAsyncFunction, logger } from '../../../utils/index.js'
 import { OCPPIncomingRequestService } from '../OCPPIncomingRequestService.js'
 
 const moduleName = 'OCPP20IncomingRequestService'
@@ -91,11 +91,12 @@ export class OCPP20IncomingRequestService extends OCPPIncomingRequestService {
           this.validatePayload(chargingStation, commandName, commandPayload)
           // Call the method to build the response
           // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
-          response = (await this.incomingRequestHandlers.get(commandName)!(
-            chargingStation,
-            commandPayload
-          )) as ResType
-          this.emit(commandName, chargingStation, commandPayload, response)
+          const incomingRequestHandler = this.incomingRequestHandlers.get(commandName)!
+          if (isAsyncFunction(incomingRequestHandler)) {
+            response = (await incomingRequestHandler(chargingStation, commandPayload)) as ResType
+          } else {
+            response = incomingRequestHandler(chargingStation, commandPayload) as ResType
+          }
         } catch (error) {
           // Log
           logger.error(
@@ -124,7 +125,7 @@ export class OCPP20IncomingRequestService extends OCPPIncomingRequestService {
           commandPayload,
           undefined,
           2
-        )} while the charging station is not registered on the central server.`,
+        )} while the charging station is not registered on the central server`,
         commandName,
         commandPayload
       )
@@ -136,6 +137,8 @@ export class OCPP20IncomingRequestService extends OCPPIncomingRequestService {
       response,
       commandName
     )
+    // Emit command name event to allow delayed handling
+    this.emit(commandName, chargingStation, commandPayload, response)
   }
 
   private validatePayload (