refactor: cleanup app message handling code
[e-mobility-charging-stations-simulator.git] / src / charging-station / ocpp / 2.0 / OCPP20ResponseService.ts
index 99ca656614c452073bca6732fa3c4ba17fc509e1..6ab91c074d01ec4f24b01a4c5fbdffb374ad1fa9 100644 (file)
@@ -2,8 +2,7 @@
 
 import type { ValidateFunction } from 'ajv'
 
-import { OCPP20ServiceUtils } from './OCPP20ServiceUtils.js'
-import { type ChargingStation, addConfigurationKey } from '../../../charging-station/index.js'
+import { addConfigurationKey, type ChargingStation } from '../../../charging-station/index.js'
 import { OCPPError } from '../../../exception/index.js'
 import {
   ErrorType,
@@ -19,25 +18,26 @@ import {
   RegistrationStatusEnumType,
   type ResponseHandler
 } from '../../../types/index.js'
-import { logger } from '../../../utils/index.js'
+import { isAsyncFunction, logger } from '../../../utils/index.js'
 import { OCPPResponseService } from '../OCPPResponseService.js'
+import { OCPP20ServiceUtils } from './OCPP20ServiceUtils.js'
 
 const moduleName = 'OCPP20ResponseService'
 
 export class OCPP20ResponseService extends OCPPResponseService {
-  public jsonSchemasIncomingRequestResponseValidateFunction: Map<
+  public incomingRequestResponsePayloadValidateFunctions: Map<
   OCPP20IncomingRequestCommand,
   ValidateFunction<JsonType>
   >
 
-  protected jsonSchemasValidateFunction: Map<OCPP20RequestCommand, ValidateFunction<JsonType>>
+  protected payloadValidateFunctions: Map<OCPP20RequestCommand, ValidateFunction<JsonType>>
   private readonly responseHandlers: Map<OCPP20RequestCommand, ResponseHandler>
 
   public constructor () {
     // if (new.target.name === moduleName) {
     //   throw new TypeError(`Cannot construct ${new.target.name} instances directly`)
     // }
-    super(OCPPVersion.VERSION_20)
+    super(OCPPVersion.VERSION_201)
     this.responseHandlers = new Map<OCPP20RequestCommand, ResponseHandler>([
       [
         OCPP20RequestCommand.BOOT_NOTIFICATION,
@@ -46,7 +46,7 @@ export class OCPP20ResponseService extends OCPPResponseService {
       [OCPP20RequestCommand.HEARTBEAT, this.emptyResponseHandler],
       [OCPP20RequestCommand.STATUS_NOTIFICATION, this.emptyResponseHandler]
     ])
-    this.jsonSchemasValidateFunction = new Map<OCPP20RequestCommand, ValidateFunction<JsonType>>([
+    this.payloadValidateFunctions = new Map<OCPP20RequestCommand, ValidateFunction<JsonType>>([
       [
         OCPP20RequestCommand.BOOT_NOTIFICATION,
         this.ajv
@@ -84,13 +84,13 @@ export class OCPP20ResponseService extends OCPPResponseService {
           .bind(this)
       ]
     ])
-    this.jsonSchemasIncomingRequestResponseValidateFunction = new Map<
+    this.incomingRequestResponsePayloadValidateFunctions = new Map<
     OCPP20IncomingRequestCommand,
     ValidateFunction<JsonType>
     >([
       [
         OCPP20IncomingRequestCommand.CLEAR_CACHE,
-        this.ajv
+        this.ajvIncomingRequest
           .compile(
             OCPP20ServiceUtils.parseJsonSchemaFile<OCPP20ClearCacheResponse>(
               'assets/json-schemas/ocpp/2.0/ClearCacheResponse.json',
@@ -118,7 +118,18 @@ export class OCPP20ResponseService extends OCPPResponseService {
         try {
           this.validatePayload(chargingStation, commandName, payload)
           // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
-          await this.responseHandlers.get(commandName)!(chargingStation, payload, requestPayload)
+          const responseHandler = this.responseHandlers.get(commandName)!
+          if (isAsyncFunction(responseHandler)) {
+            await responseHandler(chargingStation, payload, requestPayload)
+          } else {
+            (
+              responseHandler as (
+                chargingStation: ChargingStation,
+                payload: JsonType,
+                requestPayload?: JsonType
+              ) => void
+            )(chargingStation, payload, requestPayload)
+          }
         } catch (error) {
           logger.error(
             `${chargingStation.logPrefix()} ${moduleName}.responseHandler: Handle response error:`,
@@ -130,7 +141,7 @@ export class OCPP20ResponseService extends OCPPResponseService {
         // Throw exception
         throw new OCPPError(
           ErrorType.NOT_IMPLEMENTED,
-          `${commandName} is not implemented to handle response PDU ${JSON.stringify(
+          `'${commandName}' is not implemented to handle response PDU ${JSON.stringify(
             payload,
             undefined,
             2
@@ -146,7 +157,7 @@ export class OCPP20ResponseService extends OCPPResponseService {
           payload,
           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,
         payload
       )
@@ -158,7 +169,7 @@ export class OCPP20ResponseService extends OCPPResponseService {
     commandName: OCPP20RequestCommand,
     payload: JsonType
   ): boolean {
-    if (this.jsonSchemasValidateFunction.has(commandName)) {
+    if (this.payloadValidateFunctions.has(commandName)) {
       return this.validateResponsePayload(chargingStation, commandName, payload)
     }
     logger.warn(