Skip to content
Snippets Groups Projects
Commit 4b76577b authored by Dennis Eisold's avatar Dennis Eisold
Browse files

Added Sensors to backend

parent 9762a0ff
No related branches found
No related tags found
No related merge requests found
......@@ -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 fr getWeather starten
thread thrWeather{ schedulerWeather, 60000 };
std::this_thread::sleep_for(std::chrono::milliseconds(100));
// Thread fr getForecast starten
thread thrForecast{ schedulerForecast, 600000 };
thread thrForecast{ schedulerForecast, 60000 };
std::this_thread::sleep_for(std::chrono::milliseconds(100));
// Thread fr getGraphFromInfluxDb starten
thread thrGraph{ schedulerInfluxGraph, 60000 };
std::this_thread::sleep_for(std::chrono::milliseconds(100));
// Thread fr getSensorsFromInfluxDb starten
thread thrSensors{ schedulerInfluxSensors, 30000 };
std::this_thread::sleep_for(std::chrono::milliseconds(100));
while (1) {
Sleep(1000);
......
......@@ -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;
}
};
......@@ -52,6 +52,9 @@ struct Frontend {
string frontendId;
string plz;
string lngCode;
string node1Id;
string node1Innen;
string node1Aussen;
};
struct Region {
......
No preview for this file type
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment