From 7369e417367e4cc49fe81afb15cfe47eadca9459 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Thu, 21 Jul 2022 19:49:56 +0200 Subject: [PATCH] Ensure MeterValues command payload is properly formed MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- .../AutomaticTransactionGenerator.ts | 2 +- src/charging-station/ChargingStation.ts | 6 +++--- .../ocpp/1.6/OCPP16IncomingRequestService.ts | 13 +++++++----- .../ocpp/1.6/OCPP16RequestService.ts | 17 +++++++++++----- .../ocpp/1.6/OCPP16ResponseService.ts | 20 +++++++++++-------- 5 files changed, 36 insertions(+), 22 deletions(-) diff --git a/src/charging-station/AutomaticTransactionGenerator.ts b/src/charging-station/AutomaticTransactionGenerator.ts index d3517fa0..56c97eee 100644 --- a/src/charging-station/AutomaticTransactionGenerator.ts +++ b/src/charging-station/AutomaticTransactionGenerator.ts @@ -358,7 +358,7 @@ export default class AutomaticTransactionGenerator { >(this.chargingStation, RequestCommand.METER_VALUES, { connectorId, transactionId, - meterValue: transactionEndMeterValue, + meterValue: [transactionEndMeterValue], }); } stopResponse = await this.chargingStation.ocppRequestService.requestHandler< diff --git a/src/charging-station/ChargingStation.ts b/src/charging-station/ChargingStation.ts index 98cd5e83..63574b5c 100644 --- a/src/charging-station/ChargingStation.ts +++ b/src/charging-station/ChargingStation.ts @@ -1400,7 +1400,7 @@ export default class ChargingStation { } logger.debug( `${this.logPrefix()} << Command '${ - requestCommandName ?? '' + requestCommandName ?? 'unknown' }' received response payload: ${JSON.stringify(request)}` ); responseCallback(commandPayload, requestPayload); @@ -1430,7 +1430,7 @@ export default class ChargingStation { } logger.debug( `${this.logPrefix()} << Command '${ - requestCommandName ?? '' + requestCommandName ?? 'unknown' }' received error payload: ${JSON.stringify(request)}` ); errorCallback(new OCPPError(errorType, errorMessage, requestCommandName, errorDetails)); @@ -1723,7 +1723,7 @@ export default class ChargingStation { { connectorId, transactionId, - meterValue: transactionEndMeterValue, + meterValue: [transactionEndMeterValue], } ); } diff --git a/src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts b/src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts index e579395e..0159d745 100644 --- a/src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts +++ b/src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts @@ -149,7 +149,8 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer null, 2 )} while the charging station is in pending state on the central server`, - commandName + commandName, + commandPayload ); } if ( @@ -177,7 +178,8 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer null, 2 )}`, - commandName + commandName, + commandPayload ); } } else { @@ -188,7 +190,8 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer null, 2 )} while the charging station is not registered on the central server.`, - commandName + commandName, + commandPayload ); } // Send the built response @@ -253,7 +256,7 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer >(chargingStation, OCPP16RequestCommand.METER_VALUES, { connectorId, transactionId, - meterValue: transactionEndMeterValue, + meterValue: [transactionEndMeterValue], }); } const stopResponse = await chargingStation.ocppRequestService.requestHandler< @@ -840,7 +843,7 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer >(chargingStation, OCPP16RequestCommand.METER_VALUES, { connectorId, transactionId, - meterValue: transactionEndMeterValue, + meterValue: [transactionEndMeterValue], }); } await chargingStation.ocppRequestService.requestHandler< diff --git a/src/charging-station/ocpp/1.6/OCPP16RequestService.ts b/src/charging-station/ocpp/1.6/OCPP16RequestService.ts index 2a2d4dfc..3656e70f 100644 --- a/src/charging-station/ocpp/1.6/OCPP16RequestService.ts +++ b/src/charging-station/ocpp/1.6/OCPP16RequestService.ts @@ -41,7 +41,7 @@ export default class OCPP16RequestService extends OCPPRequestService { ErrorType.NOT_SUPPORTED, `${moduleName}.requestHandler: Unsupported OCPP command ${commandName}`, commandName, - { commandName } + commandParams ); } @@ -88,12 +88,19 @@ export default class OCPP16RequestService extends OCPPRequestService { case OCPP16RequestCommand.HEARTBEAT: return {} as unknown as Request; case OCPP16RequestCommand.METER_VALUES: + // Sanity check + if (!Array.isArray(commandParams?.meterValue)) { + throw new OCPPError( + ErrorType.TYPERAINT_VIOLATION, + `${moduleName}.buildRequestPayload ${commandName}: Invalid array type for meterValue payload field`, + commandName, + commandParams + ); + } return { connectorId: commandParams?.connectorId, transactionId: commandParams?.transactionId, - meterValue: Array.isArray(commandParams?.meterValue) - ? commandParams?.meterValue - : [commandParams?.meterValue], + meterValue: commandParams?.meterValue, } as unknown as Request; case OCPP16RequestCommand.STATUS_NOTIFICATION: return { @@ -139,7 +146,7 @@ export default class OCPP16RequestService extends OCPPRequestService { // eslint-disable-next-line @typescript-eslint/restrict-template-expressions `${moduleName}.buildRequestPayload: Unsupported OCPP command: ${commandName}`, commandName, - { commandName } + commandParams ); } } diff --git a/src/charging-station/ocpp/1.6/OCPP16ResponseService.ts b/src/charging-station/ocpp/1.6/OCPP16ResponseService.ts index fd972b26..38a29976 100644 --- a/src/charging-station/ocpp/1.6/OCPP16ResponseService.ts +++ b/src/charging-station/ocpp/1.6/OCPP16ResponseService.ts @@ -81,7 +81,8 @@ export default class OCPP16ResponseService extends OCPPResponseService { null, 2 )}`, - commandName + commandName, + payload ); } } else { @@ -92,7 +93,8 @@ export default class OCPP16ResponseService extends OCPPResponseService { null, 2 )} while the charging station is not registered on the central server. `, - commandName + commandName, + payload ); } } @@ -311,7 +313,7 @@ export default class OCPP16ResponseService extends OCPPResponseService { >(chargingStation, OCPP16RequestCommand.METER_VALUES, { connectorId, transactionId: payload.transactionId, - meterValue: chargingStation.getConnectorStatus(connectorId).transactionBeginMeterValue, + meterValue: [chargingStation.getConnectorStatus(connectorId).transactionBeginMeterValue], })); await chargingStation.ocppRequestService.requestHandler< OCPP16StatusNotificationRequest, @@ -407,11 +409,13 @@ export default class OCPP16ResponseService extends OCPPResponseService { >(chargingStation, OCPP16RequestCommand.METER_VALUES, { connectorId: transactionConnectorId, transactionId: requestPayload.transactionId, - meterValue: OCPP16ServiceUtils.buildTransactionEndMeterValue( - chargingStation, - transactionConnectorId, - requestPayload.meterStop - ), + meterValue: [ + OCPP16ServiceUtils.buildTransactionEndMeterValue( + chargingStation, + transactionConnectorId, + requestPayload.meterStop + ), + ], })); if ( !chargingStation.isChargingStationAvailable() || -- 2.34.1