add 'simple-json' format
This commit is contained in:
parent
a9a55d0d2d
commit
e8ceb3b5d4
@ -4,7 +4,7 @@ cmake_minimum_required(VERSION 3.0)
|
|||||||
set(CMAKE_CXX_STANDARD 17)
|
set(CMAKE_CXX_STANDARD 17)
|
||||||
add_compile_options(-Wno-psabi)
|
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)
|
if(CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT)
|
||||||
set(CMAKE_INSTALL_PREFIX /usr/local/bin)
|
set(CMAKE_INSTALL_PREFIX /usr/local/bin)
|
||||||
|
@ -6,6 +6,8 @@
|
|||||||
formatter::Format format_from_string(std::string& s) {
|
formatter::Format format_from_string(std::string& s) {
|
||||||
if (s == "json")
|
if (s == "json")
|
||||||
return formatter::Format::JSON;
|
return formatter::Format::JSON;
|
||||||
|
else if (s == "simple-json")
|
||||||
|
return formatter::Format::SimpleJSON;
|
||||||
else if (s == "table")
|
else if (s == "table")
|
||||||
return formatter::Format::Table;
|
return formatter::Format::Table;
|
||||||
else if (s == "simple-table")
|
else if (s == "simple-table")
|
||||||
|
@ -38,6 +38,7 @@ enum class Format {
|
|||||||
Table,
|
Table,
|
||||||
SimpleTable,
|
SimpleTable,
|
||||||
JSON,
|
JSON,
|
||||||
|
SimpleJSON,
|
||||||
};
|
};
|
||||||
std::ostream& operator<<(std::ostream& os, Unit val);
|
std::ostream& operator<<(std::ostream& os, Unit val);
|
||||||
|
|
||||||
@ -92,6 +93,7 @@ public:
|
|||||||
virtual ~Formattable() = default;
|
virtual ~Formattable() = default;
|
||||||
|
|
||||||
virtual std::ostream& writeJSON(std::ostream& os) const = 0;
|
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& writeTable(std::ostream& os) const = 0;
|
||||||
virtual std::ostream& writeSimpleTable(std::ostream& os) const = 0;
|
virtual std::ostream& writeSimpleTable(std::ostream& os) const = 0;
|
||||||
|
|
||||||
@ -105,6 +107,9 @@ public:
|
|||||||
|
|
||||||
case Format::JSON:
|
case Format::JSON:
|
||||||
return ref.writeJSON(os);
|
return ref.writeJSON(os);
|
||||||
|
|
||||||
|
case Format::SimpleJSON:
|
||||||
|
return ref.writeSimpleJSON(os);
|
||||||
}
|
}
|
||||||
|
|
||||||
return 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>
|
template <typename T>
|
||||||
class Table : public Formattable {
|
class Table : public Formattable {
|
||||||
protected:
|
protected:
|
||||||
@ -184,6 +189,17 @@ public:
|
|||||||
}
|
}
|
||||||
return os << j.dump();
|
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>
|
template <typename T>
|
||||||
@ -220,6 +236,19 @@ public:
|
|||||||
|
|
||||||
return os << j.dump();
|
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 {
|
class Status : public Formattable {
|
||||||
@ -250,6 +279,10 @@ public:
|
|||||||
j["message"] = message_;
|
j["message"] = message_;
|
||||||
return os << j.dump();
|
return os << j.dump();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::ostream& writeSimpleJSON(std::ostream& os) const override {
|
||||||
|
return writeJSON(os);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -45,7 +45,8 @@ static void short_usage(const char* progname) {
|
|||||||
" --device <DEVICE>: 'usb' (default), 'serial' or 'pseudo'\n"
|
" --device <DEVICE>: 'usb' (default), 'serial' or 'pseudo'\n"
|
||||||
" --timeout <TIMEOUT>: Timeout in ms (default: " << voltronic::Device::TIMEOUT << ")\n"
|
" --timeout <TIMEOUT>: Timeout in ms (default: " << voltronic::Device::TIMEOUT << ")\n"
|
||||||
" --verbose: Be verbose\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"
|
"\n"
|
||||||
"To see list of supported commands, use --help.\n";
|
"To see list of supported commands, use --help.\n";
|
||||||
exit(1);
|
exit(1);
|
||||||
@ -206,7 +207,8 @@ static void usage(const char* progname) {
|
|||||||
"Formats:\n"
|
"Formats:\n"
|
||||||
" table Human-readable table\n"
|
" table Human-readable table\n"
|
||||||
" simple-table Conveniently-parsable 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);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
@ -243,21 +243,6 @@ int main(int argc, char *argv[]) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
dev->setTimeout(timeout);
|
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) {
|
catch (voltronic::DeviceError& e) {
|
||||||
myerr << "device error: " << e.what();
|
myerr << "device error: " << e.what();
|
||||||
|
@ -107,6 +107,14 @@ public:
|
|||||||
|
|
||||||
return j;
|
return j;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
inline json toSimpleJSON() const {
|
||||||
|
json j;
|
||||||
|
std::visit([&j](const auto& elem) {
|
||||||
|
j = elem;
|
||||||
|
}, v_);
|
||||||
|
return j;
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user