コード例 #1
0
/** Get the user password field of the database connection
  *
  * \return The password
  *
  */
std::string RainbruRPG::Server::xmlServerConf::getDbUserPwd(){
  return getDbValue("pwd");
}
コード例 #2
0
/** Get the database name field of the database connection
  *
  * \return The database name
  *
  */
std::string RainbruRPG::Server::xmlServerConf::getDbName(){
  return getDbValue("dbname");
}
コード例 #3
0
/** Get the username field of the database connection
  *
  * \return The user name
  *
  */
std::string RainbruRPG::Server::xmlServerConf::getDbUserName(){
  return getDbValue("user");
}
コード例 #4
0
ファイル: libhagraph_data.c プロジェクト: elmo2k3/libhagraph
int addGraphData(struct _graph_data *graph, int modul, int sensor, int mavg_num)
{
    MYSQL_RES *mysql_res;
    MYSQL_ROW mysql_row;
    MYSQL *mysql_helper_connection;

    PGconn *pgconn = NULL;
    PGresult *pgres;

    char conninfo[300];
    const char timeout = 2;
   
    int db_num;
    char db_host[255];
    char db_user[255];
    char db_password[255];
    char db_database[255];
    char db_database_ws2000[255];
    char db_type[255];
    char db_sslmode[255];
    char *temp_value;

    int i=0,p;
    int num_points;
    double seconds, temperature;
    double moving_average[mavg_num];
    int mavg_pos;
    double mavg_val;
    char query[1024];
    double max = -999.9;
    double min = 999.9;
    long double average = 0.0;
    
    /* allocate new space for _one_graph_data */
    graph->graphs = realloc(graph->graphs, sizeof(struct _one_graph_data)*(graph->num_graphs+1));
    graph->graphs[graph->num_graphs].modul = modul;
    graph->graphs[graph->num_graphs].sensor = sensor;

    db_num = getDbNum(modul,sensor);
    temp_value = getDbValue("host",db_num,modul,sensor);
    if(temp_value)
        strncpy(db_host,temp_value,sizeof(db_host));
    else
        db_host[0] = '\0';
    temp_value = getDbValue("user",db_num,modul,sensor);
    if(temp_value)
        strncpy(db_user,temp_value,sizeof(db_user));
    else
        db_user[0] = '\0';
    temp_value = getDbValue("pass",db_num,modul,sensor);
    if(temp_value)
        strncpy(db_password,temp_value,sizeof(db_password));
    else
        db_password[0] = '\0';
    temp_value = getDbValue("db",db_num,modul,sensor);
    if(temp_value)
        strncpy(db_database,temp_value,sizeof(db_database));
    else
        db_database[0] = '\0';
    temp_value = getDbValue("db_ws2000",db_num,modul,sensor);
    if(temp_value)
        strncpy(db_database_ws2000,temp_value,sizeof(db_database_ws2000));
    else
        db_database_ws2000[0] = '\0';
    temp_value = getDbValue("type",db_num,modul,sensor);
    if(temp_value)
        strncpy(db_type,temp_value,sizeof(db_type));
    else
        db_type[0] = '\0';
    temp_value = getDbValue("sslmode",db_num,modul,sensor);
    if(temp_value)
        strncpy(db_sslmode,temp_value,sizeof(db_sslmode));
    else
        db_sslmode[0] = '\0';
    
    if(strcmp(db_type,"psql") == 0)
    {
        if(strlen(db_password))
        {
            sprintf(conninfo,"dbname = %s host = %s user = %s sslmode = %s password= %s",
                db_database,
                db_host,
                db_user,
                db_sslmode,
                db_password);
        }
        else // no password
        {
            sprintf(conninfo,"dbname = %s host = %s user = %s sslmode = %s",
                db_database,
                db_host,
                db_user,
                db_sslmode);
        }

        pgconn = PQconnectdb(conninfo);
        if (PQstatus(pgconn) != CONNECTION_OK)
        {
            fprintf(stderr, "Connection to database failed: %s",
                    PQerrorMessage(pgconn));
            PQfinish(pgconn);
            return -1;
        }

        sprintf(query,"SELECT EXTRACT(EPOCH FROM date),\
            value*1000 \
            FROM modul_%02d%02d \
            WHERE date>to_timestamp(%ld)\
            AND date<to_timestamp(%ld)\
            ORDER BY date asc", modul, sensor, graph->timestamp_from, graph->timestamp_to);
//        sprintf(query,"SELECT EXTRACT(EPOCH FROM date),\
//            value*1000 \
//            FROM \"Bochum Aussen Taupunkt\" \
//            WHERE date>to_timestamp(%ld)\
//            AND date<to_timestamp(%ld)\
//            ORDER BY date asc", graph->timestamp_from, graph->timestamp_to);

        pgres = PQexec(pgconn, query);
        if (PQresultStatus(pgres) != PGRES_TUPLES_OK)
        {
            fprintf(stderr, "failed: %s", PQerrorMessage(pgconn));
            PQclear(pgres);
            PQfinish(pgconn);
            return -1;
        }

        num_points = PQntuples(pgres);
        if(num_points == 0)
        {
            PQclear(pgres);
            PQfinish(pgconn);
            return -2;
        }
        graph->graphs[graph->num_graphs].num_points = num_points;
        graph->graphs[graph->num_graphs].points = malloc(sizeof(struct _graph_point)*num_points);

        struct _graph_point *helper = graph->graphs[graph->num_graphs].points;
        
        mavg_pos = -1;
        for (i = 0; i < PQntuples(pgres); i++)
        {
            seconds = (long long)atoi(PQgetvalue(pgres,i,0));
            temperature = (double)atoi(PQgetvalue(pgres,i,1))/1000;
            if(mavg_pos == -1)
            {
                for(p=0;p<mavg_num;p++)
                {
                    moving_average[p] = temperature;
                }
                mavg_pos = 0;
            }
            moving_average[mavg_pos] = temperature;
            if(++mavg_pos == mavg_num)
                mavg_pos = 0;
            mavg_val = 0.0;
            for(p=0;p<mavg_num;p++)
            {
                mavg_val += moving_average[p];
            }
            helper[i].x = seconds;
            helper[i].y = mavg_val / mavg_num;
            //helper[i].y = temperature;
            average += temperature;
            if(temperature > max)
                max = temperature;
            if(temperature < min)
                min = temperature;
        }
        PQclear(pgres);
        PQfinish(pgconn);

        if(min < graph->min)
            graph->min = floor(min/10.0)*10;
        if(max > graph->max)
            graph->max = ceil(max/10.0)*10; 
        
        graph->graphs[graph->num_graphs].min = min;
        graph->graphs[graph->num_graphs].max = max;
        graph->graphs[graph->num_graphs].average = average/num_points;
        graph->num_graphs++;

    return 0;

    }
    else // mysql
    {