From ae70341365e27d766c8530924209fc4826036aea Mon Sep 17 00:00:00 2001 From: "Matthias P. Braendli" Date: Fri, 31 Mar 2023 14:31:32 +0200 Subject: Add JSON output to RC --- lib/RemoteControl.cpp | 94 ++++++++++++++++++++++++++++++++++++++++++++++++--- lib/RemoteControl.h | 14 +++++--- 2 files changed, 99 insertions(+), 9 deletions(-) (limited to 'lib') diff --git a/lib/RemoteControl.cpp b/lib/RemoteControl.cpp index 30dcb60..6e9af20 100644 --- a/lib/RemoteControl.cpp +++ b/lib/RemoteControl.cpp @@ -25,6 +25,8 @@ #include #include #include +#include +#include #include #include @@ -102,6 +104,74 @@ std::list< std::vector > RemoteControllers::get_param_list_values(c return allparams; } + +static std::string escape_json(const std::string &s) { + std::ostringstream o; + for (auto c = s.cbegin(); c != s.cend(); c++) { + switch (*c) { + case '"': o << "\\\""; break; + case '\\': o << "\\\\"; break; + case '\b': o << "\\b"; break; + case '\f': o << "\\f"; break; + case '\n': o << "\\n"; break; + case '\r': o << "\\r"; break; + case '\t': o << "\\t"; break; + default: + if ('\x00' <= *c && *c <= '\x1f') { + o << "\\u" + << std::hex << std::setw(4) << std::setfill('0') << static_cast(*c); + } else { + o << *c; + } + } + } + return o.str(); +} + +std::string RemoteControllers::get_params_json(const std::string& name) { + RemoteControllable* controllable = get_controllable_(name); + const auto& values = controllable->get_all_values(); + + std::ostringstream ss; + ss << "{ "; + size_t ix = 0; + for (const auto& element : values) { + if (ix > 0) { + ss << ","; + } + + ss << "\"" << escape_json(element.first) << "\": "; + + const auto& value = element.second; + if (std::holds_alternative(value)) { + ss << "\"" << escape_json(std::get(value)) << "\""; + } + else if (std::holds_alternative(value)) { + ss << std::defaultfloat << std::get(value); + } + else if (std::holds_alternative(value)) { + ss << std::get(value); + } + else if (std::holds_alternative(value)) { + ss << std::get(value); + } + else if (std::holds_alternative(value)) { + ss << (std::get(value) ? "true" : "false"); + } + else if (std::holds_alternative(value)) { + ss << "null"; + } + else { + throw std::logic_error("variant alternative not handled"); + } + + ix++; + } + ss << " }"; + + return ss.str(); +} + std::string RemoteControllers::get_param(const std::string& name, const std::string& param) { RemoteControllable* controllable = get_controllable_(name); return controllable->get_parameter(param); @@ -427,10 +497,15 @@ void RemoteControllerZmq::recv_all(zmq::socket_t& pSocket, std::vector +#include +#include #include #include #include @@ -113,13 +115,16 @@ class RemoteControllable { } /* Base function to set parameters. */ - virtual void set_parameter( - const std::string& parameter, - const std::string& value) = 0; + virtual void set_parameter(const std::string& parameter, const std::string& value) = 0; /* Getting a parameter always returns a string. */ virtual const std::string get_parameter(const std::string& parameter) const = 0; + using value_t = std::variant; + using map_t = std::unordered_map; + + virtual const map_t get_all_values() const = 0; + protected: std::string m_rc_name; std::list< std::vector > m_parameters; @@ -135,6 +140,7 @@ class RemoteControllers { void check_faults(); std::list< std::vector > get_param_list_values(const std::string& name); std::string get_param(const std::string& name, const std::string& param); + std::string get_params_json(const std::string& name); void set_param( const std::string& name, -- cgit v1.2.3