refactor: cleanup loops over object keys
[e-mobility-charging-stations-simulator.git] / src / charging-station / ocpp / OCPPRequestService.ts
index 3eb85e3089a232f21b50b5082e644dceec573505..4ea134b6ef062d5860e2b36e29d8d7d29b794781 100644 (file)
@@ -1,16 +1,16 @@
 import Ajv, { type JSONSchemaType } from 'ajv';
 import ajvFormats from 'ajv-formats';
 
-import { OCPPConstants, type OCPPResponseService, OCPPServiceUtils } from './internal';
+import { OCPPConstants } from './OCPPConstants';
+import type { OCPPResponseService } from './OCPPResponseService';
+import { OCPPServiceUtils } from './OCPPServiceUtils';
 import type { ChargingStation } from '../../charging-station';
 import { OCPPError } from '../../exception';
 import { PerformanceStatistics } from '../../performance';
 import {
-  type EmptyObject,
   type ErrorCallback,
   type ErrorResponse,
   ErrorType,
-  type HandleErrorParams,
   type IncomingRequestCommand,
   type JsonObject,
   type JsonType,
@@ -23,10 +23,22 @@ import {
   type ResponseCallback,
   type ResponseType,
 } from '../../types';
-import { Constants, Utils, logger } from '../../utils';
+import {
+  Constants,
+  cloneObject,
+  handleSendMessageError,
+  logger,
+  promiseWithTimeout,
+} from '../../utils';
 
 const moduleName = 'OCPPRequestService';
 
+const defaultRequestParams: RequestParams = {
+  skipBufferingOnError: false,
+  triggerMessage: false,
+  throwError: false,
+};
+
 export abstract class OCPPRequestService {
   private static instance: OCPPRequestService | null = null;
   private readonly version: OCPPVersion;
@@ -127,7 +139,7 @@ export abstract class OCPPRequestService {
         commandName
       );
     } catch (error) {
-      this.handleSendMessageError(chargingStation, commandName, error as Error, {
+      handleSendMessageError(chargingStation, commandName, error as Error, {
         throwError: true,
       });
     }
@@ -149,7 +161,7 @@ export abstract class OCPPRequestService {
         commandName
       );
     } catch (error) {
-      this.handleSendMessageError(chargingStation, commandName, error as Error);
+      handleSendMessageError(chargingStation, commandName, error as Error);
     }
   }
 
@@ -158,12 +170,12 @@ export abstract class OCPPRequestService {
     messageId: string,
     messagePayload: JsonType,
     commandName: RequestCommand,
-    params: RequestParams = {
-      skipBufferingOnError: false,
-      triggerMessage: false,
-      throwError: false,
-    }
+    params: RequestParams = defaultRequestParams
   ): Promise<ResponseType> {
+    params = {
+      ...defaultRequestParams,
+      ...params,
+    };
     try {
       return await this.internalSendMessage(
         chargingStation,
@@ -174,7 +186,7 @@ export abstract class OCPPRequestService {
         params
       );
     } catch (error) {
-      this.handleSendMessageError(chargingStation, commandName, error as Error, {
+      handleSendMessageError(chargingStation, commandName, error as Error, {
         throwError: params.throwError,
       });
     }
@@ -185,7 +197,7 @@ export abstract class OCPPRequestService {
     commandName: RequestCommand | IncomingRequestCommand,
     payload: T
   ): boolean {
-    if (chargingStation.getPayloadSchemaValidation() === false) {
+    if (chargingStation.getOcppStrictCompliance() === false) {
       return true;
     }
     if (this.jsonSchemas.has(commandName as RequestCommand) === false) {
@@ -195,7 +207,7 @@ export abstract class OCPPRequestService {
       return true;
     }
     const validate = this.ajv.compile(this.jsonSchemas.get(commandName as RequestCommand));
-    payload = Utils.cloneObject<T>(payload);
+    payload = cloneObject<T>(payload);
     OCPPServiceUtils.convertDateToISOString<T>(payload);
     if (validate(payload)) {
       return true;
@@ -218,7 +230,7 @@ export abstract class OCPPRequestService {
     commandName: RequestCommand | IncomingRequestCommand,
     payload: T
   ): boolean {
-    if (chargingStation.getPayloadSchemaValidation() === false) {
+    if (chargingStation.getOcppStrictCompliance() === false) {
       return true;
     }
     if (
@@ -236,7 +248,7 @@ export abstract class OCPPRequestService {
         commandName as IncomingRequestCommand
       )
     );
-    payload = Utils.cloneObject<T>(payload);
+    payload = cloneObject<T>(payload);
     OCPPServiceUtils.convertDateToISOString<T>(payload);
     if (validate(payload)) {
       return true;
@@ -260,11 +272,12 @@ export abstract class OCPPRequestService {
     messagePayload: JsonType | OCPPError,
     messageType: MessageType,
     commandName: RequestCommand | IncomingRequestCommand,
-    params: RequestParams = {
-      skipBufferingOnError: false,
-      triggerMessage: false,
-    }
+    params: RequestParams = defaultRequestParams
   ): Promise<ResponseType> {
+    params = {
+      ...defaultRequestParams,
+      ...params,
+    };
     if (
       (chargingStation.inUnknownState() === true &&
         commandName === RequestCommand.BOOT_NOTIFICATION) ||
@@ -277,7 +290,7 @@ export abstract class OCPPRequestService {
       // eslint-disable-next-line @typescript-eslint/no-this-alias
       const self = this;
       // Send a message through wsConnection
-      return Utils.promiseWithTimeout(
+      return promiseWithTimeout(
         new Promise((resolve, reject) => {
           /**
            * Function that will receive the request's response
@@ -475,18 +488,6 @@ export abstract class OCPPRequestService {
     return messageToSend;
   }
 
-  private handleSendMessageError(
-    chargingStation: ChargingStation,
-    commandName: RequestCommand | IncomingRequestCommand,
-    error: Error,
-    params: HandleErrorParams<EmptyObject> = { throwError: false }
-  ): void {
-    logger.error(`${chargingStation.logPrefix()} Request command '${commandName}' error:`, error);
-    if (params?.throwError === true) {
-      throw error;
-    }
-  }
-
   // eslint-disable-next-line @typescript-eslint/no-unused-vars
   public abstract requestHandler<ReqType extends JsonType, ResType extends JsonType>(
     chargingStation: ChargingStation,