Skip to content

Commit bac1b2b

Browse files
authored
Merge pull request #92 from c-jimenez/fix/composite_schedule
Fix/composite schedule
2 parents a3675d8 + 1a15051 commit bac1b2b

File tree

2 files changed

+82
-6
lines changed

2 files changed

+82
-6
lines changed

src/chargepoint/smartcharging/SmartChargingManager.cpp

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -375,7 +375,8 @@ bool SmartChargingManager::handleMessage(const ocpp::messages::GetCompositeSched
375375

376376
// Compute periods
377377
std::vector<Period> periods;
378-
DateTime now = DateTime::now();
378+
bool error = false;
379+
DateTime now = DateTime::now();
379380
for (auto& profile_list : profile_lists)
380381
{
381382
unsigned int stack_level = std::numeric_limits<unsigned int>::max();
@@ -390,11 +391,11 @@ bool SmartChargingManager::handleMessage(const ocpp::messages::GetCompositeSched
390391
stack_level = profile.second.stackLevel;
391392
if (periods.empty())
392393
{
393-
break;
394+
error = true;
394395
}
395396
}
396397
}
397-
if (periods.empty())
398+
if (error)
398399
{
399400
break;
400401
}
@@ -407,15 +408,16 @@ bool SmartChargingManager::handleMessage(const ocpp::messages::GetCompositeSched
407408
// Get local limitations
408409
ChargingProfile local_profile;
409410
local_profile.chargingProfileId = 0;
410-
local_profile.chargingProfileKind = ChargingProfileKindType::Relative;
411+
local_profile.chargingProfileKind = ChargingProfileKindType::Absolute;
411412
local_profile.chargingProfilePurpose = ChargingProfilePurposeType::TxDefaultProfile;
412413
local_profile.stackLevel = 0;
413414
if (m_events_handler.getLocalLimitationsSchedule(request.connectorId, request.duration, local_profile.chargingSchedule) &&
414415
!local_profile.chargingSchedule.chargingSchedulePeriod.empty())
415416
{
416-
// Ensure profile is relative with the requested duration
417+
// Ensure profile is absolute with the requested duration
417418
local_profile.chargingSchedule.startSchedule.clear();
418-
local_profile.chargingSchedule.duration = request.duration;
419+
local_profile.chargingSchedule.startSchedule = now;
420+
local_profile.chargingSchedule.duration = request.duration;
419421

420422
// Merge periods
421423
std::vector<Period> local_periods = getProfilePeriods(connector, local_profile, now, request.duration);

tests/chargepoint/smartcharging/test_composite_schedule3.cpp

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,80 @@ TEST_SUITE("Get composite schedule - multiple OCPP profiles")
323323
CHECK_EQ(schedule.chargingSchedulePeriod[4].numberPhases.value(), 1);
324324
}
325325

326+
TEST_CASE("1 profile (relative) + local limitations - charging")
327+
{
328+
SmartChargingManager smartcharging_mgr(
329+
stack_config, ocpp_config, database, event_handler, timer_pool, worker_pool, connectors, msgs_converter, msg_dispatcher);
330+
clearAllProfiles(smartcharging_mgr);
331+
332+
DateTime now = DateTime::now();
333+
334+
Connector* connector = connectors.getConnector(1);
335+
connector->transaction_id = 1;
336+
connector->transaction_start = DateTime(now.timestamp() - 100);
337+
338+
ChargingProfile profile1;
339+
profile1.chargingProfileId = 1;
340+
profile1.stackLevel = 5;
341+
profile1.chargingProfilePurpose = ChargingProfilePurposeType::TxDefaultProfile;
342+
profile1.chargingProfileKind = ChargingProfileKindType::Relative;
343+
344+
ChargingSchedulePeriod charging_period;
345+
charging_period.limit = 16.f;
346+
charging_period.startPeriod = 0;
347+
charging_period.numberPhases = 1;
348+
profile1.chargingSchedule.chargingSchedulePeriod.push_back(charging_period);
349+
charging_period.limit = 10.f;
350+
charging_period.startPeriod = 1000;
351+
charging_period.numberPhases = 2;
352+
profile1.chargingSchedule.chargingSchedulePeriod.push_back(charging_period);
353+
charging_period.limit = 32.f;
354+
charging_period.startPeriod = 1700;
355+
charging_period.numberPhases = 3;
356+
profile1.chargingSchedule.chargingSchedulePeriod.push_back(charging_period);
357+
profile1.chargingSchedule.chargingRateUnit = ChargingRateUnitType::A;
358+
profile1.chargingSchedule.startSchedule = DateTime(now.timestamp() + 300);
359+
CHECK(installProfile(1, profile1, smartcharging_mgr));
360+
361+
ChargingSchedule& local_schedule = event_handler.schedule;
362+
local_schedule.chargingSchedulePeriod.clear();
363+
charging_period.limit = 8.f;
364+
charging_period.startPeriod = 0;
365+
charging_period.numberPhases = 2;
366+
local_schedule.chargingSchedulePeriod.push_back(charging_period);
367+
charging_period.limit = 20.f;
368+
charging_period.startPeriod = 200;
369+
charging_period.numberPhases = 3;
370+
local_schedule.chargingSchedulePeriod.push_back(charging_period);
371+
charging_period.limit = 18.f;
372+
charging_period.startPeriod = 1400;
373+
charging_period.numberPhases = 3;
374+
local_schedule.chargingSchedulePeriod.push_back(charging_period);
375+
local_schedule.chargingRateUnit = ChargingRateUnitType::A;
376+
377+
ChargingSchedule schedule;
378+
CHECK(getCompositeSchedule(1, 3600, ChargingRateUnitType::A, schedule, smartcharging_mgr));
379+
380+
CHECK_EQ(schedule.duration, 3600);
381+
CHECK_EQ(schedule.chargingRateUnit, ChargingRateUnitType::A);
382+
CHECK_GE(schedule.startSchedule.value(), now);
383+
CHECK_LE(schedule.startSchedule.value(), DateTime(now.timestamp() + 1));
384+
CHECK_EQ(schedule.chargingSchedulePeriod.size(), 4);
385+
386+
CHECK_EQ(schedule.chargingSchedulePeriod[0].startPeriod, 0);
387+
CHECK_EQ(schedule.chargingSchedulePeriod[0].limit, 8.f);
388+
CHECK_EQ(schedule.chargingSchedulePeriod[0].numberPhases.value(), 2);
389+
CHECK_EQ(schedule.chargingSchedulePeriod[1].startPeriod, 200);
390+
CHECK_EQ(schedule.chargingSchedulePeriod[1].limit, 16.f);
391+
CHECK_EQ(schedule.chargingSchedulePeriod[1].numberPhases.value(), 1);
392+
CHECK_EQ(schedule.chargingSchedulePeriod[2].startPeriod, 900);
393+
CHECK_EQ(schedule.chargingSchedulePeriod[2].limit, 10.f);
394+
CHECK_EQ(schedule.chargingSchedulePeriod[2].numberPhases.value(), 2);
395+
CHECK_EQ(schedule.chargingSchedulePeriod[3].startPeriod, 1600);
396+
CHECK_EQ(schedule.chargingSchedulePeriod[3].limit, 18.f);
397+
CHECK_EQ(schedule.chargingSchedulePeriod[3].numberPhases.value(), 3);
398+
}
399+
326400
TEST_CASE("Cleanup")
327401
{
328402
CHECK(database.close());

0 commit comments

Comments
 (0)