diff options
author | Trygve Laugstøl <trygvis@inamo.no> | 2015-07-19 21:39:28 +0200 |
---|---|---|
committer | Trygve Laugstøl <trygvis@inamo.no> | 2015-07-19 21:49:03 +0200 |
commit | ae2d05eee4ffcec4c0611d907779ce8ef61d3a6e (patch) | |
tree | 6b86d64d03dfda4efc4a41e5814a229507289cb9 /apps/sm-db-insert.h | |
parent | 0374af511d7efdb856af372f126e66e5a78841d7 (diff) | |
download | ble-toys-ae2d05eee4ffcec4c0611d907779ce8ef61d3a6e.tar.gz ble-toys-ae2d05eee4ffcec4c0611d907779ce8ef61d3a6e.tar.bz2 ble-toys-ae2d05eee4ffcec4c0611d907779ce8ef61d3a6e.tar.xz ble-toys-ae2d05eee4ffcec4c0611d907779ce8ef61d3a6e.zip |
o Going back to a bunch of cpp files instead of launcher+bunch of header files. This ends up with an easier build file and faster builds with CMake's "OBJECT" library type.
Diffstat (limited to 'apps/sm-db-insert.h')
-rw-r--r-- | apps/sm-db-insert.h | 82 |
1 files changed, 0 insertions, 82 deletions
diff --git a/apps/sm-db-insert.h b/apps/sm-db-insert.h deleted file mode 100644 index 7e08268..0000000 --- a/apps/sm-db-insert.h +++ /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" -#include <fstream> - -namespace trygvis { -namespace apps { - -template <class T> -using o = boost::optional<T>; -using namespace std; -using json = nlohmann::json; - -class sm_db_insert : public app { -public: - sm_db_insert() : app("sm-db-insert") { - } - - ~sm_db_insert() = default; - - void add_options(po::options_description_easy_init &options) override { - options("file", po::value<string>()->required(), "The file to read"); - } - - int main(app_execution &execution) override { - auto file = execution.vm["file"].as<string>(); - cout << "reading from " << file << endl; - - fstream f(file); - - json j; - - j << f; - - pqxx::connection c("host=localhost dbname=soil-moisture"); - - pqxx::work work(c); - - string mac = j["mac"]; // "aa:bb:cc:dd:ee:ff"; - - auto select_device = "select id from soil_moisture_device where mac=$1"; - auto rs = work.parameterized(select_device)(mac).exec(); - - if (!rs.size()) { - cout << "New device: " << mac << endl; - - auto insert_device = "insert into soil_moisture_device(mac) values($1) returning id"; - rs = work.parameterized(insert_device)(mac).exec(); - } - - auto deviceId = rs.begin()["id"].as<int>(); - - int sensor = j["sensor"]; - - auto select_sensor = "select id from soil_moisture_sensor where device=$1 and sensor=$2"; - rs = work.parameterized(select_sensor)(deviceId)(sensor).exec(); - - if (!rs.size()) { - cout << "New sensor: " << sensor << endl; - - auto insert_sensor = "insert into soil_moisture_sensor(device, sensor) values($1, $2) returning id"; - rs = work.parameterized(insert_sensor)(deviceId)(sensor).exec(); - } - auto sensorId = rs.begin()["id"].as<int>(); - - unsigned long timestamp = get<unsigned long>(j, "timestamp"); - unsigned int value = get<unsigned int>(j, "value"); - - auto insert_sample = "insert into soil_moisture_sample(sensor, timestamp, value) values($1, $2, $3)"; - work.parameterized(insert_sample)(sensorId)(timestamp)(value).exec(); - - cout << "Sample inserted" << endl; - - work.commit(); - - return EXIT_SUCCESS; - } -}; -} -} |