Rename a type definition
[e-mobility-charging-stations-simulator.git] / src / charging-station / ocpp / OCPPRequestService.ts
index e5047d33e2ec8d05a1a6021b6d921d5df0cded17..ef1e4749d56151d05eafd7ed438236018d8fcec1 100644 (file)
@@ -1,9 +1,8 @@
 import {
-  DiagnosticsStatus,
   IncomingRequestCommand,
   RequestCommand,
+  RequestParams,
   ResponseType,
-  SendParams,
 } from '../../types/ocpp/Requests';
 
 import type ChargingStation from '../ChargingStation';
@@ -34,7 +33,9 @@ export default abstract class OCPPRequestService {
   ) {
     this.chargingStation = chargingStation;
     this.ocppResponseService = ocppResponseService;
-    this.sendMessageHandler.bind(this);
+    this.requestHandler.bind(this);
+    this.sendResult.bind(this);
+    this.sendError.bind(this);
   }
 
   public static getInstance<T extends OCPPRequestService>(
@@ -42,13 +43,13 @@ export default abstract class OCPPRequestService {
     chargingStation: ChargingStation,
     ocppResponseService: OCPPResponseService
   ): T {
-    if (!OCPPRequestService.instances.has(chargingStation.id)) {
+    if (!OCPPRequestService.instances.has(chargingStation.hashId)) {
       OCPPRequestService.instances.set(
-        chargingStation.id,
+        chargingStation.hashId,
         new this(chargingStation, ocppResponseService)
       );
     }
-    return OCPPRequestService.instances.get(chargingStation.id) as T;
+    return OCPPRequestService.instances.get(chargingStation.hashId) as T;
   }
 
   public async sendResult(
@@ -91,7 +92,7 @@ export default abstract class OCPPRequestService {
     messageId: string,
     messagePayload: JsonType,
     commandName: RequestCommand,
-    params: SendParams = {
+    params: RequestParams = {
       skipBufferingOnError: false,
       triggerMessage: false,
     }
@@ -114,7 +115,7 @@ export default abstract class OCPPRequestService {
     messagePayload: JsonType | OCPPError,
     messageType: MessageType,
     commandName?: RequestCommand | IncomingRequestCommand,
-    params: SendParams = {
+    params: RequestParams = {
       skipBufferingOnError: false,
       triggerMessage: false,
     }
@@ -125,7 +126,8 @@ export default abstract class OCPPRequestService {
       (!this.chargingStation.getOcppStrictCompliance() &&
         this.chargingStation.isInUnknownState()) ||
       this.chargingStation.isInAcceptedState() ||
-      (this.chargingStation.isInPendingState() && params.triggerMessage)
+      (this.chargingStation.isInPendingState() &&
+        (params.triggerMessage || messageType === MessageType.CALL_RESULT_MESSAGE))
     ) {
       // eslint-disable-next-line @typescript-eslint/no-this-alias
       const self = this;
@@ -153,6 +155,11 @@ export default abstract class OCPPRequestService {
             // FIXME: Handle sending error
             this.chargingStation.wsConnection.send(messageToSend);
             PerformanceStatistics.endMeasure(commandName, beginId);
+            logger.debug(
+              `${this.chargingStation.logPrefix()} >> Command '${commandName}' sent ${this.getMessageTypeString(
+                messageType
+              )} payload: ${messageToSend}`
+            );
           } else if (!params.skipBufferingOnError) {
             // Buffer it
             this.chargingStation.bufferMessage(messageToSend);
@@ -203,7 +210,7 @@ export default abstract class OCPPRequestService {
             }
             // Handle the request's response
             try {
-              await self.ocppResponseService.handleResponse(
+              await self.ocppResponseService.responseHandler(
                 commandName as RequestCommand,
                 payload,
                 requestPayload
@@ -211,7 +218,6 @@ export default abstract class OCPPRequestService {
               resolve(payload);
             } catch (error) {
               reject(error);
-              throw error;
             } finally {
               self.chargingStation.requests.delete(messageId);
             }
@@ -302,6 +308,17 @@ export default abstract class OCPPRequestService {
     return messageToSend;
   }
 
+  private getMessageTypeString(messageType: MessageType): string {
+    switch (messageType) {
+      case MessageType.CALL_MESSAGE:
+        return 'request';
+      case MessageType.CALL_RESULT_MESSAGE:
+        return 'response';
+      case MessageType.CALL_ERROR_MESSAGE:
+        return 'error';
+    }
+  }
+
   private handleRequestError(
     commandName: RequestCommand | IncomingRequestCommand,
     error: Error,
@@ -317,9 +334,10 @@ export default abstract class OCPPRequestService {
     }
   }
 
-  public abstract sendMessageHandler(
+  // eslint-disable-next-line @typescript-eslint/no-unused-vars
+  public abstract requestHandler<Request extends JsonType, Response extends JsonType>(
     commandName: RequestCommand,
     commandParams?: JsonType,
-    params?: SendParams
-  ): Promise<ResponseType>;
+    params?: RequestParams
+  ): Promise<Response>;
 }