diff options
author | Romain Forlot <romain.forlot@iot.bzh> | 2018-07-31 11:22:14 +0200 |
---|---|---|
committer | Romain Forlot <romain.forlot@iot.bzh> | 2018-07-31 18:04:18 +0200 |
commit | cf49bd158d6b71313fbfa97bb108e4b812f2093e (patch) | |
tree | b2c414fa8127ee0bfce0972b7ea09c59718ffe35 | |
parent | 5de482b05d504ecdd8ba3aeed6ce3ec6f39535e7 (diff) |
Returns error in request response
Returns error in request response instead of binder log when
you try to get a signal value.
Change-Id: I12809591801db5718aa4713034f2bb1801b6bf2d
Signed-off-by: Romain Forlot <romain.forlot@iot.bzh>
-rw-r--r-- | signal-composer-binding/signal-composer-binding.cpp | 22 | ||||
-rw-r--r-- | signal-composer-binding/signal-composer.cpp | 68 | ||||
-rw-r--r-- | signal-composer-binding/signal.cpp | 76 | ||||
-rw-r--r-- | signal-composer-binding/signal.hpp | 6 |
4 files changed, 94 insertions, 78 deletions
diff --git a/signal-composer-binding/signal-composer-binding.cpp b/signal-composer-binding/signal-composer-binding.cpp index f7c8c73..0603656 100644 --- a/signal-composer-binding/signal-composer-binding.cpp +++ b/signal-composer-binding/signal-composer-binding.cpp @@ -236,9 +236,10 @@ void list(afb_req request) /// @brief entry point for get requests. void get(struct afb_req request) { - int err = 0; + int err = 0, i = 0; + size_t l = 0; struct json_object* args = afb_req_json(request), *ans = nullptr, - *options = nullptr; + *options = nullptr, *error = nullptr, *object = nullptr; const char* sig; // Process about Raw CAN message on CAN bus directly @@ -246,15 +247,24 @@ void get(struct afb_req request) "options", &options); if(err) { - AFB_ERROR("Can't process your request '%s'. Valid arguments are: string for 'signal' and JSON object for 'options' (optionnal)", json_object_to_json_string_ext(args, JSON_C_TO_STRING_PRETTY)); - afb_req_fail(request, "error", NULL); + afb_req_fail(request, "error", "Valid arguments are:{'signal': 'wantedsignal'[, 'options': {['average': nb_seconds,] ['minimum': nb_seconds,] ['maximum': nb_seconds] }]"); return; } ans = Composer::instance().getsignalValue(sig, options); - if (json_object_array_length(ans)) - afb_req_success(request, ans, NULL); + l = json_object_array_length(ans); + if(l) { + while(i < l) { + object = json_object_array_get_idx(ans, i++); + if(json_object_object_get_ex(object, "error", &error)) + break; + } + if(error) + afb_req_fail(request, "error", json_object_get_string(error)); + else + afb_req_success(request, ans, NULL); + } else afb_req_fail(request, "error", "No signals found."); diff --git a/signal-composer-binding/signal-composer.cpp b/signal-composer-binding/signal-composer.cpp index f9ed9c5..7a111d5 100644 --- a/signal-composer-binding/signal-composer.cpp +++ b/signal-composer-binding/signal-composer.cpp @@ -372,38 +372,34 @@ int Composer::loadSignals(AFB_ApiT apihandle, CtlSectionT* section, json_object void Composer::processOptions(const std::map<std::string, int>& opts, std::shared_ptr<Signal> sig, json_object* response) const { - for(const auto& o: opts) + json_object *value = nullptr; + if (opts.at("average")) { - bool avg = false, min = false, max = false, last = false; - if (o.first.compare("average") && !avg) - { - avg = true; - double value = sig->average(o.second); - json_object_object_add(response, "value", json_object_new_double(value)); - } - else if (o.first.compare("minimum") && !min) - { - min = true; - double value = sig->minimum(); - json_object_object_add(response, "value", json_object_new_double(value)); - } - else if (o.first.compare("maximum") && !max) - { - max = true; - double value = sig->maximum(); - json_object_object_add(response, "value", json_object_new_double(value)); - } - else if (o.first.compare("last") && !last) - { - last = true; - json_object* value = sig->last_value(); - json_object_object_add(response, "value", value); - } - else - { - json_object_object_add(response, "value", - json_object_new_string("No recorded value so far.")); - } + value = sig->average(opts.at("average")); + json_object_is_type(value, json_type_double) ? + json_object_object_add(response, "average", value) : + json_object_object_add(response, "error", value); + } + if (opts.at("minimum")) + { + value = sig->minimum(opts.at("minimum")); + json_object_is_type(value, json_type_double) ? + json_object_object_add(response, "minimum", value) : + json_object_object_add(response, "error", value); + } + if (opts.at("maximum")) + { + value = sig->minimum(opts.at("maximum")); + json_object_is_type(value, json_type_double) ? + json_object_object_add(response, "maximum", value) : + json_object_object_add(response, "error", value); + } + if (opts.at("last")) + { + value = sig->minimum(opts.at("last")); + json_object_is_type(value, json_type_null) ? + json_object_object_add(response, "error", value) : + json_object_object_add(response, "last", value); } } @@ -566,11 +562,17 @@ json_object* Composer::getsignalValue(const std::string& sig, json_object* optio std::map<std::string, int> opts; json_object *response = nullptr, *finalResponse = json_object_new_array(); - wrap_json_unpack(options, "{s?i, s?i, s?i, s?i !}", + if(options && wrap_json_unpack(options, "{s?i, s?i, s?i, s?i !}", "average", &opts["average"], "minimum", &opts["minimum"], "maximum", &opts["maximum"], - "last", &opts["last"]); + "last", &opts["last"])) + { + response = json_object_new_object(); + json_object_object_add(response, "error", json_object_new_string("Invalid options specified. Use the following: 'options': {['average': nb_seconds,] ['minimum': nb_seconds,] ['maximum': nb_seconds] }")); + json_object_array_add(finalResponse, response); + return finalResponse; + } std::vector<std::shared_ptr<Signal>> sigP = searchSignals(sig); if(!sigP.empty()) diff --git a/signal-composer-binding/signal.cpp b/signal-composer-binding/signal.cpp index 14e85ba..b77ab59 100644 --- a/signal-composer-binding/signal.cpp +++ b/signal-composer-binding/signal.cpp @@ -18,6 +18,7 @@ #include <float.h> #include <string.h> #include <fnmatch.h> +#include <sstream> #include "signal.hpp" #include "signal-composer.hpp" @@ -358,8 +359,8 @@ void Signal::attachToSourceSignals(Composer& composer) /// /// @param[in] seconds - period to calculate the average /// -/// @return Average value -double Signal::average(int seconds) const +/// @return A json_object with the Average value or an error message +json_object * Signal::average(int seconds) const { uint64_t begin = history_.begin()->first; uint64_t end = seconds ? @@ -367,6 +368,13 @@ double Signal::average(int seconds) const history_.rbegin()->first; double total = 0.0; int nbElt = 0; + std::stringstream errorMsg; + + if(history_.empty() && seconds < 0) + { + errorMsg << "There are no historized values or you requested a negative time interval for that signal: " << id_; + return json_object_new_string(errorMsg.str().c_str()); + } for(const auto& val: history_) { @@ -383,29 +391,22 @@ double Signal::average(int seconds) const total += json_object_get_double(val.second); break; default: - AFB_ERROR("The stored value '%s' for the signal '%s' isn't numeric, it is not possible to compute an average value.", json_object_get_string(val.second), id_.c_str()); - break; + errorMsg << "The stored value '" << json_object_get_string(val.second) << "' for the signal '" << id_ << "'' isn't numeric, it is not possible to compute an average value."; + return json_object_new_string(errorMsg.str().c_str()); } nbElt++; } - else - { - AFB_ERROR("There isn't numerical value to compare with in that signal '%s'. Stored value: %s", - id_.c_str(), - json_object_get_string(val.second)); - break; - } } - return total / nbElt; + return json_object_new_double(total / nbElt); } /// @brief Find minimum in the recorded values /// /// @param[in] seconds - period to find the minimum /// -/// @return Minimum value contained in the history -double Signal::minimum(int seconds) const +/// @return A json_object with the Minimum value contained in the history or the error string +json_object *Signal::minimum(int seconds) const { uint64_t begin = history_.begin()->first; uint64_t end = seconds ? @@ -414,6 +415,14 @@ double Signal::minimum(int seconds) const double current = 0.0; double min = DBL_MAX; + std::stringstream errorMsg; + + if(history_.empty() && seconds < 0) + { + errorMsg << "There is no historized values or you requested a negative time interval for that signal: " << id_; + return json_object_new_string(errorMsg.str().c_str()); + } + for(const auto& val : history_) { if(val.first >= end) { @@ -430,27 +439,21 @@ double Signal::minimum(int seconds) const min = current < min ? current : min; break; default: - AFB_ERROR("The stored value '%s' for signal '%s' isn't numeric, it is not possible to find a minimum value.", json_object_get_string(val.second), id_.c_str()); - break; + errorMsg << "The stored value '" << json_object_get_string(val.second) << "' for the signal '" << id_ << "'' isn't numeric, it is not possible to find a minimum value."; + return json_object_new_string(errorMsg.str().c_str()); } } - else - { - AFB_ERROR("There isn't numerical value to compare with in that signal '%s'. Stored value: %s", - id_.c_str(), - json_object_get_string(val.second)); - break; - } } - return min; + + return json_object_new_double(min); } /// @brief Find maximum in the recorded values /// /// @param[in] seconds - period to find the maximum /// -/// @return Maximum value contained in the history -double Signal::maximum(int seconds) const +/// @return A json_object with Maximum value contained in the history or an error string +json_object * Signal::maximum(int seconds) const { uint64_t begin = history_.begin()->first; uint64_t end = seconds ? @@ -459,6 +462,13 @@ double Signal::maximum(int seconds) const double current = 0.0; double max = 0.0; + std::stringstream errorMsg; + + if(history_.empty() && seconds < 0) { + errorMsg << "There is no historized values or you requested a negative time interval for that signal: " << id_; + return json_object_new_string(errorMsg.str().c_str()); + } + for(const auto& val : history_) { if(val.first >= end) { @@ -475,19 +485,13 @@ double Signal::maximum(int seconds) const max = current > max ? current : max; break; default: - AFB_ERROR("The stored value '%s' for signal '%s' isn't numeric, it is not possible to find a maximum value.", json_object_get_string(val.second), id_.c_str()); - break; + errorMsg << "The stored value '" << json_object_get_string(val.second) << "' for the signal '" << id_ << "'' isn't numeric, it is not possible to find a maximum value."; + return json_object_new_string(errorMsg.str().c_str()); } } - else - { - AFB_ERROR("There isn't numerical value to compare with in that signal '%s'. Stored value: %s", - id_.c_str(), - json_object_get_string(val.second)); - break; - } } - return max; + + return json_object_new_double(max); } /// @brief Return last value recorded diff --git a/signal-composer-binding/signal.hpp b/signal-composer-binding/signal.hpp index 6c7f43f..0dec877 100644 --- a/signal-composer-binding/signal.hpp +++ b/signal-composer-binding/signal.hpp @@ -87,9 +87,9 @@ public: void onReceivedCB(json_object *eventJ); void attachToSourceSignals(Composer& composer); - double average(int seconds = 0) const; - double minimum(int seconds = 0) const; - double maximum(int seconds = 0) const; + json_object* average(int seconds = 0) const; + json_object* minimum(int seconds = 0) const; + json_object* maximum(int seconds = 0) const; json_object* last_value() const; uint64_t last_timestamp() const; |