diff options
author | Trygve Laugstøl <trygvis@inamo.no> | 2015-06-21 00:15:04 +0200 |
---|---|---|
committer | Trygve Laugstøl <trygvis@inamo.no> | 2015-06-21 00:15:43 +0200 |
commit | c56840f03cf139d60c6d90b55cf16e70f6ae2bc2 (patch) | |
tree | c9f19ab065496ac704fbf855da031ef5643eefa3 /apps/sm-db-select.cpp | |
parent | d91e500592790f1ef22ebfe921f273a61ff6252f (diff) | |
download | ble-toys-c56840f03cf139d60c6d90b55cf16e70f6ae2bc2.tar.gz ble-toys-c56840f03cf139d60c6d90b55cf16e70f6ae2bc2.tar.bz2 ble-toys-c56840f03cf139d60c6d90b55cf16e70f6ae2bc2.tar.xz ble-toys-c56840f03cf139d60c6d90b55cf16e70f6ae2bc2.zip |
o Going all header file based and single-executable to launch all apps.
o Ading CMake magic to generate the launcher
Diffstat (limited to 'apps/sm-db-select.cpp')
-rw-r--r-- | apps/sm-db-select.cpp | 82 |
1 files changed, 0 insertions, 82 deletions
diff --git a/apps/sm-db-select.cpp b/apps/sm-db-select.cpp deleted file mode 100644 index 8bcce32..0000000 --- a/apps/sm-db-select.cpp +++ /dev/null @@ -1,82 +0,0 @@ -#include <boost/optional.hpp> -#include <boost/lexical_cast.hpp> -#include <pqxx/connection.hxx> -#include <pqxx/transaction.hxx> -#include "json.hpp" - -template<class T> -using o = boost::optional<T>; -using namespace std; -using json = nlohmann::json; - -class missing_key : public runtime_error { -public: - missing_key(const json::string_t key) : runtime_error("Missing key: " + key), key(key) { - } - - const json::string_t key; -}; - -template<typename T> -T get(json j, string key) { - auto ref = j[key]; - - if (ref.is_null()) { - throw missing_key(key); - } - - return ref; -} - -int main(int argc, char *argv[]) { - string mac = "aa:bb:cc:dd:ee:ff"; - int sensor = 1; - - json j; - - pqxx::connection c("host=localhost dbname=soil-moisture"); - - pqxx::work work(c); - - auto rs = work.parameterized("select id from soil_moisture_device where mac=$1")(mac).exec(); - - if (!rs.size()) { - cout << "Unknown device: " << mac << endl; - return EXIT_FAILURE; - } - - auto deviceId = rs.begin()["id"].as<int>(); - - rs = work.parameterized("select id from soil_moisture_sensor where device=$1 and sensor=$2")(deviceId)(sensor).exec(); - - if (!rs.size()) { - cout << "Unknown sensor: " << sensor << endl; - return EXIT_FAILURE; - } - - auto sensorId = rs.begin()["id"].as<int>(); - - rs = work.parameterized("select timestamp, sensor, value from soil_moisture_sample where sensor=$1")(sensorId).exec(); - - json points = json::array(); - for (auto sample: rs) { - json s; - s.push_back(sample["timestamp"].as<unsigned long>()); - s.push_back(sample["sensor"].as<unsigned long>()); - s.push_back(sample["value"].as<unsigned long>()); - points.push_back({s}); - } - - json o; - o["columns"] = json::array({"time", "sensor", "value"}); - o["points"] = json(points); - j.push_back(o); - - cout << "JSON" << endl; - cout << setw(2) << j << endl; - cout << "JSON" << endl; - - work.commit(); - - return EXIT_SUCCESS; -} |