Ensure MeterValues command payload is properly formed
authorJérôme Benoit <jerome.benoit@sap.com>
Thu, 21 Jul 2022 17:49:56 +0000 (19:49 +0200)
committerJérôme Benoit <jerome.benoit@sap.com>
Thu, 21 Jul 2022 17:49:56 +0000 (19:49 +0200)
Signed-off-by: Jérôme Benoit <jerome.benoit@sap.com>
src/charging-station/AutomaticTransactionGenerator.ts
src/charging-station/ChargingStation.ts
src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts
src/charging-station/ocpp/1.6/OCPP16RequestService.ts
src/charging-station/ocpp/1.6/OCPP16ResponseService.ts

index d3517fa010d710308aa4b09bbc7b67ee7cb175a9..56c97eee83a1093ddb741ce85bb180769b972491 100644 (file)
@@ -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<
index 98cd5e8348ca2722d057aa9dd112e94bd04237cb..63574b5c617ed3915bf7e4bdfe6fbc9edcf1d8bf 100644 (file)
@@ -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],
               }
             );
           }
index e579395ebe7790c314e9f4f1cc59e3bbfa47fefc..0159d745b75287d82542fda77d3215868266ba42 100644 (file)
@@ -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<
index 2a2d4dfcf8f74e1217901348cbebe9d5b8a53199..3656e70f0980b33f689e0047a21c48a4def37812 100644 (file)
@@ -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
         );
     }
   }
index fd972b261b6cc6e7e1334462534f8c0a19161390..38a29976a115160c558bed39b03196cf16ddd5fe 100644 (file)
@@ -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() ||