aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEvgeny Zinoviev <me@ch1p.io>2021-05-16 16:36:48 +0300
committerEvgeny Zinoviev <me@ch1p.io>2021-05-16 16:36:48 +0300
commite8ceb3b5d429c0b58d60c68cc0d00582eb0fd25a (patch)
treecf55a84d2978c35fb0c28334114c898c04265346
parenta9a55d0d2da510f6094e321af5e374895d2c0250 (diff)
add 'simple-json' format
-rw-r--r--CMakeLists.txt2
-rw-r--r--src/common.cc2
-rw-r--r--src/formatter/formatter.h35
-rw-r--r--src/inverterctl.cc6
-rw-r--r--src/inverterd.cc15
-rw-r--r--src/p18/response.h8
6 files changed, 49 insertions, 19 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 1ec6b51..e80f861 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -4,7 +4,7 @@ cmake_minimum_required(VERSION 3.0)
set(CMAKE_CXX_STANDARD 17)
add_compile_options(-Wno-psabi)
-project(inverter-tools VERSION 1.0.2)
+project(inverter-tools VERSION 1.0.3)
if(CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT)
set(CMAKE_INSTALL_PREFIX /usr/local/bin)
diff --git a/src/common.cc b/src/common.cc
index fbd1614..fb2acea 100644
--- a/src/common.cc
+++ b/src/common.cc
@@ -6,6 +6,8 @@
formatter::Format format_from_string(std::string& s) {
if (s == "json")
return formatter::Format::JSON;
+ else if (s == "simple-json")
+ return formatter::Format::SimpleJSON;
else if (s == "table")
return formatter::Format::Table;
else if (s == "simple-table")
diff --git a/src/formatter/formatter.h b/src/formatter/formatter.h
index 5dfa184..2ffa9b2 100644
--- a/src/formatter/formatter.h
+++ b/src/formatter/formatter.h
@@ -38,6 +38,7 @@ enum class Format {
Table,
SimpleTable,
JSON,
+ SimpleJSON,
};
std::ostream& operator<<(std::ostream& os, Unit val);
@@ -92,6 +93,7 @@ public:
virtual ~Formattable() = default;
virtual std::ostream& writeJSON(std::ostream& os) const = 0;
+ virtual std::ostream& writeSimpleJSON(std::ostream& os) const = 0;
virtual std::ostream& writeTable(std::ostream& os) const = 0;
virtual std::ostream& writeSimpleTable(std::ostream& os) const = 0;
@@ -105,6 +107,9 @@ public:
case Format::JSON:
return ref.writeJSON(os);
+
+ case Format::SimpleJSON:
+ return ref.writeSimpleJSON(os);
}
return os;
@@ -112,7 +117,7 @@ public:
};
-// T must have `operator<<` and `basic_json toJSON()` methods
+// T must have `operator<<`, `json toJSON()` and `json toSimpleJSON()` methods
template <typename T>
class Table : public Formattable {
protected:
@@ -184,6 +189,17 @@ public:
}
return os << j.dump();
}
+
+ std::ostream& writeSimpleJSON(std::ostream& os) const override {
+ ordered_json j = {
+ {"result", "ok"},
+ {"data", {}}
+ };
+ for (const auto &item: v_) {
+ j["data"][item.key] = item.value.toSimpleJSON();
+ }
+ return os << j.dump();
+ }
};
template <typename T>
@@ -220,6 +236,19 @@ public:
return os << j.dump();
}
+
+ std::ostream& writeSimpleJSON(std::ostream& os) const override {
+ json data = {};
+ ordered_json j;
+
+ j["result"] = "ok";
+
+ for (const auto &item: v_)
+ data.push_back(item.value.toSimpleJSON());
+ j["data"] = data;
+
+ return os << j.dump();
+ }
};
class Status : public Formattable {
@@ -250,6 +279,10 @@ public:
j["message"] = message_;
return os << j.dump();
}
+
+ std::ostream& writeSimpleJSON(std::ostream& os) const override {
+ return writeJSON(os);
+ }
};
}
diff --git a/src/inverterctl.cc b/src/inverterctl.cc
index 882c425..f071f2e 100644
--- a/src/inverterctl.cc
+++ b/src/inverterctl.cc
@@ -45,7 +45,8 @@ static void short_usage(const char* progname) {
" --device <DEVICE>: 'usb' (default), 'serial' or 'pseudo'\n"
" --timeout <TIMEOUT>: Timeout in ms (default: " << voltronic::Device::TIMEOUT << ")\n"
" --verbose: Be verbose\n"
- " --format <FORMAT>: 'table' (default), 'simple-table' or 'json'\n"
+ " --format <FORMAT>: 'table' (default), 'simple-table', 'json' or\n"
+ " 'simple-json'\n"
"\n"
"To see list of supported commands, use --help.\n";
exit(1);
@@ -206,7 +207,8 @@ static void usage(const char* progname) {
"Formats:\n"
" table Human-readable table\n"
" simple-table Conveniently-parsable table\n"
- " json JSON object or array\n";
+ " json JSON object or array\n"
+ " simple-json no units, enumerations represented as numbers\n";
exit(1);
}
diff --git a/src/inverterd.cc b/src/inverterd.cc
index 1357454..b8dfb4b 100644
--- a/src/inverterd.cc
+++ b/src/inverterd.cc
@@ -243,21 +243,6 @@ int main(int argc, char *argv[]) {
}
dev->setTimeout(timeout);
-
-// p18::Client client;
-// client.setDevice(dev);
-
- /*if (action == Action::Raw) {
- auto result = client.runOnDevice(raw);
- if (verbose)
- std::cerr << hexdump(result.first.get(), result.second);
- std::cout << std::string(result.first.get(), result.second) << std::endl;
- } else {
- auto response = client.execute(commandType, arguments);
- std::cout << *(response->format(format).get()) << std::endl;
- }*/
-
-// success = true;
}
catch (voltronic::DeviceError& e) {
myerr << "device error: " << e.what();
diff --git a/src/p18/response.h b/src/p18/response.h
index 7c40bdb..e10b72f 100644
--- a/src/p18/response.h
+++ b/src/p18/response.h
@@ -107,6 +107,14 @@ public:
return j;
}
+
+ inline json toSimpleJSON() const {
+ json j;
+ std::visit([&j](const auto& elem) {
+ j = elem;
+ }, v_);
+ return j;
+ }
};