From 4b76577b82265949051b04676035083fb15849d1 Mon Sep 17 00:00:00 2001
From: "de@itstall.de" <de@itstall.de>
Date: Mon, 3 Feb 2020 22:18:53 +0100
Subject: [PATCH] Added Sensors to backend

---
 backend.cpp       |  79 +++++++++++++++++++++++++++++++++++++++++++---
 dbSqlite.h        |  34 ++++++++++++++++----
 structs.h         |   3 ++
 weatherstation.db | Bin 552960 -> 552960 bytes
 4 files changed, 106 insertions(+), 10 deletions(-)

diff --git a/backend.cpp b/backend.cpp
index 216cfa6..374bccf 100644
--- a/backend.cpp
+++ b/backend.cpp
@@ -10,7 +10,7 @@
 #include "dbSqlite.h"
 #include "structs.h"
 
-#define DEBUG false
+#define DEBUG true
 
 using std::string;
 using std::cout;
@@ -40,6 +40,7 @@ void schedulerWeather(int time) {
             weather = db->getFrontendDataWeather().c_str();
             for (int fr = 0; fr < frontend.size(); fr++) {
                 if (frontend[fr].plz == region[i].plz) {
+                    cout << "Send weather to frontend: " << frontend[fr].frontendId << endl;
                     topic = db->getSettings().mqtt_topic_frontend + frontend[fr].frontendId + "/weather";
                     mqttClient->send_message(weather.c_str(), topic.c_str());
                     std::this_thread::sleep_for(std::chrono::milliseconds(50));
@@ -68,7 +69,8 @@ void schedulerForecast(int time) {
             forecast = db->getFrontendDataForecast().c_str();
             for (int fr = 0; fr < frontend.size(); fr++) {
                 if (frontend[fr].plz == region[i].plz) {
-                    topic = db->getSettings().mqtt_topic_frontend + frontend[i].frontendId + "/forecast";
+                    cout << "Send forecast to frontend: " << frontend[fr].frontendId << endl;
+                    topic = db->getSettings().mqtt_topic_frontend + frontend[fr].frontendId + "/forecast";
                     mqttClient->send_message(forecast.c_str(), topic.c_str());
                     std::this_thread::sleep_for(std::chrono::milliseconds(50));
                 }
@@ -78,6 +80,67 @@ void schedulerForecast(int time) {
     }
 }
 
+// Thread for dbSqlite:getGraphFromInfluxDb
+void schedulerInfluxGraph(int time) {
+    if (DEBUG) cout << "Wetterstation::schedulerInfluxGraph()" << endl;
+    string topic;
+    string influxData;
+    std::vector<Region> region;
+    std::vector<Frontend> frontend;
+
+    while (1) {
+        region = db->queryRegions();
+        frontend = db->queryFrontends();
+        for (int i = 0; i < region.size(); i++) {
+            cout << "Graph new RUN(" << region[i].plz << ") " << endl;
+            if (DEBUG) cout << "Wetterstation::schedulerInfluxGraph(" + region[i].plz + ")" << endl;
+            influxData = db->getGraphFromInfluxDb(region[i].plz);
+            for (int fr = 0; fr < frontend.size(); fr++) {
+                if (frontend[fr].plz == region[i].plz) {
+                    cout << "Send InfluxGraph to frontend: " << frontend[fr].frontendId << endl;
+                    topic = db->getSettings().mqtt_topic_frontend + frontend[fr].frontendId + "/graph";
+                    mqttClient->send_message(influxData.c_str(), topic.c_str());
+                    std::this_thread::sleep_for(std::chrono::milliseconds(50));
+                }
+            }
+        }
+        std::this_thread::sleep_for(std::chrono::milliseconds(time));
+    }
+}
+
+// Thread for dbSqlite:getSensorsFromInfluxDb
+void schedulerInfluxSensors(int time) {
+    if (DEBUG) cout << "Wetterstation::schedulerInfluxSensors()" << endl;
+    string topic;
+    std::vector<Frontend> frontend;
+
+    while (1) {
+        frontend = db->queryFrontends();
+        cout << "Sensors new RUN() " << endl;
+        for (int fr = 0; fr < frontend.size(); fr++) {
+            if (frontend[fr].node1Id.size() > 0) {
+                if (frontend[fr].node1Innen.size() > 0) {
+                    string innenData = db->getFrontendDataSensors(frontend[fr].node1Id, frontend[fr].node1Innen);
+                    if (DEBUG) cout << "Wetterstation::schedulerInfluxSensors(" + frontend[fr].node1Id + ", " + frontend[fr].node1Innen + ")" << endl;
+                    cout << "Send InfluxSensors to frontend: " << frontend[fr].frontendId << endl;
+                    topic = db->getSettings().mqtt_topic_frontend + frontend[fr].frontendId + "/sensors";
+                    mqttClient->send_message(innenData.c_str(), topic.c_str());
+                    std::this_thread::sleep_for(std::chrono::milliseconds(50));
+                }
+                if (frontend[fr].node1Aussen.size() > 0) {
+                    string aussenData = db->getFrontendDataSensors(frontend[fr].node1Id, frontend[fr].node1Aussen);
+                    if (DEBUG) cout << "Wetterstation::schedulerInfluxSensors(" + frontend[fr].node1Id + ", " + frontend[fr].node1Aussen + ")" << endl;
+                    cout << "Send InfluxSensors to frontend: " << frontend[fr].frontendId << endl;
+                    topic = db->getSettings().mqtt_topic_frontend + frontend[fr].frontendId + "/sensors";
+                    mqttClient->send_message(aussenData.c_str(), topic.c_str());
+                    std::this_thread::sleep_for(std::chrono::milliseconds(50));
+                }
+            }
+        }
+        std::this_thread::sleep_for(std::chrono::milliseconds(time));
+    }
+}
+
 void mqttStart() {
     if (DEBUG) cout << "Wetterstation::mqttStart()" << endl;
     mosqpp::lib_init();
@@ -105,14 +168,22 @@ int main() {
 
     // MQTT starten
     mqttStart();
-    std::this_thread::sleep_for(std::chrono::milliseconds(100));
 
     // Thread für getWeather starten
     thread thrWeather{ schedulerWeather, 60000 };
     std::this_thread::sleep_for(std::chrono::milliseconds(100));
 
     // Thread für getForecast starten
-    thread thrForecast{ schedulerForecast, 600000 };
+    thread thrForecast{ schedulerForecast, 60000 };
+    std::this_thread::sleep_for(std::chrono::milliseconds(100));
+
+    // Thread für getGraphFromInfluxDb starten
+    thread thrGraph{ schedulerInfluxGraph, 60000 };
+    std::this_thread::sleep_for(std::chrono::milliseconds(100));
+
+    // Thread für getSensorsFromInfluxDb starten
+    thread thrSensors{ schedulerInfluxSensors, 30000 };
+    std::this_thread::sleep_for(std::chrono::milliseconds(100));
 
     while (1) {
         Sleep(1000);
diff --git a/dbSqlite.h b/dbSqlite.h
index fd921c0..175bacd 100644
--- a/dbSqlite.h
+++ b/dbSqlite.h
@@ -110,7 +110,7 @@ public:
         return jObj.dump();
     }
 
-    string getFrontendDataSensors() {
+    string getFrontendDataSensors(string node, string sensor) {
         if (DEBUG) cout << "dbSqlite::getFrontendDataSensors()" << endl;
         nlohmann::json jObj;
         int i = 0;
@@ -118,7 +118,7 @@ public:
 
         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);
+        influxdb_cpp::query(queryResult, "SELECT * FROM Sensors WHERE client = '" + node + "' AND sensor = '" + sensor + "' AND time > now() - 5m;", si);
 
         auto response = nlohmann::json::parse(queryResult);
 
@@ -223,10 +223,13 @@ public:
         Frontend frontend;
 
         Statement select(session);
-        select << "SELECT frontendId, lngCode, plz FROM settings_frontend;",
+        select << "SELECT frontendId, lngCode, plz, node1Id, node1Innen, node1Aussen FROM settings_frontend;",
             into(frontend.frontendId),
             into(frontend.lngCode),
             into(frontend.plz),
+            into(frontend.node1Id),
+            into(frontend.node1Innen),
+            into(frontend.node1Aussen),
             range(0, 1); // iterate over result set one row at a time
 
         while (!select.done()) {
@@ -247,21 +250,40 @@ public:
         frontend.frontendId = jObj["frontendId"].ToString();
         frontend.plz = jObj["plz"].ToString();
         frontend.lngCode = jObj["lngCode"].ToString();
+        frontend.node1Id = jObj["node1Id"].ToString();
+        frontend.node1Innen = jObj["node1Innen"].ToString();
+        frontend.node1Aussen = jObj["node1Aussen"].ToString();
+
+        cout << " --> dbSqlite::updateSettings for Frontend: " << frontend.frontendId << endl;
 
         if (frontend.frontendId.size() > 0 && frontend.plz.size() > 0 && frontend.lngCode.size() > 0) {
             Statement insert(session);
-            insert << "INSERT OR IGNORE INTO settings_frontend(frontendId, plz, lngCode) VALUES(?, ?, ?);",
+            insert << "INSERT OR IGNORE INTO settings_frontend(frontendId, plz, lngCode, node1Id, node1Innen, node1Aussen) VALUES(?, ?, ?, ?, ?, ?);",
                 use(frontend.frontendId),
                 use(frontend.plz),
-                use(frontend.lngCode);
+                use(frontend.lngCode),
+                use(frontend.node1Id),
+                use(frontend.node1Innen),
+                use(frontend.node1Aussen);
             insert.execute();
 
             Statement update(session);
-            update << "UPDATE settings_frontend SET plz=?, lngCode=? WHERE frontendId = ?;",
+            update << "UPDATE settings_frontend SET plz=?, lngCode=?, node1Id=?, node1Innen=?, node1Aussen=? WHERE frontendId = ?;",
                 use(frontend.plz),
                 use(frontend.lngCode),
+                use(frontend.node1Id),
+                use(frontend.node1Innen),
+                use(frontend.node1Aussen),
                 use(frontend.frontendId);
             update.execute();
         }
     }
+
+    string getGraphFromInfluxDb(string plz) {
+        influxdb_cpp::server_info si(getSettings().influx_host, getSettings().influx_port, getSettings().influx_db, getSettings().influx_user, getSettings().influx_pass);
+
+        string result;
+        influxdb_cpp::query(result, "SELECT mean(temp) as temp, mean(humidity) as humidity, mean(pressure) as pressure, mean(wind_speed) as wind_speed FROM \"Weather\" WHERE plz = '" + plz + "' AND time > now() - 24h GROUP BY time(15m)", si);
+        return result;
+    }
 };
diff --git a/structs.h b/structs.h
index 6a30bdb..9418a19 100644
--- a/structs.h
+++ b/structs.h
@@ -52,6 +52,9 @@ struct Frontend {
     string frontendId;
     string plz;
     string lngCode;
+    string node1Id;
+    string node1Innen;
+    string node1Aussen;
 };
 
 struct Region {
diff --git a/weatherstation.db b/weatherstation.db
index add15d1515561f52593a6357af54a7a56fc90fe9..da6af422d40ec5980a787af25b07422422c51899 100644
GIT binary patch
delta 1104
zcmcIh&ui0A98a2bU4JC66-FIJ*0HLLmG0#wc}b9gU0b1GSa3rxlalVeY{JaO+IihC
zh*u>?5j^eU&Hfi2=D>eoJM`d1P`r5aWptGZWxIIz67u=}`22YLXVd#<({Bp77RPa~
zS&6KUSlxMBRwr6q<-re0?sG!A&t;B4CEZV*_Hx<P6qWmYFTb$BA3p24?M>RD-R=v2
zz0>k`w)`&jNsFwvx-Yj!;X>_6qq@>4u2dHvH;SXC!fK}El~c)0f4#OvXz`W1v$o;x
z6bbd*-OVohvZl{EN|RhRhluY}|6j1W+v)s8Q?tz4Nq&BwKWv1*)AxjfLNfd=zv~@k
z+Oa5zX827uZXY}!Ph42FBdZ5X>@Qle*~)!h2&Xf3|9@&sF(Gl_a^M#@1s_2GT=oJ>
z;BHB<d2bD4tX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