diff options
author | Trygve Laugstøl <trygvis@inamo.no> | 2015-03-22 18:12:48 +0100 |
---|---|---|
committer | Trygve Laugstøl <trygvis@inamo.no> | 2015-03-22 18:12:48 +0100 |
commit | 6bac290b92b635be047237b880144dbc163df6ec (patch) | |
tree | ab2a00f15d1c698ae37966c494000219a0a74e2f /sensor/main | |
parent | d7d545575250f616f0b9e2243e08544ab2794a03 (diff) | |
download | ble-toys-6bac290b92b635be047237b880144dbc163df6ec.tar.gz ble-toys-6bac290b92b635be047237b880144dbc163df6ec.tar.bz2 ble-toys-6bac290b92b635be047237b880144dbc163df6ec.tar.xz ble-toys-6bac290b92b635be047237b880144dbc163df6ec.zip |
o Splitting out io parts into trygvis::sensor::io.
Diffstat (limited to 'sensor/main')
-rw-r--r-- | sensor/main/io.cpp (renamed from sensor/main/SensorSample.cpp) | 94 | ||||
-rw-r--r-- | sensor/main/sensor.cpp | 79 |
2 files changed, 108 insertions, 65 deletions
diff --git a/sensor/main/SensorSample.cpp b/sensor/main/io.cpp index 5f0e9c6..18040f1 100644 --- a/sensor/main/SensorSample.cpp +++ b/sensor/main/io.cpp @@ -1,16 +1,29 @@ -#include "trygvis/SensorSample.h" +#include "trygvis/sensor/io.h" +#include <ostream> +#include <vector> +#include <map> +#include <mutex> #include "json.hpp" -#include <set> -#include <boost/regex.hpp> -#include <chrono> +#include "boost/regex.hpp" namespace trygvis { namespace sensor { +namespace io { using namespace std; using json = nlohmann::json; +ThreadSafeSampleOutputStream::ThreadSafeSampleOutputStream(unique_ptr<SampleOutputStream> underlying) + : underlying(move(underlying)) { +} + +void ThreadSafeSampleOutputStream::write(SampleRecord const &sample) { + std::unique_lock<std::mutex> lock(mutex); + + underlying->write(sample); +} + void VectorSampleOutputStream::write(SampleRecord const &sample) { if (sample.empty()) { return; @@ -24,12 +37,12 @@ CsvSampleOutputStream::CsvSampleOutputStream(shared_ptr<ostream> stream, KeyDict } void CsvSampleOutputStream::write(SampleRecord const &sample) { - // Skip empty records +// Skip empty records if (sample.empty()) { return; } - // Build the dict with the keys from the first sample. +// Build the dict with the keys from the first sample. if (dict.empty()) { SampleKeyIndex index = 0; auto ptr = sample.cbegin(); @@ -93,7 +106,7 @@ JsonSampleOutputStream::JsonSampleOutputStream(shared_ptr<ostream> stream, KeyDi } void JsonSampleOutputStream::write(SampleRecord const &sample) { - // Skip empty records +// Skip empty records if (sample.empty()) { return; } @@ -115,7 +128,7 @@ void JsonSampleOutputStream::write(SampleRecord const &sample) { auto o = sample.at(sampleKey); if (o) { - // Make sure that the key is registered in the dictionary +// Make sure that the key is registered in the dictionary dict.indexOf(sampleKey->name); doc[sampleKey->name] = o.get(); } @@ -130,7 +143,7 @@ KeyValueSampleOutputStream::KeyValueSampleOutputStream(shared_ptr<ostream> strea } void KeyValueSampleOutputStream::write(SampleRecord const &sample) { - // Skip empty records +// Skip empty records if (sample.empty()) { return; } @@ -163,7 +176,7 @@ void KeyValueSampleOutputStream::write(SampleRecord const &sample) { } else { s << ", "; } - // Make sure that the key is registered in the dictionary +// Make sure that the key is registered in the dictionary dict.indexOf(sampleKey->name); s << sampleKey->name << "=" << o.get(); } @@ -173,7 +186,8 @@ void KeyValueSampleOutputStream::write(SampleRecord const &sample) { *stream.get() << endl; } -RrdSampleOutputStream::RrdSampleOutputStream(shared_ptr<ostream> stream, KeyDictionary &dict, const SampleKey* timestamp_key, o<output_fields *> output_fields) : +RrdSampleOutputStream::RrdSampleOutputStream(shared_ptr<ostream> stream, KeyDictionary &dict, const SampleKey *timestamp_key, o<output_fields *> output_fields) + : stream(move(stream)), timestamp_key(timestamp_key) { if (output_fields) { for (auto field : output_fields.get()->fields) { @@ -187,7 +201,7 @@ RrdSampleOutputStream::RrdSampleOutputStream(shared_ptr<ostream> stream, KeyDict } void RrdSampleOutputStream::write(SampleRecord const &sample) { - // Skip empty records +// Skip empty records if (sample.empty()) { return; } @@ -311,7 +325,7 @@ void KeyValueSampleStreamParser::process_line(shared_ptr<vector<uint8_t>> packet auto start = s.cbegin(); auto end = s.cend(); - boost::match_results<std::string::const_iterator> what; + boost::match_results <std::string::const_iterator> what; boost::match_flag_type flags = boost::match_default; SampleRecord sample(dict); @@ -346,48 +360,6 @@ void AutoSampleParser::process(mutable_buffers_1 buffer) { } } -string to_string(const sample_format_type &arg) { - if (arg == sample_format_type::AUTO) - return "auto"; - else if (arg == sample_format_type::CSV) - 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 if (arg == sample_format_type::RRD) - return "rrd"; - else - return "unknown"; -} - -std::ostream& operator<<(std::ostream& os, sample_format_type const& type) { - return os << to_string(type); -} - -std::istream& operator>>(std::istream& is, sample_format_type& type) { - string s; - is >> s; - - if (s == "auto") { - type = sample_format_type::AUTO; - } else if (s == "csv") { - type = sample_format_type::CSV; - } else if (s == "key-value") { - type = sample_format_type::KEY_VALUE; - } else if (s == "json") { - type = sample_format_type::JSON; - } else if (s == "sql") { - type = sample_format_type::SQL; - } else if (s == "rrd") { - type = sample_format_type::RRD; - } - - return is; -} - unique_ptr<SampleStreamParser> open_sample_input_stream( shared_ptr<SampleOutputStream> output, KeyDictionary &dict, @@ -403,7 +375,7 @@ unique_ptr<SampleStreamParser> open_sample_input_stream( template<typename T> o<T *> find_option(vector<sample_output_stream_option *> &options) { - for (sample_output_stream_option *& option : options) { + for (sample_output_stream_option *&option : options) { T *x = dynamic_cast<T *>(option); if (x != nullptr) { @@ -441,14 +413,6 @@ unique_ptr<SampleOutputStream> open_sample_output_stream( } } -ThreadSafeSampleOutputStream::ThreadSafeSampleOutputStream(unique_ptr<SampleOutputStream> underlying) : underlying(move(underlying)) { -} - -void ThreadSafeSampleOutputStream::write(SampleRecord const &sample) { - std::unique_lock<std::mutex> lock(mutex); - - underlying->write(sample); -} - } } +}
\ No newline at end of file diff --git a/sensor/main/sensor.cpp b/sensor/main/sensor.cpp new file mode 100644 index 0000000..a773e0b --- /dev/null +++ b/sensor/main/sensor.cpp @@ -0,0 +1,79 @@ +#include "trygvis/sensor.h" + +#include "json.hpp" +#include <set> +#include <boost/regex.hpp> +#include <boost/lexical_cast.hpp> +#include <chrono> + +namespace trygvis { +namespace sensor { + +using namespace std; + +string to_string(const sample_format_type &arg) { + if (arg == sample_format_type::AUTO) + return "auto"; + else if (arg == sample_format_type::CSV) + 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 if (arg == sample_format_type::RRD) + return "rrd"; + else + return "unknown"; +} + +std::ostream& operator<<(std::ostream& os, sample_format_type const& type) { + return os << to_string(type); +} + +std::istream& operator>>(std::istream& is, sample_format_type& type) { + string s; + is >> s; + + if (s == "auto") { + type = sample_format_type::AUTO; + } else if (s == "csv") { + type = sample_format_type::CSV; + } else if (s == "key-value") { + type = sample_format_type::KEY_VALUE; + } else if (s == "json") { + type = sample_format_type::JSON; + } else if (s == "sql") { + type = sample_format_type::SQL; + } else if (s == "rrd") { + type = sample_format_type::RRD; + } + + return is; +} + +template<> +const o<long> SampleRecord::lexical_at(const SampleKey *key) const { + auto value = at(key); + + if (!value) { + return o<long>(); + } + + return o<long>(boost::lexical_cast<long>(value.get())); +} +// +//template<class A> +//const o<A> SampleRecord::lexical_at(const SampleKey *key) const { +// auto value = at(key); +// +// if (!value) { +// return o<A>(); +// } +// +// return o<A>(boost::lexical_cast<A>(value.get())); +//} + +} +} |