Cleanup and optimize OCPP request payload building
authorJérôme Benoit <jerome.benoit@sap.com>
Sat, 14 Jan 2023 23:10:08 +0000 (00:10 +0100)
committerJérôme Benoit <jerome.benoit@sap.com>
Sat, 14 Jan 2023 23:10:08 +0000 (00:10 +0100)
Signed-off-by: Jérôme Benoit <jerome.benoit@sap.com>
src/charging-station/ocpp/1.6/OCPP16RequestService.ts
src/charging-station/ocpp/2.0/OCPP20RequestService.ts

index 9fa6124312ac12a14376c80c17504f717b65455e..9979c70aa8101f41facf955a637b5d3fc2bd5b52 100644 (file)
@@ -147,118 +147,54 @@ export default class OCPP16RequestService extends OCPPRequestService {
     let energyActiveImportRegister: number;
     commandParams = commandParams as JsonObject;
     switch (commandName) {
-      case OCPP16RequestCommand.AUTHORIZE:
-        return {
-          ...(!Utils.isUndefined(commandParams?.idTag)
-            ? { idTag: commandParams.idTag }
-            : { idTag: Constants.DEFAULT_IDTAG }),
-        } as unknown as Request;
       case OCPP16RequestCommand.BOOT_NOTIFICATION:
-        return {
-          chargePointModel: commandParams?.chargePointModel,
-          chargePointVendor: commandParams?.chargePointVendor,
-          ...(!Utils.isUndefined(commandParams?.chargeBoxSerialNumber) && {
-            chargeBoxSerialNumber: commandParams.chargeBoxSerialNumber,
-          }),
-          ...(!Utils.isUndefined(commandParams?.chargePointSerialNumber) && {
-            chargePointSerialNumber: commandParams.chargePointSerialNumber,
-          }),
-          ...(!Utils.isUndefined(commandParams?.firmwareVersion) && {
-            firmwareVersion: commandParams.firmwareVersion,
-          }),
-          ...(!Utils.isUndefined(commandParams?.iccid) && { iccid: commandParams.iccid }),
-          ...(!Utils.isUndefined(commandParams?.imsi) && { imsi: commandParams.imsi }),
-          ...(!Utils.isUndefined(commandParams?.meterSerialNumber) && {
-            meterSerialNumber: commandParams.meterSerialNumber,
-          }),
-          ...(!Utils.isUndefined(commandParams?.meterType) && {
-            meterType: commandParams.meterType,
-          }),
-        } as unknown as Request;
       case OCPP16RequestCommand.DIAGNOSTICS_STATUS_NOTIFICATION:
       case OCPP16RequestCommand.FIRMWARE_STATUS_NOTIFICATION:
-        return {
-          status: commandParams?.status,
-        } as unknown as Request;
-      case OCPP16RequestCommand.HEARTBEAT:
-        return OCPPConstants.OCPP_REQUEST_EMPTY as unknown as Request;
       case OCPP16RequestCommand.METER_VALUES:
-        return {
-          connectorId: commandParams?.connectorId,
-          transactionId: commandParams?.transactionId,
-          meterValue: commandParams?.meterValue,
-        } as unknown as Request;
       case OCPP16RequestCommand.STATUS_NOTIFICATION:
+      case OCPP16RequestCommand.DATA_TRANSFER:
+        return commandParams as unknown as Request;
+      case OCPP16RequestCommand.AUTHORIZE:
         return {
-          connectorId: commandParams?.connectorId,
-          status: commandParams?.status,
-          errorCode: commandParams?.errorCode,
-          ...(!Utils.isUndefined(commandParams?.info) && {
-            info: commandParams?.info,
-          }),
-          ...(!Utils.isUndefined(commandParams?.timestamp) && {
-            timestamp: commandParams?.timestamp,
-          }),
-          ...(!Utils.isUndefined(commandParams?.vendorId) && {
-            vendorId: commandParams?.vendorId,
-          }),
-          ...(!Utils.isUndefined(commandParams?.vendorErrorCode) && {
-            vendorErrorCode: commandParams?.vendorErrorCode,
-          }),
+          idTag: Constants.DEFAULT_IDTAG,
+          ...commandParams,
         } as unknown as Request;
+      case OCPP16RequestCommand.HEARTBEAT:
+        return OCPPConstants.OCPP_REQUEST_EMPTY as unknown as Request;
       case OCPP16RequestCommand.START_TRANSACTION:
         return {
-          connectorId: commandParams?.connectorId,
-          ...(!Utils.isUndefined(commandParams?.idTag)
-            ? { idTag: commandParams?.idTag }
-            : { idTag: Constants.DEFAULT_IDTAG }),
-          meterStart:
-            commandParams?.meterStart ??
-            chargingStation.getEnergyActiveImportRegisterByConnectorId(
-              commandParams?.connectorId as number
-            ),
-          timestamp: commandParams?.timestamp ?? new Date(),
-          ...(!Utils.isUndefined(commandParams?.reservationId) && {
-            reservationId: commandParams?.reservationId,
-          }),
+          idTag: Constants.DEFAULT_IDTAG,
+          meterStart: chargingStation.getEnergyActiveImportRegisterByConnectorId(
+            commandParams?.connectorId as number
+          ),
+          timestamp: new Date(),
+          ...commandParams,
         } as unknown as Request;
       case OCPP16RequestCommand.STOP_TRANSACTION:
         chargingStation.getTransactionDataMeterValues() &&
-          Utils.isNullOrUndefined(commandParams?.transactionData) &&
           (connectorId = chargingStation.getConnectorIdByTransactionId(
             commandParams?.transactionId as number
           ));
-        Utils.isNullOrUndefined(commandParams?.meterStop) &&
-          (energyActiveImportRegister =
-            chargingStation.getEnergyActiveImportRegisterByTransactionId(
-              commandParams?.transactionId as number,
-              true
-            ));
+        energyActiveImportRegister = chargingStation.getEnergyActiveImportRegisterByTransactionId(
+          commandParams?.transactionId as number,
+          true
+        );
         return {
-          transactionId: commandParams?.transactionId,
-          idTag:
-            commandParams?.idTag ??
-            chargingStation.getTransactionIdTag(commandParams?.transactionId as number),
-          meterStop: commandParams?.meterStop ?? energyActiveImportRegister,
-          timestamp: commandParams?.timestamp ?? new Date(),
-          ...(!Utils.isUndefined(commandParams?.reason) && {
-            reason: commandParams?.reason,
+          idTag: chargingStation.getTransactionIdTag(commandParams?.transactionId as number),
+          meterStop: energyActiveImportRegister,
+          timestamp: new Date(),
+          ...(chargingStation.getTransactionDataMeterValues() && {
+            transactionData: OCPP16ServiceUtils.buildTransactionDataMeterValues(
+              chargingStation.getConnectorStatus(connectorId).transactionBeginMeterValue,
+              OCPP16ServiceUtils.buildTransactionEndMeterValue(
+                chargingStation,
+                connectorId,
+                energyActiveImportRegister
+              )
+            ),
           }),
-          ...(!Utils.isUndefined(commandParams?.transactionData)
-            ? { transactionData: commandParams?.transactionData }
-            : chargingStation.getTransactionDataMeterValues() && {
-                transactionData: OCPP16ServiceUtils.buildTransactionDataMeterValues(
-                  chargingStation.getConnectorStatus(connectorId).transactionBeginMeterValue,
-                  OCPP16ServiceUtils.buildTransactionEndMeterValue(
-                    chargingStation,
-                    connectorId,
-                    (commandParams?.meterStop as number) ?? energyActiveImportRegister
-                  )
-                ),
-              }),
+          ...commandParams,
         } as unknown as Request;
-      case OCPP16RequestCommand.DATA_TRANSFER:
-        return commandParams as unknown as Request;
       default:
         // OCPPError usage here is debatable: it's an error in the OCPP stack but not targeted to sendError().
         throw new OCPPError(
index c3f5d5d38968d6e1768b30860cbe4b217adff4fa..eddbcf8da142d243c3c28910ea10b798a25359e4 100644 (file)
@@ -94,39 +94,13 @@ export default class OCPP20RequestService extends OCPPRequestService {
     commandParams = commandParams as JsonObject;
     switch (commandName) {
       case OCPP20RequestCommand.BOOT_NOTIFICATION:
-        commandParams.chargingStation = commandParams.chargingStation as JsonObject;
-        commandParams.chargingStation.modem = commandParams.chargingStation.modem as JsonObject;
-        return {
-          reason: commandParams?.reason,
-          chargingStation: {
-            model: commandParams?.chargingStation?.model,
-            vendorName: commandParams?.chargingStation?.vendorName,
-            ...(!Utils.isUndefined(commandParams?.chargingStation?.firmwareVersion) && {
-              firmwareVersion: commandParams.chargingStation?.firmwareVersion,
-            }),
-            ...(!Utils.isUndefined(commandParams?.chargingStation?.serialNumber) && {
-              serialNumber: commandParams.chargingStation?.serialNumber,
-            }),
-            ...(!Utils.isUndefined(commandParams?.chargingStation?.modem) && {
-              modem: {
-                ...(!Utils.isUndefined(commandParams?.chargingStation?.modem?.iccid) && {
-                  iccid: commandParams.chargingStation.modem.iccid,
-                }),
-                ...(!Utils.isUndefined(commandParams?.chargingStation?.modem?.imsi) && {
-                  imsi: commandParams.chargingStation.modem.imsi,
-                }),
-              },
-            }),
-          },
-        } as unknown as Request;
+        return commandParams as unknown as Request;
       case OCPP20RequestCommand.HEARTBEAT:
         return OCPPConstants.OCPP_RESPONSE_EMPTY as unknown as Request;
       case OCPP20RequestCommand.STATUS_NOTIFICATION:
         return {
-          timestamp: commandParams?.timestamp ?? new Date(),
-          connectorStatus: commandParams?.connectorStatus,
-          evseId: commandParams?.evseId,
-          connectorId: commandParams?.connectorId,
+          timestamp: new Date(),
+          ...commandParams,
         } as unknown as Request;
       default:
         // OCPPError usage here is debatable: it's an error in the OCPP stack but not targeted to sendError().