aboutsummaryrefslogtreecommitdiff
path: root/apps/sample-add-timestamp.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'apps/sample-add-timestamp.cpp')
-rw-r--r--apps/sample-add-timestamp.cpp101
1 files changed, 101 insertions, 0 deletions
diff --git a/apps/sample-add-timestamp.cpp b/apps/sample-add-timestamp.cpp
new file mode 100644
index 0000000..dbf24a1
--- /dev/null
+++ b/apps/sample-add-timestamp.cpp
@@ -0,0 +1,101 @@
+#include "trygvis/sensor.h"
+#include "trygvis/sensor/io.h"
+#include "apps.h"
+#include <vector>
+
+namespace trygvis {
+namespace apps {
+
+using namespace std;
+using namespace trygvis::apps;
+using namespace trygvis::sensor;
+using namespace trygvis::sensor::io;
+namespace po = boost::program_options;
+
+class TimestampAddingSampleOutputStream : public SampleOutputStream {
+
+public:
+ TimestampAddingSampleOutputStream(shared_ptr<SampleOutputStream> output, KeyDictionary &dict, string timestamp_name)
+ :
+ timestamp_key(dict.indexOf(timestamp_name)) {
+ if (input_time_resolution_ == time_resolution::MILLISECONDS) {
+ factor = 1000;
+ } else {
+ factor = 1;
+ }
+ }
+
+ virtual void write(SampleRecord const &sample) override {
+ time_t now = time(NULL) * factor;
+
+ SampleRecord updated_sample(sample);
+
+ updated_sample.set(timestamp_key, std::to_string(now));
+
+ output_->write(updated_sample);
+ };
+
+private:
+ const SampleKey *timestamp_key;
+ time_resolution input_time_resolution_;
+ int factor;
+ shared_ptr<SampleOutputStream> output_;
+};
+
+class sample_add_timestamp : public app {
+
+private:
+ string timestamp_name;
+ time_resolution resolution;
+ sample_format_type output_format;
+
+public:
+ void add_options(po::options_description_easy_init &options) override {
+ options
+ ("help", "produce this help message")
+ ("resolution", po::value<time_resolution>(&resolution)->default_value(time_resolution::SECONDS))
+ ("timestamp-name", po::value<string>(&timestamp_name)->default_value("timestamp"))
+ ("output-format", po::value<sample_format_type>(&output_format)->default_value(sample_format_type::KEY_VALUE));
+ }
+
+ const int buffer_size = 1024;
+
+ int main(app_execution &execution) override {
+ shared_ptr<istream> input;
+
+ input = shared_ptr<istream>(&cin, noop_deleter);
+
+ KeyDictionary dict;
+
+ sample_output_stream_options options = {};
+ unique_ptr<SampleOutputStream> unique_output_stream = open_sample_output_stream(shared_ptr<ostream>(&cout, noop_deleter), dict, output_format, options);
+ shared_ptr<SampleOutputStream> output_stream{std::move(unique_output_stream)};
+ shared_ptr<SampleOutputStream> p = make_shared<TimestampAddingSampleOutputStream>(output_stream, dict, timestamp_name);
+ auto parser = open_sample_stream_parser(p, dict);
+
+ int recordCount = 0;
+
+ while (!input->eof()) {
+ char buffer[buffer_size];
+ input->read(buffer, buffer_size);
+ size_t gcount = (size_t)input->gcount();
+
+ recordCount++;
+
+ mutable_buffers_1 b = boost::asio::buffer(buffer, gcount);
+ parser->process(b);
+ }
+
+ return EXIT_SUCCESS;
+ };
+};
+
+}
+}
+
+using namespace trygvis::apps;
+
+int main(int argc, char *argv[]) {
+ sample_add_timestamp app;
+ return launch_app(argc, argv, app);
+}