Skip to content

Commit

Permalink
Merge pull request #485 from fledge-iot/1.9.2RC
Browse files Browse the repository at this point in the history
1.9.2RC
  • Loading branch information
YashTatkondawar authored Oct 4, 2021
2 parents 5c5340a + f7a2db7 commit f1452e8
Show file tree
Hide file tree
Showing 120 changed files with 6,696 additions and 1,036 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

# IDE
*.idea
.vscode/

# Data / cache files
data/etc/storage.json
Expand Down
37 changes: 36 additions & 1 deletion C/common/datapoint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ std::string DatapointValue::toString() const
case T_STRING:
default:
ss << "\"";
ss << *m_value.str;
ss << escape(*m_value.str);
ss << "\"";
return ss.str();
}
Expand Down Expand Up @@ -208,3 +208,38 @@ DatapointValue& DatapointValue::operator=(const DatapointValue& rhs)

return *this;
}

/**
* Escape quotes etc to allow the string to be a property value within
* a JSON document
*
* @param str The string to escape
* @return The escaped string
*/
const std::string DatapointValue::escape(const std::string& str) const
{
std::string rval;
int bscount = 0;

for (size_t i = 0; i < str.length(); i++)
{
if (str[i] == '\\')
{
bscount++;
}
else if (str[i] == '\"')
{
if ((bscount & 1) == 0) // not already escaped
{
rval += "\\"; // Add escape of "
}
bscount = 0;
}
else
{
bscount = 0;
}
rval += str[i];
}
return rval;
}
5 changes: 5 additions & 0 deletions C/common/include/config_category.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@ class ConfigCategory {
void setDescription(const std::string& description);
std::string getName() const { return m_name; };
std::string getDescription() const { return m_description; };

std::string getDisplayName() const { return m_displayName; };
void setDisplayName(const std::string& displayName) {m_displayName = displayName;};

unsigned int getCount() const { return m_items.size(); };
bool itemExists(const std::string& name) const;
bool setItemDisplayName(const std::string& name, const std::string& displayName);
Expand Down Expand Up @@ -159,6 +163,7 @@ class ConfigCategory {
std::vector<CategoryItem *> m_items;
std::string m_name;
std::string m_description;
std::string m_displayName;

public:
using iterator = std::vector<CategoryItem *>::iterator;
Expand Down
1 change: 1 addition & 0 deletions C/common/include/datapoint.h
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ class DatapointValue {

private:
void deleteNestedDPV();
const std::string escape(const std::string& str) const;
union data_t {
std::string* str;
long i;
Expand Down
4 changes: 4 additions & 0 deletions C/common/include/management_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,12 @@ class ManagementClient {
std::ostringstream payload;
payload << "{ \"key\" : \"" << JSONescape(t.getName());
payload << "\", \"description\" : \"" << JSONescape(t.getDescription());
if (! t.getDisplayName().empty() ) {
payload << "\", \"display_name\" : \"" << JSONescape(t.getDisplayName());
}
payload << "\", \"value\" : " << t.itemsToJSON();


/**
* Note:
* At the time being the keep_original_items is added into payload
Expand Down
3 changes: 2 additions & 1 deletion C/common/include/reading.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ class Reading {
void setUserTimestamp(const std::string& timestamp);
void getUserTimestamp(struct timeval *tm) { *tm = m_userTimestamp; };

typedef enum dateTimeFormat { FMT_DEFAULT, FMT_STANDARD, FMT_ISO8601 } readingTimeFormat;
typedef enum dateTimeFormat { FMT_DEFAULT, FMT_STANDARD, FMT_ISO8601, FMT_ISO8601MS } readingTimeFormat;

// Return Reading asset time - ts time
const std::string getAssetDateTime(readingTimeFormat datetimeFmt = FMT_DEFAULT, bool addMs = true) const;
Expand All @@ -76,6 +76,7 @@ class Reading {
Reading() {};
Reading& operator=(Reading const&);
void stringToTimestamp(const std::string& timestamp, struct timeval *ts);
const std::string escape(const std::string& str) const;
unsigned long m_id;
bool m_has_id;
std::string m_asset;
Expand Down
1 change: 1 addition & 0 deletions C/common/include/reading_set.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ class ReadingSet {

// Return the reading id of the last data element
unsigned long getLastId() const { return m_last_id; };
unsigned long getReadingId(uint32_t pos);
void append(ReadingSet *);
void append(ReadingSet&);
void append(const std::vector<Reading *> &);
Expand Down
1 change: 1 addition & 0 deletions C/common/include/string_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ std::string extractLastLevel(const std::string& path, char separator);
void StringStripCRLF(std::string& StringToManage);
string StringStripWhiteSpacesAll(const std::string& original);
string StringStripWhiteSpacesExtra(const std::string& original);
void StringStripQuotes(std::string& StringToManage);

string urlEncode(const string& s);
string urlDecode(const string& s);
Expand Down
2 changes: 2 additions & 0 deletions C/common/include/where.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
#include <vector>

typedef enum Conditional {
Older,
Newer,
Equals,
NotEquals,
GreaterThan,
Expand Down
85 changes: 73 additions & 12 deletions C/common/reading.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ using namespace std;
std::vector<std::string> Reading::m_dateTypes = {
DEFAULT_DATE_TIME_FORMAT,
COMBINED_DATE_STANDARD_FORMAT,
ISO8601_DATE_TIME_FORMAT
ISO8601_DATE_TIME_FORMAT,
ISO8601_DATE_TIME_FORMAT // Version with milliseconds
};

/**
Expand Down Expand Up @@ -174,7 +175,7 @@ string Reading::toJSON(bool minimal) const
ostringstream convert;

convert << "{\"asset_code\":\"";
convert << m_asset;
convert << escape(m_asset);
convert << "\",\"user_ts\":\"";

// Add date_time with microseconds + timezone UTC:
Expand Down Expand Up @@ -233,7 +234,7 @@ const string Reading::getAssetDateTime(readingTimeFormat dateFormat, bool addMS)
{
char date_time[DATE_TIME_BUFFER_LEN];
char micro_s[10];
ostringstream assetTime;
char assetTime[DATE_TIME_BUFFER_LEN + 20];

// Populate tm structure
struct tm timeinfo;
Expand All @@ -259,9 +260,20 @@ ostringstream assetTime;
m_timestamp.tv_usec);

// Add date_time + microseconds
assetTime << date_time << micro_s;

return assetTime.str();
if (dateFormat != FMT_ISO8601MS)
{
snprintf(assetTime, sizeof(assetTime), "%s%s", date_time, micro_s);
}
else
{
string dt(date_time);
size_t pos = dt.find_first_of("+");
pos--;
snprintf(assetTime, sizeof(assetTime), "%s%s%s",
dt.substr(0, pos).c_str(),
micro_s, dt.substr(pos).c_str());
}
return string(assetTime);
}
else
{
Expand All @@ -279,6 +291,7 @@ const string Reading::getAssetDateUserTime(readingTimeFormat dateFormat, bool ad
{
char date_time[DATE_TIME_BUFFER_LEN+10];
char micro_s[10];
char assetTime[DATE_TIME_BUFFER_LEN + 20];

// Populate tm structure with UTC time
struct tm timeinfo;
Expand All @@ -299,13 +312,25 @@ const string Reading::getAssetDateUserTime(readingTimeFormat dateFormat, bool ad
{
// Add microseconds
snprintf(micro_s,
sizeof(micro_s),
".%06lu",
m_userTimestamp.tv_usec);

strcat(date_time,micro_s);
sizeof(micro_s),
".%06lu",
m_userTimestamp.tv_usec);

return string(date_time);
// Add date_time + microseconds
if (dateFormat != FMT_ISO8601MS)
{
snprintf(assetTime, sizeof(assetTime), "%s%s", date_time, micro_s);
}
else
{
string dt(date_time);
size_t pos = dt.find_first_of("+");
pos--;
snprintf(assetTime, sizeof(assetTime), "%s%s%s",
dt.substr(0, pos).c_str(),
micro_s, dt.substr(pos).c_str());
}
return string(assetTime);
}
else
{
Expand Down Expand Up @@ -394,4 +419,40 @@ void Reading::stringToTimestamp(const string& timestamp, struct timeval *ts)
sscanf(ptr, "%02d:%02d", &h, &m);
ts->tv_sec += sign * ((3600 * h) + (60 * m));
}

}

/**
* Escape quotes etc to allow the string to be a property value within
* a JSON document
*
* @param str The string to escape
* @return The escaped string
*/
const string Reading::escape(const string& str) const
{
string rval;
int bscount = 0;

for (size_t i = 0; i < str.length(); i++)
{
if (str[i] == '\\')
{
bscount++;
}
else if (str[i] == '\"')
{
if ((bscount & 1) == 0) // not already escaped
{
rval += "\\"; // Add escape of "
}
bscount = 0;
}
else
{
bscount = 0;
}
rval += str[i];
}
return rval;
}
25 changes: 22 additions & 3 deletions C/common/reading_set.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ const vector<string> JSON_characters_to_be_escaped = {
/**
* Construct an empty reading set
*/
ReadingSet::ReadingSet() : m_count(0)
ReadingSet::ReadingSet() : m_count(0), m_last_id(0)
{
}

Expand All @@ -47,11 +47,13 @@ ReadingSet::ReadingSet() : m_count(0)
* of readings to be copied
* into m_readings vector
*/
ReadingSet::ReadingSet(vector<Reading *>* readings)
ReadingSet::ReadingSet(vector<Reading *>* readings) : m_last_id(0)
{
m_count = readings->size();
for (auto it = readings->begin(); it != readings->end(); ++it)
{
if ((*it)->getId() > m_last_id)
m_last_id = (*it)->getId();
m_readings.push_back(*it);
}
}
Expand All @@ -62,7 +64,7 @@ ReadingSet::ReadingSet(vector<Reading *>* readings)
*
* @param json The JSON document (as string) with readings data
*/
ReadingSet::ReadingSet(const std::string& json)
ReadingSet::ReadingSet(const std::string& json) : m_last_id(0)
{
unsigned long rows = 0;
Document doc;
Expand Down Expand Up @@ -181,6 +183,8 @@ ReadingSet::append(const vector<Reading *>& readings)
{
for (auto it = readings.cbegin(); it != readings.cend(); it++)
{
if ((*it)->getId() > m_last_id)
m_last_id = (*it)->getId();
m_readings.push_back(*it);
m_count++;
}
Expand Down Expand Up @@ -209,6 +213,21 @@ ReadingSet::clear()
m_readings.clear();
}

/**
* Return the ID of the nth reading in the reading set
*
* @param pos The position of the reading to return the ID for
*/
unsigned long ReadingSet::getReadingId(uint32_t pos)
{
if (pos < m_readings.size())
{
Reading *reading = m_readings[pos];
return reading->getId();
}
return m_last_id;
}

/**
* Construct a reading from a JSON document
*
Expand Down
14 changes: 13 additions & 1 deletion C/common/string_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ std::string StringSlashFix(const std::string& stringToFix)
}

/**
* Strips Line feed and carige return
* Strips Line feed and carriage return
*
*/
void StringStripCRLF(std::string& StringToManage)
Expand All @@ -179,6 +179,18 @@ void StringStripCRLF(std::string& StringToManage)

}

/**
* Stripes " from the string
*
*/
void StringStripQuotes(std::string& StringToManage)
{
if ( ! StringToManage.empty())
{
StringReplaceAll(StringToManage, "\"", "");
}
}

/**
* Removes all the white spaces from a string
*
Expand Down
13 changes: 12 additions & 1 deletion C/common/where.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ ostringstream json;
json << "\"condition\" : \"";
switch (m_condition)
{
case Older:
json << "older";
break;
case Newer:
json << "newer";
break;
case Equals:
json << "=";
break;
Expand All @@ -58,7 +64,12 @@ ostringstream json;
}
json << "\", ";

if (m_condition != In)
if ( (m_condition == Older) || (m_condition == Newer) )
{
json << "\"value\" : " << m_value << "";

}
else if (m_condition != In)
{
json << "\"value\" : \"" << m_value << "\"";
}
Expand Down
Loading

0 comments on commit f1452e8

Please sign in to comment.