From 47b736139f242a8fd2e5c6513b0c2e0b31110d77 Mon Sep 17 00:00:00 2001 From: Trygve Laugstøl Date: Sun, 21 Jun 2015 13:26:55 +0200 Subject: SoilMoisture: o Adding getName(). sm-get-value: o More cleanup. --- apps/SoilMoisture.cpp | 12 +++++ apps/SoilMoisture.h | 2 + apps/sm-get-value.h | 100 +++++++++++++++++++++++----------------- ble/LinuxBluetooth.cpp | 4 ++ include/ble/Bluetooth.h | 5 ++ sensor/include/trygvis/sensor.h | 3 ++ 6 files changed, 84 insertions(+), 42 deletions(-) diff --git a/apps/SoilMoisture.cpp b/apps/SoilMoisture.cpp index d1d8b7c..5edba0e 100644 --- a/apps/SoilMoisture.cpp +++ b/apps/SoilMoisture.cpp @@ -116,5 +116,17 @@ uint16_t SoilMoisture::getValue(uint8_t sensor) { return writeAndRead(req).read16le(); } + +string SoilMoisture::getName(uint8_t sensor) { + auto req = createGetSensorName(sensor); + + auto buffer = writeAndRead(req); + size_t bytesLeft = buffer.getBytesLeft(); + uint8_t bytes[bytesLeft]; + buffer.copy(bytes, bytesLeft); + + return string((char *) bytes, (unsigned long) bytesLeft); +} + } } diff --git a/apps/SoilMoisture.h b/apps/SoilMoisture.h index 938da07..45ba5d4 100644 --- a/apps/SoilMoisture.h +++ b/apps/SoilMoisture.h @@ -30,6 +30,8 @@ public: uint16_t getValue(uint8_t sensor); + string getName(uint8_t sensor); + private: SoilMoisture(shared_ptr gatt, BluetoothGattService &s, BluetoothGattCharacteristic &c); diff --git a/apps/sm-get-value.h b/apps/sm-get-value.h index 4083f8e..0073d7f 100644 --- a/apps/sm-get-value.h +++ b/apps/sm-get-value.h @@ -18,7 +18,6 @@ using namespace trygvis::apps; using namespace trygvis::bluetooth; using namespace trygvis::sensor; using namespace trygvis::sensor::io; -using json = nlohmann::json; class sm_get_value : public app { @@ -31,7 +30,16 @@ public: bool loop; sample_format_type format; unsigned int sleepTime; - vector sensors; + vector sensorIndexes; + vector> sensors; + + KeyDictionary dict; + SampleKey* hostname_key = dict.indexOf("hostname"); + SampleKey* device_key = dict.indexOf("device"); + SampleKey* sensor_key = dict.indexOf("sensor"); + SampleKey* sensor_name_key = dict.indexOf("sensor_name"); + SampleKey* timestamp_key = dict.indexOf("timestamp"); + SampleKey* value_key = dict.indexOf("value"); void add_options(po::options_description_easy_init &options) override { auto default_sleep = po::value<>(&sleepTime)->default_value(0); @@ -39,7 +47,7 @@ public: options ("device", po::value()->required(), "MAC of device to poll") - ("sensor", po::value>(&sensors)->multitoken(), "Sensor to poll, defaults to all") + ("sensor", po::value>(&sensorIndexes)->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") ("format", default_format, "Output format"); @@ -97,6 +105,31 @@ public: } } + void read_sensors(SoilMoisture &soilMoisture, string mac) { + auto epoch = system_clock::now().time_since_epoch(); + auto timestamp = duration_cast(epoch).count(); + 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 s : sensors) { + auto sensor = s.first; + auto name = s.second; + + uint16_t value = soilMoisture.getValue((uint8_t) sensor); + + auto sample = SampleRecord(dict) + .set(hostname_key, get_hostname()) + .set(device_key, mac) + .set(sensor_key, std::to_string(sensor)) + .set(sensor_name_key, name) + .set(timestamp_key, std::to_string(timestamp)) + .set(value_key, std::to_string(value)); + + output_stream->write(sample); + } + } + void withConnection(sample_format_type format, shared_ptr gatt) { SoilMoisture soilMoisture = SoilMoisture::create(gatt); @@ -109,54 +142,37 @@ public: // If the user didn't specify any sensors, add all. if (sensors.size() == 0) { for (unsigned int i = 0; i < sensorCount; i++) { - sensors.push_back(i); + sensorIndexes.push_back(i); } } - auto mac = gatt->getDevice().getMac(); - - 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"); - - time_point targetTime; - targetTime = system_clock::now(); - - do { - 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_each(begin(sensorIndexes), end(sensorIndexes), [&](auto i) { + if (i >= sensorCount) { + // Ignore invalid sensors + return; + } - auto epoch = system_clock::now().time_since_epoch(); - auto timestamp = duration_cast(epoch).count(); + sensors.push_back(make_pair(i, soilMoisture.getName(i))); + }); - for (auto sensor : sensors) { - if (sensor >= sensorCount) { - // Ignore invalid sensors - continue; - } + auto mac = gatt->getDevice().getMac().str(); - uint16_t value = soilMoisture.getValue((uint8_t) sensor); - - auto sample = SampleRecord(dict) - .set(hostname_key, get_hostname()) - .set(device_key, mac.str()) - .set(sensor_key, std::to_string(sensor)) - .set(timestamp_key, std::to_string(timestamp)) - .set(value_key, std::to_string(value)); - - output_stream->write(sample); - } + if (!loop) { + read_sensors(soilMoisture, mac); + } else { + time_point targetTime; + targetTime = system_clock::now(); do { - targetTime = targetTime + seconds(sleepTime); - } while (targetTime < system_clock::now()); + read_sensors(soilMoisture, mac); + + do { + targetTime = targetTime + seconds(sleepTime); + } while (targetTime < system_clock::now()); - this_thread::sleep_until(targetTime); - } while (loop); + this_thread::sleep_until(targetTime); + } while (loop); + } } }; diff --git a/ble/LinuxBluetooth.cpp b/ble/LinuxBluetooth.cpp index 6e3c9da..c96d145 100644 --- a/ble/LinuxBluetooth.cpp +++ b/ble/LinuxBluetooth.cpp @@ -68,6 +68,10 @@ public: ~LinuxBluetoothGatt(); + LinuxBluetoothGatt(const LinuxBluetoothGatt&) = delete; + + LinuxBluetoothGatt& operator=(const LinuxBluetoothGatt&) = delete; + bool isConnected() const override; void discoverServices() override; diff --git a/include/ble/Bluetooth.h b/include/ble/Bluetooth.h index 80729af..97e7cfe 100644 --- a/include/ble/Bluetooth.h +++ b/include/ble/Bluetooth.h @@ -122,6 +122,11 @@ public: virtual ~BluetoothGatt(); + // No copying + BluetoothGatt(const BluetoothGatt&) = delete; + + BluetoothGatt& operator=(const BluetoothGatt&) = delete; + virtual BluetoothDevice &getDevice() const = 0; virtual bool isConnected() const = 0; diff --git a/sensor/include/trygvis/sensor.h b/sensor/include/trygvis/sensor.h index 48fb509..b5199e2 100644 --- a/sensor/include/trygvis/sensor.h +++ b/sensor/include/trygvis/sensor.h @@ -82,8 +82,11 @@ public: ~KeyDictionary() { std::for_each(keys.begin(), keys.end(), std::default_delete()); } + KeyDictionary(KeyDictionary& that) = delete; + KeyDictionary& operator=(const KeyDictionary&) = delete; + SampleKey *indexOf(const string &key) { SampleKeyIndex i = 0; for (auto ptr = keys.cbegin(); ptr != keys.cend(); ptr++, i++) { -- cgit v1.2.3