Factor out OCPP message type to string method
[e-mobility-charging-stations-simulator.git] / src / charging-station / ocpp / OCPPRequestService.ts
index 9dd0e7bf9c8fdd5744d824846d190ced6e6d5247..2813d39aad5c0ca89f4e6af9ffc31615182b4dfc 100644 (file)
@@ -16,7 +16,6 @@ import OCPPError from '../../exception/OCPPError';
 import type OCPPResponseService from './OCPPResponseService';
 import PerformanceStatistics from '../../performance/PerformanceStatistics';
 import Utils from '../../utils/Utils';
-import chalk from 'chalk';
 import logger from '../../utils/Logger';
 
 export default abstract class OCPPRequestService {
@@ -152,10 +151,14 @@ export default abstract class OCPPRequestService {
           if (this.chargingStation.isWebSocketConnectionOpened()) {
             // Yes: Send Message
             const beginId = PerformanceStatistics.beginMeasure(commandName);
-            console.log(chalk`{blue >> Sending message = ${messageToSend}}`);
             // 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);
@@ -304,6 +307,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,
@@ -319,7 +333,8 @@ export default abstract class OCPPRequestService {
     }
   }
 
-  public abstract sendMessageHandler<Response extends JsonType>(
+  // eslint-disable-next-line @typescript-eslint/no-unused-vars
+  public abstract sendMessageHandler<Request extends JsonType, Response extends JsonType>(
     commandName: RequestCommand,
     commandParams?: JsonType,
     params?: SendParams