diff options
author | Trygve Laugstøl <trygvis@inamo.no> | 2015-03-06 21:07:34 +0100 |
---|---|---|
committer | Trygve Laugstøl <trygvis@inamo.no> | 2015-03-06 21:07:34 +0100 |
commit | a83e6f5960a549d54991495336bd12d549127d91 (patch) | |
tree | d21f63b718d65074215fff6edbbcdbd5f26caf4a /apps/sample-convert.cpp | |
parent | e03bf5d416776cb5ea27c7354657920939c04e71 (diff) | |
download | ble-toys-a83e6f5960a549d54991495336bd12d549127d91.tar.gz ble-toys-a83e6f5960a549d54991495336bd12d549127d91.tar.bz2 ble-toys-a83e6f5960a549d54991495336bd12d549127d91.tar.xz ble-toys-a83e6f5960a549d54991495336bd12d549127d91.zip |
o Starting on a tool to convert between sample formats.
Diffstat (limited to 'apps/sample-convert.cpp')
-rw-r--r-- | apps/sample-convert.cpp | 98 |
1 files changed, 98 insertions, 0 deletions
diff --git a/apps/sample-convert.cpp b/apps/sample-convert.cpp new file mode 100644 index 0000000..2e448ca --- /dev/null +++ b/apps/sample-convert.cpp @@ -0,0 +1,98 @@ +#include "SoilMoistureIo.h" +#include "json.hpp" +#include "apps.h" + +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 { + +using namespace std; +using namespace trygvis::apps; +using namespace trygvis::soil_moisture; +namespace po = boost::program_options; + +Format inputFormat, outputFormat; + +class sample_convert : public app { + +public: +// void add_options(po::options_description_easy_init &options) override { +// options +// ("help", "produce help message") +// ("input-format", po::value<Format>(&inputFormat)->default_value(Format::PLAIN)) +// ("output-format", po::value<Format>(&outputFormat)->default_value(Format::PLAIN)) +// } + + int main(app_execution &execution) override { + auto desc = execution.desc; + auto vm = execution.vm; + + shared_ptr<SampleOutputStream> sampleStream; + + auto field_names = vector<string>({ + "hostname", + "device_type", + "device", + "timestamp", + "sensor", + "value" + }); + + sampleStream = make_shared<JsonSampleOutputStream>(cout, field_names); + + map<string, string> values; + values["hostname"] = "my-hostname"; + values["extra"] = "wat"; + + sampleStream->write(values); + + return EXIT_SUCCESS; + } + +}; + +} +} + +using namespace trygvis::apps; +int main(int argc, char *argv[]) { + sample_convert app; + return launch_app(argc, argv, app); +} |