#include "trygvis/sensor.h" #include "trygvis/sensor/io.h" #include "json.hpp" #include "apps.h" #include #include namespace trygvis { namespace apps { using namespace std; using namespace trygvis::apps; using namespace trygvis::sensor; using namespace trygvis::sensor::io; using boost::tokenizer; namespace po = boost::program_options; class sample_convert : public app { string fields; string timestamp_field; public: void add_options(po::options_description_easy_init &options) override { options ("help", "produce this help message") ("input", po::value(&input_file)->default_value("-")) // ("input-format", po::value(&input_format)->default_value("csv")) ("output", po::value(&output_file)->default_value("-")) ("output-format", po::value(&output_format)->default_value(sample_format_type::KEY_VALUE)) ("fields", po::value(&fields)) ("timestamp-field", po::value(×tamp_field)); } void add_extra_options(po::options_description &all_options) override { po::options_description sql("SQL"); sql.add_options() ("table-name", po::value(&table_name)); all_options.add(sql); }; int main(app_execution &execution) override { auto desc = execution.desc; auto vm = execution.vm; KeyDictionary dict; istream *inputStream; if (input_file == "-") { inputStream = &cin; } else { inputStream = new ifstream(input_file); if (inputStream->fail()) { cerr << "Unable to open input file " << input_file << endl; return EXIT_FAILURE; } } shared_ptr outputStream; if (output_file == "-") { outputStream = shared_ptr(&cout, noop_deleter); } else { outputStream = make_shared(output_file); if (outputStream->fail()) { cerr << "Unable to open output file " << output_file << endl; return EXIT_FAILURE; } } std::vector options; trygvis::sensor::io::timestamp_field_option tf(timestamp_field); if (!timestamp_field.empty()) { options.push_back(&tf); } tokenizer<> tok(fields); output_fields_option fs; std::copy(tok.begin(), tok.end(), std::back_inserter(fs.fields)); if (!fs.fields.empty()) { options.push_back(&fs); } shared_ptr output = open_sample_output_stream(outputStream, dict, output_format, options); auto input = make_shared(output, dict); char data[100]; while (!inputStream->eof()) { inputStream->get(data[0]); input->process(boost::asio::buffer(data, 1)); } return EXIT_SUCCESS; } private: string input_file, output_file; sample_format_type output_format; string table_name; }; } } using namespace trygvis::apps; int main(int argc, char *argv[]) { sample_convert app; return launch_app(argc, argv, app); }