From b9a9e7ba53e83e6defbdad68de1fdc88a7f37cb2 Mon Sep 17 00:00:00 2001 From: john30 Date: Sat, 22 Apr 2017 16:06:52 +0200 Subject: [PATCH] added -N argument to read command --- src/ebusd/mainloop.cpp | 30 ++++++++++++++++++------------ src/lib/ebus/data.cpp | 10 +++++++++- src/lib/ebus/datatype.h | 9 ++++++--- 3 files changed, 33 insertions(+), 16 deletions(-) diff --git a/src/ebusd/mainloop.cpp b/src/ebusd/mainloop.cpp index 10ce22aa..710acc6f 100644 --- a/src/ebusd/mainloop.cpp +++ b/src/ebusd/mainloop.cpp @@ -608,7 +608,7 @@ string MainLoop::executeAuth(vector &args, string &user) { string MainLoop::executeRead(vector &args, const string levels) { size_t argPos = 1; - bool hex = false, numeric = false; + bool hex = false, numeric = false, valueName = false; OutputFormat verbosity = 0; time_t maxAge = 5*60; string circuit, params; @@ -637,6 +637,9 @@ string MainLoop::executeRead(vector &args, const string levels) { verbosity |= OF_NAMES|OF_UNITS|OF_COMMENTS; } else if (args[argPos] == "-n") { numeric = true; + } else if (args[argPos] == "-N") { + numeric = true; + valueName = true; } else if (args[argPos] == "-m") { argPos++; if (args.size() > argPos) { @@ -764,7 +767,7 @@ string MainLoop::executeRead(vector &args, const string levels) { return getResultCode(ret); } if (argPos == 0 || args.size() < argPos + 1 || args.size() > argPos + 2) { - return "usage: read [-f] [-m SECONDS] [-s QQ] [-d ZZ] [-c CIRCUIT] [-p PRIO] [-v|-V] [-n] [-i VALUE[;VALUE]*]" + return "usage: read [-f] [-m SECONDS] [-s QQ] [-d ZZ] [-c CIRCUIT] [-p PRIO] [-v|-V] [-n|-N] [-i VALUE[;VALUE]*]" " NAME [FIELD[.N]]\n" " or: read [-f] [-m SECONDS] [-s QQ] [-c CIRCUIT] -h ZZPBSBNNDx\n" " Read value(s) or hex message.\n" @@ -777,6 +780,7 @@ string MainLoop::executeRead(vector &args, const string levels) { " -v increase verbosity (include names/units/comments)\n" " -V be very verbose (include names, units, and comments)\n" " -n use numeric value of value=name pairs\n" + " -N use numeric and named value of value=name pairs\n" " -i VALUE read additional message parameters from VALUE\n" " NAME NAME of the message to send\n" " FIELD only retrieve the field named FIELD\n" @@ -808,7 +812,8 @@ string MainLoop::executeRead(vector &args, const string levels) { if (message != NULL && pollPriority > 0 && message->setPollPriority(pollPriority)) { m_messages->addPollMessage(message); } - + verbosity |= valueName ? OF_VALUENAME : numeric ? OF_NUMERIC : 0; + result_t ret; if (srcAddress == SYN && dstAddress == SYN && maxAge > 0 && params.length() == 0) { Message* cacheMessage = m_messages->find(circuit, args[argPos], levels, false, true); bool hasCache = cacheMessage != NULL; @@ -821,8 +826,8 @@ string MainLoop::executeRead(vector &args, const string levels) { if (verbosity & OF_NAMES) { result << cacheMessage->getCircuit() << " " << cacheMessage->getName() << " "; } - result_t ret = cacheMessage->decodeLastData(result, verbosity|(numeric?OF_NUMERIC:0), false, - fieldIndex == -2 ? NULL : fieldName.c_str(), fieldIndex); + ret = cacheMessage->decodeLastData(result, verbosity, false, fieldIndex == -2 ? NULL : fieldName.c_str(), + fieldIndex); if (ret != RESULT_OK) { if (ret < RESULT_OK) { logError(lf_main, "read %s %s cached: %s", cacheMessage->getCircuit().c_str(), @@ -847,15 +852,15 @@ string MainLoop::executeRead(vector &args, const string levels) { return getResultCode(RESULT_ERR_INVALID_ADDR); } // read directly from bus - result_t ret = m_busHandler->readFromBus(message, params, dstAddress, srcAddress); + ret = m_busHandler->readFromBus(message, params, dstAddress, srcAddress); if (ret != RESULT_OK) { return getResultCode(ret); } if (verbosity & OF_NAMES) { result << message->getCircuit() << " " << message->getName() << " "; } - ret = message->decodeLastSlaveData(result, verbosity|(numeric?OF_NUMERIC:0), false, - fieldIndex == -2 ? NULL : fieldName.c_str(), fieldIndex); + ret = message->decodeLastSlaveData(result, verbosity, false, fieldIndex == -2 ? NULL : fieldName.c_str(), + fieldIndex); if (ret < RESULT_OK) { logError(lf_main, "read %s %s: decode %s", message->getCircuit().c_str(), message->getName().c_str(), getResultCode(ret)); @@ -1509,7 +1514,7 @@ string MainLoop::executeQuit(vector &args, bool& connected) { string MainLoop::executeHelp() { return "usage:\n" - " read|r Read value(s): read [-f] [-m SECONDS] [-s QQ] [-d ZZ] [-c CIRCUIT] [-p PRIO] [-v|-V] [-n]" + " read|r Read value(s): read [-f] [-m SECONDS] [-s QQ] [-d ZZ] [-c CIRCUIT] [-p PRIO] [-v|-V] [-n|-N]" " [-i VALUE[;VALUE]*] NAME [FIELD[.N]]\n" " Read hex message: read [-f] [-m SECONDS] [-s QQ] [-c CIRCUIT] -h ZZPBSBNNDx\n" " write|w Write value(s): write [-s QQ] [-d ZZ] -c CIRCUIT NAME [VALUE[;VALUE]*]\n" @@ -1535,7 +1540,7 @@ string MainLoop::executeHelp() { string MainLoop::executeGet(vector &args, bool& connected) { result_t ret = RESULT_OK; - bool numeric = false, required = false, full = false; + bool numeric = false, valueName = false, required = false, full = false; OutputFormat verbosity = OF_NAMES; size_t argPos = 1; string uri = args[argPos++]; @@ -1585,6 +1590,8 @@ string MainLoop::executeGet(vector &args, bool& connected) { } } else if (qname == "numeric") { numeric = value.length() == 0 || value == "1"; + } else if (qname == "valuename") { + valueName = value.length() == 0 || value == "1"; } else if (qname == "full") { full = value.length() == 0 || value == "1"; } else if (qname == "required") { @@ -1608,9 +1615,8 @@ string MainLoop::executeGet(vector &args, bool& connected) { time_t maxLastUp = 0; if (ret == RESULT_OK) { deque messages = m_messages->findAll(circuit, name, getUserLevels(user), exact, true, false, true); - bool first = true; - verbosity |= (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); for (deque::iterator it = messages.begin(); it != messages.end();) { Message* message = *it++; symbol_t dstAddress = message->getDstAddress(); diff --git a/src/lib/ebus/data.cpp b/src/lib/ebus/data.cpp index 0220b959..9ccddcf9 100644 --- a/src/lib/ebus/data.cpp +++ b/src/lib/ebus/data.cpp @@ -718,8 +718,16 @@ result_t ValueListDataField::readSymbols(const SymbolString& input, } else if (outputFormat & OF_NUMERIC) { output << setw(0) << dec << static_cast(value); } else if (outputFormat & OF_JSON) { - output << '"' << it->second << '"'; + if (outputFormat & OF_VALUENAME) { + output << "{\"value\":" << setw(0) << dec << static_cast(value); + output << ",\"name\":\"" << it->second << "\"}"; + } else { + output << '"' << it->second << '"'; + } } else { + if (outputFormat & OF_VALUENAME) { + output << setw(0) << dec << static_cast(value) << '='; + } output << it->second; } return RESULT_OK; diff --git a/src/lib/ebus/datatype.h b/src/lib/ebus/datatype.h index 019a2466..7b595694 100644 --- a/src/lib/ebus/datatype.h +++ b/src/lib/ebus/datatype.h @@ -94,14 +94,17 @@ typedef int OutputFormat; /** bit flag for @a OutputFormat: numeric format (keep numeric value of value=name pairs). */ #define OF_NUMERIC 0x08 +/** bit flag for @a OutputFormat: value=name format for such pairs. */ +#define OF_VALUENAME 0x10 + /** bit flag for @a OutputFormat: JSON format. */ -#define OF_JSON 0x10 +#define OF_JSON 0x20 /** bit flag for @a OutputFormat: short format (only name and value, no indentation). */ -#define OF_SHORT 0x20 +#define OF_SHORT 0x40 /** bit flag for @a OutputFormat: include all attributes. */ -#define OF_ALL_ATTRS 0x40 +#define OF_ALL_ATTRS 0x80 /** the message part in which a data field is stored. */ enum PartType {