diff options
-rw-r--r-- | apps/SoilMoistureIo.cpp | 12 | ||||
-rw-r--r-- | apps/SoilMoistureIo.h | 7 | ||||
-rw-r--r-- | apps/apps.cpp | 53 | ||||
-rw-r--r-- | apps/apps.h | 3 | ||||
-rw-r--r-- | apps/sample-convert.cpp | 57 | ||||
-rw-r--r-- | apps/sm-serial-read.cpp | 15 |
6 files changed, 84 insertions, 63 deletions
diff --git a/apps/SoilMoistureIo.cpp b/apps/SoilMoistureIo.cpp index 983427c..d184eab 100644 --- a/apps/SoilMoistureIo.cpp +++ b/apps/SoilMoistureIo.cpp @@ -99,12 +99,12 @@ void JsonSampleOutputStream::write(Sample values) { stream << doc << endl; } -SqlSampleOutputStream::SqlSampleOutputStream(ostream &stream) : - stream(stream), filterFields(false) { +SqlSampleOutputStream::SqlSampleOutputStream(ostream &stream, string table_name) : + stream(stream), table_name(table_name), filter_fields(false) { } -SqlSampleOutputStream::SqlSampleOutputStream(ostream &stream, vector<string> fields) : - stream(stream), fields(fields), filterFields(true) { +SqlSampleOutputStream::SqlSampleOutputStream(ostream &stream, string table_name, vector<string> fields) : + stream(stream), table_name(table_name), fields(fields), filter_fields(true) { } void SqlSampleOutputStream::write(Sample values) { @@ -113,7 +113,7 @@ void SqlSampleOutputStream::write(Sample values) { fs.reserve(1024); vs.reserve(1024); - if (filterFields) { + if (filter_fields) { auto i = fields.begin(); while (i != fields.end()) { @@ -151,7 +151,7 @@ void SqlSampleOutputStream::write(Sample values) { } } - stream << "INSERT INTO (" << fs << ") VALUES(" << vs << ");" << endl; + stream << "INSERT INTO " << table_name << "(" << fs << ") VALUES(" << vs << ");" << endl; } void CsvParser::process(mutable_buffers_1 buffer) { diff --git a/apps/SoilMoistureIo.h b/apps/SoilMoistureIo.h index 4edf4f4..9a144e3 100644 --- a/apps/SoilMoistureIo.h +++ b/apps/SoilMoistureIo.h @@ -80,16 +80,17 @@ private: class SqlSampleOutputStream : public SampleOutputStream { public: - SqlSampleOutputStream(ostream &stream); + SqlSampleOutputStream(ostream &stream, string table_name); - SqlSampleOutputStream(ostream &stream, vector<string> fields); + SqlSampleOutputStream(ostream &stream, string table_name, vector<string> fields); void write(Sample values); private: ostream &stream; - bool filterFields; + bool filter_fields; vector<string> fields; + const string table_name; }; class CsvParser { diff --git a/apps/apps.cpp b/apps/apps.cpp index ca7cd81..edfb556 100644 --- a/apps/apps.cpp +++ b/apps/apps.cpp @@ -22,42 +22,47 @@ void setup_logging(po::variables_map vm) { } int launch_app(int argc, char *argv[], app& app) { - po::options_description desc("Options"); + po::options_description all("Options"); - auto x = desc.add_options(); - app.add_options(x); + auto all_options = all.add_options(); + app.add_options(all_options); - desc.add(logging_options()); + all.add(logging_options()); + app.add_extra_options(all); po::variables_map vm; - auto parsed = po::parse_command_line(argc, argv, desc); - po::store(parsed, vm); - - app_execution execution(desc, vm); try { + auto parsed = po::parse_command_line(argc, argv, all); + po::store(parsed, vm); + po::notify(vm); - } catch (boost::program_options::required_option &e) { - cerr << "Missing required option: " << e.get_option_name() << endl; - execution.usage(); - return EXIT_FAILURE; - } - auto unrecognized = po::collect_unrecognized(parsed.options, po::include_positional); + auto unrecognized = po::collect_unrecognized(parsed.options, po::include_positional); - if (vm.count("help")) { - cerr << desc << "\n"; - return EXIT_FAILURE; - } + if (vm.count("help")) { + cerr << all << "\n"; + return EXIT_FAILURE; + } - if (unrecognized.size()) { - cerr << "Unrecognized option: " << unrecognized.at(0) << "\n"; - return EXIT_FAILURE; - } + if (unrecognized.size()) { + cerr << "Unrecognized option: " << unrecognized.at(0) << "\n"; + return EXIT_FAILURE; + } - setup_logging(vm); + setup_logging(vm); - return app.main(execution); + app_execution execution(all, vm); + + return app.main(execution); + } catch (po::required_option &e) { + cerr << "Missing required option: " << e.get_option_name() << endl; + cerr << all << endl; + return EXIT_FAILURE; + } catch (po::unknown_option &e) { + cerr << e.what() << endl; + return EXIT_FAILURE; + } } diff --git a/apps/apps.h b/apps/apps.h index b50699a..3d82745 100644 --- a/apps/apps.h +++ b/apps/apps.h @@ -25,6 +25,9 @@ public: virtual void add_options(po::options_description_easy_init& options) { }; + virtual void add_extra_options(po::options_description& options) { + }; + virtual int main(app_execution &execution) = 0; }; diff --git a/apps/sample-convert.cpp b/apps/sample-convert.cpp index 23a04ff..fb74fcf 100644 --- a/apps/sample-convert.cpp +++ b/apps/sample-convert.cpp @@ -11,21 +11,30 @@ using namespace trygvis::apps; using namespace trygvis::soil_moisture; namespace po = boost::program_options; -string inputFile, inputFormat; -string outputFile, outputFormat; - class sample_convert : public app { public: + sample_convert() : table_name(""),input_file(""), input_format(""), + output_file(""), output_format("") { + } + void add_options(po::options_description_easy_init &options) override { options ("help", "produce this help message") - ("input", po::value<string>(&inputFile)->default_value("-")) - ("input-format", po::value<string>(&inputFormat)->default_value("csv")) - ("output", po::value<string>(&outputFile)->default_value("-")) - ("output-format", po::value<string>(&outputFormat)->default_value("plain")); + ("input", po::value<string>(&input_file)->default_value("-")) + ("input-format", po::value<string>(&input_format)->default_value("csv")) + ("output", po::value<string>(&output_file)->default_value("-")) + ("output-format", po::value<string>(&output_format)->default_value("plain")); } + void add_extra_options(po::options_description &all_options) override { + po::options_description sql("SQL"); + sql.add_options() + ("table-name", po::value<string>(&table_name)); + + all_options.add(sql); + }; + int main(app_execution &execution) override { auto desc = execution.desc; auto vm = execution.vm; @@ -33,35 +42,40 @@ public: shared_ptr<SampleOutputStream> output; istream *inputStream; - if (inputFile == "-") { + if (input_file == "-") { inputStream = &cin; } else { - inputStream = new ifstream(inputFile); + inputStream = new ifstream(input_file); if (inputStream->fail()) { - cerr << "Unable to open input file " << inputFile << endl; + cerr << "Unable to open input file " << input_file << endl; return EXIT_FAILURE; } } ostream *outputStream; - if (outputFile == "-") { + if (output_file == "-") { outputStream = &cout; } else { - outputStream = new ofstream(outputFile); + outputStream = new ofstream(output_file); if (outputStream->fail()) { - cerr << "Unable to open output file " << outputFile << endl; + cerr << "Unable to open output file " << output_file << endl; return EXIT_FAILURE; } } - if (outputFormat == "plain") { + if (output_format == "plain") { output = make_shared<CsvSampleOutputStream>(*outputStream); - } else if (outputFormat == "json") { + } else if (output_format == "json") { output = make_shared<JsonSampleOutputStream>(*outputStream); - } else if (outputFormat == "sql") { - output = make_shared<SqlSampleOutputStream>(*outputStream); + } else if (output_format == "sql") { + if (table_name.size() == 0) { + cerr << "Missing option: table-name" << endl; + return EXIT_FAILURE; + } + + output = make_shared<SqlSampleOutputStream>(*outputStream, table_name); } else { - cerr << "Unsupported output format: " << outputFormat << endl; + cerr << "Unsupported output format: " << output_format << endl; return EXIT_FAILURE; } @@ -73,9 +87,16 @@ public: input->process(boost::asio::buffer(data, 1)); } + delete outputStream; + return EXIT_SUCCESS; } +private: + string input_file, input_format; + string output_file, output_format; + + string table_name; }; } diff --git a/apps/sm-serial-read.cpp b/apps/sm-serial-read.cpp index 955d262..a203847 100644 --- a/apps/sm-serial-read.cpp +++ b/apps/sm-serial-read.cpp @@ -120,23 +120,14 @@ public: cerr << "port is not open" << endl; } - auto field_names = vector<string>({ - "hostname", - "device_type", - "device", - "timestamp", - "sensor", - "value" - }); - shared_ptr<SampleOutputStream> output; if (format == Format::JSON) { - output = make_shared<JsonSampleOutputStream>(cout, field_names); + output = make_shared<JsonSampleOutputStream>(cout); } else if (format == Format::SQL) { - output = make_shared<SqlSampleOutputStream>(cout, field_names); + output = make_shared<SqlSampleOutputStream>(cout, "raw"); } else if (format == Format::PLAIN) { - output = make_shared<CsvSampleOutputStream>(cout, field_names); + output = make_shared<CsvSampleOutputStream>(cout); } else { cerr << "Unsupported format: " << boost::lexical_cast<string>(format) << endl; return EXIT_FAILURE; |