feat: add message buffer flush interval
[e-mobility-charging-stations-simulator.git] / src / charging-station / ocpp / 2.0 / OCPP20ResponseService.ts
index 1eec885f9dc2fd1f3a7bb0043cd1f14479c8c6d2..111b114a34048113c4fdf2a60a77133570fe4405 100644 (file)
@@ -3,11 +3,10 @@
 import type { JSONSchemaType } from 'ajv';
 
 import { OCPP20ServiceUtils } from './OCPP20ServiceUtils';
-import { type ChargingStation, ChargingStationConfigurationUtils } from '../../../charging-station';
+import { type ChargingStation, addConfigurationKey } from '../../../charging-station';
 import { OCPPError } from '../../../exception';
 import {
   ErrorType,
-  type JsonObject,
   type JsonType,
   type OCPP20BootNotificationResponse,
   type OCPP20ClearCacheResponse,
@@ -28,11 +27,11 @@ const moduleName = 'OCPP20ResponseService';
 export class OCPP20ResponseService extends OCPPResponseService {
   public jsonIncomingRequestResponseSchemas: Map<
     OCPP20IncomingRequestCommand,
-    JSONSchemaType<JsonObject>
+    JSONSchemaType<JsonType>
   >;
 
   private responseHandlers: Map<OCPP20RequestCommand, ResponseHandler>;
-  private jsonSchemas: Map<OCPP20RequestCommand, JSONSchemaType<JsonObject>>;
+  private jsonSchemas: Map<OCPP20RequestCommand, JSONSchemaType<JsonType>>;
 
   public constructor() {
     // if (new.target?.name === moduleName) {
@@ -40,11 +39,17 @@ export class OCPP20ResponseService extends OCPPResponseService {
     // }
     super(OCPPVersion.VERSION_20);
     this.responseHandlers = new Map<OCPP20RequestCommand, ResponseHandler>([
-      [OCPP20RequestCommand.BOOT_NOTIFICATION, this.handleResponseBootNotification.bind(this)],
-      [OCPP20RequestCommand.HEARTBEAT, this.emptyResponseHandler.bind(this)],
-      [OCPP20RequestCommand.STATUS_NOTIFICATION, this.emptyResponseHandler.bind(this)],
+      [
+        OCPP20RequestCommand.BOOT_NOTIFICATION,
+        this.handleResponseBootNotification.bind(this) as ResponseHandler,
+      ],
+      [OCPP20RequestCommand.HEARTBEAT, this.emptyResponseHandler.bind(this) as ResponseHandler],
+      [
+        OCPP20RequestCommand.STATUS_NOTIFICATION,
+        this.emptyResponseHandler.bind(this) as ResponseHandler,
+      ],
     ]);
-    this.jsonSchemas = new Map<OCPP20RequestCommand, JSONSchemaType<JsonObject>>([
+    this.jsonSchemas = new Map<OCPP20RequestCommand, JSONSchemaType<JsonType>>([
       [
         OCPP20RequestCommand.BOOT_NOTIFICATION,
         OCPP20ServiceUtils.parseJsonSchemaFile<OCPP20BootNotificationResponse>(
@@ -87,11 +92,11 @@ export class OCPP20ResponseService extends OCPPResponseService {
     ) => boolean;
   }
 
-  public async responseHandler(
+  public async responseHandler<ReqType extends JsonType, ResType extends JsonType>(
     chargingStation: ChargingStation,
     commandName: OCPP20RequestCommand,
-    payload: JsonType,
-    requestPayload: JsonType,
+    payload: ResType,
+    requestPayload: ReqType,
   ): Promise<void> {
     if (
       chargingStation.isRegistered() === true ||
@@ -117,7 +122,7 @@ export class OCPP20ResponseService extends OCPPResponseService {
           ErrorType.NOT_IMPLEMENTED,
           `${commandName} is not implemented to handle response PDU ${JSON.stringify(
             payload,
-            null,
+            undefined,
             2,
           )}`,
           commandName,
@@ -129,7 +134,7 @@ export class OCPP20ResponseService extends OCPPResponseService {
         ErrorType.SECURITY_ERROR,
         `${commandName} cannot be issued to handle response PDU ${JSON.stringify(
           payload,
-          null,
+          undefined,
           2,
         )} while the charging station is not registered on the central server.`,
         commandName,
@@ -162,7 +167,7 @@ export class OCPP20ResponseService extends OCPPResponseService {
     payload: OCPP20BootNotificationResponse,
   ): void {
     if (payload.status === RegistrationStatusEnumType.ACCEPTED) {
-      ChargingStationConfigurationUtils.addConfigurationKey(
+      addConfigurationKey(
         chargingStation,
         OCPP20OptionalVariableName.HeartbeatInterval,
         payload.interval.toString(),