#include "trygvis/sensor.h" #include "trygvis/sensor/io.h" #include "apps.h" #include 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 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 output_; }; class sample_add_timestamp : public app { private: string timestamp_name; time_resolution resolution; sample_format_type output_format; public: sample_add_timestamp() : app("sample-add-timestamp") { } ~sample_add_timestamp() = default; void add_options(po::options_description_easy_init &options) override { options ("resolution", po::value(&resolution)->default_value(time_resolution::SECONDS)) ("timestamp-name", po::value(×tamp_name)->default_value("timestamp")) ("output-format", po::value(&output_format)->default_value(sample_format_type::KEY_VALUE)); } const int buffer_size = 1024; int main(app_execution &execution) override { shared_ptr input; input = shared_ptr(&cin, noop_deleter); KeyDictionary dict; sample_output_stream_options options = {}; unique_ptr unique_output_stream = open_sample_output_stream(shared_ptr(&cout, noop_deleter), dict, output_format, options); shared_ptr output_stream{std::move(unique_output_stream)}; shared_ptr p = make_shared(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; }; }; } }