From: Jérôme Benoit Date: Wed, 13 Apr 2022 18:45:49 +0000 (+0200) Subject: Add OCPP commands payload logging in the logs X-Git-Tag: v1.1.57~7 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=6f35d2daed5ff5d26de15c1547ffa3dbd013c5b3;p=e-mobility-charging-stations-simulator.git Add OCPP commands payload logging in the logs Signed-off-by: Jérôme Benoit --- diff --git a/src/charging-station/ChargingStation.ts b/src/charging-station/ChargingStation.ts index 09677dd4..3f8870ae 100644 --- a/src/charging-station/ChargingStation.ts +++ b/src/charging-station/ChargingStation.ts @@ -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: diff --git a/src/charging-station/ocpp/1.6/OCPP16ResponseService.ts b/src/charging-station/ocpp/1.6/OCPP16ResponseService.ts index c7bf6fdb..c5971056 100644 --- a/src/charging-station/ocpp/1.6/OCPP16ResponseService.ts +++ b/src/charging-station/ocpp/1.6/OCPP16ResponseService.ts @@ -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 {} } diff --git a/src/charging-station/ocpp/OCPPRequestService.ts b/src/charging-station/ocpp/OCPPRequestService.ts index af23a1f4..ed3a8da7 100644 --- a/src/charging-station/ocpp/OCPPRequestService.ts +++ b/src/charging-station/ocpp/OCPPRequestService.ts @@ -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);