Skip to content

Commit 8269f66

Browse files
authored
Merge pull request #4343 from kiddigital/optimisation/mercapi
MercApi: Request one resource per cycle to make module more responsive.
2 parents 0c55028 + 019eeda commit 8269f66

File tree

2 files changed

+29
-18
lines changed

2 files changed

+29
-18
lines changed

hardware/eVehicles/MercApi.cpp

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ CMercApi::CMercApi(const std::string username, const std::string password, const
6060

6161
m_crc = 0;
6262
m_fields = "";
63+
m_fieldcnt = -1;
6364

6465
m_capabilities.has_battery_level = false;
6566
m_capabilities.has_charge_command = false;
@@ -335,47 +336,51 @@ bool CMercApi::GetVehicleData(tVehicleData& data)
335336
bool CMercApi::GetCustomData(tCustomData& data)
336337
{
337338
Json::Value reply;
339+
std::vector<std::string> strarray;
338340
bool bCustom = true;
339341

340342
if (m_capabilities.has_custom_data)
341343
{
342-
std::vector<std::string> strarray;
343-
344+
m_fieldcnt--;
344345
StringSplit(m_fields, ",", strarray);
345-
for(uint8_t i=0; i<strarray.size(); i++)
346+
347+
if(m_fieldcnt < 0)
348+
{
349+
m_fieldcnt = strarray.size();
350+
}
351+
else
346352
{
347-
reply.clear();
348-
if (GetResourceData(strarray[i], reply))
353+
if (GetResourceData(strarray[m_fieldcnt], reply))
349354
{
350355
if(reply.size() == 0)
351356
{
352-
_log.Debug(DEBUG_NORM, "MercApi: Got empty data for resource %s", strarray[i].c_str());
357+
_log.Debug(DEBUG_NORM, "MercApi: Got empty data for resource %s", strarray[m_fieldcnt].c_str());
353358
}
354359
else
355360
{
356-
//_log.Debug(DEBUG_NORM, "MercApi: Got data for resource %s :\n%s", strarray[i].c_str(),reply.toStyledString().c_str());
361+
//_log.Debug(DEBUG_NORM, "MercApi: Got data for resource %s :\n%s", strarray[m_fieldcnt].c_str(),reply.toStyledString().c_str());
357362

358-
if (!reply[strarray[i]].empty())
363+
if (!reply[strarray[m_fieldcnt]].empty())
359364
{
360-
if (reply[strarray[i]].isMember("value"))
365+
if (reply[strarray[m_fieldcnt]].isMember("value"))
361366
{
362-
std::string resourceValue = reply[strarray[i]]["value"].asString();
367+
std::string resourceValue = reply[strarray[m_fieldcnt]]["value"].asString();
363368

364369
Json::Value customItem;
365-
customItem["id"] = i;
370+
customItem["id"] = m_fieldcnt;
366371
customItem["value"] = resourceValue;
367-
customItem["label"] = strarray[i];
372+
customItem["label"] = strarray[m_fieldcnt];
368373

369374
data.customdata.append(customItem);
370375

371-
_log.Debug(DEBUG_NORM, "MercApi: Got data for resource (%d) %s : %s", i, strarray[i].c_str(), resourceValue.c_str());
376+
_log.Debug(DEBUG_NORM, "MercApi: Got data for resource (%d) %s : %s", m_fieldcnt, strarray[m_fieldcnt].c_str(), resourceValue.c_str());
372377
}
373378
}
374379
}
375380
}
376381
else
377382
{
378-
_log.Debug(DEBUG_NORM,"MercApi: Failed to retrieve data for resource %s!", strarray[i].c_str());
383+
_log.Debug(DEBUG_NORM,"MercApi: Failed to retrieve data for resource %s!", strarray[m_fieldcnt].c_str());
379384
}
380385
}
381386
}
@@ -470,7 +475,7 @@ bool CMercApi::GetResourceData(std::string datatype, Json::Value& reply)
470475
bool CMercApi::IsAwake()
471476
{
472477
// Current Mercedes Me (API) does not have an 'Awake' state
473-
// So we fake one, we just request all available resources that are available for the current (BYO)CAR
478+
// So we fake one, we just request the list of all available resources that are available for the current (BYO)CAR
474479

475480
std::stringstream ss;
476481
ss << MERC_URL << MERC_API << "/" << m_VIN << "/resources";
@@ -521,8 +526,6 @@ bool CMercApi::ProcessAvailableResources(Json::Value& jsondata)
521526
_log.Debug(DEBUG_NORM, "CRC32 of content is the not the same (%d).. start processing", crc);
522527
}
523528

524-
m_crc = crc;
525-
526529
try
527530
{
528531
do
@@ -556,8 +559,15 @@ bool CMercApi::ProcessAvailableResources(Json::Value& jsondata)
556559

557560
if (ss.str().length() > 0)
558561
{
562+
std::vector<std::string> strarray;
563+
559564
m_fields = ss.str();
560-
_log.Log(LOG_STATUS, "Found resource fields: %s", m_fields.c_str());
565+
StringSplit(m_fields, ",", strarray);
566+
m_fieldcnt = strarray.size();
567+
568+
_log.Log(LOG_STATUS, "Found %d resource fields: %s", m_fieldcnt, m_fields.c_str());
569+
570+
m_crc = crc;
561571

562572
bProcessed = true;
563573
}

hardware/eVehicles/MercApi.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,4 +59,5 @@ class CMercApi: public CVehicleApi
5959
uint64_t m_carid;
6060
uint32_t m_crc;
6161
std::string m_fields;
62+
int16_t m_fieldcnt;
6263
};

0 commit comments

Comments
 (0)