From 7e2fb834d8ba2eb3fd6453f135d3e8ef30c852c6 Mon Sep 17 00:00:00 2001 From: Trygve Laugstøl Date: Sun, 21 Jun 2015 00:53:28 +0200 Subject: o Using ends_with when checking for which app to launch too. sm-get-value: o Using the io code for writing samples --- apps/sm-get-value.h | 69 ++++++++++++++++++++++++++++++++++------------------- 1 file changed, 45 insertions(+), 24 deletions(-) (limited to 'apps/sm-get-value.h') 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 targetTime; unsigned int sleepTime; vector 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(&cout, noop_deleter), dict, format); + shared_ptr 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(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()->required(), "MAC of device to poll") ("sensor", po::value>(&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; } -- cgit v1.2.3