From a4c9c33a1a56b39877b8bd2ee4d06c5d0208b98c Mon Sep 17 00:00:00 2001 From: Marc Emmers Date: Fri, 17 Nov 2023 09:47:35 +0100 Subject: [PATCH] Simplify the network priority update and add some comments Signed-off-by: Marc Emmers --- lib/ocpp/v201/charge_point.cpp | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/lib/ocpp/v201/charge_point.cpp b/lib/ocpp/v201/charge_point.cpp index ca0848001..c655fa445 100644 --- a/lib/ocpp/v201/charge_point.cpp +++ b/lib/ocpp/v201/charge_point.cpp @@ -836,42 +836,43 @@ void ChargePoint::next_network_configuration_priority() { } void ChargePoint::remove_network_connection_profiles_below_actual_security_profile() { - auto security_level = this->device_model->get_value(ControllerComponentVariables::SecurityProfile); + // Remove all the profiles that are a lower security level than security_level + const auto security_level = this->device_model->get_value(ControllerComponentVariables::SecurityProfile); auto network_connection_profiles = json::parse( this->device_model->get_value(ControllerComponentVariables::NetworkConnectionProfiles)); - auto check_security_level = [security_level](const SetNetworkProfileRequest& item) { + auto is_lower_security_level = [security_level](const SetNetworkProfileRequest& item) { return item.connectionData.securityProfile < security_level; }; network_connection_profiles.erase( - std::remove_if(network_connection_profiles.begin(), network_connection_profiles.end(), check_security_level), + std::remove_if(network_connection_profiles.begin(), network_connection_profiles.end(), is_lower_security_level), network_connection_profiles.end()); this->device_model->set_value(ControllerComponentVariables::NetworkConnectionProfiles.component, ControllerComponentVariables::NetworkConnectionProfiles.variable.value(), AttributeEnum::Actual, network_connection_profiles.dump()); - auto network_priority = ocpp::get_vector_from_csv( + // Update the NetworkConfigurationPriority so only remaining profiles are in there + const auto network_priority = ocpp::get_vector_from_csv( this->device_model->get_value(ControllerComponentVariables::NetworkConfigurationPriority)); - auto not_in_network_profiles = [&network_connection_profiles](const std::string& item) { + auto in_network_profiles = [&network_connection_profiles](const std::string& item) { auto is_same_slot = [&item](const SetNetworkProfileRequest& profile) { return std::to_string(profile.configurationSlot) == item; }; - return std::none_of(network_connection_profiles.begin(), network_connection_profiles.end(), is_same_slot); + return std::any_of(network_connection_profiles.begin(), network_connection_profiles.end(), is_same_slot); }; - network_priority.erase(std::remove_if(network_priority.begin(), network_priority.end(), not_in_network_profiles), - network_priority.end()); - std::string new_network_priority; for (const auto& item : network_priority) { - if (!new_network_priority.empty()) { - new_network_priority += ','; + if (in_network_profiles(item)) { + if (!new_network_priority.empty()) { + new_network_priority += ','; + } + new_network_priority += item; } - new_network_priority += item; } this->device_model->set_value(ControllerComponentVariables::NetworkConfigurationPriority.component,