美文网首页
Qt的QThread,Redis客户端的使用

Qt的QThread,Redis客户端的使用

作者: luckybabying | 来源:发表于2020-04-26 16:42 被阅读0次

    项目简介

    我自己做了一个项目,项目重要是通过软件自动向redis数据库自动写入数据,涉及知识点:
    1.hiredis库添加,建立redis客户端,通过redis常用命令给服务器端置数。
    2.QThread 的moveToThread:主要是将耗时的工作在该类的槽函数实现。
    比如说,while 或者for,操作数据量比较大的时候,这个线程还是比较好用的。防止Qt界面阻塞,无法操作,假死现象的产生。
    这个函数使用 qApp->processEvents()效果好,这两个函数我都在这个项目中实验了。

    image.png

    QThread 基本知识

    1.QThread 是用来管理线程的,它所依附的线程和它管理的线程并不是同一个东西
    2.QThread 所依附的线程,就是执行 QThread t(0) 或 QThread * t=new QThread(0) 的线程。也就是咱们这儿的主线程
    3.QThread 管理的线程,就是 run 启动的线程。也就是次线程
    4.因为QThread的对象依附在主线程中,所有他的slot函数会在主线程执行,而不是此线程。除非:
    1)QThread对象依附到次线程中(通过movetoThread)
    2)slot和信号直接连接,且信号在此线程中发射。

    Qt 有两种多线程的方法:
    第一种:继承QThread的run函数
    第二种:把继承于QObject的类用moveToThread函数转移到一个Thead里。
    在Qt4.8之后,建议用第二种方法。

    这篇文章主要使用第二种发方法。

    项目源代码

    Qt 多线程使用moveToThread

    #ifndef DATASIMULATION_H
    #define DATASIMULATION_H
    
    #include <QDialog>
    #include <QtXml/QtXml>
    #include <QDialog>
    #include <QTreeWidgetItem>
    #include <dialogs/hiredis/hiredis.h>
    #include <QButtonGroup>
    #include <QThread>
    #include <QString>
    #include <QObject>
    
    namespace Ui {
    class DataSimulation;
    }
    
    
    class Worker:public QObject
    {
        Q_OBJECT
    public:
        explicit Worker(QObject *parent=0);
        ~Worker();
    
    signals:
        void sig_finish();
    
    public slots:
        void slot_dowork();
        void updataYc_Value(QString value_str);
        void updataDd_Value(QString value_str);
        void update();
        void updateComm();
    
    
    public:
    
        QString redisIPstr ;
        int port;
        bool is_connectRedis ;
        redisContext *ctx;
    
        bool isAutoYx;
        bool isSOE;
        bool isAutoComm ;
        bool isAutoYc ;
        bool isAutoDd ;
    
        QMap<QString,QString> mapChecked_YX;
        QMap<QString,QString> mapChecked_YC;
        QMap<QString,QString> mapChecked_DD;
        QMap<QString,QString> mapChecked_COMM;
    
    };
    class DataSimulation : public QDialog
    {
        Q_OBJECT
    
    public:
        explicit DataSimulation(QWidget *parent = nullptr);
    
        void closeEvent(QCloseEvent *event);
    
        bool readXml(QString path);
        void treeWidgetExpandAll();
    
        void readBookindexElement();
        void readEntryElement(QTreeWidgetItem *parent,QString name);
        void readPageElement(QTreeWidgetItem *parent);
        void skipUnknownElement();
        QTreeWidgetItem*  addTreeWight(QTreeWidgetItem *parentm,QString name ,bool setdata = false);
    
        void updateParentItem(QTreeWidgetItem* item);   //更新树形控件的子选项
    
        bool testConnection(QString IP);
        bool connectRedis();
    
        QTreeWidget *treeWidget;
        QXmlStreamReader reader;
        int num;
    
    
        void UpdateParent(QTreeWidgetItem *itm,QString name );
        void SetParentPartiallyChecked(QTreeWidgetItem *itm);
        void UpdateChild(QTreeWidgetItem *itm);
    
        void updataYc_Value(QString value_str);
        void updataDd_Value(QString value_str);
        void dowork();
    
        ~DataSimulation();
    
    signals:
        void sig_dowork();
        void sig_doworkYc(QString value_str);
        void sig_doworkDd(QString value_str);
        void sig_doworkComm();
    
    public slots:   //申明信号与槽,当树形控件的子选项被改变时执行
        void onTreeItemChanged(QTreeWidgetItem* item, int column);
        void onTreeItemChanged_YC(QTreeWidgetItem* item, int column);
        void onTreeItemChanged_DD(QTreeWidgetItem* item, int column);
        void onTreeItemChanged_COMM(QTreeWidgetItem* item, int column);
        void onTreeItemChanged_YK(QTreeWidgetItem* item, int column);
        void onStateChanged(int);
    
        void on_pushButton_2_clicked();
    
        void slot_finish();
    
    private slots:
        void on_pushButton_Yc_clicked();
    
        void on_pushButton_Dd_clicked();
    
        void on_pushButton_COMM_clicked();
    
        void on_pushButton_yx0_clicked();
    
        void on_pushButton_COMM0_clicked();
    
        void on_pushButton_yx_auto_clicked();
    
        void update();
        void updateComm();
        void updateYc();
        void updateDd();
    
        void on_pushButton_yx_auto_close_clicked();
    
        void on_pushButton_comm_auto_start_clicked();
    
        void on_pushButton_comm_auto_stop_clicked();
    
        void operatingModeButtonsToggledYc(int,bool);
        void operatingModeButtonsToggledDd(int,bool);
        void operatingModeButtonsToggledYkSelect(int,bool);
        void operatingModeButtonsToggledYkExecute(int,bool);
        void operatingModeButtonsToggledYkRevocation(int,bool);
    
    
    
        void on_lineEdit_yc_fixValue_textChanged(const QString &arg1);
    
        void on_lineEdit_yc__MinValue_textChanged(const QString &arg1);
    
        void on_lineEdit_yc__MaxValue_textChanged(const QString &arg1);
    
        void on_lineEdit_yc_step_text_textChanged(const QString &arg1);
    
        void on_lineEdit_yc_time_textChanged(const QString &arg1);
    
        void on_lineEdit_dd_fixValue_textChanged(const QString &arg1);
    
        void on_lineEdit_dd__MinValue_textChanged(const QString &arg1);
    
        void on_lineEdit_dd__MaxValue_textChanged(const QString &arg1);
    
        void on_lineEdit_dd_time_textChanged(const QString &arg1);
    
        void on_pushButton_yc_auto_start_clicked();
    
        void on_pushButton_yc_auto_stop_clicked();
    
        void on_pushButton_dd_auto_start_clicked();
    
        void on_pushButton_dd_auto_stop_clicked();
    
        void eventTimeoutSlotSimulation();
    
        void on_pushButton_redisIp_clicked();
    
        void on_lineEdit_RedisIP_textChanged(const QString &arg1);
    
    private:
        Ui::DataSimulation *ui;
    
        Worker *m_pworker;
        QThread *m_pthread;
    
        QString redisIPstr ;
        int port;
        bool is_connectRedis ;
        redisContext *ctx;
        redisContext *eventCtx;
        redisContext *eventCtxSend;
    
    
    
        QStringList lst ;
    
        QMap<QString,QString> mapChecked_YX;
        QMap<QString,QString> mapChecked_YC;
        QMap<QString,QString> mapChecked_DD;
        QMap<QString,QString> mapChecked_COMM;
        QMap<QString,QString> mapChecked_YK;
        QMap<QString,QString> mapReceYkInfo;
    
        QButtonGroup *operatingModeGroupYc;
        QButtonGroup *operatingModeGroupDd;
    
    
        QButtonGroup *operatingModeGroupYkSelect;
        QButtonGroup *operatingModeGroupYkExecute;
        QButtonGroup *operatingModeGroupYkRevocation;
    
    
    
        QTimer *timer;
        QTimer *timerComm;
        QTimer *timerYc;
        QTimer *timerDd;
    
        bool isAutoYx;
        bool isSOE;
        bool isAutoComm ;
        bool isAutoYc ;
        bool isAutoDd ;
        int radioBtn_id_yc;
        int radioBtn_id_dd;
    
        int radioBtn_id_yk_select;
        int radioBtn_id_yk_execute;
        int radioBtn_id_yk_revocation;
    
        QString yc_fixValue;
        QString dd_fixValue;
        float yc_MinValue;
        float yc_MaxValue;
        float yc_StepValue;
    
        float sum_yc;
        float sub_yc;
    
        float dd_MinValue;
        float dd_MaxValue;
        float dd_StepValue;
    
        float sum_dd;
        float sub_dd;
        bool is_connet;
        QTimer *eventTimer;
    
        bool is_overYx;
    
    
    };
    
    
    
    #endif // DATASIMULATION_H
    
    
    #include "datasimulation.h"
    #include "ui_datasimulation.h"
    #include <QFileDialog>
    #include <QMessageBox>
    #include <QVBoxLayout>
    #include <QTcpSocket>
    #include <QMap>
    #include <QList>
    
    #include <QCloseEvent>
    
    
    Worker::Worker(QObject *parent):QObject(parent)
    {
        isAutoYx = false;
        isSOE = false;
        isAutoComm = false;
        isAutoYc = false;
        isAutoDd = false;
        qDebug()<<"worker()";
    }
    Worker::~Worker()
    {
        qDebug()<<"~worker()";
    }
    void Worker::slot_dowork()
    {
        qDebug()<< "do work,thread id = " << QThread::currentThreadId();
        emit sig_finish();
    }
    
    void Worker::update()
    {
        qDebug()<< "update  do work,thread id = " << QThread::currentThreadId();
        if(isAutoYx)
        {
            QMap<QString,QString>::Iterator it =mapChecked_YX.begin();
    
            while(it!=mapChecked_YX.end())
            {
                //            qApp->processEvents();
                QString redisName,keyName,valueName;
                QStringList listtemp = it.key().split("&");
                if(listtemp.size()==2)
                {
                    redisName = listtemp[0];
                    keyName = listtemp[1];
                    valueName = "1";
    
    
                    if(!valueName.isEmpty())
                    {
                        QString str = QString("HSET %1 %2 %3").arg(redisName).
                                arg(keyName).arg(valueName);
    
                        redisReply *reply;
                        reply = (redisReply*)redisCommand(ctx,str.toStdString().c_str());
                        if(reply==NULL||reply->type==REDIS_REPLY_ERROR)
                        {
                            //                    QMessageBox::critical(this,tr("warning"),tr("Set rtdb error"),QMessageBox::Ok,QMessageBox::NoButton);
    
                        }
    
                        if(isSOE)
                        {
                            QDateTime time = QDateTime::currentDateTime();   //获取当前时间
                            int timeT = time.toTime_t();   //将当前时间转为时间戳
                            //QString strSOE = "PUBLISH SOE CAN01$yx1;1587429797$00;255";
                            QString strSOE = QString("PUBLISH SOE %1$%2;%3$00;255").arg(redisName).
                                    arg(keyName).arg(timeT);
    
    
                            reply = (redisReply*)redisCommand(ctx,strSOE.toStdString().c_str());
                            if(reply==NULL||reply->type==REDIS_REPLY_ERROR)
                            {
                                //                        QMessageBox::critical(this,tr("warning"),tr("Set rtdb error"),QMessageBox::Ok,QMessageBox::NoButton);
    
                            }
                        }
                        if(!reply)
                            freeReplyObject(reply);
                    }
                }
    
                it++;
            }
            isAutoYx = false;
        }
        else
        {
            QMap<QString,QString>::Iterator it =mapChecked_YX.begin();
            while(it!=mapChecked_YX.end())
            {
                //            qApp->processEvents();
                QString redisName,keyName,valueName;
                QStringList listtemp = it.key().split("&");
                redisName = listtemp[0];
                keyName = listtemp[1];
                valueName = "0";
                if(!valueName.isEmpty())
                {
                    QString str = QString("HSET %1 %2 %3").arg(redisName).
                            arg(keyName).arg(valueName);
    
                    redisReply *reply;
                    reply = (redisReply*)redisCommand(ctx,str.toStdString().c_str());
                    if(reply==NULL||reply->type==REDIS_REPLY_ERROR)
                    {
                        //                    QMessageBox::critical(this,tr("warning"),tr("Set rtdb error"),QMessageBox::Ok,QMessageBox::NoButton);
    
                    }
    
    
                    if(isSOE)
                    {
                        QDateTime time = QDateTime::currentDateTime();   //获取当前时间
                        int timeT = time.toTime_t();   //将当前时间转为时间戳
                        //QString strSOE = "PUBLISH SOE CAN01$yx1;1587429797$00;0";
                        QString strSOE = QString("PUBLISH SOE %1$%2;%3$00;0").arg(redisName).
                                arg(keyName).arg(timeT);
    
    
                        reply = (redisReply*)redisCommand(ctx,strSOE.toStdString().c_str());
                        if(reply==NULL||reply->type==REDIS_REPLY_ERROR)
                        {
                            //                        QMessageBox::critical(this,tr("warning"),tr("Set rtdb error"),QMessageBox::Ok,QMessageBox::NoButton);
    
                        }
                    }
                    if(!reply)
                        freeReplyObject(reply);
                }
                it++;
            }
            isAutoYx = true;
        }
    
        emit sig_finish();
    
    }
    
    
    void Worker::updateComm()
    {
        qDebug()<< "update  do work,thread id = " << QThread::currentThreadId();
        if(isAutoComm)
        {
            QMap<QString,QString>::Iterator it =mapChecked_COMM.begin();
    
            while(it!=mapChecked_COMM.end())
            {
                //            qApp->processEvents();
                QString redisName,keyName,valueName;
                QStringList listtemp = it.key().split("&");
                redisName = listtemp[0];
                keyName = listtemp[1];
                valueName = "255";
    
                if(!valueName.isEmpty())
                {
                    QString str = QString("HSET %1 %2 %3").arg(redisName).
                            arg(keyName).arg(valueName);
    
                    redisReply *reply;
                    reply = (redisReply*)redisCommand(ctx,str.toStdString().c_str());
                    if(reply==NULL||reply->type==REDIS_REPLY_ERROR)
                    {
                        //                    QMessageBox::critical(this,tr("warning"),tr("Set rtdb error"),QMessageBox::Ok,QMessageBox::NoButton);
    
                    }
                    freeReplyObject(reply);
                }
                it++;
            }
            isAutoComm = false;
        }
        else
        {
            QMap<QString,QString>::Iterator it =mapChecked_COMM.begin();
            while(it!=mapChecked_COMM.end())
            {
                //            qApp->processEvents();
                QString redisName,keyName,valueName;
                QStringList listtemp = it.key().split("&");
                redisName = listtemp[0];
                keyName = listtemp[1];
                valueName = "0";
    
                if(!valueName.isEmpty())
                {
                    QString str = QString("HSET %1 %2 %3").arg(redisName).
                            arg(keyName).arg(valueName);
    
                    redisReply *reply;
                    reply = (redisReply*)redisCommand(ctx,str.toStdString().c_str());
                    if(reply==NULL||reply->type==REDIS_REPLY_ERROR)
                    {
                        //                    QMessageBox::critical(this,tr("warning"),tr("Set rtdb error"),QMessageBox::Ok,QMessageBox::NoButton);
    
                    }
                    freeReplyObject(reply);
                }
                it++;
            }
            isAutoComm = true;
        }
    
        emit sig_finish();
    
    }
    void Worker::updataDd_Value(QString value_str)
    {
        qDebug()<< "updataDd_Value do work,thread id = " << QThread::currentThreadId();
        qDebug()<< "updataDd_Value do work,value_str = " << value_str;
        QMap<QString,QString>::Iterator it =mapChecked_DD.begin();
        while(it!=mapChecked_DD.end())
        {
            //        qApp->processEvents();
            QString redisName,keyName,valueName;
            QStringList listtemp = it.key().split("&");
            redisName = listtemp[0];
            keyName = listtemp[1];
            valueName = value_str;
    
            if(!valueName.isEmpty())
            {
                QString str = QString("HSET %1 %2 %3").arg(redisName).
                        arg(keyName).arg(valueName);
    
                redisReply *reply;
                reply = (redisReply*)redisCommand(ctx,str.toStdString().c_str());
                if(reply==NULL||reply->type==REDIS_REPLY_ERROR)
                {
                    //                QMessageBox::critical(this,tr("warning"),tr("Set rtdb error"),QMessageBox::Ok,QMessageBox::NoButton);
    
                }
                freeReplyObject(reply);
            }
            it++;
        }
    }
    
    void Worker::updataYc_Value(QString value_str)
    {
        qDebug()<< "updataYc_Value do work,thread id = " << QThread::currentThreadId();
        qDebug()<< "updataYc_Value do work,value_str = " << value_str;
        QMap<QString,QString>::Iterator it =mapChecked_YC.begin();
        while(it!=mapChecked_YC.end())
        {
            qApp->processEvents();
    
            QString redisName,keyName,valueName;
            QStringList listtemp = it.key().split("&");
            redisName = listtemp[0];
            keyName = listtemp[1];
    
            valueName = value_str;
    
    
            if(!valueName.isEmpty())
            {
                QString str = QString("HSET %1 %2 %3").arg(redisName).
                        arg(keyName).arg(valueName);
    
                redisReply *reply;
                reply = (redisReply*)redisCommand(ctx,str.toStdString().c_str());
                if(reply==NULL||reply->type==REDIS_REPLY_ERROR)
                {
                    //                QMessageBox::critical(this,tr("warning"),tr("Set rtdb error"),QMessageBox::Ok,QMessageBox::NoButton);
    
                }
                freeReplyObject(reply);
            }
            it++;
        }
    
    
    }
    
    DataSimulation::DataSimulation(QWidget *parent) :
        QDialog(parent),
        ctx(0),
        eventCtx(0),
        ui(new Ui::DataSimulation)
    {
        ui->setupUi(this);
    
        m_pworker = new Worker();
        m_pthread = new QThread();
        m_pworker->moveToThread(m_pthread);
        qDebug()<< "start,thread id = " << QThread::currentThreadId();
    
        connect(m_pthread, &QThread::finished, m_pworker, &QObject::deleteLater);
        connect(this,SIGNAL(sig_dowork()),m_pworker,SLOT(slot_dowork()));
        connect(this,SIGNAL(sig_dowork()),m_pworker,SLOT(update()));
    
    
    
    
        connect(this,SIGNAL(sig_doworkYc(QString)),m_pworker,SLOT(updataYc_Value(QString)));
        connect(this,SIGNAL(sig_doworkDd(QString)),m_pworker,SLOT(updataDd_Value(QString)));
        connect(this,SIGNAL(sig_doworkComm()),m_pworker,SLOT(updateComm()));
    
        connect(m_pworker,SIGNAL(sig_finish()),this,SLOT(slot_finish()));
    
    
        setWindowFlags(Qt::Dialog | Qt::WindowMaximizeButtonHint |Qt::WindowMinimizeButtonHint | Qt::WindowCloseButtonHint);
        setWindowTitle(tr("DataSimulation"));
        QStringList headers;
        headers <<tr("Type")  <<tr("State");
        QStringList headers_INT;
        headers_INT << tr("Type") ;
        ui->treeWidget->setHeaderLabels(headers_INT);
        ui->treeWidget_YC->setHeaderLabels(headers);
        ui->treeWidget_DD->setHeaderLabels(headers);
        ui->treeWidget_COMM->setHeaderLabels(headers_INT);
        ui->treeWidget_YK->setHeaderLabels(headers);
    
        ui->treeWidget->setColumnWidth(0,200);
        ui->treeWidget_YC->setColumnWidth(0,180);
        ui->treeWidget_YC->setColumnWidth(1,100);
        ui->treeWidget_DD->setColumnWidth(0,180);
        ui->treeWidget_DD->setColumnWidth(1,100);
        ui->treeWidget_COMM->setColumnWidth(0,200);
        ui->treeWidget_YK->setColumnWidth(0,120);
        ui->treeWidget_YK->setColumnWidth(1,180);
    
    
        //      ui->treeWidget->header()->setHidden(true);
    
        //      ui->treeWidget->header()->setHorizontalScrollMode(QAbstractItemView::ScrollPerPixel);
    
        //      ui->treeWidget->header()->setResizeMode(0, QHeaderView::ResizeToContents);
    
        //      ui->treeWidget->header()->setStretchLastSection(false);
    
        //      ui->treeWidget->setTextElideMode(Qt::ElideNone);
    
        //      ui->treeWidget_YC->header()->setHidden(true);
    
        //      ui->treeWidget_YC->header()->setHorizontalScrollMode(QAbstractItemView::ScrollPerPixel);
    
        //      ui->treeWidget_YC->header()->setResizeMode(0, QHeaderView::ResizeToContents);
    
        //      ui->treeWidget_YC->header()->setStretchLastSection(false);
    
        //      ui->treeWidget_YC->setTextElideMode(Qt::ElideNone);
    
        //      ui->treeWidget_DD->header()->setHidden(true);
    
        //      ui->treeWidget_DD->header()->setHorizontalScrollMode(QAbstractItemView::ScrollPerPixel);
    
        //      ui->treeWidget_DD->header()->setResizeMode(0, QHeaderView::ResizeToContents);
    
        //      ui->treeWidget_DD->header()->setStretchLastSection(false);
    
        //      ui->treeWidget_DD->setTextElideMode(Qt::ElideNone);
    
        //      ui->treeWidget_COMM->header()->setHidden(true);
    
        //      ui->treeWidget_COMM->header()->setHorizontalScrollMode(QAbstractItemView::ScrollPerPixel);
    
        //      ui->treeWidget_COMM->header()->setResizeMode(0, QHeaderView::ResizeToContents);
    
        //      ui->treeWidget_COMM->header()->setStretchLastSection(false);
    
        //      ui->treeWidget_COMM->setTextElideMode(Qt::ElideNone);
    
        //    setCentralWidget(treeWidget);
    
        //设置treewidget双击可编辑
        ui->treeWidget->setEditTriggers(QAbstractItemView::DoubleClicked);
        ui->treeWidget_YC->setEditTriggers(QAbstractItemView::DoubleClicked);
        ui->treeWidget_DD->setEditTriggers(QAbstractItemView::DoubleClicked);
        ui->treeWidget_COMM->setEditTriggers(QAbstractItemView::DoubleClicked);
        ui->treeWidget_YK->setEditTriggers(QAbstractItemView::DoubleClicked);
    
        connect( ui->treeWidget, SIGNAL(itemChanged(QTreeWidgetItem*, int)), this, SLOT(onTreeItemChanged(QTreeWidgetItem*, int)));
        connect( ui->treeWidget_YC, SIGNAL(itemChanged(QTreeWidgetItem*, int)), this, SLOT(onTreeItemChanged_YC(QTreeWidgetItem*, int)));
        connect( ui->treeWidget_DD, SIGNAL(itemChanged(QTreeWidgetItem*, int)), this, SLOT(onTreeItemChanged_DD(QTreeWidgetItem*, int)));
        connect( ui->treeWidget_COMM, SIGNAL(itemChanged(QTreeWidgetItem*, int)), this, SLOT(onTreeItemChanged_COMM(QTreeWidgetItem*, int)));
        connect( ui->treeWidget_YK, SIGNAL(itemChanged(QTreeWidgetItem*, int)), this, SLOT(onTreeItemChanged_YK(QTreeWidgetItem*, int)));
    
        //是否发送SOE 连接信号槽
        connect(ui->checkBox_soe, SIGNAL(stateChanged(int)), this, SLOT(onStateChanged(int)));
    
    
        //遥信组框布局
        QVBoxLayout *pVLayout_yx = new QVBoxLayout();
        QHBoxLayout *pHLayout_yx = new QHBoxLayout();
        QHBoxLayout *pHLayout_yx_time = new QHBoxLayout();
        pHLayout_yx_time->addWidget(ui->checkBox_soe);
        pHLayout_yx_time->addWidget(ui->label_yx_time_text);
        pHLayout_yx_time->addWidget(ui->lineEdit_yx_time);
        pHLayout_yx_time->addWidget(ui->label_yx_time);
        ui->groupBox_args_yx->setLayout(pHLayout_yx_time);
    
        pVLayout_yx->addWidget(ui->treeWidget);
        pVLayout_yx->addWidget(ui->groupBox_args_yx);
        pHLayout_yx->addWidget(ui->pushButton_2);
        pHLayout_yx->addWidget(ui->pushButton_yx0);
        pHLayout_yx->addWidget(ui->pushButton_yx_auto);
        pHLayout_yx->addWidget(ui->pushButton_yx_auto_close);
        pVLayout_yx->addLayout(pHLayout_yx);
        ui->groupBox_YX->setLayout(pVLayout_yx);
    
    
        //遥测组框布局
        QVBoxLayout *pVLayout_yc = new QVBoxLayout();
        QVBoxLayout *pVLayout_yc_args = new QVBoxLayout();
        QHBoxLayout *pHLayout_yc1= new QHBoxLayout();
        pHLayout_yc1->addWidget(ui->radioButton_yc_TreeValue);
        pHLayout_yc1->addWidget(ui->radioButton_yc_fixValue);
        pHLayout_yc1->addWidget(ui->lineEdit_yc_fixValue);
    
        QHBoxLayout *pHLayout_yc2= new QHBoxLayout();
        pHLayout_yc2->addWidget(ui->label_yc_range);
        pHLayout_yc2->addWidget(ui->lineEdit_yc__MinValue);
        pHLayout_yc2->addWidget(ui->lineEdit_yc__MaxValue);
    
        QHBoxLayout *pHLayout_yc3= new QHBoxLayout();
        pHLayout_yc3->addWidget(ui->label_yc_time_text);
        pHLayout_yc3->addWidget(ui->lineEdit_yc_time);
        pHLayout_yc3->addWidget(ui->label_yc_time);
        pHLayout_yc3->addWidget(ui->label_yc_step_text);
        pHLayout_yc3->addWidget(ui->lineEdit_yc_step_text);
    
        QHBoxLayout *pHLayout_yc4= new QHBoxLayout();
    
        pHLayout_yc4->addWidget(ui->radioButton_increase);
        pHLayout_yc4->addWidget(ui->radioButton_decrease);
        pHLayout_yc4->addWidget(ui->radioButton_sin);
        pHLayout_yc4->addWidget(ui->radioButton_trapezoid);
    
        operatingModeGroupYc = new QButtonGroup(this);
        operatingModeGroupYc->addButton(ui->radioButton_yc_TreeValue,0);
        operatingModeGroupYc->addButton(ui->radioButton_yc_fixValue,1);
        operatingModeGroupYc->addButton(ui->radioButton_increase,2);
        operatingModeGroupYc->addButton(ui->radioButton_decrease,3);
        operatingModeGroupYc->addButton(ui->radioButton_sin,4);
        operatingModeGroupYc->addButton(ui->radioButton_trapezoid,5);
        ui->radioButton_increase->setChecked(true);  // 为operatingModeGroupYc组设置初选项
        connect(operatingModeGroupYc, SIGNAL(buttonToggled(int,bool)), this, SLOT(operatingModeButtonsToggledYc(int,bool)));
    
        pVLayout_yc_args->addLayout(pHLayout_yc1);
        pVLayout_yc_args->addLayout(pHLayout_yc2);
        pVLayout_yc_args->addLayout(pHLayout_yc3);
        pVLayout_yc_args->addLayout(pHLayout_yc4);
        ui->groupBox_args_yc->setLayout(pVLayout_yc_args);
    
        QHBoxLayout *pHLayout_yc_btn = new QHBoxLayout();
        pHLayout_yc_btn->addWidget(ui->pushButton_Yc);
        pHLayout_yc_btn->addWidget(ui->pushButton_yc_auto_start);
        pHLayout_yc_btn->addWidget(ui->pushButton_yc_auto_stop);
        pVLayout_yc->addWidget(ui->treeWidget_YC);
        pVLayout_yc->addWidget(ui->groupBox_args_yc);
        pVLayout_yc->addLayout(pHLayout_yc_btn);
        ui->groupBox_YC->setLayout(pVLayout_yc);
    
    
        //电度组框布局
        operatingModeGroupDd = new QButtonGroup(this);
        operatingModeGroupDd->addButton(ui->radioButton_dd_TreeValue,0);
        operatingModeGroupDd->addButton(ui->radioButton_dd_fixValue,1);
        operatingModeGroupDd->addButton(ui->radioButton_increase_dd,2);
        operatingModeGroupDd->addButton(ui->radioButton_decrease_dd,3);
        ui->radioButton_increase_dd->setChecked(true);  // 为operatingModeGroup组设置初选项
        radioBtn_id_yc = 2;
        radioBtn_id_dd = 2;
        connect(operatingModeGroupDd, SIGNAL(buttonToggled(int,bool)), this, SLOT(operatingModeButtonsToggledDd(int,bool)));
    
        QHBoxLayout *pHLayout_dd1= new QHBoxLayout();
    
        pHLayout_dd1->addWidget(ui->radioButton_dd_TreeValue);
        pHLayout_dd1->addWidget(ui->radioButton_dd_fixValue);
        pHLayout_dd1->addWidget(ui->lineEdit_dd_fixValue);
    
        QHBoxLayout *pHLayout_dd2= new QHBoxLayout();
        pHLayout_dd2->addWidget(ui->label_dd_range);
        pHLayout_dd2->addWidget(ui->lineEdit_dd__MinValue);
        pHLayout_dd2->addWidget(ui->lineEdit_dd__MaxValue);
    
        QHBoxLayout *pHLayout_dd3= new QHBoxLayout();
        pHLayout_dd3->addWidget(ui->label_dd_time_text);
        pHLayout_dd3->addWidget(ui->lineEdit_dd_time);
        pHLayout_dd3->addWidget(ui->label_dd_time);
    
        QHBoxLayout *pHLayout_dd4= new QHBoxLayout();
        pHLayout_dd4->addWidget(ui->radioButton_increase_dd);
        pHLayout_dd4->addWidget(ui->radioButton_decrease_dd);
    
        QVBoxLayout *pVLayout_dd_args = new QVBoxLayout();
        pVLayout_dd_args->addLayout(pHLayout_dd1);
        pVLayout_dd_args->addLayout(pHLayout_dd2);
        pVLayout_dd_args->addLayout(pHLayout_dd3);
        pVLayout_dd_args->addLayout(pHLayout_dd4);
        ui->groupBox_args_dd->setLayout(pVLayout_dd_args);
    
        QVBoxLayout *pVLayout_dd = new QVBoxLayout();
        QHBoxLayout *pHLayout_dd_btn = new QHBoxLayout();
        pHLayout_dd_btn->addWidget(ui->pushButton_Dd);
        pHLayout_dd_btn->addWidget(ui->pushButton_dd_auto_start);
        pHLayout_dd_btn->addWidget(ui->pushButton_dd_auto_stop);
    
        pVLayout_dd->addWidget(ui->treeWidget_DD);
        pVLayout_dd->addWidget(ui->groupBox_args_dd);
        pVLayout_dd->addLayout(pHLayout_dd_btn);
        ui->groupBox_DD->setLayout(pVLayout_dd);
    
        QHBoxLayout *pHLayout_comm_args = new QHBoxLayout();
        pHLayout_comm_args->addWidget(ui->label_comm_time_text);
        pHLayout_comm_args->addWidget(ui->lineEdit_comm_time);
        pHLayout_comm_args->addWidget(ui->label_comm_time);
        ui->groupBox_args_comm->setLayout(pHLayout_comm_args);
    
    
        //通讯状态组框布局
        QVBoxLayout *pVLayout_comm = new QVBoxLayout();
        QHBoxLayout *pHLayout_comm = new QHBoxLayout();
        pVLayout_comm->addWidget(ui->treeWidget_COMM);
        pVLayout_comm->addWidget(ui->groupBox_args_comm);
        pHLayout_comm->addWidget(ui->pushButton_COMM);
        pHLayout_comm->addWidget(ui->pushButton_COMM0);
        pHLayout_comm->addWidget(ui->pushButton_comm_auto_start);
        pHLayout_comm->addWidget(ui->pushButton_comm_auto_stop);
        pVLayout_comm->addLayout(pHLayout_comm);
        ui->groupBox_COMM->setLayout(pVLayout_comm);
    
        //遥控组框布局
        operatingModeGroupYkSelect = new QButtonGroup(this);
        operatingModeGroupYkSelect->addButton(ui->radioButton_yk_select_ok,0);
        operatingModeGroupYkSelect->addButton(ui->radioButton_yk_select_fail,1);
        ui->radioButton_yk_select_ok->setChecked(true);
    
        operatingModeGroupYkExecute = new QButtonGroup(this);
        operatingModeGroupYkExecute->addButton(ui->radioButton_yk_execute_ok,0);
        operatingModeGroupYkExecute->addButton(ui->radioButton_yk_execute_fail,1);
        ui->radioButton_yk_execute_ok->setChecked(true);
    
        operatingModeGroupYkRevocation = new QButtonGroup(this);
        operatingModeGroupYkRevocation->addButton(ui->radioButton_yk_revocation_ok,0);
        operatingModeGroupYkRevocation->addButton(ui->radioButton_yk_revocation_fail,1);
        ui->radioButton_yk_revocation_ok->setChecked(true);
    
        connect(operatingModeGroupYkSelect, SIGNAL(buttonToggled(int,bool)), this, SLOT(operatingModeButtonsToggledYkSelect(int,bool)));
        connect(operatingModeGroupYkExecute, SIGNAL(buttonToggled(int,bool)), this, SLOT(operatingModeButtonsToggledYkExecute(int,bool)));
        connect(operatingModeGroupYkRevocation, SIGNAL(buttonToggled(int,bool)), this, SLOT(operatingModeButtonsToggledYkRevocation(int,bool)));
        QVBoxLayout *pVLayout_Yk_args = new QVBoxLayout();
        QHBoxLayout *pHLayout_yk1 = new QHBoxLayout();
        pHLayout_yk1->addWidget(ui->radioButton_yk_select_ok);
        pHLayout_yk1->addWidget(ui->radioButton_yk_select_fail);
    
        QHBoxLayout *pHLayout_yk2 = new QHBoxLayout();
        pHLayout_yk2->addWidget(ui->radioButton_yk_execute_ok);
        pHLayout_yk2->addWidget(ui->radioButton_yk_execute_fail);
    
        QHBoxLayout *pHLayout_yk3 = new QHBoxLayout();
        pHLayout_yk3->addWidget(ui->radioButton_yk_revocation_ok);
        pHLayout_yk3->addWidget(ui->radioButton_yk_revocation_fail);
    
        pVLayout_Yk_args->addLayout(pHLayout_yk1);
        pVLayout_Yk_args->addLayout(pHLayout_yk2);
        pVLayout_Yk_args->addLayout(pHLayout_yk3);
    
        ui->groupBox_args_yk->setLayout(pVLayout_Yk_args);
    
        QVBoxLayout *pVLayout_yk= new QVBoxLayout();
        pVLayout_yk->addWidget(ui->treeWidget_YK);
        pVLayout_yk->addWidget(ui->groupBox_args_yk);
        pVLayout_yk->addWidget(ui->lineEdit_yk_info);
        ui->groupBox__YK->setLayout(pVLayout_yk);
    
        //redis ip 画面布局
    
        QHBoxLayout *pHLayout_redis = new QHBoxLayout();
        pHLayout_redis->addWidget(ui->label_RedisIP);
        pHLayout_redis->addWidget(ui->lineEdit_RedisIP);
        pHLayout_redis->addWidget(ui->pushButton_redisIp);
        pHLayout_redis->addWidget(ui->lineEdit_RedisIP_Info);
        ui->groupBox_redis->setLayout(pHLayout_redis);
    
    
        //整体画面布局
        QHBoxLayout *pHLayout = new QHBoxLayout();
    
        pHLayout->addWidget( ui->groupBox_YX);
        pHLayout->addWidget( ui->groupBox_YC);
        pHLayout->addWidget( ui->groupBox_DD);
        pHLayout->addWidget( ui->groupBox_COMM);
        pHLayout->addWidget( ui->groupBox__YK);
    
        QVBoxLayout *pVLayout_all = new QVBoxLayout();
        pVLayout_all->addWidget(ui->groupBox_redis);
        pVLayout_all->addLayout(pHLayout);
    
        //setStretchFactor函数后,三个控件的比例分别为:1:5
        //    pVLayout_all->setStretchFactor(ui->groupBox, 1);
        //    pVLayout_all->setStretchFactor(pHLayout, 15);
    
        setLayout(pVLayout_all);
    
        //各个变量初始化
        timer = new QTimer(this);
        connect(timer, SIGNAL(timeout()), this, SLOT(update()));
    
        timerComm = new QTimer(this);
        connect(timerComm, SIGNAL(timeout()), this, SLOT(updateComm()));
    
        timerYc = new QTimer(this);
        connect(timerYc, SIGNAL(timeout()), this, SLOT(updateYc()));
    
        timerDd = new QTimer(this);
        connect(timerDd, SIGNAL(timeout()), this, SLOT(updateDd()));
    
        isAutoYx = false;
        isSOE = false;
        isAutoComm = false;
        isAutoYc = false;
        isAutoDd = false;
    
        ui->pushButton_yx_auto->setEnabled(false);
        ui->pushButton_yx_auto_close->setEnabled(false);
        ui->pushButton_comm_auto_start->setEnabled(false);
        ui->pushButton_comm_auto_stop->setEnabled(false);
        ui->pushButton_dd_auto_start->setEnabled(false);
        ui->pushButton_dd_auto_stop->setEnabled(false);
        ui->pushButton_yc_auto_start->setEnabled(false);
        ui->pushButton_yc_auto_stop->setEnabled(false);
        ui->pushButton_2->setEnabled(false);
        ui->pushButton_Dd->setEnabled(false);
        ui->pushButton_Yc->setEnabled(false);
        ui->pushButton_yx0->setEnabled(false);
        ui->pushButton_COMM->setEnabled(false);
        ui->pushButton_COMM0->setEnabled(false);
    
        yc_MinValue = 0;
        yc_MaxValue = 100;
        yc_StepValue = 1 ;
    
        sum_yc = yc_MinValue ;
        sub_yc = yc_MaxValue ;
    
        dd_MinValue = 0;
        dd_MaxValue = 100;
        dd_StepValue = 1 ;
    
        sum_dd = dd_MinValue ;
        sub_dd = dd_MaxValue ;
    
        radioBtn_id_yk_select = 0;
        radioBtn_id_yk_execute= 0;
        radioBtn_id_yk_revocation= 0;
    
        ui->lineEdit_yk_info->setText(tr("yk info....."));
    
    
        //读取xml配置文件
        QString fileName ="map.xml";
        //
        //    readXml(fileName);
    
        is_connectRedis=false;
    
    
    
        ui->lineEdit_RedisIP_Info->setText(tr("Connect to redis database info........."));
        ui->lineEdit_RedisIP->setPlaceholderText("172.17.69.126");
        ui->lineEdit_RedisIP->setText("172.17.69.126");
    
        ui->lineEdit_dd_fixValue->setPlaceholderText("Input int/float value...");
        ui->lineEdit_yc_fixValue->setPlaceholderText("Input int/float value...");
        ui->lineEdit_dd__MaxValue->setPlaceholderText("Input maximum...");
        ui->lineEdit_dd__MinValue->setPlaceholderText("Input minimum...");
        ui->lineEdit_yc__MaxValue->setPlaceholderText("Input maximum...");
        ui->lineEdit_yc__MinValue->setPlaceholderText("Input minimum...");
    
    
        ui->lineEdit_yx_time->setText("2");
        ui->lineEdit_comm_time->setText("10");
        ui->lineEdit_dd_time->setText("5");
        ui->lineEdit_yc_fixValue->setText("123.4");
        ui->lineEdit_yc__MaxValue->setText("100");
        ui->lineEdit_yc__MinValue->setText("1");
        ui->lineEdit_yc_time->setText("2");
        ui->lineEdit_yc_step_text->setText("1");
        ui->lineEdit_dd_fixValue->setText("123456.67");
        ui->lineEdit_dd__MaxValue->setText("10000");
        ui->lineEdit_dd__MinValue->setText("1");
        ui->lineEdit_dd_time->setText("10");
    
        ui->pushButton_COMM->setText("手动合位");
        ui->pushButton_COMM0->setText("手动分位");
    
    
    
        port =  6379;
    
        // is_overYx = true;
        dowork();
    }
    
    
    
    DataSimulation::~DataSimulation()
    {
        delete ui;
        if(ctx)
            redisFree(ctx);
        if(eventCtx)
            redisFree(eventCtx);
        if(eventCtxSend)
            redisFree(eventCtxSend);
    
    }
    
    
    void DataSimulation::dowork()
    {
        m_pthread->start();
        //    emit sig_dowork();
    }
    
    void DataSimulation::slot_finish()
    {
        //is_overYx = true;
        qDebug()<< "finish,thread id = " << QThread::currentThreadId();
        qDebug()<< "fis_overYx = " << true;
    }
    bool DataSimulation::readXml(QString path)
    {
        QFile file(path);
        if (!file.open(QFile::ReadOnly | QFile::Text))
        {
            return false;
        }
        //设置QXmlStreamReader的解析设备,等价于QXmlStreamReader ( QIODevice *)构造函数
        reader.setDevice(&file);
        while (!reader.atEnd())
        {
    
            if (reader.isStartElement())
            {
    
                if (reader.name() == "global")
                {
                    readBookindexElement();//递归下降算法,层层读取
                }
                else
                {
                    reader.raiseError(tr("Not a valid book file"));
                }
            }
            else
            {
                reader.readNext(); //循坏调用首次移动3次,后面移动一次
            }
        }
        file.close();
        if (reader.hasError())
        {
            return false;
        }
        else if (file.error() != QFile::NoError)
        {
            return false;
        }
        return true;
    
    }
    
    void DataSimulation::treeWidgetExpandAll()
    {
        ui->treeWidget->expandAll();
        ui->treeWidget_YC->expandAll();
        ui->treeWidget_DD->expandAll();
        ui->treeWidget_COMM->expandAll();
        ui->treeWidget_YK->expandAll();
    }
    void DataSimulation::eventTimeoutSlotSimulation()
    {
        redisReply* reply=0;
        int ret =redisGetReply(eventCtx,(void**)&reply);
        if(ret!=REDIS_ERR)
        {
            if(reply!=NULL&&reply->type!=REDIS_REPLY_ERROR&&reply->type!=REDIS_REPLY_NIL)
            {
                if(reply->elements==3)
                {
                    QString ch =reply->element[1]->str;
                    char *msg  =reply->element[2]->str;
                    if(ch=="YK")
                    {
                        QString s =QString("%1").arg(msg);
                        QStringList sl = s.split(":");
                        int backCode = sl[2].toInt();
                        int failCode = 224;
                        if(!mapChecked_YK.isEmpty())
                        {
                            if(mapChecked_YK.contains(sl[1]))
                            {
                                //                        QMap<QString,QString>::iterator it = mapChecked_YK.find(sl[1]); //找到特定的“键-值”对
                                //                        qDebug("find 111 , value is : " + it.data()); //获取map里对应的值
    
                                QString str;
                                //PUBLISH YKFJ 1:CAN02$yk1:129
                                if(backCode ==129 ||backCode == 128)
                                {
                                    //选线成功
                                    if(radioBtn_id_yk_select == 0)
                                    {
                                        str = QString("PUBLISH YKFJ %1:%2:%3").arg(sl[0]).
                                                arg(sl[1]).arg(backCode);
                                        ui->lineEdit_yk_info->setText(tr("yk select success"));
    
                                    }
                                    else// 选线失败
                                    {
                                        str = QString("PUBLISH YKFJ %1:%2:%3").arg(sl[0]).
                                                arg(sl[1]).arg(failCode);
                                        ui->lineEdit_yk_info->setText(tr("yk select fail"));
                                    }
    
    
                                }
                                else if (backCode ==1 ||backCode == 0)
                                {
                                    QString mapYkInfoSend = mapChecked_YK.find(sl[1]).value();
                                    //执行成功
                                    if(radioBtn_id_yk_execute == 0)
                                    {
                                        QString redisName,keyName,valueName;
                                        QStringList listtemp = mapYkInfoSend.split("$");
                                        if(listtemp.size()==2)
                                        {
                                            redisName = listtemp[0];
                                            keyName = listtemp[1];
    
                                            if(backCode ==1)
                                            {
                                                valueName = "1";
                                            }
                                            else
                                            {
                                                valueName = "0";
                                            }
    
                                            QString str_yx = QString("HSET %1 %2 %3").arg(redisName).
                                                    arg(keyName).arg(valueName);
    
                                            redisReply *replyYx;
                                            replyYx = (redisReply*)redisCommand(eventCtxSend,str_yx.toStdString().c_str());
                                            if(replyYx==NULL||replyYx->type==REDIS_REPLY_ERROR)
                                            {
                                                //                                            QMessageBox::critical(this,tr("warning"),tr("Set rtdb error"),QMessageBox::Ok,QMessageBox::NoButton);
    
                                            }
                                            freeReplyObject(reply);
                                        }
    
                                        str = QString("PUBLISH YKFJ %1:%2:%3").arg(sl[0]).
                                                arg(sl[1]).arg(backCode);
                                        ui->lineEdit_yk_info->setText(tr("yk execute success"));
    
                                    }
                                    else// 执行失败
                                    {
                                        str = QString("PUBLISH YKFJ %1:%2:%3").arg(sl[0]).
                                                arg(sl[1]).arg(failCode);
                                        ui->lineEdit_yk_info->setText(tr("yk execute fail"));
                                    }
                                }
                                else if (backCode ==130 ||backCode == 68)
                                {
                                    //撤销成功
                                    if(radioBtn_id_yk_revocation == 0)
                                    {
                                        str = QString("PUBLISH YKFJ %1:%2:%3").arg(sl[0]).
                                                arg(sl[1]).arg(backCode);
                                        ui->lineEdit_yk_info->setText(tr("yk revocation success"));
                                    }
                                    else// 撤销失败
                                    {
                                        str = QString("PUBLISH YKFJ %1:%2:%3").arg(sl[0]).
                                                arg(sl[1]).arg(failCode);
                                        ui->lineEdit_yk_info->setText(tr("yk revocation fail"));
                                    }
                                }
    
    
                                redisReply *reply1;
                                reply1 = (redisReply*)redisCommand(eventCtxSend,str.toStdString().c_str());
                                if(reply1==NULL||reply1->type==REDIS_REPLY_ERROR)
                                {
                                    //                                QMessageBox::critical(this,tr("warning"),tr("Set rtdb error"),QMessageBox::Ok,QMessageBox::NoButton);
    
                                }
    
                                freeReplyObject(reply1);
                            }
                            else
                            {
                                ui->lineEdit_yk_info->setText(tr("Received no matching YK info"));
                            }
    
                        }
                        else
                            ui->lineEdit_yk_info->setText(tr("yk info empty "));
                    }
                }
    
                freeReplyObject(reply);
            }
        }else
        {
            //All asynchronous framework (include QT adapter) on windows platform was failed except libevent(bind with ae)
            //So,synchronous for pub/sub ,MUST clear the error flag for read again
            // emit log
            eventCtx->err = 0;
    
        }
    }
    
    
    void DataSimulation::operatingModeButtonsToggledYkSelect(int id,bool status)
    {
        radioBtn_id_yk_select = id;
    }
    void DataSimulation::operatingModeButtonsToggledYkExecute(int id,bool status)
    {
        radioBtn_id_yk_execute = id;
    }
    void DataSimulation::operatingModeButtonsToggledYkRevocation(int id,bool status)
    {
        radioBtn_id_yk_revocation = id;
    }
    
    void DataSimulation::operatingModeButtonsToggledYc(int id,bool status)
    {
        yc_MaxValue = ui->lineEdit_yc__MaxValue->text().toFloat();
        yc_MinValue = ui->lineEdit_yc__MinValue->text().toFloat();
        yc_StepValue = ui->lineEdit_yc_step_text->text().toFloat();
        if(yc_MaxValue == 0.0 ||yc_MinValue == 0.0 ||yc_StepValue == 0.0)
        {
            yc_MinValue = 0;
            yc_MaxValue = 100;
            yc_StepValue = 1 ;
        }
        sum_yc = yc_MinValue ;
        sub_yc = yc_MaxValue ;
    
        radioBtn_id_yc =id;
    
    
    
    }
    
    void DataSimulation::operatingModeButtonsToggledDd(int id,bool status)
    {
        dd_MaxValue = ui->lineEdit_dd__MaxValue->text().toFloat();
        dd_MinValue = ui->lineEdit_dd__MinValue->text().toFloat();
    
        if(dd_MaxValue == 0.0 ||dd_MinValue == 0.0)
        {
            dd_MinValue = 0;
            dd_MaxValue = 100;
    
    
        }
        dd_StepValue = 1 ;
        sum_dd = dd_MinValue ;
        sub_dd = dd_MaxValue ;
        radioBtn_id_dd =id;
    
    
    }
    
    //void DataSimulation::update()
    //{
    
    //    if(isAutoYx)
    //    {
    //        QMap<QString,QString>::Iterator it =mapChecked_YX.begin();
    
    //        while(it!=mapChecked_YX.end())
    //        {
    //            qApp->processEvents();
    //            QString redisName,keyName,valueName;
    //            QStringList listtemp = it.key().split("&");
    //            if(listtemp.size()==2)
    //            {
    //                redisName = listtemp[0];
    //                keyName = listtemp[1];
    //                valueName = "1";
    
    
    //                if(!valueName.isEmpty())
    //                {
    //                    QString str = QString("HSET %1 %2 %3").arg(redisName).
    //                            arg(keyName).arg(valueName);
    
    //                    redisReply *reply;
    //                    reply = (redisReply*)redisCommand(ctx,str.toStdString().c_str());
    //                    if(reply==NULL||reply->type==REDIS_REPLY_ERROR)
    //                    {
    //                        //                    QMessageBox::critical(this,tr("warning"),tr("Set rtdb error"),QMessageBox::Ok,QMessageBox::NoButton);
    
    //                    }
    
    //                    if(isSOE)
    //                    {
    //                        QDateTime time = QDateTime::currentDateTime();   //获取当前时间
    //                        int timeT = time.toTime_t();   //将当前时间转为时间戳
    //                        //QString strSOE = "PUBLISH SOE CAN01$yx1;1587429797$00;255";
    //                        QString strSOE = QString("PUBLISH SOE %1$%2;%3$00;255").arg(redisName).
    //                                arg(keyName).arg(timeT);
    
    
    //                        reply = (redisReply*)redisCommand(ctx,strSOE.toStdString().c_str());
    //                        if(reply==NULL||reply->type==REDIS_REPLY_ERROR)
    //                        {
    //                            //                        QMessageBox::critical(this,tr("warning"),tr("Set rtdb error"),QMessageBox::Ok,QMessageBox::NoButton);
    
    //                        }
    //                    }
    //                    freeReplyObject(reply);
    //                }
    //            }
    
    //            it++;
    //        }
    //        isAutoYx = false;
    //    }
    //    else
    //    {
    //        QMap<QString,QString>::Iterator it =mapChecked_YX.begin();
    //        while(it!=mapChecked_YX.end())
    //        {
    //            qApp->processEvents();
    //            QString redisName,keyName,valueName;
    //            QStringList listtemp = it.key().split("&");
    //            redisName = listtemp[0];
    //            keyName = listtemp[1];
    //            valueName = "0";
    //            if(!valueName.isEmpty())
    //            {
    //                QString str = QString("HSET %1 %2 %3").arg(redisName).
    //                        arg(keyName).arg(valueName);
    
    //                redisReply *reply;
    //                reply = (redisReply*)redisCommand(ctx,str.toStdString().c_str());
    //                if(reply==NULL||reply->type==REDIS_REPLY_ERROR)
    //                {
    //                    //                    QMessageBox::critical(this,tr("warning"),tr("Set rtdb error"),QMessageBox::Ok,QMessageBox::NoButton);
    
    //                }
    
    
    //                if(isSOE)
    //                {
    //                    QDateTime time = QDateTime::currentDateTime();   //获取当前时间
    //                    int timeT = time.toTime_t();   //将当前时间转为时间戳
    //                    //QString strSOE = "PUBLISH SOE CAN01$yx1;1587429797$00;0";
    //                    QString strSOE = QString("PUBLISH SOE %1$%2;%3$00;0").arg(redisName).
    //                            arg(keyName).arg(timeT);
    
    
    //                    reply = (redisReply*)redisCommand(ctx,strSOE.toStdString().c_str());
    //                    if(reply==NULL||reply->type==REDIS_REPLY_ERROR)
    //                    {
    //                        //                        QMessageBox::critical(this,tr("warning"),tr("Set rtdb error"),QMessageBox::Ok,QMessageBox::NoButton);
    
    //                    }
    //                }
    //                freeReplyObject(reply);
    //            }
    //            it++;
    //        }
    //        isAutoYx = true;
    //    }
    //}
    
    
    void DataSimulation::update()
    {
    
        //if(is_overYx)
        {
            //is_overYx = false;
            m_pworker->isSOE = isSOE;
            //        m_pworker->ctx = ctx;
            if(isAutoYx)
            {
    
                isAutoYx = false;
                m_pworker->isAutoYx = false;
                //qDebug()<< "update  is_overYx = " << is_overYx;
                emit sig_dowork();
            }
            else
            {
                //        m_pworker->update();
                isAutoYx = true;
                m_pworker->isAutoYx = true;
                //qDebug()<< "update  is_overYx = " << is_overYx;
                emit sig_dowork();
            }
    
        }
    
    
    
    
    }
    
    
    
    
    void DataSimulation::updateComm()
    {
    
        if(isAutoComm)
        {
    
            isAutoComm = false;
            m_pworker->isAutoComm = false;
            //qDebug()<< "update  is_overYx = " << is_overYx;
            emit sig_doworkComm();
        }
        else
        {
            //        m_pworker->update();
            isAutoComm = true;
            m_pworker->isAutoComm = true;
            //qDebug()<< "update  is_overYx = " << is_overYx;
            emit sig_doworkComm();
        }
    
    
    
    }
    
    //void DataSimulation::updataDd_Value(QString value_str)
    //{
    
    //    QMap<QString,QString>::Iterator it =mapChecked_DD.begin();
    //    while(it!=mapChecked_DD.end())
    //    {
    //        qApp->processEvents();
    //        QString redisName,keyName,valueName;
    //        QStringList listtemp = it.key().split("&");
    //        redisName = listtemp[0];
    //        keyName = listtemp[1];
    //        valueName = value_str;
    
    //        if(!valueName.isEmpty())
    //        {
    //            QString str = QString("HSET %1 %2 %3").arg(redisName).
    //                    arg(keyName).arg(valueName);
    
    //            redisReply *reply;
    //            reply = (redisReply*)redisCommand(ctx,str.toStdString().c_str());
    //            if(reply==NULL||reply->type==REDIS_REPLY_ERROR)
    //            {
    //                //                QMessageBox::critical(this,tr("warning"),tr("Set rtdb error"),QMessageBox::Ok,QMessageBox::NoButton);
    
    //            }
    //            freeReplyObject(reply);
    //        }
    //        it++;
    //    }
    //}
    
    //void DataSimulation::updataYc_Value(QString value_str)
    //{
    //    QMap<QString,QString>::Iterator it =mapChecked_YC.begin();
    //    while(it!=mapChecked_YC.end())
    //    {
    //        qApp->processEvents();
    
    //        QString redisName,keyName,valueName;
    //        QStringList listtemp = it.key().split("&");
    //        redisName = listtemp[0];
    //        keyName = listtemp[1];
    
    //        valueName = value_str;
    
    
    //        if(!valueName.isEmpty())
    //        {
    //            QString str = QString("HSET %1 %2 %3").arg(redisName).
    //                    arg(keyName).arg(valueName);
    
    //            redisReply *reply;
    //            reply = (redisReply*)redisCommand(ctx,str.toStdString().c_str());
    //            if(reply==NULL||reply->type==REDIS_REPLY_ERROR)
    //            {
    //                //                QMessageBox::critical(this,tr("warning"),tr("Set rtdb error"),QMessageBox::Ok,QMessageBox::NoButton);
    
    //            }
    //            freeReplyObject(reply);
    //        }
    //        it++;
    //    }
    
    
    //}
    
    void DataSimulation::updateYc()
    {
    
        qDebug()<< "DataSimulation::updateYc() radioBtn_id_yc= " << radioBtn_id_yc;
        if(radioBtn_id_yc ==  2)       //递增
        {
            if(sum_yc < yc_MaxValue)
            {
                sum_yc = sum_yc + yc_StepValue;
            }
            else
            {
                sum_yc = yc_MinValue;
            }
    
            QString sum_yc_str = QString::number(sum_yc);
    
            //        updataYc_Value(sum_yc_str);
    
            sig_doworkYc(sum_yc_str);
            qDebug()<< "DataSimulation::updateYc() sig_doworkYc " << sum_yc_str;
    
        }
        else if (radioBtn_id_yc == 3)  //递减
        {
            if(sub_yc > yc_MinValue)
            {
                sub_yc =sub_yc - yc_StepValue;
            }
            else
            {
                sub_yc = yc_MaxValue;
            }
    
            QString sub_yc_str =  QString("%1").arg(sub_yc);
    
            sig_doworkYc(sub_yc_str);
            qDebug()<< "DataSimulation::updateYc() sig_doworkYc " << sub_yc_str;
    
        }
        else if (radioBtn_id_yc == 4)  //正弦
        {
    
        }
        else if (radioBtn_id_yc == 5)  //梯形
        {
    
        }
    
    }
    
    void DataSimulation::updateDd()
    {
        if(radioBtn_id_dd ==  2)       //递增
        {
            if(sum_dd < dd_MaxValue)
            {
                sum_dd = sum_dd + dd_StepValue;
            }
            else
            {
                sum_dd = dd_MinValue;
            }
    
            QString sum_dd_str = QString::number(sum_dd);
    
            //        updataDd_Value(sum_dd_str);
            sig_doworkDd(sum_dd_str);
    
        }
        else if (radioBtn_id_dd == 3)  //递减
        {
            if(sub_dd > dd_MinValue)
            {
                sub_dd =sub_dd - dd_StepValue;
            }
            else
            {
                sub_dd = dd_MaxValue;
            }
    
            QString sub_dd_str =  QString("%1").arg(sub_dd);
    
            //        updataDd_Value(sub_dd_str);
            sig_doworkDd(sub_dd_str);
    
        }
    
    }
    void DataSimulation::closeEvent(QCloseEvent *event) //根据不同的需求进行添加,比如:
    {
    
    
        if(is_connectRedis)
        {
    
            int ret = QMessageBox::warning(0,"Warning",QString("Do you want to close it?"),QMessageBox::Yes |QMessageBox::Cancel,QMessageBox::Yes);
            if(ret == QMessageBox::Yes) //如果选择yes,则删除所有的key
            {
    
    
                timer->stop();
                timerComm->stop();
                timerYc->stop();
                timerDd->stop();
    
    
    
                disconnect();
    
                m_pthread ->quit();
    
                m_pthread ->wait();//使线程正常退出。
    
    
                QMap<QString,QString>::Iterator it =mapChecked_YX.begin();
                while(it!=mapChecked_YX.end())
                {
                    QString redisName,keyName;
                    QStringList listtemp = it.key().split("&");
                    redisName = listtemp[0];
                    keyName = listtemp[1];
    
    
                    if(!redisName.isEmpty())
                    {
                        QString str = QString("HDEL %1 %2 ").arg(redisName).
                                arg(keyName);
    
                        redisReply *reply;
                        reply = (redisReply*)redisCommand(ctx,str.toStdString().c_str());
                        if(reply==NULL||reply->type==REDIS_REPLY_ERROR)
                        {
                            //                        QMessageBox::critical(this,tr("warning"),tr("Set rtdb error"),QMessageBox::Ok,QMessageBox::NoButton);
    
                        }
    
                        freeReplyObject(reply);
                    }
                    it++;
                }
                QMap<QString,QString>::Iterator it_YC =mapChecked_YC.begin();
                while(it_YC!=mapChecked_YC.end())
                {
    
                    QString redisName,keyName;
                    QStringList listtemp = it_YC.key().split("&");
                    redisName = listtemp[0];
                    keyName = listtemp[1];
    
    
                    if(!redisName.isEmpty())
                    {
                        QString str = QString("HDEL %1 %2 ").arg(redisName).
                                arg(keyName);
    
                        redisReply *reply;
                        reply = (redisReply*)redisCommand(ctx,str.toStdString().c_str());
                        if(reply==NULL||reply->type==REDIS_REPLY_ERROR)
                        {
                            //                        QMessageBox::critical(this,tr("warning"),tr("Set rtdb error"),QMessageBox::Ok,QMessageBox::NoButton);
    
                        }
    
                        freeReplyObject(reply);
                    }
                    it_YC++;
                }
    
                QMap<QString,QString>::Iterator it_DD =mapChecked_DD.begin();
                while(it_DD!=mapChecked_DD.end())
                {
    
                    QString redisName,keyName;
                    QStringList listtemp = it_DD.key().split("&");
                    redisName = listtemp[0];
                    keyName = listtemp[1];
    
    
                    if(!redisName.isEmpty())
                    {
                        QString str = QString("HDEL %1 %2 ").arg(redisName).
                                arg(keyName);
    
                        redisReply *reply;
                        reply = (redisReply*)redisCommand(ctx,str.toStdString().c_str());
                        if(reply==NULL||reply->type==REDIS_REPLY_ERROR)
                        {
                            //                        QMessageBox::critical(this,tr("warning"),tr("Set rtdb error"),QMessageBox::Ok,QMessageBox::NoButton);
    
                        }
    
                        freeReplyObject(reply);
                    }
                    it_DD++;
                }
                QMap<QString,QString>::Iterator it_COMM =mapChecked_COMM.begin();
                while(it_COMM!=mapChecked_COMM.end())
                {
    
                    QString redisName,keyName;
                    QStringList listtemp = it_COMM.key().split("&");
                    redisName = listtemp[0];
                    keyName = listtemp[1];
    
    
                    if(!redisName.isEmpty())
                    {
                        QString str = QString("HDEL %1 %2 ").arg(redisName).
                                arg(keyName);
    
                        redisReply *reply;
                        reply = (redisReply*)redisCommand(ctx,str.toStdString().c_str());
                        if(reply==NULL||reply->type==REDIS_REPLY_ERROR)
                        {
                            //                        QMessageBox::critical(this,tr("warning"),tr("Set rtdb error"),QMessageBox::Ok,QMessageBox::NoButton);
    
                        }
    
                        freeReplyObject(reply);
                    }
                    it_COMM++;
                }
    
                mapChecked_DD.clear();
                mapChecked_YC.clear();
                mapChecked_YX.clear();
                mapChecked_COMM.clear();
    
            }
            else if(ret == QMessageBox::Cancel)
            {
                event->ignore();
            }
    
        }
    
    }
    
    
    void DataSimulation::readBookindexElement()
    {
        Q_ASSERT(reader.isStartElement() && reader.name() == "global");//不是则会报错
        reader.readNext(); // 读取下一个记号,它返回记号的类型
        while (!reader.atEnd())
        {
            if (reader.isEndElement())
            {
                reader.readNext();
                break;
            }
    
            if (reader.isStartElement())
            {
                if (reader.name() == "DI")
                {
                    readEntryElement( ui->treeWidget->invisibleRootItem(),tr("DI"));
                }
                else if (reader.name() == "COMM")
                {
                    readEntryElement( ui->treeWidget_COMM->invisibleRootItem(),tr("COMM"));
                }
                else if (reader.name() == "AI")
                {
                    readEntryElement( ui->treeWidget_YC->invisibleRootItem(),tr("AI"));
                }
                else if (reader.name() == "EE")
                {
                    readEntryElement( ui->treeWidget_DD->invisibleRootItem(),tr("EE"));
                }
                else if (reader.name() == "DO")
                {
                    readEntryElement( ui->treeWidget_YK->invisibleRootItem(),tr("YK"));
                }
    
                else
                {
                    skipUnknownElement();
                }
    
            }
            else
            {
                reader.readNext();
            }
        }
    }
    
    
    void DataSimulation::readEntryElement(QTreeWidgetItem *parent,QString name)
    {
        //    QTreeWidgetItem *item = new QTreeWidgetItem(parent);
        //    item->setText(0, reader.attributes().value("PATH").toString());//元素的属性
    
        QTreeWidgetItem *ritem;
        QTreeWidgetItem *item = new QTreeWidgetItem(parent);
        item->setText(0, name);//元素的属性
        item->setCheckState(0,Qt::Checked);
        QString oldnamestr;
    
        reader.readNext();
        while (!reader.atEnd())
        {
            if (reader.isEndElement())
            {
                reader.readNext();
                break;
            }
    
    
            if (reader.isStartElement())
            {
                QStringRef aaa =reader.name();
                if (reader.name() == "Item")
                {
                    QString  bbb =reader.attributes().value("PATH").toString();
    
                    bool containBool = bbb.contains("$",Qt::CaseSensitive);
                    QStringList itemlst = bbb.split("$");
    
                    if(oldnamestr != itemlst[0])
                    {
                        ritem = addTreeWight(item,itemlst[0]);
    
                        oldnamestr = itemlst[0];
                    }
                    addTreeWight(ritem,itemlst[1],true);
    
                    QString page = reader.readElementText();
                    if (reader.isEndElement())
                    {
                        reader.readNext();
                    }
                }
                else
                {
                    skipUnknownElement();
                }
            }
            else
            {
                reader.readNext();
            }
        }
    }
    
    
    
    void DataSimulation::skipUnknownElement()
    {
        reader.readNext();
        while (!reader.atEnd())
        {
            if (reader.isEndElement())
            {
                reader.readNext();
                break;
            }
    
            if (reader.isStartElement())
            {
                skipUnknownElement();
            }
            else
            {
                reader.readNext();
            }
        }
    }
    QTreeWidgetItem* DataSimulation::addTreeWight(QTreeWidgetItem* parent ,QString name,bool is_setdata)
    {
        QTreeWidgetItem *item = new QTreeWidgetItem(parent);
        item->setCheckState(0,Qt::Checked);
        item->setFlags((Qt::ItemIsEditable|parent->flags()));
        item->setText(0, name);//遥信名称
        if(is_setdata)
            item->setText(1, "0");//遥信名称
        item->addChild(parent);
        return item;
    }
    void DataSimulation::onTreeItemChanged(QTreeWidgetItem* item, int column)
    {
        int count = item->childCount(); //返回子项的个数
        int currutnum = column;
        int currutnumb = item->checkState(column);
    
        if (Qt::Checked == item->checkState(0))
        {
    
            if (count > 0)
            {
    
                for (int i = 0; i < count; i++)
                {
                    QString itemText0 =  item->text(0);
                    QString itemText1 =  item->text(1);
    
                    item->child(i)->setCheckState(0, Qt::Checked);
    
                }
    
            }
            else
            {
                UpdateParent(item,"YX");
            }
    
        }
        else if (Qt::Unchecked == item->checkState(0)  )
        {
            if (count > 0)
            {
                for (int i = 0; i < count; i++)
                {
                    item->child(i)->setCheckState(0, Qt::Unchecked);
                }
    
            }
            else
            {
                UpdateParent(item,"YX");
            }
        }
    }
    
    
    void DataSimulation::onTreeItemChanged_YK(QTreeWidgetItem* item, int column)
    {
        int count = item->childCount(); //返回子项的个数
        int currutnum = column;
        int currutnumb = item->checkState(column);
    
    
    
        if (Qt::Checked == item->checkState(0))
        {
    
            if (count > 0)
            {
    
                for (int i = 0; i < count; i++)
                {
                    QString itemText0 =  item->text(0);
                    QString itemText1 =  item->text(1);
    
                    item->child(i)->setCheckState(0, Qt::Checked);
    
                }
    
            }
            else
            {
                UpdateParent(item,"YK");
            }
    
        }
        else if (Qt::Unchecked == item->checkState(0)  )
        {
            if (count > 0)
            {
                for (int i = 0; i < count; i++)
                {
                    item->child(i)->setCheckState(0, Qt::Unchecked);
                }
    
            }
            else
            {
                UpdateParent(item,"YK");
            }
        }
    }
    
    
    void DataSimulation::onTreeItemChanged_YC(QTreeWidgetItem* item, int column)
    {
        int count = item->childCount(); //返回子项的个数
    
        if (Qt::Checked == item->checkState(0))
        {
    
            if (count > 0)
            {
    
                for (int i = 0; i < count; i++)
                {
                    QString itemText0 =  item->text(0);
                    QString itemText1 =  item->text(1);
    
                    item->child(i)->setCheckState(0, Qt::Checked);
    
                }
    
            }
            else
            {
                UpdateParent(item,"YC");
    
            }
    
        }
        else if (Qt::Unchecked == item->checkState(0)  )
        {
            if (count > 0)
            {
                for (int i = 0; i < count; i++)
                {
                    item->child(i)->setCheckState(0, Qt::Unchecked);
                }
    
            }
            else
            {
                UpdateParent(item,"YC");
            }
        }
    }
    
    void DataSimulation::onTreeItemChanged_DD(QTreeWidgetItem* item, int column)
    {
        int count = item->childCount(); //返回子项的个数
    
        if (Qt::Checked == item->checkState(0))
        {
    
            if (count > 0)
            {
    
                for (int i = 0; i < count; i++)
                {
                    QString itemText0 =  item->text(0);
                    QString itemText1 =  item->text(1);
    
                    item->child(i)->setCheckState(0, Qt::Checked);
    
                }
    
            }
            else
            {
                UpdateParent(item,"DD");
    
            }
    
        }
        else if (Qt::Unchecked == item->checkState(0)  )
        {
            if (count > 0)
            {
                for (int i = 0; i < count; i++)
                {
                    item->child(i)->setCheckState(0, Qt::Unchecked);
                }
    
            }
            else
            {
                UpdateParent(item,"DD");
            }
        }
    }
    
    void DataSimulation::onTreeItemChanged_COMM(QTreeWidgetItem* item, int column)
    {
        int count = item->childCount(); //返回子项的个数
    
        if (Qt::Checked == item->checkState(0))
        {
            if (count > 0)
            {
                for (int i = 0; i < count; i++)
                {
                    QString itemText0 =  item->text(0);
                    QString itemText1 =  item->text(1);
    
                    item->child(i)->setCheckState(0, Qt::Checked);
                }
    
            }
            else
            {
                UpdateParent(item,"COMM");
    
            }
    
        }
        else if (Qt::Unchecked == item->checkState(0)  )
        {
            if (count > 0)
            {
                for (int i = 0; i < count; i++)
                {
                    item->child(i)->setCheckState(0, Qt::Unchecked);
                }
    
            }
            else
            {
                UpdateParent(item,"COMM");
            }
        }
    }
    
    
    
    
    bool DataSimulation::testConnection(QString ip)
    {
    
        QTcpSocket *pSocket = new QTcpSocket(this);
        pSocket->connectToHost(ip,6379);
        is_connectRedis = false ;
        if(pSocket->waitForConnected(1000))
            is_connectRedis = true ;
    
        pSocket->close();
        return is_connectRedis ;
    
    }
    
    bool DataSimulation::connectRedis()
    {
    
        redisReply *reply;
    
    
        ctx = redisConnect(redisIPstr.toStdString().c_str(), port);
        if (ctx == NULL || ctx->err)  {
            ctx = 0 ;
            QMessageBox::warning(this,tr("Warning"),tr("cannot connect remote rtdb"),
                                 QMessageBox::Ok/*,
                                                                                                                                                                                    QMessageBox::Cancel*/);
            return false;
        }
        struct timeval t={1,0};
        redisSetTimeout(ctx,t);
    
        m_pworker->ctx = redisConnect(redisIPstr.toStdString().c_str(), port);
        if (m_pworker->ctx == NULL || m_pworker->ctx->err)  {
            m_pworker->ctx = 0 ;
            QMessageBox::warning(this,tr("Warning"),tr("cannot connect remote rtdb"),
                                 QMessageBox::Ok/*,
                                                                                                                                                                                    QMessageBox::Cancel*/);
            return false;
        }
    
        struct timeval t4={1,0};
        redisSetTimeout(m_pworker->ctx,t4);
    
    
        eventCtxSend = redisConnect(redisIPstr.toStdString().c_str(), port);
        if (eventCtxSend == NULL || eventCtxSend->err)  {
            eventCtxSend = 0 ;
            QMessageBox::warning(this,tr("Warning"),tr("cannot connect remote rtdb"),
                                 QMessageBox::Ok/*,
                                                                                                                                                                                    QMessageBox::Cancel*/);
    
        }
        struct timeval t2={1,0};
        redisSetTimeout(eventCtxSend,t2);
    
    
        eventCtx =  redisConnect(redisIPstr.toStdString().c_str(), port);
        if (eventCtx == NULL || eventCtx->err) {
            eventCtx = 0 ;
            QMessageBox::warning(this,tr("Warning"),tr("cannot connect remote rtdb"),
                                 QMessageBox::Ok/*,
                                                                                                                                                                                    QMessageBox::Cancel*/);
            return false ;
        }
    
        struct timeval t1={0,100000};
        redisSetTimeout(eventCtx,t1);
    
        redisAppendCommand(eventCtx,"SUBSCRIBE YK");
        redisGetReply(eventCtx,(void**)&reply);
        freeReplyObject(reply);
        return true ;
    }
    
    void DataSimulation::UpdateChild(QTreeWidgetItem *itm)
    {
        int childCount = itm->childCount();
        if(childCount > 0)
        {
            for(int i = 0; i < childCount; i++)
            {
                itm->child(i)->setCheckState(0,itm->checkState(0));
                if(itm->child(i)->childCount() > 0)
                    UpdateChild(itm->child(i));
            }
        }
    }
    
    void DataSimulation::SetParentPartiallyChecked(QTreeWidgetItem *itm)
    {
        QTreeWidgetItem *parent = itm->parent();
        if(parent)
        {
            parent->setCheckState(0,Qt::PartiallyChecked);
            SetParentPartiallyChecked(parent);
        }
    }
    
    void DataSimulation::UpdateParent(QTreeWidgetItem *itm,QString name)
    {
        QTreeWidgetItem *parent = itm->parent();
    
        if(parent)
        {
            int checkedCount = 0;
            int patiallyCount = 0;
            int itemCount = parent->childCount();
            for(int i = 0; i < itemCount; i++)
            {
                QString itemTextP0 =  NULL;
                QString childItem0 =  NULL;
                QString childItem1 = NULL;
    
    
                QTreeWidgetItem *item = parent->child(i);
    
                itemTextP0 =  parent->text(0);
                childItem0 =  item->text(0);
                childItem1 =  item->text(1);
    
                if(Qt::Checked == item->checkState(0))
                {
                    QString keyname = QString("%1&%2")
                            .arg(itemTextP0).arg(childItem0);
                    if(!childItem1.isEmpty())
                    {
                        if(name == "YX")
                        {
                            mapChecked_YX.insert(keyname,childItem1);
                            m_pworker->mapChecked_YX.insert(keyname,childItem1);
                        }
    
                        else if (name == "YC")
                        {
                            mapChecked_YC.insert(keyname,childItem1);
                            m_pworker->mapChecked_YC.insert(keyname,childItem1);
                        }
                        else if (name == "DD")
                        {
                            mapChecked_DD.insert(keyname,childItem1);
                            m_pworker->mapChecked_DD.insert(keyname,childItem1);
                        }
    
                        else if (name == "COMM")
                        {
                            mapChecked_COMM.insert(keyname,childItem1);
                            m_pworker->mapChecked_COMM.insert(keyname,childItem1);
                        }
                        else if (name == "YK")
                        {
                            QString keynameyk = QString("%1$%2")
                                    .arg(itemTextP0).arg(childItem0);
                            mapChecked_YK.insert(keynameyk,childItem1);
                        }
    
    
                    }
    
                    checkedCount++;
    
                }
    
                else if(Qt::PartiallyChecked == item->checkState(0))
                {
                    patiallyCount++;
                    QString keyname = QString("%1&%2")
                            .arg(itemTextP0).arg(childItem0);
    
                    QString keynameyk = QString("%1$%2")
                            .arg(itemTextP0).arg(childItem0);
    
                    if(name == "YX" &&  mapChecked_YX.contains(keyname))
                    {
                        mapChecked_YX.remove(keyname);
                        m_pworker-> mapChecked_YX.remove(keyname);
    
                    }
                    else if (name == "YC" &&  mapChecked_YC.contains(keyname))
                    {
                        mapChecked_YC.remove(keyname);
                        m_pworker-> mapChecked_YC.remove(keyname);
    
                    }
                    else if (name == "DD" &&  mapChecked_DD.contains(keyname))
                    {
                        mapChecked_DD.remove(keyname);
                        m_pworker-> mapChecked_DD.remove(keyname);
    
                    }
                    else if (name == "COMM" &&  mapChecked_COMM.contains(keyname))
                    {
                        mapChecked_COMM.remove(keyname);
                        m_pworker-> mapChecked_COMM.remove(keyname);
    
                    }
                    else if (name == "YK" &&  mapChecked_YK.contains(keynameyk))
                        mapChecked_YK.remove(keynameyk);
    
    
    
                }
                else
                {
                    QString keyname = QString("%1&%2")
                            .arg(itemTextP0).arg(childItem0);
    
                    QString keynameyk = QString("%1$%2")
                            .arg(itemTextP0).arg(childItem0);
    
                    if(name == "YX" &&  mapChecked_YX.contains(keyname))
                    {
                        mapChecked_YX.remove(keyname);
                        m_pworker-> mapChecked_YX.remove(keyname);
                    }
                    else if (name == "YC" &&  mapChecked_YC.contains(keyname))
                    {
                        mapChecked_YC.remove(keyname);
                        m_pworker-> mapChecked_YC.remove(keyname);
    
                    }
                    else if (name == "DD" &&  mapChecked_DD.contains(keyname))
                    {
                        mapChecked_DD.remove(keyname);
                        m_pworker-> mapChecked_DD.remove(keyname);
    
                    }
                    else if (name == "COMM" &&  mapChecked_COMM.contains(keyname))
                    {
                        mapChecked_COMM.remove(keyname);
                        m_pworker-> mapChecked_COMM.remove(keyname);
    
                    }
                    else if (name == "YK" &&  mapChecked_YK.contains(keynameyk))
                        mapChecked_YK.remove(keynameyk);
                }
            }
    
    
            if(checkedCount <= 0 )
            {
                if(patiallyCount > 0)
                    goto Partially;
                else
                    parent->setCheckState(0,Qt::Unchecked);
            }
            else if(checkedCount > 0 && checkedCount < itemCount  )
            {
    Partially:
                parent->setCheckState(0,Qt::PartiallyChecked);
                SetParentPartiallyChecked(parent);
                return;
            }
            else if(checkedCount > 0 && checkedCount == itemCount)
            {
                parent->setCheckState(0,Qt::Checked);
            }
            // Recursively iterate up
            UpdateParent(parent,name);
        }
    
    
    }
    
    //单一或批量或全部给遥信置数为1
    void DataSimulation::on_pushButton_2_clicked()
    {
        QMap<QString,QString>::Iterator it =mapChecked_YX.begin();
    
        while(it!=mapChecked_YX.end())
        {
            QString redisName,keyName,valueName;
            QStringList listtemp = it.key().split("&");
            redisName = listtemp[0];
            keyName = listtemp[1];
    
            valueName = "1";
    
    
            if(!valueName.isEmpty())
            {
                QString str = QString("HSET %1 %2 %3").arg(redisName).
                        arg(keyName).arg(valueName);
    
    
    
                redisReply *reply;
                reply = (redisReply*)redisCommand(ctx,str.toStdString().c_str());
                if(reply==NULL||reply->type==REDIS_REPLY_ERROR)
                {
                    //                QMessageBox::critical(this,tr("warning"),tr("Set rtdb error"),QMessageBox::Ok,QMessageBox::NoButton);
    
                }
    
                if(isSOE)
                {
                    QDateTime time = QDateTime::currentDateTime();   //获取当前时间
                    int timeT = time.toTime_t();   //将当前时间转为时间戳
                    //QString strSOE = "PUBLISH SOE CAN01$yx1;1587429797$00;255";
                    QString strSOE = QString("PUBLISH SOE %1$%2;%3$00;255").arg(redisName).
                            arg(keyName).arg(timeT);
    
    
                    reply = (redisReply*)redisCommand(ctx,strSOE.toStdString().c_str());
                    if(reply==NULL||reply->type==REDIS_REPLY_ERROR)
                    {
                        //                    QMessageBox::critical(this,tr("warning"),tr("Set rtdb error"),QMessageBox::Ok,QMessageBox::NoButton);
    
                    }
                }
    
    
                freeReplyObject(reply);
            }
            it++;
        }
    }
    
    //手动置遥测树形里面的值和固定值
    void DataSimulation::on_pushButton_Yc_clicked()
    {
    
        QMap<QString,QString>::Iterator it =mapChecked_YC.begin();
    
        while(it!=mapChecked_YC.end())
        {
            QString redisName,keyName,valueName;
            QStringList listtemp = it.key().split("&");
            redisName = listtemp[0];
            keyName = listtemp[1];
            switch(radioBtn_id_yc)
            {
            case 0:
                valueName = it.value();
                break;
            case 1:
                valueName = yc_fixValue;
                break;
            default:
                break;
            }
    
            if(!valueName.isEmpty())
            {
                QString str = QString("HSET %1 %2 %3").arg(redisName).
                        arg(keyName).arg(valueName);
    
                redisReply *reply;
                reply = (redisReply*)redisCommand(ctx,str.toStdString().c_str());
                if(reply==NULL||reply->type==REDIS_REPLY_ERROR)
                {
                    //                QMessageBox::critical(this,tr("warning"),tr("Set rtdb error"),QMessageBox::Ok,QMessageBox::NoButton);
    
                }
                freeReplyObject(reply);
            }
            it++;
        }
    }
    
    void DataSimulation::on_pushButton_Dd_clicked()
    {
        QMap<QString,QString>::Iterator it =mapChecked_DD.begin();
        while(it!=mapChecked_DD.end())
        {
            QString redisName,keyName,valueName;
            QStringList listtemp = it.key().split("&");
            redisName = listtemp[0];
            keyName = listtemp[1];
    
    
            switch(radioBtn_id_dd)
            {
            case 0:
                valueName = it.value();
                break;
            case 1:
                valueName = dd_fixValue;
                break;
            default:
                break;
            }
    
            if(!valueName.isEmpty())
            {
                QString str = QString("HSET %1 %2 %3").arg(redisName).
                        arg(keyName).arg(valueName);
    
                redisReply *reply;
                reply = (redisReply*)redisCommand(ctx,str.toStdString().c_str());
                if(reply==NULL||reply->type==REDIS_REPLY_ERROR)
                {
                    //                QMessageBox::critical(this,tr("warning"),tr("Set rtdb error"),QMessageBox::Ok,QMessageBox::NoButton);
    
                }
                freeReplyObject(reply);
            }
            it++;
        }
    }
    
    void DataSimulation::on_pushButton_COMM_clicked()
    {    
        QMap<QString,QString>::Iterator it =mapChecked_COMM.begin();
    
        while(it!=mapChecked_COMM.end())
        {
    
            QString redisName,keyName,valueName;
            QStringList listtemp = it.key().split("&");
            redisName = listtemp[0];
            keyName = listtemp[1];
            valueName = "255";
    
            if(!valueName.isEmpty())
            {
                QString str = QString("HSET %1 %2 %3").arg(redisName).
                        arg(keyName).arg(valueName);
    
                redisReply *reply;
                reply = (redisReply*)redisCommand(ctx,str.toStdString().c_str());
                if(reply==NULL||reply->type==REDIS_REPLY_ERROR)
                {
                    //                QMessageBox::critical(this,tr("warning"),tr("Set rtdb error"),QMessageBox::Ok,QMessageBox::NoButton);
    
                }
                freeReplyObject(reply);
            }
            it++;
        }
    }
    
    void DataSimulation::on_pushButton_yx0_clicked()
    {
    
        QMap<QString,QString>::Iterator it =mapChecked_YX.begin();
    
        while(it!=mapChecked_YX.end())
        {
            qDebug()<<it.key()<<"\t"<<it.value();
            QString redisName,keyName,valueName;
            QStringList listtemp = it.key().split("&");
            redisName = listtemp[0];
            keyName = listtemp[1];
            //        valueName = it.value();
            valueName = "0";
    
    
            if(!valueName.isEmpty())
            {
                QString str = QString("HSET %1 %2 %3").arg(redisName).
                        arg(keyName).arg(valueName);
    
                redisReply *reply;
                reply = (redisReply*)redisCommand(ctx,str.toStdString().c_str());
                if(reply==NULL||reply->type==REDIS_REPLY_ERROR)
                {
                    //                QMessageBox::critical(this,tr("warning"),tr("Set rtdb error"),QMessageBox::Ok,QMessageBox::NoButton);
    
                }
    
    
                if(isSOE)
                {
                    QDateTime time = QDateTime::currentDateTime();   //获取当前时间
                    int timeT = time.toTime_t();   //将当前时间转为时间戳
                    //QString strSOE = "PUBLISH SOE CAN01$yx1;1587429797$00;0";
                    QString strSOE = QString("PUBLISH SOE %1$%2;%3$00;0").arg(redisName).
                            arg(keyName).arg(timeT);
    
    
                    reply = (redisReply*)redisCommand(ctx,strSOE.toStdString().c_str());
                    if(reply==NULL||reply->type==REDIS_REPLY_ERROR)
                    {
                        //                    QMessageBox::critical(this,tr("warning"),tr("Set rtdb error"),QMessageBox::Ok,QMessageBox::NoButton);
    
                    }
                }
                freeReplyObject(reply);
            }
            it++;
        }
    }
    
    void DataSimulation::on_pushButton_COMM0_clicked()
    {   
        QMap<QString,QString>::Iterator it =mapChecked_COMM.begin();
    
        while(it!=mapChecked_COMM.end())
        {
    
            QString redisName,keyName,valueName;
            QStringList listtemp = it.key().split("&");
            redisName = listtemp[0];
            keyName = listtemp[1];
            valueName = "0";
    
            if(!valueName.isEmpty())
            {
                QString str = QString("HSET %1 %2 %3").arg(redisName).
                        arg(keyName).arg(valueName);
    
                redisReply *reply;
                reply = (redisReply*)redisCommand(ctx,str.toStdString().c_str());
                if(reply==NULL||reply->type==REDIS_REPLY_ERROR)
                {
                    //                QMessageBox::critical(this,tr("warning"),tr("Set rtdb error"),QMessageBox::Ok,QMessageBox::NoButton);
    
                }
                freeReplyObject(reply);
            }
            it++;
        }
    
    }
    
    void DataSimulation::on_pushButton_yx_auto_clicked()
    {
        ui->pushButton_yx_auto->setEnabled(false);
        ui->pushButton_yx0->setEnabled(false);
        ui->pushButton_2->setEnabled(false);
        ui->pushButton_yx_auto_close->setEnabled(true);
        QString abc = ui->lineEdit_yx_time->text();
        int sec = abc.toInt();
        if(sec==0)
            sec=10;
    
        timer->start(sec*1000);
    }
    
    void DataSimulation::on_pushButton_yx_auto_close_clicked()
    {
        timer->stop();
        ui->pushButton_yx_auto->setEnabled(true);
        ui->pushButton_yx0->setEnabled(true);
        ui->pushButton_2->setEnabled(true);
        ui->pushButton_yx_auto_close->setEnabled(false);
    
    }
    
    void DataSimulation::onStateChanged(int status)
    {
        if(status)
            isSOE= true;
        else
            isSOE=false;
    
        qDebug()<<"soe slots"<<status;
    }
    
    void DataSimulation::on_pushButton_comm_auto_start_clicked()
    {
        ui->pushButton_comm_auto_start->setEnabled(false);
        ui->pushButton_COMM->setEnabled(false);
        ui->pushButton_COMM0->setEnabled(false);
        ui->pushButton_comm_auto_stop->setEnabled(true);
        QString abc = ui->lineEdit_comm_time->text();
        int sec = abc.toInt();
        if(sec==0)
            sec=10;
    
        timerComm->start(sec*1000);
    
    }
    
    void DataSimulation::on_pushButton_comm_auto_stop_clicked()
    {
        ui->pushButton_comm_auto_start->setEnabled(true);
        ui->pushButton_COMM->setEnabled(true);
        ui->pushButton_COMM0->setEnabled(true);
        ui->pushButton_comm_auto_stop->setEnabled(false);
        timerComm->stop();
    }
    
    void DataSimulation::on_lineEdit_yc_fixValue_textChanged(const QString &arg1)
    {
        yc_fixValue = ui->lineEdit_yc_fixValue->text();
    
    }
    
    void DataSimulation::on_lineEdit_yc__MinValue_textChanged(const QString &arg1)
    {
        yc_MinValue= ui->lineEdit_yc__MinValue->text().toFloat();
    
    }
    
    void DataSimulation::on_lineEdit_yc__MaxValue_textChanged(const QString &arg1)
    {
        yc_MaxValue = ui->lineEdit_yc__MaxValue->text().toFloat();
    }
    
    void DataSimulation::on_lineEdit_yc_step_text_textChanged(const QString &arg1)
    {
        yc_StepValue = ui->lineEdit_yc_step_text->text().toFloat();
    }
    
    void DataSimulation::on_lineEdit_yc_time_textChanged(const QString &arg1)
    {
    
    }
    
    void DataSimulation::on_lineEdit_dd_fixValue_textChanged(const QString &arg1)
    {
        dd_fixValue = ui->lineEdit_dd_fixValue->text();
    
    }
    
    void DataSimulation::on_lineEdit_dd__MinValue_textChanged(const QString &arg1)
    {
    
        dd_MinValue = ui->lineEdit_dd__MinValue->text().toFloat();
    }
    
    void DataSimulation::on_lineEdit_dd__MaxValue_textChanged(const QString &arg1)
    {
        dd_MaxValue = ui->lineEdit_dd__MaxValue->text().toFloat();
    }
    
    void DataSimulation::on_lineEdit_dd_time_textChanged(const QString &arg1)
    {
    
    }
    
    void DataSimulation::on_pushButton_yc_auto_start_clicked()
    {
        if(radioBtn_id_yc > 1)
        {
            ui->pushButton_yc_auto_start->setEnabled(false);
            ui->pushButton_Yc->setEnabled(false);
            ui->pushButton_yc_auto_stop->setEnabled(true);
            QString abc = ui->lineEdit_yc_time->text();
            int sec = abc.toInt();
            if(sec==0)
                sec=5;
    
    
            timerYc->start(sec*1000);
        }
    }
    
    void DataSimulation::on_pushButton_yc_auto_stop_clicked()
    {
        if(radioBtn_id_yc > 1)
        {
            ui->pushButton_yc_auto_start->setEnabled(true);
            ui->pushButton_Yc->setEnabled(true);
            ui->pushButton_yc_auto_stop->setEnabled(false);
            timerYc->stop();
        }
    }
    
    void DataSimulation::on_pushButton_dd_auto_start_clicked()
    {
        if(radioBtn_id_dd > 1)
        {
            ui->pushButton_dd_auto_start->setEnabled(false);
            ui->pushButton_Dd->setEnabled(false);
            ui->pushButton_dd_auto_stop->setEnabled(true);
            QString abc = ui->lineEdit_dd_time->text();
            int sec = abc.toInt();
            if(sec==0)
                sec=1;
            timerDd->start(sec*1000);
        }
    }
    
    void DataSimulation::on_pushButton_dd_auto_stop_clicked()
    {
        if(radioBtn_id_dd > 1)
        {
            ui->pushButton_dd_auto_start->setEnabled(true);
            ui->pushButton_Dd->setEnabled(true);
            ui->pushButton_dd_auto_stop->setEnabled(false);
            timerDd->stop();
        }
    
    }
    
    void DataSimulation::on_pushButton_redisIp_clicked()
    {
        //连接redis服务端
        if(testConnection(redisIPstr))
        {
            is_connet=connectRedis();
            if(is_connet)
            {
                eventTimer = new QTimer(this);
                connect(eventTimer,SIGNAL(timeout()),this,SLOT(eventTimeoutSlotSimulation()));
                eventTimer->start(1000);
            }
            ui->lineEdit_RedisIP_Info->setText(tr("Succeed to connect to redis database"));
            ui->pushButton_yx_auto->setEnabled(true);
            ui->pushButton_yx_auto_close->setEnabled(false);
            ui->pushButton_comm_auto_start->setEnabled(true);
            ui->pushButton_comm_auto_stop->setEnabled(false);
            ui->pushButton_dd_auto_start->setEnabled(true);
            ui->pushButton_dd_auto_stop->setEnabled(false);
            ui->pushButton_yc_auto_start->setEnabled(true);
            ui->pushButton_yc_auto_stop->setEnabled(false);
            ui->pushButton_2->setEnabled(true);
            ui->pushButton_Dd->setEnabled(true);
            ui->pushButton_Yc->setEnabled(true);
            ui->pushButton_yx0->setEnabled(true);
            ui->pushButton_COMM->setEnabled(true);
            ui->pushButton_COMM0->setEnabled(true);
            ui->pushButton_redisIp->setEnabled(false);
            ui->lineEdit_RedisIP->setEnabled(false);
        }
        else{
            //连接RTDB提示     无法连接到实时数据库
            ui->lineEdit_RedisIP_Info->setText(tr("Failed to connect to redis database"));
            ui->pushButton_yx_auto->setEnabled(false);
            ui->pushButton_yx_auto_close->setEnabled(false);
            ui->pushButton_comm_auto_start->setEnabled(false);
            ui->pushButton_comm_auto_stop->setEnabled(false);
            ui->pushButton_dd_auto_start->setEnabled(false);
            ui->pushButton_dd_auto_stop->setEnabled(false);
            ui->pushButton_yc_auto_start->setEnabled(false);
            ui->pushButton_yc_auto_stop->setEnabled(false);
            ui->pushButton_2->setEnabled(false);
            ui->pushButton_Dd->setEnabled(false);
            ui->pushButton_Yc->setEnabled(false);
            ui->pushButton_yx0->setEnabled(false);
            ui->pushButton_COMM->setEnabled(false);
            ui->pushButton_COMM0->setEnabled(false);
    
    
        }
    
    
    
    }
    
    void DataSimulation::on_lineEdit_RedisIP_textChanged(const QString &arg1)
    {
        redisIPstr = arg1;
    }
    
    

    主程序调用!!!!!

    DataSimulation* m_datasimulation = new DataSimulation;
            QString name = currItem->text(0);
            QString filename =open_dir+"/"+m_cont[name].drive_path+"/"+m_cont[name].drive_path1;
    //        qDebug()<<filename;
            bool is_map = filename.contains("map.xml",Qt::CaseSensitive);//成功返回true 第二个参数表示是否大小写敏感
            if(is_map)
            {
                if(m_datasimulation->readXml(filename))
                {
                    m_datasimulation->treeWidgetExpandAll();
                    m_datasimulation->show();
    //                m_datasimulation->dowork();
                }
            }
    
    

    自己也是Qt使用的新手,在新手成长的道路上,离不开一些巨人分享。感谢,记录自己成长的,感谢简书平台。
    项目的中参考链接如下:
    https://www.cnblogs.com/nanqiang/p/10818609.html
    https://www.cnblogs.com/liushui-sky/p/5833931.html

    相关文章

      网友评论

          本文标题:Qt的QThread,Redis客户端的使用

          本文链接:https://www.haomeiwen.com/subject/brufwhtx.html