diff options
-rw-r--r-- | apps/SoilMoistureIo.cpp | 23 | ||||
-rw-r--r-- | apps/SoilMoistureIo.h | 8 | ||||
-rw-r--r-- | apps/sample-convert.cpp | 2 | ||||
-rw-r--r-- | apps/sm-serial-read.cpp | 6 | ||||
-rw-r--r-- | test/SoilMoistureIoTest.cpp | 8 |
5 files changed, 23 insertions, 24 deletions
diff --git a/apps/SoilMoistureIo.cpp b/apps/SoilMoistureIo.cpp index c9cfa1e..40f2f7a 100644 --- a/apps/SoilMoistureIo.cpp +++ b/apps/SoilMoistureIo.cpp @@ -174,7 +174,7 @@ void SqlSampleOutputStream::write(SampleRecord values) { // (*stream.get()) << "INSERT INTO " << table_name << "(" << fs << ") VALUES(" << vs << ");" << endl; } -void CsvSampleParser::process(mutable_buffers_1 buffer) { +void KeyValueSampleParser::process(mutable_buffers_1 buffer) { size_t size = buffer_size(buffer); @@ -199,14 +199,14 @@ void CsvSampleParser::process(mutable_buffers_1 buffer) { } -void CsvSampleParser::process_line(shared_ptr<vector<uint8_t>> packet) { +void KeyValueSampleParser::process_line(shared_ptr<vector<uint8_t>> packet) { auto timestamp = std::chrono::system_clock::now().time_since_epoch().count(); auto s = std::string((char *) packet->data(), packet->size()); static const boost::regex e("([_a-zA-Z0-9]+)=([0-9]+)"); - std::string::const_iterator start = s.begin(); - std::string::const_iterator end = s.end(); + auto start = s.cbegin(); + auto end = s.cend(); boost::match_results<std::string::const_iterator> what; boost::match_flag_type flags = boost::match_default; @@ -217,9 +217,6 @@ void CsvSampleParser::process_line(shared_ptr<vector<uint8_t>> packet) { auto value = static_cast<string>(what[2]); start = what[0].second; - - - auto key = dict.indexOf(name); sample.set(key, value); @@ -231,10 +228,10 @@ void CsvSampleParser::process_line(shared_ptr<vector<uint8_t>> packet) { } AutoSampleParser::AutoSampleParser(shared_ptr<SampleOutputStream> output, KeyDictionary &dict) : - SampleStreamParser(sample_format_type::AUTO), csvParser(new CsvSampleParser(output, dict)) { + SampleStreamParser(sample_format_type::AUTO), keyValueParser(new KeyValueSampleParser(output, dict)) { // Directly select the parser now until we have more than one parser - parser = std::move(csvParser); - type_ = sample_format_type::CSV; + parser = std::move(keyValueParser); + type_ = sample_format_type::KEY_VALUE; } void AutoSampleParser::process(mutable_buffers_1 buffer) { @@ -252,6 +249,8 @@ string to_string(const sample_format_type &arg) { return "csv"; else if (arg == sample_format_type::JSON) return "json"; + else if (arg == sample_format_type::KEY_VALUE) + return "key-value"; else if (arg == sample_format_type::SQL) return "sql"; else @@ -262,8 +261,8 @@ unique_ptr<SampleStreamParser> open_sample_input_stream( shared_ptr<SampleOutputStream> output, KeyDictionary &dict, sample_format_type type) { - if (type == sample_format_type::CSV) { - return make_unique<CsvSampleParser>(output, dict); + if (type == sample_format_type::KEY_VALUE) { + return make_unique<KeyValueSampleParser>(output, dict); } else if (type == sample_format_type::AUTO) { return make_unique<AutoSampleParser>(output, dict); } else { diff --git a/apps/SoilMoistureIo.h b/apps/SoilMoistureIo.h index 0d93d2a..5324114 100644 --- a/apps/SoilMoistureIo.h +++ b/apps/SoilMoistureIo.h @@ -24,6 +24,7 @@ using o = boost::optional<A>; enum class sample_format_type { AUTO, CSV, + KEY_VALUE, JSON, SQL }; @@ -296,11 +297,10 @@ protected: } }; -// TODO: rename to "KeyValueParser", this is not CSV. -class CsvSampleParser : public SampleStreamParser { +class KeyValueSampleParser : public SampleStreamParser { public: - CsvSampleParser(shared_ptr<SampleOutputStream> output, KeyDictionary &dict) : + KeyValueSampleParser(shared_ptr<SampleOutputStream> output, KeyDictionary &dict) : SampleStreamParser(sample_format_type::CSV), output(output), dict(dict), line(make_shared<vector<uint8_t>>()) { } @@ -322,7 +322,7 @@ public: private: unique_ptr<SampleStreamParser> parser; - unique_ptr<CsvSampleParser> csvParser; + unique_ptr<KeyValueSampleParser> keyValueParser; public: virtual void process(mutable_buffers_1 buffer); }; diff --git a/apps/sample-convert.cpp b/apps/sample-convert.cpp index c5e9c4f..79d6f1c 100644 --- a/apps/sample-convert.cpp +++ b/apps/sample-convert.cpp @@ -78,7 +78,7 @@ public: return EXIT_FAILURE; } - auto input = make_shared<CsvSampleParser>(output, dict); + auto input = make_shared<KeyValueSampleParser>(output, dict); char data[100]; while (!inputStream->eof()) { diff --git a/apps/sm-serial-read.cpp b/apps/sm-serial-read.cpp index 0d0bfe6..10981e0 100644 --- a/apps/sm-serial-read.cpp +++ b/apps/sm-serial-read.cpp @@ -59,7 +59,7 @@ string hostname = get_hostname(); class port_handler { public: - port_handler(string device, serial_port &serial_port, shared_ptr<CsvSampleParser> input) : + port_handler(string device, serial_port &serial_port, shared_ptr<KeyValueSampleParser> input) : device(device), port(serial_port), input(input) { } @@ -84,7 +84,7 @@ private: uint8_t data[size]; mutable_buffers_1 buffer = boost::asio::buffer(data, size); - shared_ptr<CsvSampleParser> input; + shared_ptr<KeyValueSampleParser> input; }; class sm_serial_read : public app { @@ -136,7 +136,7 @@ public: return EXIT_FAILURE; } - shared_ptr<CsvSampleParser> input = make_shared<CsvSampleParser>(output, dict); + shared_ptr<KeyValueSampleParser> input = make_shared<KeyValueSampleParser>(output, dict); port_handler(port_name, port, input).run(); diff --git a/test/SoilMoistureIoTest.cpp b/test/SoilMoistureIoTest.cpp index 4a508b9..3e62b05 100644 --- a/test/SoilMoistureIoTest.cpp +++ b/test/SoilMoistureIoTest.cpp @@ -6,12 +6,12 @@ using namespace trygvis::soil_moisture; -BOOST_AUTO_TEST_CASE(csv_parser) { +BOOST_AUTO_TEST_CASE(key_value_parser) { KeyDictionary dict; auto buffer = make_shared<VectorSampleOutputStream>(); - auto parser = new CsvSampleParser(buffer, dict); + auto parser = new KeyValueSampleParser(buffer, dict); char data[] = "a=1, b=2, c=3\n"; parser->process(boost::asio::buffer(data, sizeof(data))); @@ -26,7 +26,7 @@ BOOST_AUTO_TEST_CASE(csv_parser) { BOOST_CHECK_EQUAL((*it++)->index, 2); } -BOOST_AUTO_TEST_CASE(csv_parser_with_custom_dict) { +BOOST_AUTO_TEST_CASE(key_value_parser_with_custom_dict) { KeyDictionary dict; dict.indexOf("c"); @@ -34,7 +34,7 @@ BOOST_AUTO_TEST_CASE(csv_parser_with_custom_dict) { auto buffer = make_shared<VectorSampleOutputStream>(); - auto parser = new CsvSampleParser(buffer, dict); + auto parser = new KeyValueSampleParser(buffer, dict); char data[] = "a=1, b=2, c=3\n"; parser->process(boost::asio::buffer(data, sizeof(data))); |