diff options
-rw-r--r-- | apps/apps.cpp | 10 | ||||
-rw-r--r-- | apps/generate.cpp | 6 | ||||
-rw-r--r-- | apps/launcher.cpp | 14 | ||||
-rw-r--r-- | apps/sample-add-timestamp.h | 1 | ||||
-rw-r--r-- | apps/sample-convert.h | 1 | ||||
-rw-r--r-- | apps/sample-select.h | 1 | ||||
-rw-r--r-- | apps/sm-get-value.h | 69 | ||||
-rw-r--r-- | apps/sm-serial-read-all.h | 1 | ||||
-rw-r--r-- | apps/sm-serial-read.h | 1 |
9 files changed, 65 insertions, 39 deletions
diff --git a/apps/apps.cpp b/apps/apps.cpp index 783c0b0..e0bb631 100644 --- a/apps/apps.cpp +++ b/apps/apps.cpp @@ -9,6 +9,12 @@ using namespace std; namespace po = boost::program_options; std::string get_hostname() { + static string s = ""; + + if (!s.empty()) { + return s; + } + struct addrinfo hints, *info, *p; // int gai_result; @@ -25,10 +31,8 @@ std::string get_hostname() { return "uknown"; } - string s = "unknown"; - if (info) { - s = string(info->ai_canonname); + s = info->ai_canonname; } freeaddrinfo(info); diff --git a/apps/generate.cpp b/apps/generate.cpp index f0bf0fd..ca6f8de 100644 --- a/apps/generate.cpp +++ b/apps/generate.cpp @@ -18,7 +18,9 @@ int main(int argc, char *argv[]) { } out << "#pragma once" << endl + << endl << "#include <string>" << endl + << "#include <boost/algorithm/string/predicate.hpp>" << endl << endl; vector<pair<string, string>> apps; @@ -45,7 +47,7 @@ int main(int argc, char *argv[]) { bool first = true; out << "template<typename App>" << endl - << "int launch_app(int argc, const char *argv[]);" + << "int launch_app(int argc, const char *argv[]);" << endl << endl; out << "int launch(const std::string app_name, int argc, const char *argv[]) {" << endl; @@ -58,7 +60,7 @@ int main(int argc, char *argv[]) { first = false; } - out << "if (app_name == \"" << pair.first << "\") {" << endl + out << "if (boost::ends_with(app_name, \"" << pair.first << "\")) {" << endl << " return launch_app<" << pair.second << ">(argc, argv);" << endl; }); diff --git a/apps/launcher.cpp b/apps/launcher.cpp index dc31abd..4c1f687 100644 --- a/apps/launcher.cpp +++ b/apps/launcher.cpp @@ -13,9 +13,12 @@ const po::options_description logging_options() { return desc; } -void setup_logging(po::variables_map vm) { +void setup_logging(string app_name) { Appender *console = new ConsoleAppender(true, true); - PatternLayout *layout = new PatternLayout(LOG4CPLUS_TEXT("%-5p" /*" %6r"*/ " %-20c %m%n")); + + string pattern = string("%-5p ") /*"%6r "*/ + app_name + "/%-20c %m%n"; + + PatternLayout *layout = new PatternLayout(LOG4CPLUS_TEXT(pattern)); console->setLayout(auto_ptr<Layout>(layout)); Hierarchy &h = Logger::getDefaultHierarchy(); @@ -29,6 +32,7 @@ int launch_app(int argc, const char *argv[]) { po::options_description all("Options"); auto all_options = all.add_options(); + all_options("help", "This help message"); app.add_options(all_options); all.add(logging_options()); @@ -54,9 +58,9 @@ int launch_app(int argc, const char *argv[]) { return EXIT_FAILURE; } - setup_logging(vm); + setup_logging(app.app_name); - Logger logger = Logger::getInstance(LOG4CPLUS_TEXT(app.app_name)); + Logger logger = Logger::getInstance(LOG4CPLUS_TEXT("main")); app_execution execution(all, vm, logger); @@ -85,7 +89,7 @@ int main(int argc, const char *argv[]) { app_name = argv[1]; --argc; - argv = &argv[1]; + ++argv; } else { app_name = argv[0]; } diff --git a/apps/sample-add-timestamp.h b/apps/sample-add-timestamp.h index 13d2216..711cf49 100644 --- a/apps/sample-add-timestamp.h +++ b/apps/sample-add-timestamp.h @@ -57,7 +57,6 @@ 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>(×tamp_name)->default_value("timestamp")) ("output-format", po::value<sample_format_type>(&output_format)->default_value(sample_format_type::KEY_VALUE)); diff --git a/apps/sample-convert.h b/apps/sample-convert.h index 0b805be..6b0f368 100644 --- a/apps/sample-convert.h +++ b/apps/sample-convert.h @@ -33,7 +33,6 @@ public: void add_options(po::options_description_easy_init &options) override { options - ("help", "produce this help message") ("input", po::value<string>(&input_file)->default_value("-")) // ("input-format", po::value<string>(&input_format)->default_value("csv")) ("output", po::value<string>(&output_file)->default_value("-")) diff --git a/apps/sample-select.h b/apps/sample-select.h index 2701338..60bda32 100644 --- a/apps/sample-select.h +++ b/apps/sample-select.h @@ -24,7 +24,6 @@ public: void add_options(po::options_description_easy_init &options) override { options - ("help", "produce this help message") ("fields", po::value<string>(&fields)->required()); } diff --git a/apps/sm-get-value.h b/apps/sm-get-value.h index e0275c1..11e797f 100644 --- a/apps/sm-get-value.h +++ b/apps/sm-get-value.h @@ -15,6 +15,7 @@ using namespace std::chrono; using namespace trygvis::apps; using namespace trygvis::bluetooth; using namespace trygvis::sensor; +using namespace trygvis::sensor::io; using json = nlohmann::json; bool loop; @@ -23,7 +24,7 @@ time_point<system_clock> targetTime; unsigned int sleepTime; vector<unsigned int> sensors; -void withConnection(BluetoothGatt &gatt) { +void withConnection(sample_format_type format, BluetoothGatt &gatt) { SoilMoisture soilMoisture = SoilMoisture::create(gatt); const int sensorCount = soilMoisture.getSensorCount(); @@ -34,16 +35,26 @@ void withConnection(BluetoothGatt &gatt) { // If the user didn't specify any sensors, add all. if (sensors.size() == 0) { - for (int i = 0; i < sensorCount; i++) { + for (unsigned int i = 0; i < sensorCount; i++) { sensors.push_back(i); } } - auto device = gatt.getDevice().getMac(); + auto mac = gatt.getDevice().getMac(); targetTime = system_clock::now(); do { + KeyDictionary dict; + auto hostname_key = dict.indexOf("hostname"); + auto device_key = dict.indexOf("device"); + auto sensor_key = dict.indexOf("sensor"); + auto timestamp_key = dict.indexOf("timestamp"); + auto value_key = dict.indexOf("value"); + + auto unique_output_stream = open_sample_output_stream(shared_ptr<ostream>(&cout, noop_deleter), dict, format); + shared_ptr<SampleOutputStream> output_stream{std::move(unique_output_stream)}; + for (auto sensor : sensors) { if (sensor >= sensorCount) { // Ignore invalid sensors @@ -54,25 +65,35 @@ void withConnection(BluetoothGatt &gatt) { auto timestamp = duration_cast<seconds>(epoch).count(); uint16_t value = soilMoisture.getValue((uint8_t) sensor); - if (format == sample_format_type::KEY_VALUE) { - cout << "device=" << device.str() - << ", sensor=" << to_string(sensor) - << ", timestamp=" << to_string(timestamp) - << ", value=" << (int) value << endl; - } else if (format == sample_format_type::JSON) { - json j; - j["device"] = device.str(); - j["sensor"] = sensor; - j["timestamp"] = timestamp; - j["value"] = value; - cout << j << endl; - } else if (format == sample_format_type::SQL) { - cout << "INSERT INTO soil_moisture_sample(device, sensor, timestamp, value) VALUES(" - << "'" << device.str() << "', " - << sensor << ", " - << timestamp << ", " - << value << ";" << endl; - } + SampleRecord sample(dict); + + sample.set(hostname_key, get_hostname()); + sample.set(device_key, mac.str()); + sample.set(sensor_key, std::to_string(sensor)); + sample.set(timestamp_key, std::to_string(timestamp)); + sample.set(value_key, std::to_string(value)); + + output_stream->write(sample); + +// if (format == sample_format_type::KEY_VALUE) { +// cout << "device=" << device.str() +// << ", sensor=" << to_string(sensor) +// << ", timestamp=" << to_string(timestamp) +// << ", value=" << (int) value << endl; +// } else if (format == sample_format_type::JSON) { +// json j; +// j["device"] = device.str(); +// j["sensor"] = sensor; +// j["timestamp"] = timestamp; +// j["value"] = value; +// cout << j << endl; +// } else if (format == sample_format_type::SQL) { +// cout << "INSERT INTO soil_moisture_sample(device, sensor, timestamp, value) VALUES(" +// << "'" << device.str() << "', " +// << sensor << ", " +// << timestamp << ", " +// << value << ";" << endl; +// } } targetTime = targetTime + seconds(sleepTime); @@ -94,7 +115,7 @@ public: void add_options(po::options_description_easy_init &options) override { auto default_sleep = po::value<>(&sleepTime)->default_value(0); - options("help", "produce help message") + options ("device", po::value<string>()->required(), "MAC of device to poll") ("sensor", po::value<vector<unsigned int>>(&sensors)->multitoken(), "Sensor to poll, defaults to all") ("sleep", default_sleep, "How long to sleep in seconds between each poll. If not given, it will exit after first poll") @@ -131,7 +152,7 @@ public: auto &gatt = device.connectGatt(); try { - withConnection(gatt); + withConnection(format, gatt); } catch (runtime_error &e) { cout << "exception: " << e.what() << endl; } diff --git a/apps/sm-serial-read-all.h b/apps/sm-serial-read-all.h index 39b585a..14cdf1e 100644 --- a/apps/sm-serial-read-all.h +++ b/apps/sm-serial-read-all.h @@ -101,7 +101,6 @@ public: void add_options(po::options_description_easy_init &options) override { options - ("help", "produce help message") ("port", po::value<string>()->required(), "The serial port to read") ("format", po::value<sample_format_type>(&format)->default_value(sample_format_type::KEY_VALUE), "Output format"); } diff --git a/apps/sm-serial-read.h b/apps/sm-serial-read.h index a3e3872..7a6bcb4 100644 --- a/apps/sm-serial-read.h +++ b/apps/sm-serial-read.h @@ -65,7 +65,6 @@ public: void add_options(po::options_description_easy_init &options) override { options - ("help", "produce help message") ("port", po::value<string>()->required(), "The serial port to read") ("format", po::value<sample_format_type>(&format)->default_value(sample_format_type::KEY_VALUE), "Output format"); } |