From db45a68715c07e6b8fe35e322ddcbbc6c473cb2b Mon Sep 17 00:00:00 2001 From: Romain Forlot Date: Mon, 25 Sep 2017 18:22:49 +0200 Subject: No errors on get if no values has been recorded Change-Id: Ib1d5da084b3a39fbfc9816070aba008e3486cfae Signed-off-by: Romain Forlot --- .../signal-composer-binding.cpp | 7 +-- signal-composer-binding/signal-composer.cpp | 55 ++++++++++++++-------- signal-composer-binding/signal.cpp | 3 +- 3 files changed, 41 insertions(+), 24 deletions(-) (limited to 'signal-composer-binding') diff --git a/signal-composer-binding/signal-composer-binding.cpp b/signal-composer-binding/signal-composer-binding.cpp index cc3b9a8..1e14aad 100644 --- a/signal-composer-binding/signal-composer-binding.cpp +++ b/signal-composer-binding/signal-composer-binding.cpp @@ -186,12 +186,13 @@ void get(struct afb_req request) return; } - ans = Composer::instance().getSignalValue(sig, options); + ans = Composer::instance().getsignalValue(sig, options); - if (ans) + if (json_object_array_length(ans)) afb_req_success(request, ans, NULL); else - afb_req_fail(request, "error", NULL); + afb_req_fail(request, "error", "No signals found."); + } int loadConf() diff --git a/signal-composer-binding/signal-composer.cpp b/signal-composer-binding/signal-composer.cpp index db35e76..b0c7d6a 100644 --- a/signal-composer-binding/signal-composer.cpp +++ b/signal-composer-binding/signal-composer.cpp @@ -337,42 +337,52 @@ void Composer::processOptions(const char** opts, Signal* sig, json_object* respo { avg = true; double value = sig->average(); - json_object_object_add(response, "average", + json_object_object_add(response, "value", json_object_new_double(value)); } - if (strcasestr(opts[idx], "min") && !min) + else if (strcasestr(opts[idx], "min") && !min) { min = true; double value = sig->minimum(); - json_object_object_add(response, "min", + json_object_object_add(response, "value", json_object_new_double(value)); } - if (strcasestr(opts[idx], "max") && !max) + else if (strcasestr(opts[idx], "max") && !max) { max = true; double value = sig->maximum(); - json_object_object_add(response, "max", + json_object_object_add(response, "value", json_object_new_double(value)); } - if (strcasestr(opts[idx], "last") && !last) + else if (strcasestr(opts[idx], "last") && !last) { last = true; - struct SignalValue value = sig->last(); + struct signalValue value = sig->last(); if(value.hasBool) { - json_object_object_add(response, "last", + json_object_object_add(response, "value", json_object_new_boolean(value.boolVal)); } - if(value.hasNum) + else if(value.hasNum) { - json_object_object_add(response, "last", + json_object_object_add(response, "value", json_object_new_double(value.numVal)); } - if(value.hasStr) + else if(value.hasStr) { - json_object_object_add(response, "last", + json_object_object_add(response, "value", json_object_new_string(value.strVal.c_str())); } + else + { + json_object_object_add(response, "value", + json_object_new_string("No recorded value so far.")); + } + } + else + { + json_object_object_add(response, "value", + json_object_new_string("No recorded value so far.")); } } } @@ -496,7 +506,7 @@ std::vector Composer::searchSignals(const std::string& aName) return signals; } -json_object* Composer::getSignalValue(const std::string& sig, json_object* options) +json_object* Composer::getsignalValue(const std::string& sig, json_object* options) { const char **opts = nullptr; json_object *response = nullptr, *finalResponse = json_object_new_array(); @@ -516,22 +526,27 @@ json_object* Composer::getSignalValue(const std::string& sig, json_object* optio "signal", sig->id().c_str()); if (!opts) { - struct SignalValue value = sig->last(); + struct signalValue value = sig->last(); if(value.hasBool) { - json_object_object_add(response, "last", + json_object_object_add(response, "value", json_object_new_boolean(value.boolVal)); } - if(value.hasNum) + else if(value.hasNum) { - json_object_object_add(response, "last", + json_object_object_add(response, "value", json_object_new_double(value.numVal)); } - if(value.hasStr) + else if(value.hasStr) { - json_object_object_add(response, "last", + json_object_object_add(response, "value", json_object_new_string(value.strVal.c_str())); } + else + { + json_object_object_add(response, "value", + json_object_new_string("No recorded value so far.")); + } } else {processOptions(opts, sig, response);} @@ -539,7 +554,7 @@ json_object* Composer::getSignalValue(const std::string& sig, json_object* optio } } - return response; + return finalResponse; } int Composer::execSignalsSubscription() diff --git a/signal-composer-binding/signal.cpp b/signal-composer-binding/signal.cpp index a74889d..faae4a3 100644 --- a/signal-composer-binding/signal.cpp +++ b/signal-composer-binding/signal.cpp @@ -288,8 +288,9 @@ double Signal::maximum(int seconds) const /// @brief Return last value recorded /// /// @return Last value -struct SignalValue Signal::last() const +struct signalValue Signal::last() const { + if(history_.empty()) {return signalValue({0,0,0,0,0,""});} return history_.rbegin()->second; } -- cgit 1.2.3-korg