Commit ceaab152 authored by Christopher Reis's avatar Christopher Reis

Added MySQL database

parent dc197c58
...@@ -10,8 +10,8 @@ TARGET = gm_telemetry_server ...@@ -10,8 +10,8 @@ TARGET = gm_telemetry_server
include ../AppMake.mf include ../AppMake.mf
INCS += -I ./websocketpp INCS += -I ./websocketpp -I/user/include/mysql
LDFLAGS += -lboost_system LDFLAGS += -lboost_system -lmysqlclient -lz
CXXFLAGS += -std=c++11 CXXFLAGS += -std=c++11
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
#include <fstream> #include <fstream>
#include <string> #include <string>
#include <chrono> #include <chrono>
#include <mysql/mysql.h>
#include <boost/any.hpp> #include <boost/any.hpp>
...@@ -35,12 +36,17 @@ struct measurement { ...@@ -35,12 +36,17 @@ struct measurement {
struct subsystem { struct subsystem {
std::string name; std::string name;
std::vector<measurement> v; std::vector<measurement> v;
MYSQL *con=mysql_init(NULL);
MYSQL_RES *res;
void updateMeasurement(std::string m, std::string value, unsigned long t){ void updateMeasurement(std::string m, std::string value, unsigned long t){
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){ for(std::vector<measurement>::iterator it = v.begin();it != v.end();++it){
if(it->name==m){ if(it->name==m){
it->v = value; it->v = value;
it->timestamp = t; it->timestamp = t;
std::string insert = "INSERT INTO " + m + " VALUES(" + value + "," + std::to_string(t) +");";
mysql_query(con,insert.c_str());
return; return;
} }
} }
...@@ -49,6 +55,26 @@ struct subsystem { ...@@ -49,6 +55,26 @@ struct subsystem {
meas.v = value; meas.v = value;
meas.timestamp = t; meas.timestamp = t;
v.push_back(meas); 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';
str = "INSERT INTO " + m + " VALUES(" + value + "," + std::to_string(t) +");";
mysql_query(con,str.c_str());
std::cout << mysql_error(con) << '\n';
return;
}
void connectToDB(std::string s,std::string prefix){
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);";
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; return;
} }
......
...@@ -61,6 +61,7 @@ std::mutex systemdata_mutex; ...@@ -61,6 +61,7 @@ std::mutex systemdata_mutex;
std::vector<std::time_t> time_vector; std::vector<std::time_t> time_vector;
std::mutex sys_mutex; std::mutex sys_mutex;
nlohmann::json j; nlohmann::json j;
std::string SQL_Server;
class gmsub class gmsub
{ {
...@@ -467,6 +468,12 @@ int main(int argc, char* argv[]) ...@@ -467,6 +468,12 @@ int main(int argc, char* argv[])
//load dictionary //load dictionary
std::string filename = example::get(config, "dictionary", "/GMSEC_API/bin/dictionary.json"); std::string filename = example::get(config, "dictionary", "/GMSEC_API/bin/dictionary.json");
dictionary = load_dictionary(filename); dictionary = load_dictionary(filename);
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");
//Threads //Threads
std::thread gmsec(&gmsub::run,&g); std::thread gmsec(&gmsub::run,&g);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment