corrected uniqueness of json keys, added write as option to json get

This commit is contained in:
john30
2017-05-25 12:54:09 +02:00
parent dbe70f0126
commit d36315ae35
3 changed files with 34 additions and 9 deletions
+18 -5
View File
@@ -1616,7 +1616,7 @@ result_t MainLoop::executeHelp(ostringstream* ostream) {
} }
result_t MainLoop::executeGet(const vector<string>& args, bool* connected, ostringstream* ostream) { result_t MainLoop::executeGet(const vector<string>& args, bool* connected, ostringstream* ostream) {
bool numeric = false, valueName = false, required = false, full = false; bool numeric = false, valueName = false, required = false, full = false, withWrite = false;
OutputFormat verbosity = OF_NAMES; OutputFormat verbosity = OF_NAMES;
size_t argPos = 1; size_t argPos = 1;
string uri = args[argPos++]; string uri = args[argPos++];
@@ -1671,6 +1671,8 @@ result_t MainLoop::executeGet(const vector<string>& args, bool* connected, ostri
full = value.length() == 0 || value == "1" || value == "true"; full = value.length() == 0 || value == "1" || value == "true";
} else if (qname == "required") { } else if (qname == "required") {
required = value.length() == 0 || value == "1" || value == "true"; required = value.length() == 0 || value == "1" || value == "true";
} else if (qname == "write") {
withWrite = value.length() == 0 || value == "1" || value == "true";
} else if (qname == "user") { } else if (qname == "user") {
user = value; user = value;
} else if (qname == "secret") { } else if (qname == "secret") {
@@ -1691,9 +1693,12 @@ result_t MainLoop::executeGet(const vector<string>& args, bool* connected, ostri
if (ret == RESULT_OK) { if (ret == RESULT_OK) {
bool first = true; bool first = true;
verbosity |= (valueName ? OF_VALUENAME : numeric ? OF_NUMERIC : 0) | OF_JSON | (full ? OF_ALL_ATTRS : 0); verbosity |= (valueName ? OF_VALUENAME : numeric ? OF_NUMERIC : 0) | OF_JSON | (full ? OF_ALL_ATTRS : 0);
deque<Message *> messages; deque<Message*> messages;
m_messages->findAll(circuit, name, getUserLevels(user), exact, true, false, true, true, true, 0, 0, &messages); m_messages->findAll(circuit, name, getUserLevels(user), exact, true, withWrite, true, true, true, 0, 0,
for (const auto message : messages) { &messages);
string lastName;
for (deque<Message*>::iterator it = messages.begin(); it != messages.end(); it++) {
Message* message = *it;
symbol_t dstAddress = message->getDstAddress(); symbol_t dstAddress = message->getDstAddress();
if (dstAddress == SYN) { if (dstAddress == SYN) {
continue; continue;
@@ -1728,8 +1733,16 @@ result_t MainLoop::executeGet(const vector<string>& args, bool* connected, ostri
if (full && m_messages->decodeCircuit(lastCircuit, verbosity, ostream)) { // add circuit specific values if (full && m_messages->decodeCircuit(lastCircuit, verbosity, ostream)) { // add circuit specific values
first = false; first = false;
} }
lastName = "";
} }
message->decode(!first, verbosity, ostream); name = message->getName();
bool same = name == lastName;
if (!same && !lastName.empty() && it+1 != messages.end()) {
Message* next = *(it+1);
same = next->getCircuit() == lastCircuit && next->getName() == name;
}
message->decode(!first, same, verbosity, ostream);
lastName = name;
first = false; first = false;
} }
+12 -2
View File
@@ -927,11 +927,21 @@ void Message::dumpField(const string& fieldName, bool withConditions, ostream* o
dumpAttribute(false, fieldName, output); dumpAttribute(false, fieldName, output);
} }
void Message::decode(bool leadingSeparator, OutputFormat outputFormat, ostringstream* output) const { void Message::decode(bool leadingSeparator, bool appendDirection, OutputFormat outputFormat, ostringstream* output)
const {
if (leadingSeparator) { if (leadingSeparator) {
*output << ","; *output << ",";
} }
*output << "\n \"" << getName() << "\": {" // TODO include read/write/passive for overlapping names *output << "\n \"" << getName();
if (appendDirection) {
if (isPassive()) {
*output << "-u";
} else if (isWrite()) {
*output << "-w";
}
}
*output << "\": {"
<< "\n \"name\": \"" << getName() << "\""
<< "\n \"lastup\": " << setw(0) << dec << static_cast<unsigned>(getLastUpdateTime()); << "\n \"lastup\": " << setw(0) << dec << static_cast<unsigned>(getLastUpdateTime());
if (getLastUpdateTime() != 0) { if (getLastUpdateTime() != 0) {
*output << ",\n \"zz\": \"" << setfill('0') << setw(2) << hex << static_cast<unsigned>(getDstAddress()) << "\""; *output << ",\n \"zz\": \"" << setfill('0') << setw(2) << hex << static_cast<unsigned>(getDstAddress()) << "\"";
+4 -2
View File
@@ -549,12 +549,14 @@ class Message : public AttributedItem {
virtual void dumpField(const string& fieldName, bool withConditions, ostream* output) const; virtual void dumpField(const string& fieldName, bool withConditions, ostream* output) const;
/** /**
* Decode the message from the last stored data. * Decode the message from the last stored data in JSON format.
* @param leadingSeparator whether to prepend a separator before the first value. * @param leadingSeparator whether to prepend a separator before the first value.
* @param appendDirection whether to append the direction to the name key (for passive and write).
* @param outputFormat the @a OutputFormat options to use. * @param outputFormat the @a OutputFormat options to use.
* @param output the @a ostringstream to append the decoded value(s) to. * @param output the @a ostringstream to append the decoded value(s) to.
*/ */
virtual void decode(bool leadingSeparator, OutputFormat outputFormat, ostringstream* output) const; virtual void decode(bool leadingSeparator, bool appendDirection, OutputFormat outputFormat, ostringstream* output)
const;
protected: protected:
/** the optional circuit name. */ /** the optional circuit name. */