diff options
Diffstat (limited to 'apps/SoilMoistureIo.cpp')
-rw-r--r-- | apps/SoilMoistureIo.cpp | 140 |
1 files changed, 88 insertions, 52 deletions
diff --git a/apps/SoilMoistureIo.cpp b/apps/SoilMoistureIo.cpp index 98a256b..24618ff 100644 --- a/apps/SoilMoistureIo.cpp +++ b/apps/SoilMoistureIo.cpp @@ -11,94 +11,140 @@ namespace soil_moisture { using namespace std; using json = nlohmann::json; +CsvSampleOutputStream::CsvSampleOutputStream(ostream &stream) : + stream(stream), filterFields(false), headerWritten(false) { +} + CsvSampleOutputStream::CsvSampleOutputStream(ostream &stream, vector<string> fields) : - stream(stream), fields(fields) { + stream(stream), fields(fields), filterFields(true), headerWritten(false) { +} - auto i = fields.begin(); - while (true) { - stream << *i; +void CsvSampleOutputStream::write(Sample values) { + if (!headerWritten) { + writeHeader(); + headerWritten = true; + } - i++; + if (filterFields) { + auto i = fields.begin(); + while (i != fields.end()) { + if (i != fields.begin()) { + stream << ","; + } - if (i != fields.end()) { - stream << ","; - } else { - break; + auto field = *i++; + auto value = values.find(field); + + if (value != values.end()) { + stream << value->second; + } + } + } else { + for (auto i = values.begin(); i != values.end();) { + stream << "\"" << (*i).second << "\""; + + if (++i != values.end()) { + stream << ","; + } } } stream << endl; } -void CsvSampleOutputStream::write(Sample values) { - auto i = fields.begin(); - while (true) { - auto field = *i; - auto value = values.find(field); +void CsvSampleOutputStream::writeHeader() { + if (fields.size() == 0) { + return; + } - if (value != values.end()) { - stream << value->second; - } + auto i = fields.begin(); + while (i != fields.end()) { + stream << *i; i++; if (i != fields.end()) { stream << ","; - } else { - break; } } stream << endl; } +JsonSampleOutputStream::JsonSampleOutputStream(ostream &stream) : + stream(stream), fields(), filterFields(false) { +} + JsonSampleOutputStream::JsonSampleOutputStream(ostream &stream, vector<string> fields) : - stream(stream), fields(fields) { + stream(stream), fields(fields), filterFields(true) { } void JsonSampleOutputStream::write(Sample values) { json doc({}); - for (auto &f: fields) { - auto value = values.find(f); + if (filterFields) { + for (auto &f: fields) { + auto value = values.find(f); - if (value != values.end()) { - doc[f] = value->second; + if (value != values.end()) { + doc[f] = value->second; + } + } + } else { + for (auto &v: values) { + doc[v.first] = v.second; } } stream << doc << endl; } +SqlSampleOutputStream::SqlSampleOutputStream(ostream &stream) : + stream(stream), filterFields(false) { +} + SqlSampleOutputStream::SqlSampleOutputStream(ostream &stream, vector<string> fields) : - stream(stream), fields(fields) { + stream(stream), fields(fields), filterFields(true) { } void SqlSampleOutputStream::write(Sample values) { - auto i = fields.begin(); - stringstream fs, vs; - while (true) { - auto field = *i; + if (filterFields) { + auto i = fields.begin(); - fs << field; + while (i != fields.end()) { + auto field = *i; - auto value = values.find(field); + fs << field; - if (value != values.end()) { - vs << "'" << value->second << "'"; - } else { - vs << "NULL"; - } + auto value = values.find(field); - i++; + if (value != values.end()) { + vs << "'" << value->second << "'"; + } else { + vs << "NULL"; + } - if (i != fields.end()) { - fs << ","; - vs << ","; - } else { - break; + i++; + + if (i != fields.end()) { + fs << ","; + vs << ","; + } + } + } else { + auto i = values.begin(); + while (i != values.end()) { + auto v = *i++; + + fs << v.first; + vs << "'" << v.second << "'"; + + if (i != values.end()) { + fs << ","; + vs << ","; + } } } @@ -126,7 +172,6 @@ void CsvParser::process(mutable_buffers_1 buffer) { void CsvParser::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()); -// cerr << "packet: " << s << ", size=" << packet->size() << endl; static const boost::regex e("([_a-zA-Z0-9]+)=([0-9]+)"); @@ -142,17 +187,8 @@ void CsvParser::process_line(shared_ptr<vector<uint8_t>> packet) { auto value = static_cast<string>(what[2]); start = what[0].second; -// static const string device_type = "serial"; map<string, string> values; values[key] = value; -// values["hostname"] = hostname; -// values["device"] = device; -// values["device_type"] = device_type; -// values["timestamp"] = to_string(timestamp); -// values["sensor"] = sensor; -// values["value"] = value; - -// cerr << key << " => " << value << endl; sample[key] = value; |