Skip to content

Commit 6f2c7c3

Browse files
committed
[smartcharging] GetCompositeSchedule implementation : handle profiles on connector 0 + add new unit tests
1 parent 6cd19b9 commit 6f2c7c3

File tree

6 files changed

+331
-18
lines changed

6 files changed

+331
-18
lines changed

src/chargepoint/smartcharging/ProfileDatabase.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,12 @@ 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 */
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
5858
{
59-
return (lhs.second.stackLevel > rhs.second.stackLevel);
59+
return ((lhs.second.stackLevel > rhs.second.stackLevel) || (lhs.first > rhs.first));
6060
}
6161
};
6262
/** @brief List of charging profiles stored by stack level */

src/chargepoint/smartcharging/SmartChargingManager.cpp

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -354,16 +354,19 @@ bool SmartChargingManager::handleMessage(const ocpp::messages::GetCompositeSched
354354
std::string& error_code,
355355
std::string& error_message)
356356
{
357-
bool ret = false;
358357
(void)error_code;
358+
(void)error_message;
359359

360360
LOG_INFO << "GetCompositeSchedule requested : connectorId = " << request.connectorId << " - duration = " << request.duration
361361
<< " - chargingRateUnit = "
362362
<< (request.chargingRateUnit.isSet() ? ChargingRateUnitTypeHelper.toString(request.chargingRateUnit) : "not set");
363363

364+
// Prepare response
365+
response.status = GetCompositeScheduleStatus::Rejected;
366+
364367
// Get connector
365368
Connector* connector = m_connectors.getConnector(request.connectorId);
366-
if (connector)
369+
if (connector || (request.connectorId == 0u))
367370
{
368371
// Get profiles list
369372
std::vector<const ProfileDatabase::ChargingProfileList*> profile_lists;
@@ -378,12 +381,16 @@ bool SmartChargingManager::handleMessage(const ocpp::messages::GetCompositeSched
378381
DateTime now = DateTime::now();
379382
for (auto& profile_list : profile_lists)
380383
{
384+
unsigned int stack_level = std::numeric_limits<unsigned int>::max();
381385
for (const auto& profile : (*profile_list))
382386
{
383-
if (profile.first == request.connectorId)
387+
// Add the profile if it matches the selected connector
388+
// or take the profile installed for connector 0 if it doesn't exists
389+
if ((profile.first == request.connectorId) || ((profile.first == 0u) && (stack_level != profile.second.stackLevel)))
384390
{
385391
std::vector<Period> profile_periods = getProfilePeriods(connector, profile.second, now, request.duration);
386392
periods = mergeProfilePeriods(periods, profile_periods);
393+
stack_level = profile.second.stackLevel;
387394
if (periods.empty())
388395
{
389396
break;
@@ -462,19 +469,16 @@ bool SmartChargingManager::handleMessage(const ocpp::messages::GetCompositeSched
462469
else
463470
{
464471
// No profiles, couldn't compute any schedule
465-
response.status = GetCompositeScheduleStatus::Rejected;
466472
}
467-
468-
ret = true;
469473
}
470474
else
471475
{
472-
error_message = "Invalid connector id";
476+
LOG_ERROR << "Invalid connector id : " << request.connectorId;
473477
}
474478

475479
LOG_INFO << "GetCompositeSchedule status : " << GetCompositeScheduleStatusHelper.toString(response.status);
476480

477-
return ret;
481+
return true;
478482
}
479483

480484
/** @brief Periodically cleanup expired profiles */
@@ -853,7 +857,13 @@ std::vector<SmartChargingManager::Period> SmartChargingManager::getProfilePeriod
853857
}
854858
else
855859
{
856-
p.duration = profile.chargingSchedule.chargingSchedulePeriod[period].startPeriod + delta_start - p.start;
860+
p.duration = profile.chargingSchedule.chargingSchedulePeriod[period].startPeriod + delta_start - p.start;
861+
if ((p.start + p.duration + ts_time_point) >= ts_end_of_schedule)
862+
{
863+
// The current period ends after the requested schedule end
864+
p.duration = ts_end_of_schedule - (p.start + ts_time_point);
865+
end = true;
866+
}
857867
current_start = p.start + p.duration;
858868
}
859869

tests/chargepoint/smartcharging/test_composite_schedule1.cpp

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,15 @@ TEST_SUITE("Get composite schedule - single OCPP profile")
7474
connectors.initDatabaseTable();
7575
}
7676

77+
void clearAllProfiles(SmartChargingManager & smartcharging_mgr)
78+
{
79+
ClearChargingProfileReq clearprofiles_req;
80+
ClearChargingProfileConf clearprofiles_conf;
81+
std::string error_code;
82+
std::string error_message;
83+
smartcharging_mgr.handleMessage(clearprofiles_req, clearprofiles_conf, error_code, error_message);
84+
}
85+
7786
bool installProfile(unsigned int connector_id, const ChargingProfile& profile, SmartChargingManager& smartcharging_mgr)
7887
{
7988
SetChargingProfileReq setprofile_req;
@@ -109,6 +118,7 @@ TEST_SUITE("Get composite schedule - single OCPP profile")
109118
{
110119
SmartChargingManager smartcharging_mgr(
111120
stack_config, ocpp_config, database, event_handler, timer_pool, worker_pool, connectors, msgs_converter, msg_dispatcher);
121+
clearAllProfiles(smartcharging_mgr);
112122

113123
ChargingProfile profile1;
114124
profile1.chargingProfileId = 1;
@@ -154,6 +164,7 @@ TEST_SUITE("Get composite schedule - single OCPP profile")
154164
{
155165
SmartChargingManager smartcharging_mgr(
156166
stack_config, ocpp_config, database, event_handler, timer_pool, worker_pool, connectors, msgs_converter, msg_dispatcher);
167+
clearAllProfiles(smartcharging_mgr);
157168

158169
ChargingProfile profile1;
159170
profile1.chargingProfileId = 1;
@@ -201,6 +212,7 @@ TEST_SUITE("Get composite schedule - single OCPP profile")
201212
{
202213
SmartChargingManager smartcharging_mgr(
203214
stack_config, ocpp_config, database, event_handler, timer_pool, worker_pool, connectors, msgs_converter, msg_dispatcher);
215+
clearAllProfiles(smartcharging_mgr);
204216

205217
ChargingProfile profile1;
206218
profile1.chargingProfileId = 1;
@@ -247,6 +259,7 @@ TEST_SUITE("Get composite schedule - single OCPP profile")
247259
{
248260
SmartChargingManager smartcharging_mgr(
249261
stack_config, ocpp_config, database, event_handler, timer_pool, worker_pool, connectors, msgs_converter, msg_dispatcher);
262+
clearAllProfiles(smartcharging_mgr);
250263

251264
ChargingProfile profile1;
252265
profile1.chargingProfileId = 1;
@@ -267,6 +280,10 @@ TEST_SUITE("Get composite schedule - single OCPP profile")
267280
charging_period.startPeriod = 1700;
268281
charging_period.numberPhases = 3;
269282
profile1.chargingSchedule.chargingSchedulePeriod.push_back(charging_period);
283+
charging_period.limit = 24.f;
284+
charging_period.startPeriod = 3700;
285+
charging_period.numberPhases = 1;
286+
profile1.chargingSchedule.chargingSchedulePeriod.push_back(charging_period);
270287
profile1.chargingSchedule.duration = 5000;
271288
profile1.chargingSchedule.chargingRateUnit = ChargingRateUnitType::A;
272289
CHECK(installProfile(1, profile1, smartcharging_mgr));
@@ -279,9 +296,9 @@ TEST_SUITE("Get composite schedule - single OCPP profile")
279296
CHECK_EQ(schedule.chargingRateUnit, ChargingRateUnitType::A);
280297
CHECK_GE(schedule.startSchedule.value(), now);
281298
CHECK_LE(schedule.startSchedule.value(), DateTime(now.timestamp() + 1));
282-
CHECK_EQ(schedule.chargingSchedulePeriod.size(), profile1.chargingSchedule.chargingSchedulePeriod.size());
299+
CHECK_EQ(schedule.chargingSchedulePeriod.size(), profile1.chargingSchedule.chargingSchedulePeriod.size() - 1);
283300

284-
for (size_t i = 0; i < profile1.chargingSchedule.chargingSchedulePeriod.size(); i++)
301+
for (size_t i = 0; i < profile1.chargingSchedule.chargingSchedulePeriod.size() - 1; i++)
285302
{
286303
CHECK_EQ(schedule.chargingSchedulePeriod[i].startPeriod, profile1.chargingSchedule.chargingSchedulePeriod[i].startPeriod);
287304
CHECK_EQ(schedule.chargingSchedulePeriod[i].limit, profile1.chargingSchedule.chargingSchedulePeriod[i].limit);
@@ -293,6 +310,7 @@ TEST_SUITE("Get composite schedule - single OCPP profile")
293310
{
294311
SmartChargingManager smartcharging_mgr(
295312
stack_config, ocpp_config, database, event_handler, timer_pool, worker_pool, connectors, msgs_converter, msg_dispatcher);
313+
clearAllProfiles(smartcharging_mgr);
296314

297315
DateTime now = DateTime::now();
298316

@@ -340,6 +358,7 @@ TEST_SUITE("Get composite schedule - single OCPP profile")
340358
{
341359
SmartChargingManager smartcharging_mgr(
342360
stack_config, ocpp_config, database, event_handler, timer_pool, worker_pool, connectors, msgs_converter, msg_dispatcher);
361+
clearAllProfiles(smartcharging_mgr);
343362

344363
DateTime now = DateTime::now();
345364

@@ -390,6 +409,7 @@ TEST_SUITE("Get composite schedule - single OCPP profile")
390409
{
391410
SmartChargingManager smartcharging_mgr(
392411
stack_config, ocpp_config, database, event_handler, timer_pool, worker_pool, connectors, msgs_converter, msg_dispatcher);
412+
clearAllProfiles(smartcharging_mgr);
393413

394414
DateTime now = DateTime::now();
395415

0 commit comments

Comments
 (0)