aboutsummaryrefslogtreecommitdiff
path: root/apps/SoilMoistureIo.h
diff options
context:
space:
mode:
Diffstat (limited to 'apps/SoilMoistureIo.h')
-rw-r--r--apps/SoilMoistureIo.h113
1 files changed, 99 insertions, 14 deletions
diff --git a/apps/SoilMoistureIo.h b/apps/SoilMoistureIo.h
index 9a144e3..b8f08e9 100644
--- a/apps/SoilMoistureIo.h
+++ b/apps/SoilMoistureIo.h
@@ -4,8 +4,11 @@
#include <ostream>
#include <vector>
#include <map>
+#include <map>
#include <memory>
#include <boost/asio/buffer.hpp>
+#include <boost/optional.hpp>
+#include <boost/lexical_cast.hpp>
#include <functional>
namespace trygvis {
@@ -14,6 +17,27 @@ namespace soil_moisture {
using namespace std;
using namespace boost::asio;
+template<typename A>
+using o = boost::optional<A>;
+
+enum class sample_format_type {
+ AUTO,
+ CSV,
+ JSON,
+ SQL
+};
+
+string to_string(const sample_format_type &arg);
+
+class SampleStreamParser;
+
+class SampleOutputStream;
+
+unique_ptr<SampleStreamParser> open_sample_input_stream(shared_ptr<SampleOutputStream> output, sample_format_type type = sample_format_type::AUTO);
+
+unique_ptr<SampleOutputStream> open_sample_output_stream(sample_format_type type, unique_ptr<ostream> output,
+ o<vector<string>> fields = o<vector<string>>());
+
class Sample {
public:
Sample() : entries() {
@@ -34,31 +58,65 @@ public:
return entries.end();
}
- string &operator[](string key) {
- return entries[key];
+ /**
+ * @throws std::out_of_range
+ */
+ inline const string &operator[](string key) {
+ return at(key);
+ }
+
+ /**
+ * @throws std::out_of_range
+ */
+ const string &at(string key) {
+ return entries.at(key);
+ }
+
+ template<class A>
+ const A lexical_at(string key) {
+ return boost::lexical_cast<A>(entries.at(key));
+ }
+
+ void set(const std::string &key, const std::string &value) {
+ entries[key] = value;
}
private:
map<string, string> entries;
};
+class sample_exception : public runtime_error {
+public:
+ sample_exception(const string &what) : runtime_error(what) {
+ }
+};
+
class SampleOutputStream {
public:
virtual void write(Sample sample) = 0;
};
+class VectorSampleOutputStream : public SampleOutputStream {
+
+public:
+ virtual void write(Sample sample) override;
+
+public:
+ vector<Sample> samples;
+};
+
class CsvSampleOutputStream : public SampleOutputStream {
public:
- CsvSampleOutputStream(ostream &stream);
+ CsvSampleOutputStream(unique_ptr<ostream> stream);
- CsvSampleOutputStream(ostream &stream, vector<string> fields);
+ CsvSampleOutputStream(unique_ptr<ostream> stream, vector<string> fields);
void write(Sample values);
private:
void writeHeader();
- ostream &stream;
+ unique_ptr<ostream> stream;
bool headerWritten;
bool filterFields;
vector<string> fields;
@@ -66,40 +124,56 @@ private:
class JsonSampleOutputStream : public SampleOutputStream {
public:
- JsonSampleOutputStream(ostream &stream);
+ JsonSampleOutputStream(unique_ptr<ostream> stream);
- JsonSampleOutputStream(ostream &stream, vector<string> fields);
+ JsonSampleOutputStream(unique_ptr<ostream> stream, vector<string> fields);
void write(Sample values);
private:
- ostream &stream;
+ unique_ptr<ostream> stream;
bool filterFields;
vector<string> fields;
};
class SqlSampleOutputStream : public SampleOutputStream {
public:
- SqlSampleOutputStream(ostream &stream, string table_name);
+ SqlSampleOutputStream(unique_ptr<ostream> stream, string table_name);
- SqlSampleOutputStream(ostream &stream, string table_name, vector<string> fields);
+ SqlSampleOutputStream(unique_ptr<ostream> stream, string table_name, vector<string> fields);
void write(Sample values);
private:
- ostream &stream;
+ unique_ptr<ostream> stream;
bool filter_fields;
vector<string> fields;
const string table_name;
};
-class CsvParser {
+class SampleStreamParser {
+public:
+ virtual void process(mutable_buffers_1 buffer) = 0;
+
+ virtual sample_format_type type() {
+ return type_;
+ }
+
+protected:
+ sample_format_type type_;
+
+ SampleStreamParser(const sample_format_type type) : type_(type) {
+ }
+};
+
+class CsvSampleParser : public SampleStreamParser {
public:
- CsvParser(shared_ptr<SampleOutputStream> output) : output(output), line(make_shared<vector<uint8_t>>()) {
+ CsvSampleParser(shared_ptr<SampleOutputStream> output) : SampleStreamParser(sample_format_type::CSV),
+ output(output), line(make_shared<vector<uint8_t>>()) {
}
- void process(mutable_buffers_1 buffer);
+ void process(mutable_buffers_1 buffer) override;
private:
void process_line(shared_ptr<vector<uint8_t>> packet);
@@ -109,6 +183,17 @@ private:
shared_ptr<vector<uint8_t>> line;
};
+class AutoSampleParser : public SampleStreamParser {
+public:
+ AutoSampleParser(shared_ptr<SampleOutputStream> output);
+
+private:
+ unique_ptr<SampleStreamParser> parser;
+ unique_ptr<CsvSampleParser> csvParser;
+public:
+ virtual void process(mutable_buffers_1 buffer);
+};
+
}
}