Skip to content

Commit

Permalink
Raise error if charging schedules are empty
Browse files Browse the repository at this point in the history
Signed-off-by: Ivan Rogach <[email protected]>
  • Loading branch information
jannejy committed Dec 18, 2024
1 parent 00137ba commit af818d0
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 3 deletions.
3 changes: 2 additions & 1 deletion include/ocpp/v201/smart_charging.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ enum class ProfileValidationResultEnum {
ChargingStationMaxProfileEvseIdGreaterThanZero,
DuplicateTxDefaultProfileFound,
DuplicateProfileValidityPeriod,
RequestStartTransactionNonTxProfile
RequestStartTransactionNonTxProfile,
ChargingProfileEmptyChargingSchedules
};

/// \brief This is used to associate charging profiles with a source.
Expand Down
8 changes: 8 additions & 0 deletions lib/ocpp/v201/smart_charging.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ std::string profile_validation_result_to_string(ProfileValidationResultEnum e) {
return "DuplicateProfileValidityPeriod";
case ProfileValidationResultEnum::RequestStartTransactionNonTxProfile:
return "RequestStartTransactionNonTxProfile";
case ProfileValidationResultEnum::ChargingProfileEmptyChargingSchedules:
return "ChargingProfileEmptyChargingSchedules";
}

throw EnumToStringException{e, "ProfileValidationResultEnum"};
Expand Down Expand Up @@ -105,6 +107,7 @@ std::string profile_validation_result_to_reason_code(ProfileValidationResultEnum
case ProfileValidationResultEnum::ChargingSchedulePeriodUnsupportedNumberPhases:
case ProfileValidationResultEnum::ChargingSchedulePeriodExtraneousPhaseValues:
case ProfileValidationResultEnum::ChargingSchedulePeriodPhaseToUseACPhaseSwitchingUnsupported:
case ProfileValidationResultEnum::ChargingProfileEmptyChargingSchedules:
return "InvalidSchedule";
case ProfileValidationResultEnum::TxProfileMissingTransactionId:
return "MissingParam";
Expand Down Expand Up @@ -338,6 +341,11 @@ SmartChargingHandler::validate_tx_profile(const ChargingProfile& profile, int32_
ProfileValidationResultEnum
SmartChargingHandler::validate_profile_schedules(ChargingProfile& profile,
std::optional<EvseInterface*> evse_opt) const {

if (profile.chargingSchedule.empty()) {
return ProfileValidationResultEnum::ChargingProfileEmptyChargingSchedules;
}

auto charging_station_supply_phases =
this->device_model->get_value<int32_t>(ControllerComponentVariables::ChargingStationSupplyPhases);

Expand Down
23 changes: 21 additions & 2 deletions tests/lib/ocpp/v201/test_smart_charging_handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -146,12 +146,12 @@ class SmartChargingHandlerTestFixtureV201 : public DatabaseTestingUtils {

ChargingProfile
create_charging_profile(int32_t charging_profile_id, ChargingProfilePurposeEnum charging_profile_purpose,
ChargingSchedule charging_schedule, std::optional<std::string> transaction_id = {},
std::vector<ChargingSchedule> charging_schedules,
std::optional<std::string> transaction_id = {},
ChargingProfileKindEnum charging_profile_kind = ChargingProfileKindEnum::Absolute,
int stack_level = DEFAULT_STACK_LEVEL, std::optional<ocpp::DateTime> validFrom = {},
std::optional<ocpp::DateTime> validTo = {}) {
auto recurrency_kind = RecurrencyKindEnum::Daily;
std::vector<ChargingSchedule> charging_schedules = {charging_schedule};
ChargingProfile charging_profile;
charging_profile.id = charging_profile_id;
charging_profile.stackLevel = stack_level;
Expand All @@ -166,6 +166,17 @@ class SmartChargingHandlerTestFixtureV201 : public DatabaseTestingUtils {
return charging_profile;
}

ChargingProfile
create_charging_profile(int32_t charging_profile_id, ChargingProfilePurposeEnum charging_profile_purpose,
ChargingSchedule charging_schedule, std::optional<std::string> transaction_id = {},
ChargingProfileKindEnum charging_profile_kind = ChargingProfileKindEnum::Absolute,
int stack_level = DEFAULT_STACK_LEVEL, std::optional<ocpp::DateTime> validFrom = {},
std::optional<ocpp::DateTime> validTo = {}) {
return create_charging_profile(charging_profile_id, charging_profile_purpose,
std::vector<ChargingSchedule>{charging_schedule}, transaction_id,
charging_profile_kind, stack_level, validFrom, validTo);
}

ChargingProfileCriterion create_charging_profile_criteria(
std::optional<std::vector<ocpp::v201::ChargingLimitSourceEnum>> sources = std::nullopt,
std::optional<std::vector<int32_t>> ids = std::nullopt,
Expand Down Expand Up @@ -1747,4 +1758,12 @@ TEST_F(SmartChargingHandlerTestFixtureV201, K01_ValidateTxProfile_AllowsExisting
EXPECT_THAT(sut, testing::Eq(ProfileValidationResultEnum::Valid));
}

TEST_F(SmartChargingHandlerTestFixtureV201, K01_ValidateTxProfile_EmptyChargingSchedule) {
auto profile = create_charging_profile(DEFAULT_PROFILE_ID, ChargingProfilePurposeEnum::ChargingStationMaxProfile,
std::vector<ChargingSchedule>{}, ocpp::DateTime("2024-01-17T17:00:00"));

auto sut = handler.conform_and_validate_profile(profile, DEFAULT_EVSE_ID);
ASSERT_THAT(sut, testing::Eq(ProfileValidationResultEnum::ChargingProfileEmptyChargingSchedules));
}

} // namespace ocpp::v201

0 comments on commit af818d0

Please sign in to comment.