aboutsummaryrefslogtreecommitdiff
path: root/apps/sm-get-value.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'apps/sm-get-value.cpp')
-rw-r--r--apps/sm-get-value.cpp49
1 files changed, 6 insertions, 43 deletions
diff --git a/apps/sm-get-value.cpp b/apps/sm-get-value.cpp
index a61132f..9f9308a 100644
--- a/apps/sm-get-value.cpp
+++ b/apps/sm-get-value.cpp
@@ -5,6 +5,7 @@
#include <thread>
#include "ble/Bluetooth.h"
#include "SoilMoisture.h"
+#include "SoilMoistureIo.h"
#include "json.hpp"
#include "apps.h"
@@ -17,46 +18,8 @@ using namespace trygvis::soil_moisture;
namespace po = boost::program_options;
using json = nlohmann::json;
-enum class Format {
- PLAIN,
- JSON,
- SQL
-};
-
-void validate(boost::any &v, const std::vector<std::string> &values, Format *, int) {
- using namespace boost::program_options;
-
- const std::string &s = validators::get_single_string(values);
-
- if (s == "plain") {
- v = boost::any(Format::PLAIN);
- } else if (s == "json") {
- v = boost::any(Format::JSON);
- } else if (s == "sql") {
- v = boost::any(Format::SQL);
- } else {
- throw validation_error(validation_error::invalid_option_value);
- }
-}
-
-namespace boost {
-
-template<>
-string lexical_cast(const Format &arg) {
- if (arg == Format::PLAIN)
- return "plain";
- else if (arg == Format::JSON)
- return "json";
- else if (arg == Format::SQL)
- return "sql";
- else
- throw std::runtime_error("Unknown format value: " + lexical_cast<string>(arg));
-}
-
-}
-
bool loop;
-Format format;
+sample_format_type format;
time_point<system_clock> targetTime;
unsigned int sleepTime;
vector<unsigned int> sensors;
@@ -92,19 +55,19 @@ void withConnection(BluetoothGatt &gatt) {
auto timestamp = duration_cast<seconds>(epoch).count();
uint16_t value = soilMoisture.getValue((uint8_t) sensor);
- if (format == Format::PLAIN) {
+ 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 == Format::JSON) {
+ } 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 == Format::SQL) {
+ } else if (format == sample_format_type::SQL) {
cout << "INSERT INTO soil_moisture_sample(device, sensor, timestamp, value) VALUES("
<< "'" << device.str() << "', "
<< sensor << ", "
@@ -131,7 +94,7 @@ public:
("sensor", po::value<vector<unsigned int>>(&sensors)->multitoken(), "Sensor to poll, defaults to all")
("sleep", po::value<unsigned int>(&sleepTime)->default_value(0),
"How long to sleep in seconds between each poll. If not give, it will exit after first poll")
- ("format", po::value<Format>(&format)->default_value(Format::PLAIN), "Output format");
+ ("format", po::value<sample_format_type>(&format)->default_value(sample_format_type::KEY_VALUE), "Output format");
}