Skip to content

Commit 8aee7fa

Browse files
committed
[smartcharging] Fix setpoint computation + add unit tests
1 parent 6f2c7c3 commit 8aee7fa

File tree

4 files changed

+952
-29
lines changed

4 files changed

+952
-29
lines changed

src/chargepoint/smartcharging/ProfileDatabase.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ class ProfileDatabase
5151

5252
/** @brief Stores a profile alongside its target connector */
5353
typedef std::pair<unsigned int, ocpp::types::ChargingProfile> ChargingProfileInfo;
54-
/** @brief Allow sorting of profiles by stack level and connector id*/
54+
/** @brief Allow sorting of profiles by stack level and connector id*/
5555
struct ChargingProfileInfoLess
5656
{
5757
bool operator()(const ChargingProfileInfo& lhs, const ChargingProfileInfo& rhs) const

src/chargepoint/smartcharging/SmartChargingManager.cpp

Lines changed: 6 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -105,15 +105,12 @@ bool SmartChargingManager::getSetpoint(unsigned int
105105
}
106106
}
107107

108-
// Compute connector setpoint if a transaction is active on the connector
108+
// Compute connector setpoint
109109
connector_setpoint.clear();
110-
if (connector->transaction_id != 0)
110+
computeSetpoint(connector, connector_setpoint, unit, m_profile_db.txProfiles());
111+
if (!connector_setpoint.isSet())
111112
{
112-
computeSetpoint(connector, connector_setpoint, unit, m_profile_db.txProfiles());
113-
if (!connector_setpoint.isSet())
114-
{
115-
computeSetpoint(connector, connector_setpoint, unit, m_profile_db.txDefaultProfiles());
116-
}
113+
computeSetpoint(connector, connector_setpoint, unit, m_profile_db.txDefaultProfiles());
117114
}
118115

119116
// Connector setpoint cannot be greater than charge point setpoint
@@ -541,16 +538,8 @@ void SmartChargingManager::computeSetpoint(Connector*
541538
ocpp::types::ChargingRateUnitType unit,
542539
const ProfileDatabase::ChargingProfileList& profiles_list)
543540
{
544-
unsigned int level = 0;
545541
for (const auto& profile : profiles_list)
546542
{
547-
// Check if the profile has been found
548-
if (connector_setpoint.isSet() && (profile.second.stackLevel < level))
549-
{
550-
// Profile found
551-
break;
552-
}
553-
554543
// Check connector
555544
if ((profile.first == connector->id) || (profile.first == 0))
556545
{
@@ -560,19 +549,8 @@ void SmartChargingManager::computeSetpoint(Connector*
560549
{
561550
// Apply setpoint
562551
fillSetpoint(connector_setpoint, unit, profile.second, profile.second.chargingSchedule.chargingSchedulePeriod[period]);
563-
}
564552

565-
// Check connector type
566-
if (profile.first == 0)
567-
{
568-
// Any connector profile, save stack level in case of a connector specific
569-
// profile with the same stack level exists
570-
level = profile.second.stackLevel;
571-
}
572-
else
573-
{
574-
// Connector specific profile, stop search since it has highest priority
575-
// over any connector profile
553+
// Highest applicable profile found
576554
break;
577555
}
578556
}
@@ -598,7 +576,7 @@ bool SmartChargingManager::isProfileActive(Connector* c
598576

599577
// Check schedule validity
600578
if ((start_of_schedule <= time_point) &&
601-
(!profile.chargingSchedule.duration.isSet() || ((start_of_schedule + profile.chargingSchedule.duration) <= time_point)))
579+
(!profile.chargingSchedule.duration.isSet() || ((start_of_schedule + profile.chargingSchedule.duration) >= time_point)))
602580
{
603581
// Look for the matching period
604582
const auto& schedule_periods = profile.chargingSchedule.chargingSchedulePeriod;

tests/chargepoint/smartcharging/CMakeLists.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,11 @@ add_test(
3030
NAME test_profile_database
3131
COMMAND test_profile_database
3232
)
33+
34+
# Unit tests for smartcharging setpoint
35+
add_executable(test_smartcharging_setpoint test_smartcharging_setpoint.cpp)
36+
target_link_libraries(test_smartcharging_setpoint unit_tests_stubs doctest sqlite3 pthread dl )
37+
add_test(
38+
NAME test_smartcharging_setpoint
39+
COMMAND test_smartcharging_setpoint
40+
)

0 commit comments

Comments
 (0)