From fae340f0be19c470c1c4632c5da686d5f48bc974 Mon Sep 17 00:00:00 2001 From: john30 Date: Sun, 10 May 2015 11:20:52 +0200 Subject: [PATCH] added global status and lastup to JSON response, added parameter support for JSON --- src/ebusd/mainloop.cpp | 42 ++++++++++++++++++++++++++++++++---------- 1 file changed, 32 insertions(+), 10 deletions(-) diff --git a/src/ebusd/mainloop.cpp b/src/ebusd/mainloop.cpp index 60be2e7d..3e49b970 100644 --- a/src/ebusd/mainloop.cpp +++ b/src/ebusd/mainloop.cpp @@ -769,7 +769,7 @@ string MainLoop::executeGet(vector &args, bool& connected) { result_t ret = RESULT_OK; size_t argPos = 1; - string uri = args[argPos]; + string uri = args[argPos++]; ostringstream result; if (strncmp(uri.c_str(), "/data/", 6) == 0) { @@ -781,21 +781,41 @@ string MainLoop::executeGet(vector &args, bool& connected) clazz = uri.substr(6, pos-6); name = uri.substr(pos+1); } - bool onlyWithData = false; - + time_t since = 0; + if (args.size() > argPos) { + string query = args[argPos++]; + istringstream stream(query); + string token; + while (getline(stream, token, '&') != 0) { + pos = token.find('='); + if (pos != string::npos) { + const char* qname = query.substr(0, pos).c_str(); + if (strcmp(qname, "since") == 0) { + since = parseInt(query.substr(pos+1).c_str(), 10, 0, 0xffffffff, ret); + if (ret != RESULT_OK) { + ret = RESULT_ERR_INVALID_ARG; + break; + } + } + } + } + } deque messages = m_messages->findAll(clazz, name, -1, false, true, false, true); bool first = true; result << "{"; string lastCircuit = ""; - for (deque::iterator it = messages.begin(); it < messages.end();) { + time_t maxLastUp = 0; + for (deque::iterator it = messages.begin(); ret == RESULT_OK && it < messages.end();) { Message* message = *it++; time_t lastup = message->getLastUpdateTime(); - if (onlyWithData && lastup == 0) + if (since > 0 && lastup <= since) continue; unsigned char dstAddress = message->getDstAddress(); if (dstAddress == SYN) continue; + if (lastup > maxLastUp) + maxLastUp = lastup; if (message->getCircuit() != lastCircuit) { if (lastCircuit.length() > 0) result << "\n },"; @@ -813,19 +833,21 @@ string MainLoop::executeGet(vector &args, bool& connected) result << ",\n \"zz\": \"" << setfill('0') << setw(2) << hex << static_cast(dstAddress) << "\""; result << ",\n \"fields\": ["; ret = message->decodeLastData(result, false, df_json); - if (ret < RESULT_OK) - break; result << "\n ]"; } result << ",\n \"passive\": " << (message->isPassive() ? "true" : "false"); result << ",\n \"write\": " << (message->isWrite() ? "true" : "false"); result << "\n }"; } - if (lastCircuit.length() > 0) - result << "\n }"; - result << "\n}"; if (ret == RESULT_OK) { + if (lastCircuit.length() > 0) + result << "\n },"; + result << "\n \"global\": {"; + result << "\n \"signal\": " << (m_busHandler->hasSignal() ? "1" : "0"); + result << ",\n \"lastup\": " << setw(0) << dec << static_cast(maxLastUp); + result << "\n }"; + result << "\n}"; string str = result.str(); result.str(""); result.clear();