0
点赞
收藏
分享

微信扫一扫

Qt实现发电机功率数据读取计算并存储

诗远 2022-02-28 阅读 10
  • 版本qt4
  • Power_Get_Data.pro
  • dbfunction.h
  • dbfunction.cpp
  • main.cpp

实现功能:读取达梦数据库中发电机组有功功率 ,计算发电机组深调开始时间,深调结束时间,最低出力时间,以及发电机组最低出力,深调开始时输出功率,深调结束时输出功率,在达梦数据库中建表,将计算得到的数据存到达梦数据库中。

Power_Get_Data.pro

QT       += core gui sql

TARGET = PowerGetData_test
TEMPLATE = app


DESTDIR =/home/d5000/test/bin

SOURCES += main.cpp\
    dbfunction.cpp

HEADERS  += \
    dbfunction.h

FORMS    +=

INCLUDEPATH += /usr/local/include \
                $(HOME)/src/include \
                $(HOME)/src/inc \
                $(HOME)/db_def \
                $(HOME)/src/advanced/wams/include \             #
                $(HOME)/src/advanced/wams/wams_lib/inc \        #
                $(HOME)/src/inc_whdb/inc \
                $$PWD/ppm_gen_excit \
#                $$PWD/../libwhdb_napi \
                $$PWD/../communal_file


LIBS += -L$(HOME)/lib  -lcrd_server -lwrdb_api_v2
LIBS += -L$(HOME)/lib -ldcisg -lservices -lservice_client -lrtdb_api

dbfunction.h

#ifndef DBFUNCTION_H
#define DBFUNCTION_H
#include "qstring.h"
#include "qmap.h"
#include "dcisg.h"
#include "dcidefine.h"
#include <QDebug>
#include <QDateTime>
#include <QTime>


typedef struct
{
    QString stname;  // 厂站中文名
    QString devname; // 发电机组中文名

    int p_pmuid; //有功功率地址
    int f_pmuid; //频率地址

    double ed_power;  // 额定有功功率
    long biaoshi;  // 额定有功功率

}MACHINEPARAID;


typedef struct
{
    int i_LcFlag;    // 励磁接反
    float ed_mvol;   // 额定机端电压
    float ed_lvol;   // 额定励磁电压
    float ed_jvol;   // 基准励磁电压
    float ed_flow;   // 额定励磁电流
    float ed_power;  // 额定有功功率
    float ed_fre;    // 额定频率
    float ed_siqu;   // 一次调频死区
    float ed_zhuan;  // 机组速度变动率

    float f_initup_mvol;    // 机端电压初始值上限
    float f_initdown_mvol;  // 机端电压初始值下限
    float f_initup_lvol;    // 励磁电压初始值上限
    float f_initdown_lvol;  // 励磁电压初始值下限
    float f_initup2_lvol;   // 励磁电压初始值上上限
    float f_initdown2_lvol; // 励磁电压初始值下下限
    float f_initup_fre;     // 频率初始值上限
    float f_initdown_fre;   // 频率初始值下限
    float f_steadyup_fre;   // 频率稳态上限
    float f_steadydown_fre; // 频率稳态下限
    float f_change_power;   // 功率变化值
    int i_beginvalue;       // 扰动开始缓存点数
    int i_endvalue;         // 扰动结束缓存点数
    float f_percent;        // 当前波动幅值倍数
    float f_lcpercent;      // 励磁电压当前波动幅值倍数
    int i_changecount;      // 励磁扰动开始点数
    int i_govchangecount;   // 调速器扰动开始点数
    int i_closecount;       // 判断开机停机点数
    int i_lcclosecount;     // 判断停止检测励磁点数
}MACHINEPARA;


class dbfunction
{

public:
    dbfunction();
    ~dbfunction();


    // 连接数据库
    bool ConnToDB();
    //
    bool ExecSingleDB(QString strSql);

    // 读取发电机参数表
    void SelectMacPara(QMap<QString,MACHINEPARA>& mac_para);

    void SelectMacParadci(QVector<MACHINEPARAID>& mac_para);

    QMap<QString,MACHINEPARAID> map_macparaid;

    CDci g_CDci;
    ErrorInfo error;
    ColAttr* attrs;
    char* buf;
    char ***data;
    int *c1;
    int *c2;
    int *c3;
};

#endif // DBFUNCTION_H

dbfunction.cpp

#include "dbfunction.h"

dbfunction::dbfunction()
{
    attrs = NULL;
    buf = NULL;
    data = NULL;
    c1 = NULL;
    c2 = NULL;
    c3 = NULL;
}

dbfunction::~dbfunction()
{
    delete attrs;
    delete buf;
    delete c1;
    delete c2;
    delete c3;
    attrs = NULL;
    buf = NULL;
    c1 = NULL;
    c2 = NULL;
    c3 = NULL;
    bool ret_code = g_CDci.DisConnect(&error);
    if(!ret_code)
        qDebug() << "dci disconnent failed " << error.error_no << error.error_info;
}

bool dbfunction::ConnToDB()
{
    //bool ret_code = g_CDci.Connect("DPMS","PM","pm.2015",error);
    bool ret_code = g_CDci.Connect("192.168.1.5","SYSDBA","SYSDBA",error);      // XB
    if(!ret_code)
    {
        qDebug() << "dci connect failed!";
    }
    return ret_code;
}
bool dbfunction::ExecSingleDB(QString strSql)
{
    qDebug()<<strSql;
    if(!g_CDci.IsConnected())
    {
        g_CDci.DisConnect(&error);
        this->ConnToDB();
    }
    bool ret_code = g_CDci.ExecSingle(strSql.toLocal8Bit().data(), &error);
    return ret_code;
}
//机组配置参数表
void dbfunction::SelectMacParadci(QVector<MACHINEPARAID> &mac_para)
{
    int row = 0, col = 0;
    ColAttr* attrs = NULL;
    char* buf = NULL;

    // 机组配置参数表
    QString str_sql = "select 厂站中文名,发电机组中文名,有功功率地址,";
    str_sql += "频率地址,额定有功功率 from DPMS.DLSOFTSA.XB_机组配置参数表 where 有功功率地址!=0";
    qDebug() << str_sql;

    bool ret_code = g_CDci.ReadData(str_sql.toLocal8Bit().data(), &row, &col, &attrs, &buf, &error);
    qDebug() << ret_code << row << col;
    if(ret_code != true)
    {
        qDebug() << error.error_no << " " << error.error_info;
        return;
    }
    char ***data = NULL;
    data = g_CDci.ParseResultsForReadData(row,col,attrs,buf);
    if(data)
    {
        for(int i = 0; i < row; ++i)
        {
            MACHINEPARAID para;

            para.stname = QString(data[i][0]); //厂站中文名
            para.devname= QString(data[i][1]);//发电机组中文名
            para.p_pmuid = *((int*)data[i][2]);    // 有功功率地址
            para.f_pmuid = *((int*)data[i][3]);    // 频率地址
            para.ed_power = *((double*)data[i][4]);;   // 额定有功功率

            //qDebug() <<para.stname<<para.devname<<para.p_pmuid <<para.f_pmuid << para.ed_power;
            mac_para.append(para);
            map_macparaid[para.devname] = para;

        }
    }
    g_CDci.FreeSpaceForReadData(row,data);

}

main.cpp

#include <QCoreApplication>
#include <QDateTime>
#include <QDir>
#include <QFile>
#include <QMutex>
#include <QtDebug>
#include <QtGlobal>
#include <QTextCodec>
#include "dbfunction.h"

#include <pthread.h>
#include <signal.h>


#include <cstdlib>
#include <vector>
#include <string>
#include <map>
#include <pthread.h>
#include <signal.h>

#include <cstdlib>
#include <vector>
#include <string>
#include <map>

#include <wrdb_api.h>
#include <sam_service.h>
#include <dcisg.h>
#include <system.h>
#include <pub_ctx.h>
#include "qvector.h"

#include <db_api/odb_tableop.h>
#include <db_api/odb_tablenet.h>
#include <db_api/odb_apiop.h>
#include <db_api/odb_apinet.h>
typedef struct VGlobalVars
{
    QString stname;  // 厂站中文名
    QString devname; // 发电机组中文名
    bool Peak_start;
    bool Insert_table;
    float Peakshaving_Power_Start;
    float Peakshaving_Power_End;
    float Peakshaving_Power_Min;
    int Peakshaving_Time_Start;
    int Peakshaving_Time_End;
    int Peakshaving_Time_Min;
    QVector<WRDB_Value> Peak_Power;
    void globle_init()
    {
        Peak_start=false;
        Insert_table=false;
        Peakshaving_Power_Start =0;
        Peakshaving_Power_End = 0;
        Peakshaving_Power_Min = 0;
        Peakshaving_Time_Start = 0;
        Peakshaving_Time_End = 0;
        Peakshaving_Time_Min = 0;
        Peak_Power.clear();
    }
}VGlobalVars;


#define DATANUM 50
enum { DATA_FRAMES = 50 } ;
void * power_get_data_online(void * arg);

int main(int argc, char *argv[])
{
    //QApplication a(argc, argv);
    //先注册自己的MsgHandler
    //qInstallMsgHandler(outputMessage);

    QCoreApplication a(argc, argv);

    QTextCodec::setCodecForLocale(QTextCodec::codecForName("GB18030"));
    QTextCodec::setCodecForCStrings(QTextCodec::codecForName("GB18030"));
    QTextCodec::setCodecForTr(QTextCodec::codecForName("GB18030"));

    char *cmd = NULL;
    int err = 0;
    pthread_t tid_detet_online;
    pthread_attr_t attr_detet_online;
    err = pthread_attr_init(&attr_detet_online);
    if (err != 0)
    {
        //qCritical("%s pthread_attr_init failed : %s", cmd, strerror(errno));
        exit(EXIT_FAILURE);
    }
    qDebug() << "open pthread";
    err = pthread_create(&tid_detet_online, &attr_detet_online, power_get_data_online, NULL);
    if (err != 0)
    {
        //qCritical("%s pthread_create failed : %d", cmd, err);
        exit(EXIT_FAILURE);
    }

    sleep(5);

    //    if (signal(SIGABRT, exitfunction) < 0)
    //    {
    //        qWarning() << "SIGABRT exit thread";
    //        exit(0);
    //    }
    //    if (signal(SIGTERM, exitfunction) < 0)
    //    {
    //        qWarning() << "SIGTERM exit thread";
    //        exit(0);
    //    }

    return a.exec();
}

void * power_get_data_online(void * arg)
{
    //获取机组配置
    dbfunction dbtool; // 连接数据库
    qDebug() <<"open db";
    if(!dbtool.ConnToDB())
    {
        qDebug() << "ProcGovernor connect dci error!";
        return NULL;
    }

    QVector<MACHINEPARAID> mac_para;
    dbtool.SelectMacParadci(mac_para);

    // 将启动参数传递给Context类,分析启动的态号(默认设置成实时态)
    CContext::SetContextNo(1, NULL, AC_REALTIME_NO);
    WRDB_DESCR descr;
    char hostName[128];
    CServicesManage srvManage;
    //    srvManage.RequestService(AP_WAMS, 1, hostName, AC_REALTIME_NO);
    //    int retCode = WRDB_Open(hostName, &descr, AC_REALTIME_NO);
    //    if(retCode < 0)
    //    {

    //        qDebug() << "ProcGovernor WRDB_Open failed!";
    //        //return NULL;
    //    }

    int oldhour=0;

    float MaxUpvalue =500.04; //深调开始判定值 %50Pn

    float Peakshaving_Power_Start = 0;
    float Peakshaving_Power_End = 0;
    float Peakshaving_Power_Min = MaxUpvalue;
    int Peakshaving_Time_Start = 0;
    int Peakshaving_Time_End = 0;
    int Peakshaving_Time_Min = 0;
    // 读取实时数据
    QVector<WRDB_Value> vec_power_real;//存放有功功率容器
    //QMap<QString,QVector<WRDB_Value> > map_data;
    //vec_power_real= map_data[ "有功功率"];
    //int p_pmuid = mac_para[iAll].p_pmuid;; //有功功率地址
    QString filepath="/home/d5000/test/ppm_xibei/wanghuisan/power1.csv";
    QFile inFile(filepath);  
    if (inFile.open(QIODevice::ReadOnly))
    {
        QTextStream stream_text(&inFile);
        while(!stream_text.atEnd())
        {
            QString values_p = stream_text.readLine();
            QStringList val_p=values_p.split(",");
            //qDebug() <<val_p[0]<<val_p[1]<<val_p[2]<<val_p[3];
//取当前时间
//            QDateTime currenttime = QDateTime::currentDateTime();
//            int values_secs = currenttime.toTime_t();
//            qDebug() <<"currenttime"<<currenttime;
//读表中的时间
            QDate values_ymd = QDate::fromString(val_p[0],"yyyy/MM/dd");   //
            QTime values_hms = QTime::fromString(val_p[1],"h:mm:ss");
            QDateTime values_i = QDateTime(values_ymd,values_hms);
            int values_secs = values_i.toTime_t();
            //qDebug() <<"values_i"<<values_secs <<values_ymd<<values_hms<<values_i;
            WRDB_Value tt;
            tt.time.secs = values_secs;
            int values_usecs = val_p[2].toInt();
            tt.time.usecs=values_usecs;
            float values_f = val_p[3].toFloat();
            tt.value=values_f;
            //qDebug() <<tt.time.secs<<tt.time.usecs ;
            vec_power_real.push_back(tt);
            if (values_p.isEmpty())
            {
                continue;
            }
          }
      }
   //vec_power_real =--5000

    QMap<QString, VGlobalVars*> mapVGlob; // 厂站与全局变量的映射
    int start_index=0;
    //每1秒插值
    while (true)
    {
        sleep(1);
        QDateTime currenttime = QDateTime::currentDateTime();
        int values_secs = currenttime.toTime_t();
        qDebug() <<"currenttime"<<currenttime;
        //qDebug() <<" vec_power_real.size()"<< vec_power_real.size();//5000
        qDebug() <<"mac_para.size()"<<mac_para.size();
        for(int iAll = 0; iAll < mac_para.size(); ++iAll)
        {
            QString devname=mac_para[iAll].devname;
            QString stname=mac_para[iAll].stname;
            VGlobalVars* globalVar = NULL;
            QMap<QString, VGlobalVars*>::iterator itGlobalVar = mapVGlob.find(devname);
            if(itGlobalVar != mapVGlob.end()) // 如果找到了该场站的全局变量
            {
                globalVar = itGlobalVar.value();
            }
            else // 如果没找到,则新建一个,插入到map中
            {
                globalVar = new VGlobalVars;
                globalVar->globle_init();
                mapVGlob[devname] = globalVar;
            }
//            //取值
//            WRDB_Value values_p[secs_xt*DATA_FRAMES];
//            int iOne = 0;
//            //retCode = WRDB_Read(&descr, p_pmuid, beginTime_xt, endTime_xt, values_p, &iOne);
//            vec_power_real.clear();
//            for(int iXT = 0; iXT < secs_xt*DATA_FRAMES; ++iXT)
//            {
//                vec_power_real.push_back(values_p[iXT]);
//            }
            for(int iXT = start_index; iXT < start_index+DATA_FRAMES; ++iXT)
            {
                globalVar->Peak_Power.push_back(vec_power_real[iXT]);
                if(iXT==start_index)                
                    qDebug()<<vec_power_real_usecs<<vec_power_real[iXT].time.usecs<<vec_power_real[iXT].value;
            }
            int Power_size=globalVar->Peak_Power.size();            //控制数据长度 150
            if(globalVar->Peak_Power.size()>3*DATA_FRAMES)
                globalVar->Peak_Power.erase(globalVar->Peak_Power.begin(),globalVar->Peak_Power.begin()+50);
            //qDebug() <<" globalVar->Peak_Power.size()"<< globalVar->Peak_Power.size();
            int IndexCount_Start=0;                                            //深调开始判断次数
            int IndexCount_End=0;                                              //深调结束判断次数
            if(!globalVar->Peak_start)
            {
                for(int iXT = 0; iXT < globalVar->Peak_Power.size(); iXT++)   //判断是否开始深调
                {
                    if(globalVar->Peak_Power[iXT].value<MaxUpvalue)
                    {
                        IndexCount_Start++;   //计数
                        if(IndexCount_Start==150)
                        {                            
                            Peakshaving_Time_Start = values_secs;//globalVar->Peak_Power[1].time.secs;
                            Peakshaving_Power_Start = globalVar->Peak_Power[iXT-149].value;;
                            qDebug() <<" Peakshaving_Time_Start"<< Peakshaving_Time_Start;
                            qDebug() <<" Peakshaving_Power_Start"<<Peakshaving_Power_Start<< globalVar->Peak_Power[iXT].value;
                            IndexCount_Start=0;
                            globalVar->Peak_start=true;
                            break;
                        }
                    }
                }
            }
            if(globalVar->Peak_start)  //深调开始后,判断最低出力和深调结束
            {
                for(int iXT = 0; iXT < globalVar->Peak_Power.size(); iXT++)
                {
                     //qDebug() <<"2";
                    //判断最小出力
                    if(globalVar->Peak_Power[iXT].value<Peakshaving_Power_Min)
                    {
                        Peakshaving_Time_Min =values_secs;//globalVar->Peak_Power[iXT].time.secs;
                        Peakshaving_Power_Min = globalVar->Peak_Power[iXT].value;
                    }

                    //判断深调结束
                    if(globalVar->Peak_Power[iXT].value>MaxUpvalue)
                    {
                        IndexCount_End++;   //计数
                        if(IndexCount_End==150)
                        {
                            qDebug() <<" Peakshaving_Time_Min"<< Peakshaving_Time_Min;
                            qDebug() <<" Peakshaving_Power_Min"<<Peakshaving_Power_Min;
                            Peakshaving_Time_End=values_secs;// globalVar->Peak_Power[iXT-149].time.secs;
                            Peakshaving_Power_End = globalVar->Peak_Power[iXT-149].value;                     
                            qDebug() <<" Peakshaving_Time_End"<< Peakshaving_Time_End;
                            qDebug() <<" Peakshaving_Power_End"<<Peakshaving_Power_End;

                            IndexCount_End=0;
                            globalVar->Peak_start=false;
                            globalVar->Insert_table=true;
                            break;
                        }
                    }
                }
            }

            //是否开始存数据
            if(globalVar->Insert_table &&(Peakshaving_Time_End-Peakshaving_Time_Start>1))
            {
                globalVar->Insert_table=false;
                //insert sql;
                QString strSql="insert into DPMS.DLSOFTSA.Tbl_Gen_Power_Rt_Val values('";
                //厂站名 机组名
                strSql += stname + "','";
                strSql += devname + "','";
                // 时间
                QString Peakshaving_Time_Start_usecs = QDateTime::fromTime_t(Peakshaving_Time_Start).toString("yyyy-MM-dd hh:mm:ss.zzz");
                QString Peakshaving_Time_Min_usecs = QDateTime::fromTime_t(Peakshaving_Time_Min).toString("yyyy-MM-dd hh:mm:ss.zzz");
                QString Peakshaving_Time_End_usecs = QDateTime::fromTime_t(Peakshaving_Time_End).toString("yyyy-MM-dd hh:mm:ss.zzz");
                qDebug() <<" Peakshaving_Time_Start_usecs"<< Peakshaving_Time_Start_usecs;
                qDebug() <<" Peakshaving_Time_Min_usecs"<< Peakshaving_Time_Min_usecs;
                qDebug() <<" Peakshaving_Time_End_usecs"<< Peakshaving_Time_End_usecs;
                strSql += Peakshaving_Time_Start_usecs + "','";
                strSql += Peakshaving_Time_Min_usecs + "','";
                strSql += Peakshaving_Time_End_usecs + "',";

                //strSql += QString::number(Peakshaving_Time_Start_usecs) + ",";
                //strSql += QString::number(Peakshaving_Time_Min_usecs) + ",";
                //strSql += QString::number(Peakshaving_Time_End_usecs) + ",";
                // 功率
                strSql += QString::number(Peakshaving_Power_Start) + ",";
                strSql += QString::number(Peakshaving_Power_Min) + ",";
                strSql += QString::number(Peakshaving_Power_End) + ")";

                //qDebug()<<strSql;
                bool ret_code =dbtool.ExecSingleDB(strSql);
                qDebug()<<ret_code;
            }

        }

        start_index=start_index+DATA_FRAMES;
        if(start_index>vec_power_real.size()-50)
            start_index=0;
     }



 }
举报

相关推荐

大泽动力柴油发电机

0 条评论