allow passive messages to be found as well, added leadingSeparator to decode(), fixed default part for passive set

This commit is contained in:
john30
2014-12-06 23:41:14 +01:00
parent 97d4e40172
commit c0c1cf3966
3 changed files with 37 additions and 33 deletions
+31 -30
View File
@@ -113,7 +113,7 @@ result_t Message::create(vector<string>::iterator& it, const vector<string>::ite
defaultsChar = 'r'; defaultsChar = 'r';
} else { // any other: passive set/get } else { // any other: passive set/get
isPassive = true; isPassive = true;
isSet = strncasecmp(str+1, "R", 1) == 0; isSet = strncasecmp(str+1, "W", 1) == 0;
defaultsChar = str[0]; defaultsChar = str[0];
} }
@@ -272,14 +272,14 @@ result_t Message::prepareMaster(const unsigned char srcAddress, SymbolString& ma
} }
result_t Message::decode(const PartType partType, SymbolString& data, result_t Message::decode(const PartType partType, SymbolString& data,
ostringstream& output, char separator) ostringstream& output, bool leadingSeparator, char separator)
{ {
unsigned char offset; unsigned char offset;
if (partType == pt_masterData) if (partType == pt_masterData)
offset = m_id.size() - 2; offset = m_id.size() - 2;
else else
offset = 0; offset = 0;
result_t result = m_data->read(partType, data, offset, output, false, false, separator); result_t result = m_data->read(partType, data, offset, output, leadingSeparator, false, separator);
if (result != RESULT_OK) if (result != RESULT_OK)
return result; return result;
/*if (m_isPassive == false && answer == true) { /*if (m_isPassive == false && answer == true) {
@@ -294,35 +294,36 @@ result_t Message::decode(const PartType partType, SymbolString& data,
result_t MessageMap::add(Message* message) result_t MessageMap::add(Message* message)
{ {
if (message->isPassive() == false) { unsigned long long pkey = message->getKey();
bool isSet = message->isSet(); bool isPassive = message->isPassive();
string clazz = message->getClass(); if (isPassive == true) {
string name = message->getName(); map<unsigned long long, Message*>::iterator keyIt = m_passiveMessagesByKey.find(pkey);
string key = string(isSet ? "W" : "R") + clazz + ";" + name; if (keyIt != m_passiveMessagesByKey.end()) {
map<string, Message*>::iterator nameIt = m_messagesByName.find(key);
if (nameIt != m_messagesByName.end()) {
return RESULT_ERR_DUPLICATE; // duplicate key return RESULT_ERR_DUPLICATE; // duplicate key
} }
m_messagesByName[key] = message;
key = string(isSet ? "-W" : "-R") + name; // also store without class
m_messagesByName[key] = message;
return RESULT_OK;
} }
bool isSet = message->isSet();
unsigned long long key = message->getKey(); string clazz = message->getClass();
map<unsigned long long, Message*>::iterator keyIt = m_passiveMessagesByKey.find(key); string name = message->getName();
if (keyIt != m_passiveMessagesByKey.end()) { string key = string(isPassive ? "P" : (isSet ? "W" : "R")) + clazz + ";" + name;
map<string, Message*>::iterator nameIt = m_messagesByName.find(key);
if (nameIt != m_messagesByName.end()) {
return RESULT_ERR_DUPLICATE; // duplicate key return RESULT_ERR_DUPLICATE; // duplicate key
} }
unsigned char idLength = message->getId().size() - 2; m_messagesByName[key] = message;
if (idLength < m_minIdLength)
m_minIdLength = idLength; key = string(isPassive ? "-P;" : (isSet ? "-W;" : "-R;")) + name; // also store without class
if (idLength > m_maxIdLength) m_messagesByName[key] = message; // last key without class overrides previous
m_maxIdLength = idLength;
m_passiveMessagesByKey[key] = message; if (message->isPassive() == true) {
unsigned char idLength = message->getId().size() - 2;
if (idLength < m_minIdLength)
m_minIdLength = idLength;
if (idLength > m_maxIdLength)
m_maxIdLength = idLength;
m_passiveMessagesByKey[pkey] = message;
}
return RESULT_OK; return RESULT_OK;
} }
@@ -353,14 +354,14 @@ result_t MessageMap::addFromFile(vector<string>& row, DataFieldTemplates* arg, v
return result; return result;
} }
Message* MessageMap::find(const string& clazz, const string& name, const bool isSet) Message* MessageMap::find(const string& clazz, const string& name, const bool isSet,const bool isPassive)
{ {
string key;
for (int i=0; i<2; i++) { for (int i=0; i<2; i++) {
string key;
if (i==0) if (i==0)
key = string(isSet ? "W" : "R") + clazz + ";" + name; key = string(isPassive ? "P" : (isSet ? "W" : "R")) + clazz + ";" + name;
else else
key = string(isSet ? "-W" : "-R") + name; // second try: without class key = string(isPassive ? "-P;" : (isSet ? "-W;" : "-R;")) + name; // second try: without class
map<string, Message*>::iterator it = m_messagesByName.find(key); map<string, Message*>::iterator it = m_messagesByName.find(key);
if (it != m_messagesByName.end()) if (it != m_messagesByName.end())
return it->second; return it->second;
+4 -2
View File
@@ -137,11 +137,12 @@ public:
* @param partType the @a PartType of the data. * @param partType the @a PartType of the data.
* @param data the unescaped data @a SymbolString for reading binary data. * @param data the unescaped data @a SymbolString for reading binary data.
* @param output the @a ostringstream to append the formatted value to. * @param output the @a ostringstream to append the formatted value to.
* @param leadingSeparator whether to prepend a separator before the formatted value.
* @param separator the separator character between multiple fields. * @param separator the separator character between multiple fields.
* @return @a RESULT_OK on success, or an error code. * @return @a RESULT_OK on success, or an error code.
*/ */
result_t decode(const PartType partType, SymbolString& data, result_t decode(const PartType partType, SymbolString& data,
ostringstream& output, char separator=';'); ostringstream& output, bool leadingSeparator=false, char separator=';');
private: private:
@@ -200,10 +201,11 @@ public:
* @param class the optional device class. * @param class the optional device class.
* @param name the message name. * @param name the message name.
* @param isSet whether this is a set message. * @param isSet whether this is a set message.
* @param isPassive whether this is a passive message.
* @return the @a Message instance, or NULL. * @return the @a Message instance, or NULL.
* Note: the caller may not free the returned instance. * Note: the caller may not free the returned instance.
*/ */
Message* find(const string& clazz, const string& name, const bool isSet); Message* find(const string& clazz, const string& name, const bool isSet, const bool isPassive=false);
/** /**
* @brief Find the @a Message instance for the specified master data. * @brief Find the @a Message instance for the specified master data.
* @param master the master @a SymbolString for identifying the @a Message. * @param master the master @a SymbolString for identifying the @a Message.
+2 -1
View File
@@ -52,6 +52,7 @@ int main()
{"r;ehp;time;;;08;b509;0d2800;;;time", "15:00:17", "ff08b509030d2800ea", "0311000f00", "m"}, {"r;ehp;time;;;08;b509;0d2800;;;time", "15:00:17", "ff08b509030d2800ea", "0311000f00", "m"},
{"r;ehp;date;;;08;b509;0d2900;;;hda:3", "23.11.2014", "ff08b509030d290071", "03170b0e5a", "m"}, {"r;ehp;date;;;08;b509;0d2900;;;hda:3", "23.11.2014", "ff08b509030d290071", "03170b0e5a", "m"},
{"u;ehp;ActualEnvironmentPower;Energiebezug;;08;B509;29BA00;;s;IGN:2;;;;;s;power", "8", "1008b5090329ba00", "03ba0008", "pm"}, {"u;ehp;ActualEnvironmentPower;Energiebezug;;08;B509;29BA00;;s;IGN:2;;;;;s;power", "8", "1008b5090329ba00", "03ba0008", "pm"},
{"uw;ehp;test;Test;;08;B5de;ab;;;power;;;;;s;hex:1", "8;39", "1008b5de02ab08", "0139", "pm"},
{"","55.50;ok","1025b50903290000","050000780300",""}, {"","55.50;ok","1025b50903290000","050000780300",""},
{"","no;25","10feb505042700190023","",""}, {"","no;25","10feb505042700190023","",""},
}; };
@@ -151,7 +152,7 @@ int main()
ostringstream output; ostringstream output;
result = message->decode(pt_masterData, mstr, output); result = message->decode(pt_masterData, mstr, output);
if (result == RESULT_OK) if (result == RESULT_OK)
result = message->decode(pt_slaveData, sstr, output); result = message->decode(pt_slaveData, sstr, output, output.str().empty() == false);
if (result != RESULT_OK) { if (result != RESULT_OK) {
cout << " \"" << inputStr << "\": decode error: " cout << " \"" << inputStr << "\": decode error: "
<< getResultCode(result) << endl; << getResultCode(result) << endl;