From 6115f90f4964fd360604a405831a2cf5cdc33747 Mon Sep 17 00:00:00 2001 From: Jacob Mortensen Date: Fri, 5 Jul 2024 11:41:17 +0200 Subject: [PATCH] Refactor to avoid #return statements > 4 --- .../v16/profile/SmartChargingClientHandler.kt | 34 ++++++++++--------- 1 file changed, 18 insertions(+), 16 deletions(-) diff --git a/v16/src/jvmMain/kotlin/com/monta/ocpp/emulator/v16/profile/SmartChargingClientHandler.kt b/v16/src/jvmMain/kotlin/com/monta/ocpp/emulator/v16/profile/SmartChargingClientHandler.kt index 94a6092..159ca27 100644 --- a/v16/src/jvmMain/kotlin/com/monta/ocpp/emulator/v16/profile/SmartChargingClientHandler.kt +++ b/v16/src/jvmMain/kotlin/com/monta/ocpp/emulator/v16/profile/SmartChargingClientHandler.kt @@ -85,29 +85,15 @@ class SmartChargingClientHandler( } } - private suspend fun setDefaultChargingProfile( - ocppSessionInfo: OcppSession.Info, + private suspend fun validateSetDefaultChargingProfile( + connector: ChargePointConnectorDAO, request: SetChargingProfileRequest ): Boolean { - if (request.csChargingProfiles.chargingProfilePurpose != ChargingProfilePurposeType.TxDefaultProfile) { - return false - } - - val connector = getConnector( - ocppSessionInfo = ocppSessionInfo, - connectorId = request.connectorId - ) - - if (connector == null) { - return false - } - if (request.connectorId != 0) { GlobalLogger.warn(connector, "TxDefault must be on connector 0") return false } val chargingProfile = request.csChargingProfiles - if (chargingProfile.chargingProfilePurpose != ChargingProfilePurposeType.TxDefaultProfile) { GlobalLogger.warn(connector, "rejected charging profile, chargingProfilePurpose is not TxDefaultProfile") return false @@ -118,6 +104,22 @@ class SmartChargingClientHandler( return false } + return true + } + + private suspend fun setDefaultChargingProfile( + ocppSessionInfo: OcppSession.Info, + request: SetChargingProfileRequest + ): Boolean { + if (request.csChargingProfiles.chargingProfilePurpose != ChargingProfilePurposeType.TxDefaultProfile) { + return false + } + + val connector = getConnector(ocppSessionInfo = ocppSessionInfo, connectorId = request.connectorId) ?: return false + if (!validateSetDefaultChargingProfile(connector, request)) { + return false + } + val chargePoint = chargePointService.getByIdentity( ocppSessionInfo.identity )