Skip to content

Commit

Permalink
Fixed GetCompositeScheduleRequest invalid optional access (#910)
Browse files Browse the repository at this point in the history
* Fixed GetCompositeSchedule invalid optional access

---------

Signed-off-by: Alexandre Carrillo <[email protected]>
  • Loading branch information
alexandrecarrillo authored Dec 13, 2024
1 parent 68b375b commit 5a7a008
Showing 1 changed file with 22 additions and 8 deletions.
30 changes: 22 additions & 8 deletions lib/ocpp/v201/charge_point.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4438,27 +4438,41 @@ ChargePoint::get_composite_schedule_internal(const GetCompositeScheduleRequest&
GetCompositeScheduleResponse response;
response.status = GenericStatusEnum::Rejected;

auto supported_charging_rate_units =
this->device_model->get_value<std::string>(ControllerComponentVariables::ChargingScheduleChargingRateUnit);
auto unit_supported = supported_charging_rate_units.find(conversions::charging_rate_unit_enum_to_string(
request.chargingRateUnit.value())) != supported_charging_rate_units.npos;
std::vector<std::string> supported_charging_rate_units = ocpp::split_string(
this->device_model->get_value<std::string>(ControllerComponentVariables::ChargingScheduleChargingRateUnit), ',',
true);

std::optional<ChargingRateUnitEnum> charging_rate_unit = std::nullopt;
if (request.chargingRateUnit.has_value()) {
bool unit_supported = std::any_of(
supported_charging_rate_units.begin(), supported_charging_rate_units.end(), [&request](std::string item) {
return conversions::string_to_charging_rate_unit_enum(item) == request.chargingRateUnit.value();
});

if (unit_supported) {
charging_rate_unit = request.chargingRateUnit.value();
}
} else if (supported_charging_rate_units.size() > 0) {
charging_rate_unit = conversions::string_to_charging_rate_unit_enum(supported_charging_rate_units.at(0));
}

// K01.FR.05 & K01.FR.07
if (this->evse_manager->does_evse_exist(request.evseId) and unit_supported) {
if (this->evse_manager->does_evse_exist(request.evseId) and charging_rate_unit.has_value()) {
auto start_time = ocpp::DateTime();
auto end_time = ocpp::DateTime(start_time.to_time_point() + std::chrono::seconds(request.duration));

std::vector<ChargingProfile> valid_profiles =
this->smart_charging_handler->get_valid_profiles(request.evseId, profiles_to_ignore);

auto schedule = this->smart_charging_handler->calculate_composite_schedule(
valid_profiles, start_time, end_time, request.evseId, request.chargingRateUnit);
valid_profiles, start_time, end_time, request.evseId, charging_rate_unit.value());

response.schedule = schedule;
response.status = GenericStatusEnum::Accepted;
} else {
auto reason = unit_supported ? ProfileValidationResultEnum::EvseDoesNotExist
: ProfileValidationResultEnum::ChargingScheduleChargingRateUnitUnsupported;
auto reason = charging_rate_unit.has_value()
? ProfileValidationResultEnum::EvseDoesNotExist
: ProfileValidationResultEnum::ChargingScheduleChargingRateUnitUnsupported;
response.statusInfo = StatusInfo();
response.statusInfo->reasonCode = conversions::profile_validation_result_to_reason_code(reason);
response.statusInfo->additionalInfo = conversions::profile_validation_result_to_string(reason);
Expand Down

0 comments on commit 5a7a008

Please sign in to comment.