added option to set poll priority to read command and http get, corrected HTTP result on invalid query string

This commit is contained in:
john30
2015-05-25 14:09:25 +02:00
parent 6725cb526f
commit 6458991826
+35 -11
View File
@@ -256,7 +256,7 @@ string MainLoop::executeRead(vector<string> &args)
bool hex = false, verbose = false; bool hex = false, verbose = false;
time_t maxAge = 5*60; time_t maxAge = 5*60;
string circuit; string circuit;
unsigned char dstAddress = SYN; unsigned char dstAddress = SYN, pollPriority = 0;
while (args.size() > argPos && args[argPos][0] == '-') { while (args.size() > argPos && args[argPos][0] == '-') {
if (args[argPos] == "-h") { if (args[argPos] == "-h") {
hex = true; hex = true;
@@ -299,13 +299,23 @@ string MainLoop::executeRead(vector<string> &args)
dstAddress = (unsigned char)parseInt(args[argPos].c_str(), 16, 0, 0xff, ret); dstAddress = (unsigned char)parseInt(args[argPos].c_str(), 16, 0, 0xff, ret);
if (ret != RESULT_OK || !isValidAddress(dstAddress) || isMaster(dstAddress)) if (ret != RESULT_OK || !isValidAddress(dstAddress) || isMaster(dstAddress))
return getResultCode(RESULT_ERR_INVALID_ADDR); return getResultCode(RESULT_ERR_INVALID_ADDR);
} else if (args[argPos] == "-p") {
argPos++;
if (argPos >= args.size()) {
argPos = 0; // print usage
break;
}
result_t ret;
pollPriority = (unsigned char)parseInt(args[argPos].c_str(), 10, 1, 9, ret);
if (ret != RESULT_OK)
return getResultCode(RESULT_ERR_INVALID_NUM);
} else { } else {
argPos = 0; // print usage argPos = 0; // print usage
break; break;
} }
argPos++; argPos++;
} }
if (hex && (dstAddress != SYN || verbose || args.size() < argPos + 1)) { if (hex && (dstAddress != SYN || verbose || pollPriority > 0 || args.size() < argPos + 1)) {
argPos = 0; // print usage argPos = 0; // print usage
} }
@@ -356,13 +366,14 @@ string MainLoop::executeRead(vector<string> &args)
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] [-c CIRCUIT] [-d ZZ] [-v] NAME [FIELD[.N]]\n" return "usage: read [-f] [-m SECONDS] [-c CIRCUIT] [-d ZZ] [-p PRIO] [-v] NAME [FIELD[.N]]\n"
" or: read [-f] [-m SECONDS] [-c CIRCUIT] -h ZZPBSBNNDx\n" " or: read [-f] [-m SECONDS] [-c CIRCUIT] -h ZZPBSBNNDx\n"
" Read value(s) or hex message.\n" " Read value(s) or hex message.\n"
" -f force reading from the bus (same as '-m 0')\n" " -f force reading from the bus (same as '-m 0')\n"
" -m SECONDS only return cached value if age is less than SECONDS [300]\n" " -m SECONDS only return cached value if age is less than SECONDS [300]\n"
" -c CIRCUIT limit to messages of CIRCUIT\n" " -c CIRCUIT limit to messages of CIRCUIT\n"
" -d ZZ override destination address ZZ\n" " -d ZZ override destination address ZZ\n"
" -p PRIO set the message poll priority (1-9)\n"
" -v be verbose (include field names, units, and comments)\n" " -v be verbose (include field names, units, and comments)\n"
" NAME the NAME of the message to send\n" " NAME the NAME of the message to send\n"
" FIELD only retrieve the field named FIELD\n" " FIELD only retrieve the field named FIELD\n"
@@ -390,6 +401,11 @@ string MainLoop::executeRead(vector<string> &args)
ostringstream result; ostringstream result;
Message* message = m_messages->find(circuit, args[argPos], false); Message* message = m_messages->find(circuit, args[argPos], false);
// adjust poll priority
if (message != NULL && pollPriority > 0 && message->setPollPriority(pollPriority)) {
m_messages->addPollMessage(message);
}
if (dstAddress==SYN && maxAge > 0) { if (dstAddress==SYN && maxAge > 0) {
Message* cacheMessage = m_messages->find(circuit, args[argPos], false, true); Message* cacheMessage = m_messages->find(circuit, args[argPos], false, true);
bool hasCache = cacheMessage != NULL; bool hasCache = cacheMessage != NULL;
@@ -824,7 +840,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] [-c CIRCUIT] [-d ZZ] [-v] NAME [FIELD[.N]]\n" " read|r Read value(s): read [-f] [-m SECONDS] [-c CIRCUIT] [-d ZZ] [-p PRIO] [-v] NAME [FIELD[.N]]\n"
" Read hex message: read [-f] [-m SECONDS] [-c CIRCUIT] -h ZZPBSBNNDx\n" " Read hex message: read [-f] [-m SECONDS] [-c CIRCUIT] -h ZZPBSBNNDx\n"
" write|w Write value(s): write [-c] CIRCUIT NAME [VALUE[;VALUE]*]\n" " write|w Write value(s): write [-c] CIRCUIT NAME [VALUE[;VALUE]*]\n"
" Write hex message: write -h ZZPBSBNNDx\n" " Write hex message: write -h ZZPBSBNNDx\n"
@@ -864,6 +880,7 @@ string MainLoop::executeGet(vector<string> &args, bool& connected)
name = uri.substr(pos+1); name = uri.substr(pos+1);
} }
time_t since = 0; time_t since = 0;
unsigned char pollPriority = 0;
if (args.size() > argPos) { if (args.size() > argPos) {
string query = args[argPos++]; string query = args[argPos++];
istringstream stream(query); istringstream stream(query);
@@ -872,12 +889,15 @@ string MainLoop::executeGet(vector<string> &args, bool& connected)
pos = token.find('='); pos = token.find('=');
if (pos != string::npos) { if (pos != string::npos) {
const char* qname = query.substr(0, pos).c_str(); const char* qname = query.substr(0, pos).c_str();
const char* value = query.substr(pos+1).c_str();
if (strcmp(qname, "since") == 0) { if (strcmp(qname, "since") == 0) {
since = parseInt(query.substr(pos+1).c_str(), 10, 0, 0xffffffff, ret); since = parseInt(value, 10, 0, 0xffffffff, ret);
if (ret != RESULT_OK) { if (ret != RESULT_OK)
ret = RESULT_ERR_INVALID_ARG; break;
} else if (strcmp(qname, "poll") == 0) {
pollPriority = (unsigned char)parseInt(value, 10, 1, 9, ret);
if (ret != RESULT_OK)
break; break;
}
} }
} }
} }
@@ -890,12 +910,14 @@ string MainLoop::executeGet(vector<string> &args, bool& connected)
time_t maxLastUp = 0; time_t maxLastUp = 0;
for (deque<Message*>::iterator it = messages.begin(); ret == RESULT_OK && it < messages.end();) { for (deque<Message*>::iterator it = messages.begin(); ret == RESULT_OK && it < messages.end();) {
Message* message = *it++; Message* message = *it++;
time_t lastup = message->getLastUpdateTime();
if (since > 0 && lastup <= since)
continue;
unsigned char dstAddress = message->getDstAddress(); unsigned char dstAddress = message->getDstAddress();
if (dstAddress == SYN) if (dstAddress == SYN)
continue; continue;
if (pollPriority > 0 && message->setPollPriority(pollPriority))
m_messages->addPollMessage(message);
time_t lastup = message->getLastUpdateTime();
if (since > 0 && lastup <= since)
continue;
if (lastup > maxLastUp) if (lastup > maxLastUp)
maxLastUp = lastup; maxLastUp = lastup;
if (message->getCircuit() != lastCircuit) { if (message->getCircuit() != lastCircuit) {
@@ -1017,6 +1039,8 @@ string MainLoop::executeGet(vector<string> &args, bool& connected)
result << "404 Not Found"; result << "404 Not Found";
break; break;
case RESULT_ERR_INVALID_ARG: case RESULT_ERR_INVALID_ARG:
case RESULT_ERR_INVALID_NUM:
case RESULT_ERR_OUT_OF_RANGE:
result << "400 Bad Request"; result << "400 Bad Request";
break; break;
default: default: