added -N argument to read command

This commit is contained in:
john30
2017-04-22 16:06:52 +02:00
parent 586a21fc9f
commit b9a9e7ba53
3 changed files with 33 additions and 16 deletions
+18 -12
View File
@@ -608,7 +608,7 @@ string MainLoop::executeAuth(vector<string> &args, string &user) {
string MainLoop::executeRead(vector<string> &args, const string levels) { string MainLoop::executeRead(vector<string> &args, const string levels) {
size_t argPos = 1; size_t argPos = 1;
bool hex = false, numeric = false; bool hex = false, numeric = false, valueName = false;
OutputFormat verbosity = 0; OutputFormat verbosity = 0;
time_t maxAge = 5*60; time_t maxAge = 5*60;
string circuit, params; string circuit, params;
@@ -637,6 +637,9 @@ string MainLoop::executeRead(vector<string> &args, const string levels) {
verbosity |= OF_NAMES|OF_UNITS|OF_COMMENTS; verbosity |= OF_NAMES|OF_UNITS|OF_COMMENTS;
} else if (args[argPos] == "-n") { } else if (args[argPos] == "-n") {
numeric = true; numeric = true;
} else if (args[argPos] == "-N") {
numeric = true;
valueName = true;
} else if (args[argPos] == "-m") { } else if (args[argPos] == "-m") {
argPos++; argPos++;
if (args.size() > argPos) { if (args.size() > argPos) {
@@ -764,7 +767,7 @@ string MainLoop::executeRead(vector<string> &args, const string levels) {
return getResultCode(ret); return getResultCode(ret);
} }
if (argPos == 0 || args.size() < argPos + 1 || args.size() > argPos + 2) { 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" " NAME [FIELD[.N]]\n"
" or: read [-f] [-m SECONDS] [-s QQ] [-c CIRCUIT] -h ZZPBSBNNDx\n" " or: read [-f] [-m SECONDS] [-s QQ] [-c CIRCUIT] -h ZZPBSBNNDx\n"
" Read value(s) or hex message.\n" " Read value(s) or hex message.\n"
@@ -777,6 +780,7 @@ string MainLoop::executeRead(vector<string> &args, const string levels) {
" -v increase verbosity (include names/units/comments)\n" " -v increase verbosity (include names/units/comments)\n"
" -V be very verbose (include names, units, and comments)\n" " -V be very verbose (include names, units, and comments)\n"
" -n use numeric value of value=name pairs\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" " -i VALUE read additional message parameters from VALUE\n"
" NAME NAME of the message to send\n" " NAME NAME of the message to send\n"
" FIELD only retrieve the field named FIELD\n" " FIELD only retrieve the field named FIELD\n"
@@ -808,7 +812,8 @@ string MainLoop::executeRead(vector<string> &args, const string levels) {
if (message != NULL && pollPriority > 0 && message->setPollPriority(pollPriority)) { if (message != NULL && pollPriority > 0 && message->setPollPriority(pollPriority)) {
m_messages->addPollMessage(message); 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) { if (srcAddress == SYN && dstAddress == SYN && maxAge > 0 && params.length() == 0) {
Message* cacheMessage = m_messages->find(circuit, args[argPos], levels, false, true); Message* cacheMessage = m_messages->find(circuit, args[argPos], levels, false, true);
bool hasCache = cacheMessage != NULL; bool hasCache = cacheMessage != NULL;
@@ -821,8 +826,8 @@ string MainLoop::executeRead(vector<string> &args, const string levels) {
if (verbosity & OF_NAMES) { if (verbosity & OF_NAMES) {
result << cacheMessage->getCircuit() << " " << cacheMessage->getName() << " "; result << cacheMessage->getCircuit() << " " << cacheMessage->getName() << " ";
} }
result_t ret = cacheMessage->decodeLastData(result, verbosity|(numeric?OF_NUMERIC:0), false, ret = cacheMessage->decodeLastData(result, verbosity, false, fieldIndex == -2 ? NULL : fieldName.c_str(),
fieldIndex == -2 ? NULL : fieldName.c_str(), fieldIndex); fieldIndex);
if (ret != RESULT_OK) { if (ret != RESULT_OK) {
if (ret < RESULT_OK) { if (ret < RESULT_OK) {
logError(lf_main, "read %s %s cached: %s", cacheMessage->getCircuit().c_str(), logError(lf_main, "read %s %s cached: %s", cacheMessage->getCircuit().c_str(),
@@ -847,15 +852,15 @@ string MainLoop::executeRead(vector<string> &args, const string levels) {
return getResultCode(RESULT_ERR_INVALID_ADDR); return getResultCode(RESULT_ERR_INVALID_ADDR);
} }
// read directly from bus // 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) { if (ret != RESULT_OK) {
return getResultCode(ret); return getResultCode(ret);
} }
if (verbosity & OF_NAMES) { if (verbosity & OF_NAMES) {
result << message->getCircuit() << " " << message->getName() << " "; result << message->getCircuit() << " " << message->getName() << " ";
} }
ret = message->decodeLastSlaveData(result, verbosity|(numeric?OF_NUMERIC:0), false, ret = message->decodeLastSlaveData(result, verbosity, false, fieldIndex == -2 ? NULL : fieldName.c_str(),
fieldIndex == -2 ? NULL : fieldName.c_str(), fieldIndex); fieldIndex);
if (ret < RESULT_OK) { if (ret < RESULT_OK) {
logError(lf_main, "read %s %s: decode %s", message->getCircuit().c_str(), message->getName().c_str(), logError(lf_main, "read %s %s: decode %s", message->getCircuit().c_str(), message->getName().c_str(),
getResultCode(ret)); getResultCode(ret));
@@ -1509,7 +1514,7 @@ string MainLoop::executeQuit(vector<string> &args, bool& connected) {
string MainLoop::executeHelp() { string MainLoop::executeHelp() {
return "usage:\n" 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" " [-i VALUE[;VALUE]*] NAME [FIELD[.N]]\n"
" Read hex message: read [-f] [-m SECONDS] [-s QQ] [-c CIRCUIT] -h ZZPBSBNNDx\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" " 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<string> &args, bool& connected) { string MainLoop::executeGet(vector<string> &args, bool& connected) {
result_t ret = RESULT_OK; 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; OutputFormat verbosity = OF_NAMES;
size_t argPos = 1; size_t argPos = 1;
string uri = args[argPos++]; string uri = args[argPos++];
@@ -1585,6 +1590,8 @@ string MainLoop::executeGet(vector<string> &args, bool& connected) {
} }
} else if (qname == "numeric") { } else if (qname == "numeric") {
numeric = value.length() == 0 || value == "1"; numeric = value.length() == 0 || value == "1";
} else if (qname == "valuename") {
valueName = value.length() == 0 || value == "1";
} else if (qname == "full") { } else if (qname == "full") {
full = value.length() == 0 || value == "1"; full = value.length() == 0 || value == "1";
} else if (qname == "required") { } else if (qname == "required") {
@@ -1608,9 +1615,8 @@ string MainLoop::executeGet(vector<string> &args, bool& connected) {
time_t maxLastUp = 0; time_t maxLastUp = 0;
if (ret == RESULT_OK) { if (ret == RESULT_OK) {
deque<Message *> messages = m_messages->findAll(circuit, name, getUserLevels(user), exact, true, false, true); deque<Message *> messages = m_messages->findAll(circuit, name, getUserLevels(user), exact, true, false, true);
bool first = 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<Message*>::iterator it = messages.begin(); it != messages.end();) { for (deque<Message*>::iterator it = messages.begin(); it != messages.end();) {
Message* message = *it++; Message* message = *it++;
symbol_t dstAddress = message->getDstAddress(); symbol_t dstAddress = message->getDstAddress();
+9 -1
View File
@@ -718,8 +718,16 @@ result_t ValueListDataField::readSymbols(const SymbolString& input,
} else if (outputFormat & OF_NUMERIC) { } else if (outputFormat & OF_NUMERIC) {
output << setw(0) << dec << static_cast<unsigned>(value); output << setw(0) << dec << static_cast<unsigned>(value);
} else if (outputFormat & OF_JSON) { } else if (outputFormat & OF_JSON) {
output << '"' << it->second << '"'; if (outputFormat & OF_VALUENAME) {
output << "{\"value\":" << setw(0) << dec << static_cast<unsigned>(value);
output << ",\"name\":\"" << it->second << "\"}";
} else {
output << '"' << it->second << '"';
}
} else { } else {
if (outputFormat & OF_VALUENAME) {
output << setw(0) << dec << static_cast<unsigned>(value) << '=';
}
output << it->second; output << it->second;
} }
return RESULT_OK; return RESULT_OK;
+6 -3
View File
@@ -94,14 +94,17 @@ typedef int OutputFormat;
/** bit flag for @a OutputFormat: numeric format (keep numeric value of value=name pairs). */ /** bit flag for @a OutputFormat: numeric format (keep numeric value of value=name pairs). */
#define OF_NUMERIC 0x08 #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. */ /** 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). */ /** 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. */ /** 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. */ /** the message part in which a data field is stored. */
enum PartType { enum PartType {