diff options
-rw-r--r-- | CMakeLists.txt | 11 | ||||
-rw-r--r-- | misc_support.h | 5 | ||||
-rw-r--r-- | raw-mqtt-consumer.cpp | 9 | ||||
-rw-r--r-- | sm-http-server.cpp | 1 | ||||
-rw-r--r-- | sm-mqtt-consumer.cpp | 16 |
5 files changed, 24 insertions, 18 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index f63f023..95bcf89 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,5 +1,5 @@ cmake_minimum_required(VERSION 3.2) -project(soil-moisture) +project(mqtt_cassandra_bridge) find_package(Boost COMPONENTS system program_options unit_test_framework REQUIRED) @@ -14,7 +14,8 @@ ExternalProject_Add(cpp-driver URL https://github.com/datastax/cpp-driver/archive/2.1.0-beta.tar.gz URL_MD5 d3cfde8731acc2f0f51ef9caf41068dc PREFIX ${CPP_DRIVER} - CMAKE_ARGS -DCMAKE_INSTALL_PREFIX:PATH=<INSTALL_DIR> -DCMAKE_C_COMPILER:FILE=${CMAKE_C_COMPILER} -DCMAKE_CXX_COMPILER:FILE=${CMAKE_CXX_COMPILER} +# PATCH_COMMAND cp ${CMAKE_CURRENT_SOURCE_DIR}/cmake-cassandra.tmp.cmake <SOURCE_DIR>/CMakeLists.txt + CMAKE_ARGS -DCMAKE_INSTALL_PREFIX:PATH=<INSTALL_DIR> -DCMAKE_C_COMPILER:FILE=${CMAKE_C_COMPILER} -DCMAKE_CXX_COMPILER:FILE=${CMAKE_CXX_COMPILER} -DCMAKE_C_FLAGS=${CMAKE_C_FLAGS} -DCMAKE_CXX_FLAGS=${CMAKE_CXX_FLAGS} -DCMAKE_LD_FLAGS=${CMAKE_LD_FLAGS} ) # ble-toys @@ -24,7 +25,7 @@ ExternalProject_Add(ble-toys GIT_TAG f6493150c1a7172bcd8c9cc1790829285f707ee9 GIT_SUBMODULES json PREFIX ${BLE_TOYS} - CMAKE_ARGS -DCMAKE_INSTALL_PREFIX:PATH=<INSTALL_DIR> -DCMAKE_C_COMPILER:FILE=${CMAKE_C_COMPILER} -DCMAKE_CXX_COMPILER:FILE=${CMAKE_CXX_COMPILER} + CMAKE_ARGS -DCMAKE_INSTALL_PREFIX:PATH=<INSTALL_DIR> -DCMAKE_C_COMPILER:FILE=${CMAKE_C_COMPILER} -DCMAKE_CXX_COMPILER:FILE=${CMAKE_CXX_COMPILER} -DCMAKE_C_FLAGS=${CMAKE_C_FLAGS} -DCMAKE_CXX_FLAGS=${CMAKE_CXX_FLAGS} -DCMAKE_LD_FLAGS=${CMAKE_LD_FLAGS} ) # nghttp2 @@ -34,8 +35,8 @@ ExternalProject_Add(nghttp2 URL_MD5 ff25d732d79128c4fa426393a635c21e PREFIX ${NGHTTP2} BUILD_ALWAYS 0 - CONFIGURE_COMMAND cd <SOURCE_DIR> && autoreconf -i && automake && autoconf && ./configure --prefix=<INSTALL_DIR> --enable-asio-lib - BUILD_COMMAND cd <SOURCE_DIR> && "$(MAKE)" + CONFIGURE_COMMAND cd <SOURCE_DIR> && autoreconf -i && automake && autoconf && ./configure CFLAGS=${CMAKE_C_FLAGS} CXXFLAGS=${CMAKE_CXX_FLAGS} --prefix=<INSTALL_DIR> --enable-asio-lib ${NGHTTP2_CONFIGURE_EXTRA_ARGS} + BUILD_COMMAND cd <SOURCE_DIR> && make INSTALL_COMMAND cd <SOURCE_DIR> && make install ) diff --git a/misc_support.h b/misc_support.h index b00f6a8..52d4a46 100644 --- a/misc_support.h +++ b/misc_support.h @@ -9,6 +9,11 @@ namespace trygvis { namespace misc_support { +using namespace std; +using namespace std::chrono; + +using timestamp_t = time_point<system_clock, milliseconds>; + using namespace boost; using namespace std; namespace po = boost::program_options; diff --git a/raw-mqtt-consumer.cpp b/raw-mqtt-consumer.cpp index 86815c6..04453e6 100644 --- a/raw-mqtt-consumer.cpp +++ b/raw-mqtt-consumer.cpp @@ -1,17 +1,15 @@ #include "cassandra_support.h" #include "mqtt_support.h" +#include "misc_support.h" #include <thread> -#include <boost/lexical_cast.hpp> -#include <boost/program_options.hpp> #include <trygvis/sensor.h> #include <trygvis/sensor/io.h> namespace raw_mqtt_consumer { -using namespace std; -using namespace std::chrono; using namespace trygvis::cassandra_support; using namespace trygvis::mqtt_support; +using namespace trygvis::misc_support; using namespace trygvis::sensor; using namespace trygvis::sensor::io; using namespace boost; @@ -99,7 +97,7 @@ cassandra_future2 *insert_into_raw(unique_ptr<cassandra_session> &session, const cout << "day=" << day << endl; auto now_ms = std::chrono::time_point_cast<std::chrono::milliseconds>(system_now); - long timestamp = now_ms.time_since_epoch().count(); + auto timestamp = now_ms.time_since_epoch().count(); q.bind(1, timestamp); cout << "timestamp=" << timestamp << endl; @@ -192,6 +190,7 @@ int main(int argc, const char **argv) { } catch (po::required_option &e) { cerr << "Missing required option: " << e.get_option_name() << endl; cerr << all << endl; + return EXIT_FAILURE; } catch (po::unknown_option &e) { cerr << e.what() << endl; return EXIT_FAILURE; diff --git a/sm-http-server.cpp b/sm-http-server.cpp index b9c3203..80dd68f 100644 --- a/sm-http-server.cpp +++ b/sm-http-server.cpp @@ -171,6 +171,7 @@ int main(int argc, const char *const argv[]) { } catch (po::required_option &e) { cerr << "Missing required option: " << e.get_option_name() << endl; cerr << all << endl; + return EXIT_FAILURE; } catch (po::unknown_option &e) { cerr << e.what() << endl; return EXIT_FAILURE; diff --git a/sm-mqtt-consumer.cpp b/sm-mqtt-consumer.cpp index e905d02..5a0a23e 100644 --- a/sm-mqtt-consumer.cpp +++ b/sm-mqtt-consumer.cpp @@ -6,8 +6,6 @@ namespace sm_mqtt_consumer { -using namespace std; -using namespace std::chrono; using namespace trygvis::sensor; using namespace trygvis::sensor::io; using namespace trygvis::cassandra_support; @@ -40,10 +38,10 @@ struct sensor_measurement { struct device_measurement { string device; - long timestamp; + timestamp_t timestamp; vector<sensor_measurement> sensors; - device_measurement(string &device, long timestamp, vector<sensor_measurement> &&sensors) : + device_measurement(string &device, timestamp_t timestamp, vector<sensor_measurement> &&sensors) : device(device), timestamp(timestamp), sensors(std::move(sensors)) { }; @@ -52,7 +50,7 @@ struct device_measurement { string str() { stringstream buf; buf << "device=" << device; - buf << ", timestamp=" << timestamp; + buf << ", timestamp=" << timestamp.time_since_epoch().count(); std::for_each(sensors.begin(), sensors.end(), [&](auto &sensor) { buf << ", #" << sensor.sensor << "=" << sensor.value; }); @@ -141,12 +139,12 @@ cassandra_future2* insert_into_sm_by_day(cassandra_session *session, device_meas stmt.bind(0, measurement.device); - std::time_t t = measurement.timestamp; + std::time_t t = system_clock::to_time_t(measurement.timestamp); char day[100]; std::strftime(day, sizeof(day), "%Y-%m-%d", std::localtime(&t)); stmt.bind(1, day); - stmt.bind(2, measurement.timestamp * 1000); + stmt.bind(2, measurement.timestamp.time_since_epoch().count()); cassandra_collection sensors(CASS_COLLECTION_TYPE_LIST, measurement.sensors.size()); for_each(measurement.sensors.cbegin(), measurement.sensors.cend(), [&](auto sensor) { @@ -223,7 +221,8 @@ void on_message(const struct mosquitto_message *message) { return; } - device_measurement measurement(device, timestamp.get(), std::move(sensors)); + timestamp_t ts(milliseconds(timestamp.get())); + device_measurement measurement(device, ts, std::move(sensors)); cout << "Measurement: " << measurement.str() << endl; @@ -277,6 +276,7 @@ int main(int argc, const char **argv) { } catch (po::required_option &e) { cerr << "Missing required option: " << e.get_option_name() << endl; cerr << all << endl; + return EXIT_FAILURE; } catch (po::unknown_option &e) { cerr << e.what() << endl; return EXIT_FAILURE; |