Skip to content

Commit

Permalink
Merge pull request #1338 from fledge-iot/2.4.0RC
Browse files Browse the repository at this point in the history
2.4.0RC
  • Loading branch information
Mohit04tomar authored Apr 17, 2024
2 parents ded8e25 + 508d264 commit ad48db9
Show file tree
Hide file tree
Showing 157 changed files with 6,946 additions and 917 deletions.
25 changes: 23 additions & 2 deletions C/common/asset_tracking.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ AssetTracker *AssetTracker::getAssetTracker()
* @param service Service name
*/
AssetTracker::AssetTracker(ManagementClient *mgtClient, string service)
: m_mgtClient(mgtClient), m_service(service)
: m_mgtClient(mgtClient), m_service(service), m_updateInterval(MIN_ASSET_TRACKER_UPDATE)
{
instance = this;
m_shutdown = false;
Expand Down Expand Up @@ -338,6 +338,27 @@ void AssetTracker::queue(TrackingTuple *tuple)
m_cv.notify_all();
}

/**
* Set the update interval for the asset tracker.
*
* @param interval The number of milliseconds between update of the asset tracker
* @return bool Was the update accepted
*/
bool AssetTracker::tune(unsigned long interval)
{
unique_lock<mutex> lck(m_mutex);
if (interval >= MIN_ASSET_TRACKER_UPDATE)
{
m_updateInterval = interval;
}
else
{
Logger::getLogger()->error("Attempt to set asset tracker update to less than minimum interval");
return false;
}
return true;
}

/**
* The worker thread that will flush any pending asset tuples to
* the database.
Expand All @@ -347,7 +368,7 @@ void AssetTracker::workerThread()
unique_lock<mutex> lck(m_mutex);
while (m_pending.empty() && m_shutdown == false)
{
m_cv.wait_for(lck, chrono::milliseconds(500));
m_cv.wait_for(lck, chrono::milliseconds(m_updateInterval));
processQueue();
}
// Process any items left in the queue at shutdown
Expand Down
1 change: 1 addition & 0 deletions C/common/base64databuffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ using namespace std;
*/
Base64DataBuffer::Base64DataBuffer(const string& encoded)
{
m_data = NULL;
m_itemSize = encoded[0] - '0';
size_t in_len = encoded.size() - 1;
if (in_len % 4 != 0)
Expand Down
226 changes: 223 additions & 3 deletions C/common/config_category.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ ConfigCategory::ConfigCategory(const string& name, const string& json) : m_name(
Logger::getLogger()->error("Configuration parse error in category '%s', %s: %s at %d, '%s'",
name.c_str(), json.c_str(),
GetParseError_En(doc.GetParseError()), (unsigned)doc.GetErrorOffset(),
StringAround(json, (unsigned)doc.GetErrorOffset()));
StringAround(json, (unsigned)doc.GetErrorOffset()).c_str());
throw new ConfigMalformed();
}

Expand Down Expand Up @@ -439,6 +439,114 @@ string ConfigCategory::getValue(const string& name) const
throw new ConfigItemNotFound();
}

/**
* Return the value of the configuration category item list, this
* is a convience function used when simple lists are defined
* and allows for central processing of the list values
*
* @param name The name of the configuration item to return
* @return string The configuration item name
* @throws exception if the item does not exist in the category
*/
vector<string> ConfigCategory::getValueList(const string& name) const
{
for (unsigned int i = 0; i < m_items.size(); i++)
{
if (name.compare(m_items[i]->m_name) == 0)
{
if (m_items[i]->m_type.compare("list"))
{
throw new ConfigItemNotAList();
}
Document d;
vector<string> list;
d.Parse(m_items[i]->m_value.c_str());
if (d.HasParseError())
{
Logger::getLogger()->error("The JSON value for a list item %s has a parse error: %s, %s",
name.c_str(), GetParseError_En(d.GetParseError()), m_items[i]->m_value.c_str());
return list;
}
if (d.IsArray())
{
for (auto& v : d.GetArray())
{
if (v.IsString())
{
list.push_back(v.GetString());
}
}
}
else
{
Logger::getLogger()->error("The value of the list item %s should be a JSON array and it is not", name.c_str());
}
return list;
}
}
throw new ConfigItemNotFound();
}

/**
* Return the value of the configuration category item kvlist, this
* is a convience function used when key/value lists are defined
* and allows for central processing of the list values
*
* @param name The name of the configuration item to return
* @return string The configuration item name
* @throws exception if the item does not exist in the category
*/
map<string, string> ConfigCategory::getValueKVList(const string& name) const
{
for (unsigned int i = 0; i < m_items.size(); i++)
{
if (name.compare(m_items[i]->m_name) == 0)
{
if (m_items[i]->m_type.compare("kvlist"))
{
throw new ConfigItemNotAList();
}
map<string, string> list;
Document d;
d.Parse(m_items[i]->m_value.c_str());
if (d.HasParseError())
{
Logger::getLogger()->error("The JSON value for a kvlist item %s has a parse error: %s, %s",
name.c_str(), GetParseError_En(d.GetParseError()), m_items[i]->m_value.c_str());
return list;
}
for (auto& v : d.GetObject())
{
string key = v.name.GetString();
string value = to_string(v.value);
list.insert(pair<string, string>(key, value));
}
return list;
}
}
throw new ConfigItemNotFound();
}

/**
* Convert a RapidJSON value to a string
*
* @param v The RapidJSON value
*/
std::string ConfigCategory::to_string(const rapidjson::Value& v) const
{
if (v.IsString())
{
return { v.GetString(), v.GetStringLength() };
}
else
{
StringBuffer strbuf;
Writer<rapidjson::StringBuffer> writer(strbuf);
v.Accept(writer);
return { strbuf.GetString(), strbuf.GetLength() };
}
}

/**
* Return the requested attribute of a configuration category item
*
Expand Down Expand Up @@ -478,6 +586,10 @@ string ConfigCategory::getItemAttribute(const string& itemName,
return m_items[i]->m_rule;
case BUCKET_PROPERTIES_ATTR:
return m_items[i]->m_bucketProperties;
case LIST_SIZE_ATTR:
return m_items[i]->m_listSize;
case ITEM_TYPE_ATTR:
return m_items[i]->m_listItemType;
default:
throw new ConfigItemAttributeNotFound();
}
Expand Down Expand Up @@ -546,6 +658,12 @@ bool ConfigCategory::setItemAttribute(const string& itemName,
case BUCKET_PROPERTIES_ATTR:
m_items[i]->m_bucketProperties = value;
return true;
case LIST_SIZE_ATTR:
m_items[i]->m_listSize = value;
return true;
case ITEM_TYPE_ATTR:
m_items[i]->m_listItemType = value;
return true;
default:
return false;
}
Expand Down Expand Up @@ -882,6 +1000,44 @@ bool ConfigCategory::isDeprecated(const string& name) const
throw new ConfigItemNotFound();
}

/**
* Return if the configuration item is a list item
*
* @param name The name of the item to test
* @return bool True if the item is a Numeric type
* @throws exception If the item was not found in the configuration category
*/
bool ConfigCategory::isList(const string& name) const
{
for (unsigned int i = 0; i < m_items.size(); i++)
{
if (name.compare(m_items[i]->m_name) == 0)
{
return (m_items[i]->m_type.compare("list") == 0);
}
}
throw new ConfigItemNotFound();
}

/**
* Return if the configuration item is a kvlist item
*
* @param name The name of the item to test
* @return bool True if the item is a Numeric type
* @throws exception If the item was not found in the configuration category
*/
bool ConfigCategory::isKVList(const string& name) const
{
for (unsigned int i = 0; i < m_items.size(); i++)
{
if (name.compare(m_items[i]->m_name) == 0)
{
return (m_items[i]->m_type.compare("kvlist") == 0);
}
}
throw new ConfigItemNotFound();
}

/**
* Set the description for the configuration category
*
Expand Down Expand Up @@ -1047,6 +1203,14 @@ ConfigCategory::CategoryItem::CategoryItem(const string& name,
{
m_itemType = BucketItem;
}
if (m_type.compare("list") == 0)
{
m_itemType = ListItem;
}
if (m_type.compare("kvlist") == 0)
{
m_itemType = KVListItem;
}

if (item.HasMember("deprecated"))
{
Expand Down Expand Up @@ -1131,6 +1295,33 @@ ConfigCategory::CategoryItem::CategoryItem(const string& name,
}
}

if (item.HasMember("items"))
{
if (item["items"].IsString())
{
m_listItemType = item["items"].GetString();
}
else
{
throw new runtime_error("Items configuration item property is not a string");
}
}
else if (m_itemType == ListItem || m_itemType == KVListItem)
{
throw new runtime_error("List configuration item is missing the \"items\" attribute");
}
if (item.HasMember("listSize"))
{
if (item["listSize"].IsString())
{
m_listSize = item["listSize"].GetString();
}
else
{
throw new runtime_error("ListSize configuration item property is not a string");
}
}

std::string m_typeUpperCase = m_type;
for (auto & c: m_typeUpperCase) c = toupper(c);

Expand Down Expand Up @@ -1414,6 +1605,8 @@ ConfigCategory::CategoryItem::CategoryItem(const CategoryItem& rhs)
m_group = rhs.m_group;
m_rule = rhs.m_rule;
m_bucketProperties = rhs.m_bucketProperties;
m_listSize = rhs.m_listSize;
m_listItemType = rhs.m_listItemType;
}

/**
Expand Down Expand Up @@ -1447,7 +1640,10 @@ ostringstream convert;

if (m_itemType == StringItem ||
m_itemType == BoolItem ||
m_itemType == EnumerationItem)
m_itemType == EnumerationItem ||
m_itemType == BucketItem ||
m_itemType == ListItem ||
m_itemType == KVListItem)
{
convert << "\"value\" : \"" << JSONescape(m_value) << "\", ";
convert << "\"default\" : \"" << JSONescape(m_default) << "\"";
Expand All @@ -1461,6 +1657,10 @@ ostringstream convert;
convert << "\"value\" : " << m_value << ", ";
convert << "\"default\" : " << m_default;
}
else
{
Logger::getLogger()->error("Unknown item type in configuration category");
}

if (full)
{
Expand Down Expand Up @@ -1518,6 +1718,15 @@ ostringstream convert;
{
convert << ", \"file\" : \"" << m_file << "\"";
}

if (!m_listSize.empty())
{
convert << ", \"listSize\" : \"" << m_listSize << "\"";
}
if (!m_listItemType.empty())
{
convert << ", \"items\" : \"" << m_listItemType << "\"";
}
}
convert << " }";

Expand Down Expand Up @@ -1605,10 +1814,21 @@ ostringstream convert;
}
convert << "]";
}
if (!m_listSize.empty())
{
convert << ", \"listSize\" : \"" << m_listSize << "\"";
}
if (!m_listItemType.empty())
{
convert << ", \"items\" : \"" << m_listItemType << "\"";
}

if (m_itemType == StringItem ||
m_itemType == EnumerationItem ||
m_itemType == BoolItem)
m_itemType == BoolItem ||
m_itemType == BucketItem ||
m_itemType == ListItem ||
m_itemType == KVListItem)
{
convert << ", \"default\" : \"" << JSONescape(m_default) << "\" }";
}
Expand Down
Loading

0 comments on commit ad48db9

Please sign in to comment.