aboutsummaryrefslogtreecommitdiff
path: root/apps/sm-serial-read.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'apps/sm-serial-read.cpp')
-rw-r--r--apps/sm-serial-read.cpp55
1 files changed, 3 insertions, 52 deletions
diff --git a/apps/sm-serial-read.cpp b/apps/sm-serial-read.cpp
index 10981e0..aa997e5 100644
--- a/apps/sm-serial-read.cpp
+++ b/apps/sm-serial-read.cpp
@@ -5,44 +5,6 @@
#include <thread>
#include <boost/asio/serial_port.hpp>
-enum class Format {
- PLAIN,
- JSON,
- SQL
-};
-
-void validate(boost::any &v, const std::vector<std::string> &values, Format *, int) {
- using namespace boost::program_options;
-
- const std::string &s = validators::get_single_string(values);
-
- if (s == "plain") {
- v = boost::any(Format::PLAIN);
- } else if (s == "json") {
- v = boost::any(Format::JSON);
- } else if (s == "sql") {
- v = boost::any(Format::SQL);
- } else {
- throw validation_error(validation_error::invalid_option_value);
- }
-}
-
-namespace boost {
-
-template<>
-std::string lexical_cast(const Format &arg) {
- if (arg == Format::PLAIN)
- return "plain";
- else if (arg == Format::JSON)
- return "json";
- else if (arg == Format::SQL)
- return "sql";
- else
- throw std::runtime_error("Unknown format value: " + lexical_cast<std::string>(arg));
-}
-
-}
-
namespace trygvis {
namespace apps {
@@ -54,7 +16,7 @@ using namespace trygvis::soil_moisture;
namespace po = boost::program_options;
using json = nlohmann::json;
-Format format;
+sample_format_type format;
string hostname = get_hostname();
class port_handler {
@@ -94,7 +56,7 @@ public:
options
("help", "produce help message")
("port", po::value<string>()->required(), "The serial port to read")
- ("format", po::value<Format>(&format)->default_value(Format::PLAIN), "Output format");
+ ("format", po::value<sample_format_type>(&format)->default_value(sample_format_type::KEY_VALUE), "Output format");
}
int main(app_execution &execution) override {
@@ -122,19 +84,8 @@ public:
cerr << "port is not open" << endl;
}
- shared_ptr<SampleOutputStream> output;
shared_ptr<ostream> outputStream = shared_ptr<ostream>(&cout, noop_deleter);
-
- if (format == Format::JSON) {
- output = make_shared<JsonSampleOutputStream>(outputStream, dict);
- } else if (format == Format::SQL) {
- output = make_shared<SqlSampleOutputStream>(outputStream, dict, "raw");
- } else if (format == Format::PLAIN) {
- output = make_shared<CsvSampleOutputStream>(outputStream, dict);
- } else {
- cerr << "Unsupported format: " << boost::lexical_cast<string>(format) << endl;
- return EXIT_FAILURE;
- }
+ shared_ptr<SampleOutputStream> output = open_sample_output_stream(outputStream, dict, format);
shared_ptr<KeyValueSampleParser> input = make_shared<KeyValueSampleParser>(output, dict);