Use eslint extension for import sorting instead of unmaintained external ones
[e-mobility-charging-stations-simulator.git] / src / charging-station / ocpp / OCPPRequestService.ts
index f8b6b227a90b3936e3a167d9f22f91ef21138fd5..6695fcbc010e451a85740966da781f8fee392165 100644 (file)
@@ -1,4 +1,10 @@
-import { ErrorResponse, Response } from '../../types/ocpp/Responses';
+import OCPPError from '../../exception/OCPPError';
+import PerformanceStatistics from '../../performance/PerformanceStatistics';
+import { EmptyObject } from '../../types/EmptyObject';
+import { HandleErrorParams } from '../../types/Error';
+import { JsonObject, JsonType } from '../../types/JsonType';
+import { ErrorType } from '../../types/ocpp/ErrorType';
+import { MessageType } from '../../types/ocpp/MessageType';
 import {
   IncomingRequestCommand,
   OutgoingRequest,
@@ -6,34 +12,19 @@ import {
   RequestParams,
   ResponseType,
 } from '../../types/ocpp/Requests';
-import { JsonObject, JsonType } from '../../types/JsonType';
-
-import type ChargingStation from '../ChargingStation';
+import { ErrorResponse, Response } from '../../types/ocpp/Responses';
 import Constants from '../../utils/Constants';
-import { EmptyObject } from '../../types/EmptyObject';
-import { ErrorType } from '../../types/ocpp/ErrorType';
-import { HandleErrorParams } from '../../types/Error';
-import { MessageType } from '../../types/ocpp/MessageType';
-import OCPPError from '../../exception/OCPPError';
-import type OCPPResponseService from './OCPPResponseService';
-import PerformanceStatistics from '../../performance/PerformanceStatistics';
-import Utils from '../../utils/Utils';
 import logger from '../../utils/Logger';
+import Utils from '../../utils/Utils';
+import type ChargingStation from '../ChargingStation';
+import type OCPPResponseService from './OCPPResponseService';
 
 export default abstract class OCPPRequestService {
-  private static readonly instances: Map<string, OCPPRequestService> = new Map<
-    string,
-    OCPPRequestService
-  >();
+  private static instance: OCPPRequestService | null = null;
 
-  protected readonly chargingStation: ChargingStation;
   private readonly ocppResponseService: OCPPResponseService;
 
-  protected constructor(
-    chargingStation: ChargingStation,
-    ocppResponseService: OCPPResponseService
-  ) {
-    this.chargingStation = chargingStation;
+  protected constructor(ocppResponseService: OCPPResponseService) {
     this.ocppResponseService = ocppResponseService;
     this.requestHandler.bind(this);
     this.sendResponse.bind(this);
@@ -41,20 +32,17 @@ export default abstract class OCPPRequestService {
   }
 
   public static getInstance<T extends OCPPRequestService>(
-    this: new (chargingStation: ChargingStation, ocppResponseService: OCPPResponseService) => T,
-    chargingStation: ChargingStation,
+    this: new (ocppResponseService: OCPPResponseService) => T,
     ocppResponseService: OCPPResponseService
   ): T {
-    if (!OCPPRequestService.instances.has(chargingStation.hashId)) {
-      OCPPRequestService.instances.set(
-        chargingStation.hashId,
-        new this(chargingStation, ocppResponseService)
-      );
+    if (!OCPPRequestService.instance) {
+      OCPPRequestService.instance = new this(ocppResponseService);
     }
-    return OCPPRequestService.instances.get(chargingStation.hashId) as T;
+    return OCPPRequestService.instance as T;
   }
 
   public async sendResponse(
+    chargingStation: ChargingStation,
     messageId: string,
     messagePayload: JsonType,
     commandName: IncomingRequestCommand
@@ -62,17 +50,19 @@ export default abstract class OCPPRequestService {
     try {
       // Send response message
       return await this.internalSendMessage(
+        chargingStation,
         messageId,
         messagePayload,
         MessageType.CALL_RESULT_MESSAGE,
         commandName
       );
     } catch (error) {
-      this.handleRequestError(commandName, error as Error);
+      this.handleRequestError(chargingStation, commandName, error as Error);
     }
   }
 
   public async sendError(
+    chargingStation: ChargingStation,
     messageId: string,
     ocppError: OCPPError,
     commandName: RequestCommand | IncomingRequestCommand
@@ -80,17 +70,19 @@ export default abstract class OCPPRequestService {
     try {
       // Send error message
       return await this.internalSendMessage(
+        chargingStation,
         messageId,
         ocppError,
         MessageType.CALL_ERROR_MESSAGE,
         commandName
       );
     } catch (error) {
-      this.handleRequestError(commandName, error as Error);
+      this.handleRequestError(chargingStation, commandName, error as Error);
     }
   }
 
   protected async sendMessage(
+    chargingStation: ChargingStation,
     messageId: string,
     messagePayload: JsonType,
     commandName: RequestCommand,
@@ -101,6 +93,7 @@ export default abstract class OCPPRequestService {
   ): Promise<ResponseType> {
     try {
       return await this.internalSendMessage(
+        chargingStation,
         messageId,
         messagePayload,
         MessageType.CALL_MESSAGE,
@@ -108,11 +101,12 @@ export default abstract class OCPPRequestService {
         params
       );
     } catch (error) {
-      this.handleRequestError(commandName, error as Error, { throwError: false });
+      this.handleRequestError(chargingStation, commandName, error as Error, { throwError: false });
     }
   }
 
   private async internalSendMessage(
+    chargingStation: ChargingStation,
     messageId: string,
     messagePayload: JsonType | OCPPError,
     messageType: MessageType,
@@ -123,12 +117,10 @@ export default abstract class OCPPRequestService {
     }
   ): Promise<ResponseType> {
     if (
-      (this.chargingStation.isInUnknownState() &&
-        commandName === RequestCommand.BOOT_NOTIFICATION) ||
-      (!this.chargingStation.getOcppStrictCompliance() &&
-        this.chargingStation.isInUnknownState()) ||
-      this.chargingStation.isInAcceptedState() ||
-      (this.chargingStation.isInPendingState() &&
+      (chargingStation.isInUnknownState() && commandName === RequestCommand.BOOT_NOTIFICATION) ||
+      (!chargingStation.getOcppStrictCompliance() && chargingStation.isInUnknownState()) ||
+      chargingStation.isInAcceptedState() ||
+      (chargingStation.isInPendingState() &&
         (params.triggerMessage || messageType === MessageType.CALL_RESULT_MESSAGE))
     ) {
       // eslint-disable-next-line @typescript-eslint/no-this-alias
@@ -137,6 +129,7 @@ export default abstract class OCPPRequestService {
       return Utils.promiseWithTimeout(
         new Promise((resolve, reject) => {
           const messageToSend = this.buildMessageToSend(
+            chargingStation,
             messageId,
             messagePayload,
             messageType,
@@ -144,27 +137,24 @@ export default abstract class OCPPRequestService {
             responseCallback,
             errorCallback
           );
-          if (this.chargingStation.getEnableStatistics()) {
-            this.chargingStation.performanceStatistics.addRequestStatistic(
-              commandName,
-              messageType
-            );
+          if (chargingStation.getEnableStatistics()) {
+            chargingStation.performanceStatistics.addRequestStatistic(commandName, messageType);
           }
           // Check if wsConnection opened
-          if (this.chargingStation.isWebSocketConnectionOpened()) {
+          if (chargingStation.isWebSocketConnectionOpened()) {
             // Yes: Send Message
             const beginId = PerformanceStatistics.beginMeasure(commandName);
             // FIXME: Handle sending error
-            this.chargingStation.wsConnection.send(messageToSend);
+            chargingStation.wsConnection.send(messageToSend);
             PerformanceStatistics.endMeasure(commandName, beginId);
             logger.debug(
-              `${this.chargingStation.logPrefix()} >> Command '${commandName}' sent ${this.getMessageTypeString(
+              `${chargingStation.logPrefix()} >> Command '${commandName}' sent ${this.getMessageTypeString(
                 messageType
               )} payload: ${messageToSend}`
             );
           } else if (!params.skipBufferingOnError) {
             // Buffer it
-            this.chargingStation.bufferMessage(messageToSend);
+            chargingStation.bufferMessage(messageToSend);
             const ocppError = new OCPPError(
               ErrorType.GENERIC_ERROR,
               `WebSocket closed for buffered message id '${messageId}' with content '${messageToSend}'`,
@@ -204,8 +194,8 @@ export default abstract class OCPPRequestService {
             payload: JsonType,
             requestPayload: JsonType
           ): Promise<void> {
-            if (self.chargingStation.getEnableStatistics()) {
-              self.chargingStation.performanceStatistics.addRequestStatistic(
+            if (chargingStation.getEnableStatistics()) {
+              chargingStation.performanceStatistics.addRequestStatistic(
                 commandName,
                 MessageType.CALL_RESULT_MESSAGE
               );
@@ -213,6 +203,7 @@ export default abstract class OCPPRequestService {
             // Handle the request's response
             try {
               await self.ocppResponseService.responseHandler(
+                chargingStation,
                 commandName as RequestCommand,
                 payload,
                 requestPayload
@@ -221,7 +212,7 @@ export default abstract class OCPPRequestService {
             } catch (error) {
               reject(error);
             } finally {
-              self.chargingStation.requests.delete(messageId);
+              chargingStation.requests.delete(messageId);
             }
           }
 
@@ -232,19 +223,19 @@ export default abstract class OCPPRequestService {
            * @param requestStatistic
            */
           function errorCallback(error: OCPPError, requestStatistic = true): void {
-            if (requestStatistic && self.chargingStation.getEnableStatistics()) {
-              self.chargingStation.performanceStatistics.addRequestStatistic(
+            if (requestStatistic && chargingStation.getEnableStatistics()) {
+              chargingStation.performanceStatistics.addRequestStatistic(
                 commandName,
                 MessageType.CALL_ERROR_MESSAGE
               );
             }
             logger.error(
-              `${self.chargingStation.logPrefix()} Error %j occurred when calling command %s with message data %j`,
+              `${chargingStation.logPrefix()} Error %j occurred when calling command %s with message data %j`,
               error,
               commandName,
               messagePayload
             );
-            self.chargingStation.requests.delete(messageId);
+            chargingStation.requests.delete(messageId);
             reject(error);
           }
         }),
@@ -256,19 +247,19 @@ export default abstract class OCPPRequestService {
           (messagePayload as JsonObject)?.details ?? {}
         ),
         () => {
-          messageType === MessageType.CALL_MESSAGE &&
-            this.chargingStation.requests.delete(messageId);
+          messageType === MessageType.CALL_MESSAGE && chargingStation.requests.delete(messageId);
         }
       );
     }
     throw new OCPPError(
       ErrorType.SECURITY_ERROR,
-      `Cannot send command ${commandName} payload when the charging station is in ${this.chargingStation.getRegistrationStatus()} state on the central server`,
+      `Cannot send command ${commandName} payload when the charging station is in ${chargingStation.getRegistrationStatus()} state on the central server`,
       commandName
     );
   }
 
   private buildMessageToSend(
+    chargingStation: ChargingStation,
     messageId: string,
     messagePayload: JsonType | OCPPError,
     messageType: MessageType,
@@ -282,7 +273,7 @@ export default abstract class OCPPRequestService {
       // Request
       case MessageType.CALL_MESSAGE:
         // Build request
-        this.chargingStation.requests.set(messageId, [
+        chargingStation.requests.set(messageId, [
           responseCallback,
           errorCallback,
           commandName,
@@ -327,15 +318,12 @@ export default abstract class OCPPRequestService {
   }
 
   private handleRequestError(
+    chargingStation: ChargingStation,
     commandName: RequestCommand | IncomingRequestCommand,
     error: Error,
     params: HandleErrorParams<EmptyObject> = { throwError: true }
   ): void {
-    logger.error(
-      this.chargingStation.logPrefix() + ' Request command %s error: %j',
-      commandName,
-      error
-    );
+    logger.error(chargingStation.logPrefix() + ' Request command %s error: %j', commandName, error);
     if (params?.throwError) {
       throw error;
     }
@@ -343,6 +331,7 @@ export default abstract class OCPPRequestService {
 
   // eslint-disable-next-line @typescript-eslint/no-unused-vars
   public abstract requestHandler<Request extends JsonType, Response extends JsonType>(
+    chargingStation: ChargingStation,
     commandName: RequestCommand,
     commandParams?: JsonType,
     params?: RequestParams