From cd87ff41255a975714c93eb7729f39350bfe2f99 Mon Sep 17 00:00:00 2001 From: "de@itstall.de" <de@itstall.de> Date: Wed, 29 Jan 2020 20:53:02 +0100 Subject: [PATCH] sensors in output to frontend now --- MqttClient.h | 5 +- backend.cpp | 15 +++++- backend.vcxproj | 1 - backend.vcxproj.filters | 3 -- dbSqlite.h | 108 ++++++++++++++++++++++++---------------- influx.h | 19 +++++++ openweathermap.h | 2 +- 7 files changed, 102 insertions(+), 51 deletions(-) create mode 100644 influx.h diff --git a/MqttClient.h b/MqttClient.h index 7eaa94e..bda20d2 100644 --- a/MqttClient.h +++ b/MqttClient.h @@ -98,8 +98,9 @@ public: std::cout << "MqttClient::on_message() - Request found" << std::endl; std::string client = str.substr(str.find("_") + 1); std::cout << "Client " + client + " requested update" << std::endl; - snprintf(buf, payload_size, db->getFrontendData().c_str()); - publish(NULL, db->getSettings().mqtt_topic_frontend.c_str(), strlen(db->getFrontendData().c_str()), db->getFrontendData().c_str()); + std::string result = db->getFrontendData().c_str(); + snprintf(buf, payload_size, result.c_str()); + publish(NULL, db->getSettings().mqtt_topic_frontend.c_str(), strlen(result.c_str()), result.c_str()); } } } diff --git a/backend.cpp b/backend.cpp index e9c00f7..8963d22 100644 --- a/backend.cpp +++ b/backend.cpp @@ -13,6 +13,7 @@ // Database object dbSqlite* db; openweathermap* owmw; +MqttClient* mqttClient; // Thread for openweathermap:getWeather void schedulerWeather(int time) { @@ -32,12 +33,22 @@ void schedulerForecast(int time) { } } +// Thread for MQTT:sendWeather +//void schedulerMqttSendWeather(int time) { +// if (DEBUG) std::cout << "Wetterstation::schedulerMqttSendWeather()" << std::endl; +// while (1) { +// //mqttClient->publish(); +// std::this_thread::sleep_for(std::chrono::milliseconds(time)); +// } +//} + void mqttStart() { if (DEBUG) std::cout << "Wetterstation::mqttStart()" << std::endl; mosqpp::lib_init(); - MqttClient* mqttClient = new MqttClient("Wetterstation", db); + mqttClient = new MqttClient("Wetterstation", db); + //std::thread thrMqttSendWeather{ schedulerMqttSendWeather, 10000 }; mosqpp::lib_cleanup(); } @@ -64,7 +75,7 @@ int main() { std::this_thread::sleep_for(std::chrono::milliseconds(50)); // Thread für getForecast starten - std::thread thrForecast{ schedulerForecast, 10000 }; + std::thread thrForecast{ schedulerForecast, 90000 }; std::this_thread::sleep_for(std::chrono::milliseconds(50)); // MQTT starten diff --git a/backend.vcxproj b/backend.vcxproj index 9188a47..bd8672c 100644 --- a/backend.vcxproj +++ b/backend.vcxproj @@ -154,7 +154,6 @@ <None Include="README.md" /> </ItemGroup> <ItemGroup> - <ClInclude Include="compatibility.h" /> <ClInclude Include="dbSqlite.h" /> <ClInclude Include="influxdb.h" /> <ClInclude Include="json.h" /> diff --git a/backend.vcxproj.filters b/backend.vcxproj.filters index 1f3b277..d8be000 100644 --- a/backend.vcxproj.filters +++ b/backend.vcxproj.filters @@ -40,9 +40,6 @@ <ClInclude Include="openweathermap.h"> <Filter>Quelldateien</Filter> </ClInclude> - <ClInclude Include="compatibility.h"> - <Filter>Quelldateien</Filter> - </ClInclude> <ClInclude Include="structs.h"> <Filter>Quelldateien</Filter> </ClInclude> diff --git a/dbSqlite.h b/dbSqlite.h index c56cde0..b4435b4 100644 --- a/dbSqlite.h +++ b/dbSqlite.h @@ -2,10 +2,11 @@ #include <iostream> #include <string> #include <vector> +#include "influxdb.h" #include "Poco/Data/Session.h" #include "Poco/Data/SQLite/Connector.h" -#include "structs.h" #include "nlohmann/json.hpp" +#include "structs.h" using namespace Poco::Data::Keywords; using Poco::Data::Session; @@ -49,48 +50,71 @@ public: std::string getFrontendData() { nlohmann::json jObj; - for (int i = 0; i < vecForecast.size(); i++) { - jObj["result"]["forecast"][i]["plz"] = vecForecast[i].plz; - jObj["result"]["forecast"][i]["lngCode"] = vecForecast[i].lngCode; - jObj["result"]["forecast"][i]["sunrise"] = vecForecast[i].sunrise; - jObj["result"]["forecast"][i]["sunset"] = vecForecast[i].sunset; - jObj["result"]["forecast"][i]["visibility"] = vecForecast[i].visibility; - jObj["result"]["forecast"][i]["temp"] = vecForecast[i].temp; - jObj["result"]["forecast"][i]["tempFeelsLike"] = vecForecast[i].tempFeelsLike; - jObj["result"]["forecast"][i]["tempMin"] = vecForecast[i].tempMin; - jObj["result"]["forecast"][i]["tempMax"] = vecForecast[i].tempMax; - jObj["result"]["forecast"][i]["humidity"] = vecForecast[i].humidity; - jObj["result"]["forecast"][i]["pressure"] = vecForecast[i].pressure; - jObj["result"]["forecast"][i]["windSpeed"] = vecForecast[i].windSpeed; - jObj["result"]["forecast"][i]["windDeg"] = vecForecast[i].windDeg; - jObj["result"]["forecast"][i]["clouds"] = vecForecast[i].clouds; - jObj["result"]["forecast"][i]["rain1h"] = vecForecast[i].rain1h; - jObj["result"]["forecast"][i]["rain3h"] = vecForecast[i].rain3h; - jObj["result"]["forecast"][i]["snow1h"] = vecForecast[i].snow1h; - jObj["result"]["forecast"][i]["snow3h"] = vecForecast[i].snow3h; - jObj["result"]["forecast"][i]["icons"] = vecForecast[i].icons; - jObj["result"]["forecast"][i]["from"] = vecForecast[i].from; + std::string queryResult; + influxdb_cpp::server_info si(this->getSettings().influx_host, this->getSettings().influx_port, this->getSettings().influx_db, this->getSettings().influx_user, this->getSettings().influx_pass); + influxdb_cpp::query(queryResult, "SELECT * FROM Sensors WHERE client = 'DVES_06236C' AND time > now() - 5m;", si); + + auto response = nlohmann::json::parse(queryResult); + + nlohmann::json columns = response["results"][0]["series"][0]["columns"]; + + int i = 0; + int j = 0; + for (nlohmann::json line : response["results"][0]["series"][0]["values"]) { + j = 0; + nlohmann::json temp; + for (nlohmann::json column : columns) { + temp[column.get<std::string>()] = line[j]; + j++; + } + jObj["sensors"][i++] = temp; } - jObj["result"]["current"]["plz"] = sWeather.plz; - jObj["result"]["current"]["lngCode"] = sWeather.lngCode; - jObj["result"]["current"]["sunrise"] = sWeather.sunrise; - jObj["result"]["current"]["sunset"] = sWeather.sunset; - jObj["result"]["current"]["visibility"] = sWeather.visibility; - jObj["result"]["current"]["temp"] = sWeather.temp; - jObj["result"]["current"]["tempFeelsLike"] = sWeather.tempFeelsLike; - jObj["result"]["current"]["tempMin"] = sWeather.tempMin; - jObj["result"]["current"]["tempMax"] = sWeather.tempMax; - jObj["result"]["current"]["humidity"] = sWeather.humidity; - jObj["result"]["current"]["pressure"] = sWeather.pressure; - jObj["result"]["current"]["windSpeed"] = sWeather.windSpeed; - jObj["result"]["current"]["windDeg"] = sWeather.windDeg; - jObj["result"]["current"]["clouds"] = sWeather.clouds; - jObj["result"]["current"]["rain1h"] = sWeather.rain1h; - jObj["result"]["current"]["rain3h"] = sWeather.rain3h; - jObj["result"]["current"]["snow1h"] = sWeather.snow1h; - jObj["result"]["current"]["snow3h"] = sWeather.snow3h; - jObj["result"]["current"]["icons"] = sWeather.icons; - jObj["result"]["current"]["from"] = sWeather.from; + + /*for (size_t i = 0; i < a.size(); ++i) { + std::cout << a[i]; + }*/ + + /*for (int i = 0; i < vecForecast.size(); i++) { + jObj["forecast"][i]["plz"] = vecForecast[i].plz; + jObj["forecast"][i]["lngCode"] = vecForecast[i].lngCode; + jObj["forecast"][i]["sunrise"] = vecForecast[i].sunrise; + jObj["forecast"][i]["sunset"] = vecForecast[i].sunset; + jObj["forecast"][i]["visibility"] = vecForecast[i].visibility; + jObj["forecast"][i]["temp"] = vecForecast[i].temp; + jObj["forecast"][i]["tempFeelsLike"] = vecForecast[i].tempFeelsLike; + jObj["forecast"][i]["tempMin"] = vecForecast[i].tempMin; + jObj["forecast"][i]["tempMax"] = vecForecast[i].tempMax; + jObj["forecast"][i]["humidity"] = vecForecast[i].humidity; + jObj["forecast"][i]["pressure"] = vecForecast[i].pressure; + jObj["forecast"][i]["windSpeed"] = vecForecast[i].windSpeed; + jObj["forecast"][i]["windDeg"] = vecForecast[i].windDeg; + jObj["forecast"][i]["clouds"] = vecForecast[i].clouds; + jObj["forecast"][i]["rain1h"] = vecForecast[i].rain1h; + jObj["forecast"][i]["rain3h"] = vecForecast[i].rain3h; + jObj["forecast"][i]["snow1h"] = vecForecast[i].snow1h; + jObj["forecast"][i]["snow3h"] = vecForecast[i].snow3h; + jObj["forecast"][i]["icons"] = vecForecast[i].icons; + jObj["forecast"][i]["from"] = vecForecast[i].from; + }*/ + jObj["current"]["plz"] = sWeather.plz; + jObj["current"]["lngCode"] = sWeather.lngCode; + jObj["current"]["sunrise"] = sWeather.sunrise; + jObj["current"]["sunset"] = sWeather.sunset; + jObj["current"]["visibility"] = sWeather.visibility; + jObj["current"]["temp"] = sWeather.temp; + jObj["current"]["tempFeelsLike"] = sWeather.tempFeelsLike; + jObj["current"]["tempMin"] = sWeather.tempMin; + jObj["current"]["tempMax"] = sWeather.tempMax; + jObj["current"]["humidity"] = sWeather.humidity; + jObj["current"]["pressure"] = sWeather.pressure; + jObj["current"]["windSpeed"] = sWeather.windSpeed; + jObj["current"]["windDeg"] = sWeather.windDeg; + jObj["current"]["clouds"] = sWeather.clouds; + jObj["current"]["rain1h"] = sWeather.rain1h; + jObj["current"]["rain3h"] = sWeather.rain3h; + jObj["current"]["snow1h"] = sWeather.snow1h; + jObj["current"]["snow3h"] = sWeather.snow3h; + jObj["current"]["icons"] = sWeather.icons; return jObj.dump(); } diff --git a/influx.h b/influx.h new file mode 100644 index 0000000..8b859e9 --- /dev/null +++ b/influx.h @@ -0,0 +1,19 @@ +#include <iostream> +#include <string> +#include <vector> +#include "influxdb.h" +#include "structs.h" + +using std::string; +using std::cout; +using std::endl; + +class influx { + std::string getData(std::string time) { + string response; + influxdb_cpp::server_info si("influxdb.itstall.de", 8086, "wetterstation", "wetterstation", "wetterstation"); + influxdb_cpp::query(response, "SELECT * FROM Sensors WHERE time > now() - " + time, si); + + return response; + } +}; \ No newline at end of file diff --git a/openweathermap.h b/openweathermap.h index e5e204e..49bf4f1 100644 --- a/openweathermap.h +++ b/openweathermap.h @@ -93,7 +93,7 @@ public: item.tempMin = jObj["list"][i]["main"]["temp_min"].ToFloat(); item.tempMax = jObj["list"][i]["main"]["temp_max"].ToFloat(); item.humidity = jObj["list"][i]["main"]["humidity"].ToInt(); - item.pressure = jObj["list"][i]["main"]["pressure"].ToInt(); + item.pressure = jObj["list"][i]["main"]["grnd_level"].ToInt(); item.windSpeed = jObj["list"][i]["wind"]["speed"].ToFloat(); item.windDeg = jObj["list"][i]["wind"]["deg"].ToInt(); item.clouds = jObj["list"][i]["clouds"]["all"].ToInt(); -- GitLab