Add OCPP commands payload logging in the logs
authorJérôme Benoit <jerome.benoit@sap.com>
Wed, 13 Apr 2022 18:45:49 +0000 (20:45 +0200)
committerJérôme Benoit <jerome.benoit@sap.com>
Wed, 13 Apr 2022 18:45:49 +0000 (20:45 +0200)
Signed-off-by: Jérôme Benoit <jerome.benoit@piment-noir.org>
src/charging-station/ChargingStation.ts
src/charging-station/ocpp/1.6/OCPP16ResponseService.ts
src/charging-station/ocpp/OCPPRequestService.ts

index 09677dd448851342a83879d7fa2a7b601ffd031e..3f8870aed541262d3fd592478c184483d2b6f766 100644 (file)
@@ -76,7 +76,6 @@ import { SampledValueTemplate } from '../types/MeasurandPerPhaseSampledValueTemp
 import { SupervisionUrlDistribution } from '../types/ConfigurationData';
 import { URL } from 'url';
 import Utils from '../utils/Utils';
-import chalk from 'chalk';
 import crypto from 'crypto';
 import fs from 'fs';
 import logger from '../utils/Logger';
@@ -1487,8 +1486,6 @@ export default class ChargingStation {
     let errMsg: string;
     try {
       const request = JSON.parse(data.toString()) as IncomingRequest;
-      const requestAsString = JSON.stringify(request);
-      console.log(chalk`{yellow << Received message = ${requestAsString}}`);
       if (Utils.isIterable(request)) {
         // Parse the message
         [messageType, messageId, commandName, commandPayload, errorDetails] = request;
@@ -1496,7 +1493,8 @@ export default class ChargingStation {
         throw new OCPPError(
           ErrorType.PROTOCOL_ERROR,
           'Incoming request is not iterable',
-          commandName
+          Utils.isString(commandName) && commandName,
+          { payload: request }
         );
       }
       // Check the Type of message
@@ -1512,18 +1510,23 @@ export default class ChargingStation {
             commandName,
             commandPayload
           );
+          logger.debug(
+            `${this.logPrefix()} << Command '${commandName}' received request payload: ${JSON.stringify(
+              request
+            )}`
+          );
           break;
         // Outcome Message
         case MessageType.CALL_RESULT_MESSAGE:
           // Respond
           cachedRequest = this.requests.get(messageId);
           if (Utils.isIterable(cachedRequest)) {
-            [responseCallback, , , requestPayload] = cachedRequest;
+            [responseCallback, , requestCommandName, requestPayload] = cachedRequest;
           } else {
             throw new OCPPError(
               ErrorType.PROTOCOL_ERROR,
               `Cached request for message id ${messageId} response is not iterable`,
-              commandName
+              requestCommandName
             );
           }
           if (!responseCallback) {
@@ -1531,10 +1534,15 @@ export default class ChargingStation {
             throw new OCPPError(
               ErrorType.INTERNAL_ERROR,
               `Response for unknown message id ${messageId}`,
-              commandName
+              requestCommandName
             );
           }
           responseCallback(commandName, requestPayload);
+          logger.debug(
+            `${this.logPrefix()} << Command '${requestCommandName}' received response payload: ${JSON.stringify(
+              request
+            )}`
+          );
           break;
         // Error Message
         case MessageType.CALL_ERROR_MESSAGE:
@@ -1558,6 +1566,11 @@ export default class ChargingStation {
           rejectCallback(
             new OCPPError(commandName, commandPayload.toString(), requestCommandName, errorDetails)
           );
+          logger.debug(
+            `${this.logPrefix()} << Command '${requestCommandName}' received error payload: ${JSON.stringify(
+              request
+            )}`
+          );
           break;
         // Error
         default:
index c7bf6fdb74dc592414aebbcba4f644e90cca494d..c59710560cc956ad3b79b13e3d8a3ee76a7833d8 100644 (file)
@@ -138,17 +138,8 @@ export default class OCPP16ResponseService extends OCPPResponseService {
     }
   }
 
-  private handleResponseHeartbeat(
-    payload: OCPP16HeartbeatResponse,
-    requestPayload: OCPP16HeartbeatRequest
-  ): void {
-    logger.debug(
-      this.chargingStation.logPrefix() +
-        ' Heartbeat response received: %j to Heartbeat request: %j',
-      payload,
-      requestPayload
-    );
-  }
+  // eslint-disable-next-line @typescript-eslint/no-empty-function
+  private handleResponseHeartbeat(): void {}
 
   private handleResponseAuthorize(
     payload: OCPP16AuthorizeResponse,
@@ -474,27 +465,9 @@ export default class OCPP16ResponseService extends OCPPResponseService {
     }
   }
 
-  private handleResponseStatusNotification(
-    payload: OCPP16StatusNotificationRequest,
-    requestPayload: OCPP16StatusNotificationResponse
-  ): void {
-    logger.debug(
-      this.chargingStation.logPrefix() +
-        ' Status notification response received: %j to StatusNotification request: %j',
-      payload,
-      requestPayload
-    );
-  }
+  // eslint-disable-next-line @typescript-eslint/no-empty-function
+  private handleResponseStatusNotification(): void {}
 
-  private handleResponseMeterValues(
-    payload: OCPP16MeterValuesRequest,
-    requestPayload: OCPP16MeterValuesResponse
-  ): void {
-    logger.debug(
-      this.chargingStation.logPrefix() +
-        ' MeterValues response received: %j to MeterValues request: %j',
-      payload,
-      requestPayload
-    );
-  }
+  // eslint-disable-next-line @typescript-eslint/no-empty-function
+  private handleResponseMeterValues(): void {}
 }
index af23a1f4fcbdd55ac4c357dfc67c12e3c35d7e8e..ed3a8da7461d06bdeee98bbc6a8ba834de6f72aa 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,24 @@ 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);
+            let msgTypeStr: string;
+            switch (messageType) {
+              case MessageType.CALL_MESSAGE:
+                msgTypeStr = 'request';
+                break;
+              case MessageType.CALL_RESULT_MESSAGE:
+                msgTypeStr = 'response';
+                break;
+              case MessageType.CALL_ERROR_MESSAGE:
+                msgTypeStr = 'error';
+                break;
+            }
+            logger.debug(
+              `${this.chargingStation.logPrefix()} >> Command '${commandName}' sent ${msgTypeStr} payload: ${messageToSend}`
+            );
           } else if (!params.skipBufferingOnError) {
             // Buffer it
             this.chargingStation.bufferMessage(messageToSend);