diff --git a/backend.cpp b/backend.cpp index 216cfa6a4de4dd12a00caefae4f69f55c7779770..374bccfb0f7bea8671c20fe8e0d0e940123569ce 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 fd921c0b4f15d61d7da493cfc93e096f98c2cc85..175bacd7f54b7a9372264824df74cb6d18092f28 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 6a30bdb9c4792a785b6a9f93703a16e32b208a9b..9418a1979634c6a245d5e78485dafd9e3d2aa3af 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 Binary files a/weatherstation.db and b/weatherstation.db differ