refactor: add helper to test connector
[e-mobility-charging-stations-simulator.git] / src / charging-station / ocpp / OCPPRequestService.ts
index e00e954c9bfebe5be9e6dd7c6200182559248d90..a50500dc24047f7a7eb3276a03d725cedf69fb44 100644 (file)
@@ -1,7 +1,7 @@
 import Ajv, { type JSONSchemaType } from 'ajv';
 import ajvFormats from 'ajv-formats';
 
-import { type OCPPResponseService, OCPPServiceUtils } from './internal';
+import { OCPPConstants, type OCPPResponseService, OCPPServiceUtils } from './internal';
 import type { ChargingStation } from '../../charging-station';
 import { OCPPError } from '../../exception';
 import { PerformanceStatistics } from '../../performance';
@@ -42,14 +42,63 @@ export 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>(
@@ -353,7 +402,7 @@ export abstract class OCPPRequestService {
             return resolve(messagePayload);
           }
         }),
-        Constants.OCPP_WEBSOCKET_TIMEOUT,
+        OCPPConstants.OCPP_WEBSOCKET_TIMEOUT,
         new OCPPError(
           ErrorType.GENERIC_ERROR,
           `Timeout for message id '${messageId}'`,