feat: add message buffer flush interval
[e-mobility-charging-stations-simulator.git] / src / charging-station / ocpp / 2.0 / OCPP20IncomingRequestService.ts
index 6cfb4ec1374f1b76de353b6cb4b7546d75797fa5..0d657d99f7bd67b365660c3e2888653157485e5c 100644 (file)
@@ -8,7 +8,6 @@ import { OCPPError } from '../../../exception';
 import {
   ErrorType,
   type IncomingRequestHandler,
-  type JsonObject,
   type JsonType,
   type OCPP20ClearCacheRequest,
   OCPP20IncomingRequestCommand,
@@ -20,7 +19,7 @@ import { OCPPIncomingRequestService } from '../OCPPIncomingRequestService';
 const moduleName = 'OCPP20IncomingRequestService';
 
 export class OCPP20IncomingRequestService extends OCPPIncomingRequestService {
-  protected jsonSchemas: Map<OCPP20IncomingRequestCommand, JSONSchemaType<JsonObject>>;
+  protected jsonSchemas: Map<OCPP20IncomingRequestCommand, JSONSchemaType<JsonType>>;
   private incomingRequestHandlers: Map<OCPP20IncomingRequestCommand, IncomingRequestHandler>;
 
   public constructor() {
@@ -31,7 +30,7 @@ export class OCPP20IncomingRequestService extends OCPPIncomingRequestService {
     this.incomingRequestHandlers = new Map<OCPP20IncomingRequestCommand, IncomingRequestHandler>([
       [OCPP20IncomingRequestCommand.CLEAR_CACHE, this.handleRequestClearCache.bind(this)],
     ]);
-    this.jsonSchemas = new Map<OCPP20IncomingRequestCommand, JSONSchemaType<JsonObject>>([
+    this.jsonSchemas = new Map<OCPP20IncomingRequestCommand, JSONSchemaType<JsonType>>([
       [
         OCPP20IncomingRequestCommand.CLEAR_CACHE,
         OCPP20ServiceUtils.parseJsonSchemaFile<OCPP20ClearCacheRequest>(
@@ -48,15 +47,15 @@ export class OCPP20IncomingRequestService extends OCPPIncomingRequestService {
     ) => boolean;
   }
 
-  public async incomingRequestHandler(
+  public async incomingRequestHandler<ReqType extends JsonType, ResType extends JsonType>(
     chargingStation: ChargingStation,
     messageId: string,
     commandName: OCPP20IncomingRequestCommand,
-    commandPayload: JsonType,
+    commandPayload: ReqType,
   ): Promise<void> {
-    let response: JsonType;
+    let response: ResType;
     if (
-      chargingStation.getOcppStrictCompliance() === true &&
+      chargingStation.stationInfo?.ocppStrictCompliance === true &&
       chargingStation.inPendingState() === true &&
       (commandName === OCPP20IncomingRequestCommand.REQUEST_START_TRANSACTION ||
         commandName === OCPP20IncomingRequestCommand.REQUEST_STOP_TRANSACTION)
@@ -65,7 +64,7 @@ export class OCPP20IncomingRequestService extends OCPPIncomingRequestService {
         ErrorType.SECURITY_ERROR,
         `${commandName} cannot be issued to handle request PDU ${JSON.stringify(
           commandPayload,
-          null,
+          undefined,
           2,
         )} while the charging station is in pending state on the central server`,
         commandName,
@@ -74,7 +73,7 @@ export class OCPP20IncomingRequestService extends OCPPIncomingRequestService {
     }
     if (
       chargingStation.isRegistered() === true ||
-      (chargingStation.getOcppStrictCompliance() === false &&
+      (chargingStation.stationInfo?.ocppStrictCompliance === false &&
         chargingStation.inUnknownState() === true)
     ) {
       if (
@@ -84,10 +83,10 @@ export class OCPP20IncomingRequestService extends OCPPIncomingRequestService {
         try {
           this.validatePayload(chargingStation, commandName, commandPayload);
           // Call the method to build the response
-          response = await this.incomingRequestHandlers.get(commandName)(
+          response = (await this.incomingRequestHandlers.get(commandName)!(
             chargingStation,
             commandPayload,
-          );
+          )) as ResType;
         } catch (error) {
           // Log
           logger.error(
@@ -102,7 +101,7 @@ export class OCPP20IncomingRequestService extends OCPPIncomingRequestService {
           ErrorType.NOT_IMPLEMENTED,
           `${commandName} is not implemented to handle request PDU ${JSON.stringify(
             commandPayload,
-            null,
+            undefined,
             2,
           )}`,
           commandName,
@@ -114,7 +113,7 @@ export class OCPP20IncomingRequestService extends OCPPIncomingRequestService {
         ErrorType.SECURITY_ERROR,
         `${commandName} cannot be issued to handle request PDU ${JSON.stringify(
           commandPayload,
-          null,
+          undefined,
           2,
         )} while the charging station is not registered on the central server.`,
         commandName,
@@ -139,7 +138,7 @@ export class OCPP20IncomingRequestService extends OCPPIncomingRequestService {
       return this.validateIncomingRequestPayload(
         chargingStation,
         commandName,
-        this.jsonSchemas.get(commandName),
+        this.jsonSchemas.get(commandName)!,
         commandPayload,
       );
     }