Merge pull request #207 from jerome-benoit/fix-response-pending
[e-mobility-charging-stations-simulator.git] / src / charging-station / ocpp / OCPPRequestService.ts
index 4ab79d9d2bdb400cbbad9dca3f2befb7d93505b5..021e09f369edb855d5be7d161e512f073d3504a3 100644 (file)
@@ -34,6 +34,8 @@ export default abstract class OCPPRequestService {
     this.chargingStation = chargingStation;
     this.ocppResponseService = ocppResponseService;
     this.sendMessageHandler.bind(this);
+    this.sendResult.bind(this);
+    this.sendError.bind(this);
   }
 
   public static getInstance<T extends OCPPRequestService>(
@@ -124,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;
@@ -152,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);
@@ -300,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,
@@ -315,9 +334,10 @@ export default abstract class OCPPRequestService {
     }
   }
 
-  public abstract sendMessageHandler(
+  // eslint-disable-next-line @typescript-eslint/no-unused-vars
+  public abstract sendMessageHandler<Request extends JsonType, Response extends JsonType>(
     commandName: RequestCommand,
     commandParams?: JsonType,
     params?: SendParams
-  ): Promise<ResponseType>;
+  ): Promise<Response>;
 }