Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in / Register
Toggle navigation
G
gm_telemetry_server
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Christopher Reis
gm_telemetry_server
Commits
d1173d94
Commit
d1173d94
authored
May 17, 2017
by
Christopher Reis
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Completed MySQL integration
parent
a7486a3b
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
20 additions
and
15 deletions
+20
-15
dataset.hpp
dataset.hpp
+16
-11
gm_telemetry_server.cpp
gm_telemetry_server.cpp
+4
-4
No files found.
dataset.hpp
View file @
d1173d94
#include <cstdlib>
#include <iostream>
#include <fstream>
...
...
@@ -15,7 +16,6 @@ using namespace gmsec::api::util;
std
::
vector
<
std
::
string
>
telemetry_available
;
// struct measurement {
// std::string name;
// std::vector<boost::any> v;
...
...
@@ -37,10 +37,9 @@ struct subsystem {
std
::
string
server
;
std
::
string
name
;
std
::
vector
<
measurement
>
v
;
MYSQL
*
con
=
mysql_init
(
NULL
);
MYSQL_RES
*
res
;
void
updateMeasurement
(
std
::
string
m
,
std
::
string
value
,
unsigned
long
t
){
MYSQL
*
con
=
mysql_init
(
NULL
);
mysql_real_connect
(
con
,
"192.168.1.4"
,
"root"
,
"root"
,
"gmsec"
,
0
,
NULL
,
0
);
for
(
std
::
vector
<
measurement
>::
iterator
it
=
v
.
begin
();
it
!=
v
.
end
();
++
it
){
if
(
it
->
name
==
m
){
...
...
@@ -48,6 +47,8 @@ struct subsystem {
it
->
timestamp
=
t
;
std
::
string
insert
=
"INSERT INTO "
+
m
+
" VALUES("
+
std
::
to_string
(
t
)
+
","
+
value
+
");"
;
mysql_query
(
con
,
insert
.
c_str
());
//std::cout << mysql_error(con) << '\n';
mysql_close
(
con
);
return
;
}
}
...
...
@@ -58,47 +59,51 @@ struct subsystem {
v
.
push_back
(
meas
);
//mysql_query
std
::
string
str
=
"CREATE TABLE "
+
m
+
"(Timestamp TEXT, Value TEXT);"
;
std
::
cout
<<
str
<<
'\n'
;
mysql_query
(
con
,
str
.
c_str
());
std
::
cout
<<
mysql_error
(
con
)
<<
'\n'
;
//
std::cout << mysql_error(con) << '\n';
str
=
"INSERT INTO "
+
m
+
" VALUES("
+
std
::
to_string
(
t
)
+
","
+
value
+
");"
;
mysql_query
(
con
,
str
.
c_str
());
std
::
cout
<<
mysql_error
(
con
)
<<
'\n'
;
//std::cout << mysql_error(con) << '\n';
mysql_close
(
con
);
return
;
}
nlohmann
::
json
getHistory
(
std
::
string
his
){
std
::
cout
<<
"Getting History of "
<<
his
<<
v
.
size
()
<<
'\n'
;
nlohmann
::
json
json_re
;
nlohmann
::
json
json_val
;
for
(
std
::
vector
<
measurement
>::
iterator
it
=
v
.
begin
();
it
!=
v
.
end
();
++
it
){
if
(
it
->
name
==
his
){
MYSQL
*
c
=
mysql_init
(
NULL
);
mysql_real_connect
(
c
,
server
.
c_str
(),
"root"
,
"root"
,
"gmsec"
,
0
,
NULL
,
0
);
std
::
cout
<<
mysql_error
(
c
)
<<
'\n'
;
std
::
string
q
=
"SELECT * FROM "
+
his
;
mysql_query
(
c
,
q
.
c_str
());
std
::
cout
<<
mysql_error
(
c
)
<<
'\n'
;
MYSQL_RES
*
r
=
mysql_store_result
(
c
);
int
num_fields
=
mysql_num_fields
(
r
);
MYSQL_ROW
*
row
;
while
(
row
=
mysql_fetch_row
(
result
)){
//
int num_fields = mysql_num_fields(r);
MYSQL_ROW
row
;
while
(
(
row
=
mysql_fetch_row
(
r
)
)){
json_val
[
"timestamp"
]
=
row
[
0
];
json_val
[
"value"
]
=
row
[
1
];
json_re
[
"value"
]
+=
json_val
;
}
std
::
cout
<<
json_re
.
dump
()
<<
'\n'
;
mysql_close
(
c
)
;
}
}
return
json_re
;
}
void
connectToDB
(
std
::
string
s
,
std
::
string
prefix
){
MYSQL
*
con
=
mysql_init
(
NULL
);
server
=
s
;
std
::
cout
<<
"Connecting to server "
<<
s
<<
" version: "
<<
mysql_get_client_info
()
<<
'\n'
;
mysql_real_connect
(
con
,
s
.
c_str
(),
"root"
,
"root"
,
"gmsec"
,
0
,
NULL
,
0
);
std
::
string
test
=
"CREATE TABLE "
+
prefix
+
"(Timestamp TEXT, Value TEXT);"
;
std
::
cout
<<
test
<<
'\n'
;
mysql_query
(
con
,
test
.
c_str
());
test
=
"INSERT INTO "
+
prefix
+
" VALUES(2000,9999);"
;
mysql_query
(
con
,
test
.
c_str
());
mysql_free_result
(
res
);
mysql_close
(
con
);
return
;
}
...
...
gm_telemetry_server.cpp
View file @
d1173d94
...
...
@@ -347,6 +347,9 @@ class telemetry_server {
//remove whitespace
val
.
erase
(
remove_if
(
val
.
begin
(),
val
.
end
(),
isspace
),
val
.
end
());
nlohmann
::
json
re
;
re
[
"type"
]
=
"history"
;
re
[
"id"
]
=
val
;
re
[
"value"
]
=
systemdata
.
getHistory
(
val
);
return
re
;
}
...
...
@@ -471,16 +474,13 @@ int main(int argc, char* argv[])
port
=
example
::
get
(
config
,
"port"
,
8081
);
//set sql server
SQL_Server
=
example
::
get
(
config
,
"SQL_Server"
,
"192.168.1.4"
);
nav_sys
.
connectToDB
(
SQL_Server
,
"N"
);
orb_sys
.
connectToDB
(
SQL_Server
,
"O"
);
veh_sys
.
connectToDB
(
SQL_Server
,
"V"
);
systemdata
.
connectToDB
(
SQL_Server
,
"Kerbal"
);
//Threads
std
::
thread
gmsec
(
&
gmsub
::
run
,
&
g
);
std
::
this_thread
::
sleep_for
(
std
::
chrono
::
seconds
(
5
));
std
::
thread
websocket
(
&
telemetry_server
::
run
,
&
s
,
docroot
,
port
);
int
counter
=
0
;
while
(
true
){
std
::
cout
<<
"Still running..."
<<
counter
<<
'\n'
;
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment