Update src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts
[e-mobility-charging-stations-simulator.git] / src / charging-station / ocpp / OCPPRequestService.ts
index 13cb170789f502fce73c4e6847b9e45f12fad6da..d2122a65ad2f281c7a9de9cd6b9afcbbeeb87a9d 100644 (file)
@@ -1,34 +1,33 @@
 import Ajv, { type JSONSchemaType } from 'ajv';
 import ajvFormats from 'ajv-formats';
 
-import type OCPPResponseService from './OCPPResponseService';
+import { OCPPConstants } from './OCPPConstants';
+import type { OCPPResponseService } from './OCPPResponseService';
 import { OCPPServiceUtils } from './OCPPServiceUtils';
-import OCPPError from '../../exception/OCPPError';
-import PerformanceStatistics from '../../performance/PerformanceStatistics';
-import type { EmptyObject } from '../../types/EmptyObject';
-import type { HandleErrorParams } from '../../types/Error';
-import type { JsonObject, JsonType } from '../../types/JsonType';
-import { ErrorType } from '../../types/ocpp/ErrorType';
-import { MessageType } from '../../types/ocpp/MessageType';
-import type { OCPPVersion } from '../../types/ocpp/OCPPVersion';
+import type { ChargingStation } from '../../charging-station';
+import { OCPPError } from '../../exception';
+import { PerformanceStatistics } from '../../performance';
 import {
   type ErrorCallback,
+  type ErrorResponse,
+  ErrorType,
   type IncomingRequestCommand,
+  type JsonObject,
+  type JsonType,
+  MessageType,
+  type OCPPVersion,
   type OutgoingRequest,
   RequestCommand,
   type RequestParams,
+  type Response,
   type ResponseCallback,
   type ResponseType,
-} from '../../types/ocpp/Requests';
-import type { ErrorResponse, Response } from '../../types/ocpp/Responses';
-import Constants from '../../utils/Constants';
-import logger from '../../utils/Logger';
-import Utils from '../../utils/Utils';
-import type ChargingStation from '../ChargingStation';
+} from '../../types';
+import { Constants, ErrorUtils, Utils, logger } from '../../utils';
 
 const moduleName = 'OCPPRequestService';
 
-export default abstract class OCPPRequestService {
+export abstract class OCPPRequestService {
   private static instance: OCPPRequestService | null = null;
   private readonly version: OCPPVersion;
   private readonly ajv: Ajv;
@@ -43,14 +42,63 @@ export default abstract class OCPPRequestService {
     });
     ajvFormats(this.ajv);
     this.ocppResponseService = ocppResponseService;
-    this.requestHandler.bind(this);
-    this.sendMessage.bind(this);
-    this.sendResponse.bind(this);
-    this.sendError.bind(this);
-    this.internalSendMessage.bind(this);
-    this.buildMessageToSend.bind(this);
-    this.validateRequestPayload.bind(this);
-    this.validateIncomingRequestResponsePayload.bind(this);
+    this.requestHandler = this.requestHandler.bind(this) as <
+      ReqType extends JsonType,
+      ResType extends JsonType
+    >(
+      chargingStation: ChargingStation,
+      commandName: RequestCommand,
+      commandParams?: JsonType,
+      params?: RequestParams
+    ) => Promise<ResType>;
+    this.sendMessage = this.sendMessage.bind(this) as (
+      chargingStation: ChargingStation,
+      messageId: string,
+      messagePayload: JsonType,
+      commandName: RequestCommand,
+      params?: RequestParams
+    ) => Promise<ResponseType>;
+    this.sendResponse = this.sendResponse.bind(this) as (
+      chargingStation: ChargingStation,
+      messageId: string,
+      messagePayload: JsonType,
+      commandName: IncomingRequestCommand
+    ) => Promise<ResponseType>;
+    this.sendError = this.sendError.bind(this) as (
+      chargingStation: ChargingStation,
+      messageId: string,
+      ocppError: OCPPError,
+      commandName: RequestCommand | IncomingRequestCommand
+    ) => Promise<ResponseType>;
+    this.internalSendMessage = this.internalSendMessage.bind(this) as (
+      chargingStation: ChargingStation,
+      messageId: string,
+      messagePayload: JsonType | OCPPError,
+      messageType: MessageType,
+      commandName: RequestCommand | IncomingRequestCommand,
+      params?: RequestParams
+    ) => Promise<ResponseType>;
+    this.buildMessageToSend = this.buildMessageToSend.bind(this) as (
+      chargingStation: ChargingStation,
+      messageId: string,
+      messagePayload: JsonType | OCPPError,
+      messageType: MessageType,
+      commandName: RequestCommand | IncomingRequestCommand,
+      responseCallback: ResponseCallback,
+      errorCallback: ErrorCallback
+    ) => string;
+    this.validateRequestPayload = this.validateRequestPayload.bind(this) as <T extends JsonObject>(
+      chargingStation: ChargingStation,
+      commandName: RequestCommand | IncomingRequestCommand,
+      payload: T
+    ) => boolean;
+    this.validateIncomingRequestResponsePayload = this.validateIncomingRequestResponsePayload.bind(
+      this
+    ) as <T extends JsonObject>(
+      chargingStation: ChargingStation,
+      commandName: RequestCommand | IncomingRequestCommand,
+      payload: T
+    ) => boolean;
   }
 
   public static getInstance<T extends OCPPRequestService>(
@@ -79,7 +127,7 @@ export default abstract class OCPPRequestService {
         commandName
       );
     } catch (error) {
-      this.handleSendMessageError(chargingStation, commandName, error as Error, {
+      ErrorUtils.handleSendMessageError(chargingStation, commandName, error as Error, {
         throwError: true,
       });
     }
@@ -101,7 +149,7 @@ export default abstract class OCPPRequestService {
         commandName
       );
     } catch (error) {
-      this.handleSendMessageError(chargingStation, commandName, error as Error);
+      ErrorUtils.handleSendMessageError(chargingStation, commandName, error as Error);
     }
   }
 
@@ -116,6 +164,10 @@ export default abstract class OCPPRequestService {
       throwError: false,
     }
   ): Promise<ResponseType> {
+    params = {
+      ...{ skipBufferingOnError: false, triggerMessage: false, throwError: false },
+      ...params,
+    };
     try {
       return await this.internalSendMessage(
         chargingStation,
@@ -126,7 +178,7 @@ export default abstract class OCPPRequestService {
         params
       );
     } catch (error) {
-      this.handleSendMessageError(chargingStation, commandName, error as Error, {
+      ErrorUtils.handleSendMessageError(chargingStation, commandName, error as Error, {
         throwError: params.throwError,
       });
     }
@@ -211,19 +263,24 @@ export default abstract class OCPPRequestService {
     messageId: string,
     messagePayload: JsonType | OCPPError,
     messageType: MessageType,
-    commandName?: RequestCommand | IncomingRequestCommand,
+    commandName: RequestCommand | IncomingRequestCommand,
     params: RequestParams = {
       skipBufferingOnError: false,
       triggerMessage: false,
+      throwError: false,
     }
   ): Promise<ResponseType> {
+    params = {
+      ...{ skipBufferingOnError: false, triggerMessage: false, throwError: false },
+      ...params,
+    };
     if (
-      (chargingStation.isInUnknownState() === true &&
+      (chargingStation.inUnknownState() === true &&
         commandName === RequestCommand.BOOT_NOTIFICATION) ||
       (chargingStation.getOcppStrictCompliance() === false &&
-        chargingStation.isInUnknownState() === true) ||
-      chargingStation.isInAcceptedState() === true ||
-      (chargingStation.isInPendingState() === true &&
+        chargingStation.inUnknownState() === true) ||
+      chargingStation.inAcceptedState() === true ||
+      (chargingStation.inPendingState() === true &&
         (params.triggerMessage === true || messageType === MessageType.CALL_RESULT_MESSAGE))
     ) {
       // eslint-disable-next-line @typescript-eslint/no-this-alias
@@ -231,8 +288,64 @@ export default abstract class OCPPRequestService {
       // Send a message through wsConnection
       return Utils.promiseWithTimeout(
         new Promise((resolve, reject) => {
+          /**
+           * Function that will receive the request's response
+           *
+           * @param payload -
+           * @param requestPayload -
+           */
+          const responseCallback = (payload: JsonType, requestPayload: JsonType): void => {
+            if (chargingStation.getEnableStatistics() === true) {
+              chargingStation.performanceStatistics?.addRequestStatistic(
+                commandName,
+                MessageType.CALL_RESULT_MESSAGE
+              );
+            }
+            // Handle the request's response
+            self.ocppResponseService
+              .responseHandler(
+                chargingStation,
+                commandName as RequestCommand,
+                payload,
+                requestPayload
+              )
+              .then(() => {
+                resolve(payload);
+              })
+              .catch((error) => {
+                reject(error);
+              })
+              .finally(() => {
+                chargingStation.requests.delete(messageId);
+              });
+          };
+
+          /**
+           * Function that will receive the request's error response
+           *
+           * @param error -
+           * @param requestStatistic -
+           */
+          const errorCallback = (error: OCPPError, requestStatistic = true): void => {
+            if (requestStatistic === true && chargingStation.getEnableStatistics() === true) {
+              chargingStation.performanceStatistics?.addRequestStatistic(
+                commandName,
+                MessageType.CALL_ERROR_MESSAGE
+              );
+            }
+            logger.error(
+              `${chargingStation.logPrefix()} Error occurred at ${OCPPServiceUtils.getMessageTypeString(
+                messageType
+              )} command ${commandName} with PDU %j:`,
+              messagePayload,
+              error
+            );
+            chargingStation.requests.delete(messageId);
+            reject(error);
+          };
+
           if (chargingStation.getEnableStatistics() === true) {
-            chargingStation.performanceStatistics.addRequestStatistic(commandName, messageType);
+            chargingStation.performanceStatistics?.addRequestStatistic(commandName, messageType);
           }
           const messageToSend = this.buildMessageToSend(
             chargingStation,
@@ -249,7 +362,7 @@ export default abstract class OCPPRequestService {
           if (wsOpened) {
             const beginId = PerformanceStatistics.beginMeasure(commandName);
             try {
-              chargingStation.wsConnection.send(messageToSend);
+              chargingStation.wsConnection?.send(messageToSend);
               logger.debug(
                 `${chargingStation.logPrefix()} >> Command '${commandName}' sent ${OCPPServiceUtils.getMessageTypeString(
                   messageType
@@ -276,7 +389,7 @@ export default abstract class OCPPRequestService {
                 ErrorType.GENERIC_ERROR,
                 `WebSocket closed or errored for buffered message id '${messageId}' with content '${messageToSend}'`,
                 commandName,
-                (messagePayload as JsonObject)?.details ?? {}
+                (messagePayload as JsonObject)?.details ?? Constants.EMPTY_FREEZED_OBJECT
               )
             );
           } else if (wsClosedOrErrored) {
@@ -284,7 +397,7 @@ export default abstract class OCPPRequestService {
               ErrorType.GENERIC_ERROR,
               `WebSocket closed or errored for non buffered message id '${messageId}' with content '${messageToSend}'`,
               commandName,
-              (messagePayload as JsonObject)?.details ?? {}
+              (messagePayload as JsonObject)?.details ?? Constants.EMPTY_FREEZED_OBJECT
             );
             // Reject response
             if (messageType !== MessageType.CALL_MESSAGE) {
@@ -297,69 +410,13 @@ export default abstract class OCPPRequestService {
           if (messageType !== MessageType.CALL_MESSAGE) {
             return resolve(messagePayload);
           }
-
-          /**
-           * Function that will receive the request's response
-           *
-           * @param payload -
-           * @param requestPayload -
-           */
-          function responseCallback(payload: JsonType, requestPayload: JsonType): void {
-            if (chargingStation.getEnableStatistics() === true) {
-              chargingStation.performanceStatistics.addRequestStatistic(
-                commandName,
-                MessageType.CALL_RESULT_MESSAGE
-              );
-            }
-            // Handle the request's response
-            self.ocppResponseService
-              .responseHandler(
-                chargingStation,
-                commandName as RequestCommand,
-                payload,
-                requestPayload
-              )
-              .then(() => {
-                resolve(payload);
-              })
-              .catch((error) => {
-                reject(error);
-              })
-              .finally(() => {
-                chargingStation.requests.delete(messageId);
-              });
-          }
-
-          /**
-           * Function that will receive the request's error response
-           *
-           * @param error -
-           * @param requestStatistic -
-           */
-          function errorCallback(error: OCPPError, requestStatistic = true): void {
-            if (requestStatistic === true && chargingStation.getEnableStatistics() === true) {
-              chargingStation.performanceStatistics.addRequestStatistic(
-                commandName,
-                MessageType.CALL_ERROR_MESSAGE
-              );
-            }
-            logger.error(
-              `${chargingStation.logPrefix()} Error occurred at ${OCPPServiceUtils.getMessageTypeString(
-                messageType
-              )} command ${commandName} with PDU %j:`,
-              messagePayload,
-              error
-            );
-            chargingStation.requests.delete(messageId);
-            reject(error);
-          }
         }),
-        Constants.OCPP_WEBSOCKET_TIMEOUT,
+        OCPPConstants.OCPP_WEBSOCKET_TIMEOUT,
         new OCPPError(
           ErrorType.GENERIC_ERROR,
           `Timeout for message id '${messageId}'`,
           commandName,
-          (messagePayload as JsonObject)?.details ?? {}
+          (messagePayload as JsonObject)?.details ?? Constants.EMPTY_FREEZED_OBJECT
         ),
         () => {
           messageType === MessageType.CALL_MESSAGE && chargingStation.requests.delete(messageId);
@@ -378,9 +435,9 @@ export default abstract class OCPPRequestService {
     messageId: string,
     messagePayload: JsonType | OCPPError,
     messageType: MessageType,
-    commandName?: RequestCommand | IncomingRequestCommand,
-    responseCallback?: ResponseCallback,
-    errorCallback?: ErrorCallback
+    commandName: RequestCommand | IncomingRequestCommand,
+    responseCallback: ResponseCallback,
+    errorCallback: ErrorCallback
   ): string {
     let messageToSend: string;
     // Type of message
@@ -427,18 +484,6 @@ export default 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,