Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
B
Backend
Manage
Activity
Members
Labels
Plan
Issues
0
Issue boards
Milestones
Wiki
Code
Merge requests
0
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container Registry
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Wetterstation
Backend
Commits
4b76577b
Commit
4b76577b
authored
5 years ago
by
Dennis Eisold
Browse files
Options
Downloads
Patches
Plain Diff
Added Sensors to backend
parent
9762a0ff
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
backend.cpp
+75
-4
75 additions, 4 deletions
backend.cpp
dbSqlite.h
+28
-6
28 additions, 6 deletions
dbSqlite.h
structs.h
+3
-0
3 additions, 0 deletions
structs.h
weatherstation.db
+0
-0
0 additions, 0 deletions
weatherstation.db
with
106 additions
and
10 deletions
backend.cpp
+
75
−
4
View file @
4b76577b
...
...
@@ -10,7 +10,7 @@
#include
"dbSqlite.h"
#include
"structs.h"
#define DEBUG
fals
e
#define DEBUG
tru
e
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
);
...
...
This diff is collapsed.
Click to expand it.
dbSqlite.h
+
28
−
6
View file @
4b76577b
...
...
@@ -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
;
}
};
This diff is collapsed.
Click to expand it.
structs.h
+
3
−
0
View file @
4b76577b
...
...
@@ -52,6 +52,9 @@ struct Frontend {
string
frontendId
;
string
plz
;
string
lngCode
;
string
node1Id
;
string
node1Innen
;
string
node1Aussen
;
};
struct
Region
{
...
...
This diff is collapsed.
Click to expand it.
weatherstation.db
+
0
−
0
View file @
4b76577b
No preview for this file type
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment