aboutsummaryrefslogtreecommitdiff
path: root/apps/sm-get-value.h
diff options
context:
space:
mode:
Diffstat (limited to 'apps/sm-get-value.h')
-rw-r--r--apps/sm-get-value.h100
1 files changed, 58 insertions, 42 deletions
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<unsigned int> sensors;
+ vector<unsigned int> sensorIndexes;
+ vector<pair<unsigned int, string>> 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<string>()->required(), "MAC of device to poll")
- ("sensor", po::value<vector<unsigned int>>(&sensors)->multitoken(), "Sensor to poll, defaults to all")
+ ("sensor", po::value<vector<unsigned int>>(&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<seconds>(epoch).count();
+ 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 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<BluetoothGatt> 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<system_clock> targetTime;
- targetTime = system_clock::now();
-
- do {
- 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_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<seconds>(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<system_clock> 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);
+ }
}
};