aboutsummaryrefslogtreecommitdiff
path: root/apps/sm-serial-read.cpp
diff options
context:
space:
mode:
authorTrygve Laugstøl <trygvis@inamo.no>2015-03-18 23:02:25 +0100
committerTrygve Laugstøl <trygvis@inamo.no>2015-03-18 23:02:25 +0100
commit17de795175d0573d8ac6c2517d0eb35e8a4f8f69 (patch)
treeb8702d89a2e4e63a4f979ca4d73ccdf0cbb61bb3 /apps/sm-serial-read.cpp
parent225ed55918377ba2b53bb535cb636bfae8fc1ab4 (diff)
downloadble-toys-17de795175d0573d8ac6c2517d0eb35e8a4f8f69.tar.gz
ble-toys-17de795175d0573d8ac6c2517d0eb35e8a4f8f69.tar.bz2
ble-toys-17de795175d0573d8ac6c2517d0eb35e8a4f8f69.tar.xz
ble-toys-17de795175d0573d8ac6c2517d0eb35e8a4f8f69.zip
o Replacing custom Format with common sample_format_type.
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);