From 5a8fa2161ba5b53ab05e43649e8ae5532cf0a8df Mon Sep 17 00:00:00 2001 From: Trygve Laugstøl Date: Thu, 26 Feb 2015 08:43:27 +0100 Subject: o Adding utility to insert samples from json files. --- apps/sm-get-value.cpp | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) (limited to 'apps/sm-get-value.cpp') diff --git a/apps/sm-get-value.cpp b/apps/sm-get-value.cpp index 97858f4..14a90de 100644 --- a/apps/sm-get-value.cpp +++ b/apps/sm-get-value.cpp @@ -18,7 +18,8 @@ using json = nlohmann::json; enum class Format { PLAIN, - JSON + JSON, + SQL }; void validate(boost::any &v, const std::vector &values, Format *, int) { @@ -26,10 +27,12 @@ void validate(boost::any &v, const std::vector &values, Format *, i const std::string &s = validators::get_single_string(values); - if (s == "json") { - v = boost::any(Format::JSON); - } else if (s == "plain") { + 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); } @@ -43,8 +46,10 @@ string lexical_cast(const Format &arg) { return "plain"; else if (arg == Format::JSON) return "json"; + else if (arg == Format::SQL) + return "sql"; else - throw std::runtime_error("Unknown Format value: " + to_string(static_cast::type>(arg))); + throw std::runtime_error("Unknown format value: " + lexical_cast(arg)); } } @@ -98,6 +103,12 @@ void withConnection(BluetoothGatt &gatt) { j["timestamp"] = timestamp; j["value"] = value; cout << j << endl; + } else if (format == Format::SQL) { + cout << "INSERT INTO soil_moisture_sample(device, sensor, timestamp, value) VALUES(" + << "'" << device.str() << "', " + << sensor << ", " + << timestamp << ", " + << value << ";" << endl; } } -- cgit v1.2.3