Skip to content

Commit 48b2705

Browse files
authored
Merge pull request #26 from c-jimenez/fix/clear_charing_profiles_crash
[smart charging] Fix crash when receiving empty ClearChargingProfile …
2 parents 0620fda + ff77294 commit 48b2705

File tree

2 files changed

+25
-14
lines changed

2 files changed

+25
-14
lines changed

src/chargepoint/smartcharging/ProfileDatabase.cpp

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -63,18 +63,23 @@ bool ProfileDatabase::clear(ocpp::types::Optional<int>
6363
// Clear all ?
6464
if (!id.isSet() && !connector_id.isSet() && !purpose.isSet() && !level.isSet())
6565
{
66-
// Clear lists
67-
m_chargepoint_max_profiles.clear();
68-
m_txdefault_profiles.clear();
69-
m_tx_profiles.clear();
70-
71-
// Clear database
72-
auto query = m_database.query("DELETE FROM ChargingProfiles WHERE TRUE;");
73-
if (query)
66+
// Check existing profiles
67+
if (!m_chargepoint_max_profiles.empty() || !m_txdefault_profiles.empty() || !m_tx_profiles.empty())
7468
{
75-
query->exec();
69+
// Clear lists
70+
m_chargepoint_max_profiles.clear();
71+
m_txdefault_profiles.clear();
72+
m_tx_profiles.clear();
73+
74+
// Clear database
75+
auto query = m_database.query("DELETE FROM ChargingProfiles WHERE TRUE;");
76+
if (query)
77+
{
78+
query->exec();
79+
}
80+
81+
ret = true;
7682
}
77-
ret = true;
7883
}
7984
else if (id.isSet())
8085
{

src/messages/ClearChargingProfile.cpp

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,12 @@ bool ClearChargingProfileReqConverter::fromJson(const rapidjson::Value& json,
4747
std::string& error_message)
4848
{
4949
extract(json, "id", data.id);
50-
bool ret = extract(json, "connectorId", data.connectorId, error_message);
51-
data.chargingProfilePurpose = ChargingProfilePurposeTypeHelper.fromString(json["chargingProfilePurpose"].GetString());
52-
ret = ret && extract(json, "stackLevel", data.stackLevel, error_message);
50+
bool ret = extract(json, "connectorId", data.connectorId, error_message);
51+
if (json.HasMember("chargingProfilePurpose"))
52+
{
53+
data.chargingProfilePurpose = ChargingProfilePurposeTypeHelper.fromString(json["chargingProfilePurpose"].GetString());
54+
}
55+
ret = ret && extract(json, "stackLevel", data.stackLevel, error_message);
5356
if (!ret)
5457
{
5558
error_code = ocpp::rpc::IRpc::RPC_ERROR_TYPE_CONSTRAINT_VIOLATION;
@@ -62,7 +65,10 @@ bool ClearChargingProfileReqConverter::toJson(const ClearChargingProfileReq& dat
6265
{
6366
fill(json, "id", data.id);
6467
fill(json, "connectorId", data.connectorId);
65-
fill(json, "chargingProfilePurpose", ChargingProfilePurposeTypeHelper.toString(data.chargingProfilePurpose));
68+
if (data.chargingProfilePurpose.isSet())
69+
{
70+
fill(json, "chargingProfilePurpose", ChargingProfilePurposeTypeHelper.toString(data.chargingProfilePurpose));
71+
}
6672
fill(json, "stackLevel", data.stackLevel);
6773
return true;
6874
}

0 commit comments

Comments
 (0)