refactor: use keyof to build the reservation filter key
[e-mobility-charging-stations-simulator.git] / src / charging-station / ocpp / OCPPResponseService.ts
index ab17b05ad6bf2626dd03d7db2d34a196f71d9de7..7934500ed3ebd7612cfb05ad58b0c68699396dc1 100644 (file)
@@ -2,6 +2,7 @@ import Ajv, { type JSONSchemaType } from 'ajv';
 import ajvFormats from 'ajv-formats';
 
 import { OCPPServiceUtils } from './OCPPServiceUtils';
+import type { ChargingStation } from '../../charging-station';
 import { OCPPError } from '../../exception';
 import type {
   IncomingRequestCommand,
@@ -10,8 +11,7 @@ import type {
   OCPPVersion,
   RequestCommand,
 } from '../../types';
-import { logger } from '../../utils/Logger';
-import type { ChargingStation } from '../ChargingStation';
+import { logger } from '../../utils';
 
 const moduleName = 'OCPPResponseService';
 
@@ -31,8 +31,21 @@ export abstract class OCPPResponseService {
       multipleOfPrecision: 2,
     });
     ajvFormats(this.ajv);
-    this.responseHandler.bind(this);
-    this.validateResponsePayload.bind(this);
+    this.responseHandler = this.responseHandler.bind(this) as <
+      ReqType extends JsonType,
+      ResType extends JsonType,
+    >(
+      chargingStation: ChargingStation,
+      commandName: RequestCommand,
+      payload: ResType,
+      requestPayload: ReqType,
+    ) => Promise<void>;
+    this.validateResponsePayload = this.validateResponsePayload.bind(this) as <T extends JsonType>(
+      chargingStation: ChargingStation,
+      commandName: RequestCommand,
+      schema: JSONSchemaType<T>,
+      payload: T,
+    ) => boolean;
   }
 
   public static getInstance<T extends OCPPResponseService>(this: new () => T): T {
@@ -46,9 +59,9 @@ export abstract class OCPPResponseService {
     chargingStation: ChargingStation,
     commandName: RequestCommand,
     schema: JSONSchemaType<T>,
-    payload: T
+    payload: T,
   ): boolean {
-    if (chargingStation.getPayloadSchemaValidation() === false) {
+    if (chargingStation.getOcppStrictCompliance() === false) {
       return true;
     }
     const validate = this.ajv.compile(schema);
@@ -57,13 +70,13 @@ export abstract class OCPPResponseService {
     }
     logger.error(
       `${chargingStation.logPrefix()} ${moduleName}.validateResponsePayload: Command '${commandName}' response PDU is invalid: %j`,
-      validate.errors
+      validate.errors,
     );
     throw new OCPPError(
-      OCPPServiceUtils.ajvErrorsToErrorType(validate.errors),
+      OCPPServiceUtils.ajvErrorsToErrorType(validate.errors!),
       'Response PDU is invalid',
       commandName,
-      JSON.stringify(validate.errors, null, 2)
+      JSON.stringify(validate.errors, null, 2),
     );
   }
 
@@ -71,10 +84,10 @@ export abstract class OCPPResponseService {
     /* This is intentional */
   }
 
-  public abstract responseHandler(
+  public abstract responseHandler<ReqType extends JsonType, ResType extends JsonType>(
     chargingStation: ChargingStation,
     commandName: RequestCommand,
-    payload: JsonType,
-    requestPayload: JsonType
+    payload: ResType,
+    requestPayload: ReqType,
   ): Promise<void>;
 }