#include "mainpage_a_v.h"

mainpage_A_V::mainpage_A_V(QWidget *parent) : QWidget(parent)
{
    label_3_1=new QLabel(this);
    label_3_2=new QLabel(this);
    label_3_3=new QLabel(this);
    label_3_4=new QLabel(this);
    label_3_5=new QLabel(this);
    label_3_6=new QLabel(this);
    label_3_7=new QLabel(this);
    label_3_8=new QLabel(this);
    label_line_vol_AB=new QLabel(this);
    label_line_vol_BC=new QLabel(this);
    label_line_vol_CA=new QLabel(this);
    line3_1=new QFrame(this);

    label_disconnect_current=new QLabel(this);
    label_disconnect_current->setGeometry(520,10,160,20);
    label_disconnect_current->setStyleSheet("background: rgb(145, 145, 145);font: 12pt 黑体 bold;color: rgb(240, 0, 0)");
    label_disconnect_voltage=new QLabel(this);
    label_disconnect_voltage->setGeometry(140,10,200,20);
    label_disconnect_voltage->setStyleSheet("background: rgb(145, 145, 145);font: 12pt 黑体 bold;color: rgb(240, 0, 0)");

    label_3_1->setGeometry(20,10,100,20);
    label_3_1->setStyleSheet("background: rgb(145, 145, 145);font: 12pt 黑体 bold;color: rgb(255, 255, 255)");
    label_3_1->setText("电压：");
    label_3_2->setGeometry(20,30,200,20);
    label_3_2->setStyleSheet("background: rgb(145, 145, 145);font: 12pt 黑体 bold;color: rgb(0, 0, 0)");
    label_3_3->setGeometry(20,50,200,20);
    label_3_3->setStyleSheet("background: rgb(145, 145, 145);font: 12pt 黑体 bold;color: rgb(0, 0, 0)");
    label_3_4->setGeometry(20,70,200,20);
    label_3_4->setStyleSheet("background: rgb(145, 145, 145);font: 12pt 黑体 bold;color: rgb(0, 0, 0)");
    label_line_vol_AB->setGeometry(20,90,200,20);
    label_line_vol_AB->setStyleSheet("background: rgb(145, 145, 145);font: 12pt 黑体 bold;color: rgb(0, 0, 0)");
    label_line_vol_BC->setGeometry(20,110,200,20);
    label_line_vol_BC->setStyleSheet("background: rgb(145, 145, 145);font: 12pt 黑体 bold;color: rgb(0, 0, 0)");
    label_line_vol_CA->setGeometry(20,130,200,20);
    label_line_vol_CA->setStyleSheet("background: rgb(145, 145, 145);font: 12pt 黑体 bold;color: rgb(0, 0, 0)");

    label_3_5->setGeometry(400,10,100,20);
    label_3_5->setStyleSheet("background: rgb(145, 145, 145);font: 12pt 黑体 bold;color: rgb(255, 255, 255)");
    label_3_5->setText("电流：");
    label_3_6->setGeometry(400,30,200,20);
    label_3_6->setStyleSheet("background: rgb(145, 145, 145);font: 12pt 黑体 bold;color: rgb(0, 0, 0)");
    label_3_7->setGeometry(400,50,200,20);
    label_3_7->setStyleSheet("background: rgb(145, 145, 145);font: 12pt 黑体 bold;color: rgb(0, 0, 0)");
    label_3_8->setGeometry(400,70,200,20);
    label_3_8->setStyleSheet("background: rgb(145, 145, 145);font: 12pt 黑体 bold;color: rgb(0, 0, 0)");
    line3_1->setGeometry(380,10,1,150);
    line3_1->setFrameShape(QFrame::VLine);

}

void mainpage_A_V::labelShow()
{
    QFile file_read("/home/data/I_state.txt");
    if(file_read.open(QIODevice::ReadOnly | QIODevice::Text))
    {
        QTextStream in(&file_read);//创建一个对象让它去读文件
        if(in.readLine()=="1")//电流互感器未连接
        {
            label_disconnect_current->setText("电流互感器未连接！");
            label_disconnect_current->show();
            label_disconnect_current->raise();
        }
        else
        {
            label_disconnect_current->hide();
        }
        file_read.close();
    }

    QString aXiang = "";
    QString bXiang = "";
    QString cXiang = "";
    QFile fileVA("/home/data/V_A_state.txt");
    if(fileVA.exists())
    {
        if(fileVA.open(QIODevice::ReadOnly | QIODevice::Text))
        {
            QTextStream in(&fileVA);//创建一个对象让它去读文件
            aXiang = in.readLine();
        }
        fileVA.close();
    }
    QFile fileVB("/home/data/V_B_state.txt");
    if(fileVB.exists())
    {
        if(fileVB.open(QIODevice::ReadOnly | QIODevice::Text))
        {
            QTextStream in(&fileVB);//创建一个对象让它去读文件
            bXiang = in.readLine();
        }
        fileVB.close();
    }

    QFile fileVC("/home/data/V_C_state.txt");

    if(fileVC.exists())
    {
        if(fileVC.open(QIODevice::ReadOnly | QIODevice::Text))
        {
            QTextStream in(&fileVC);//创建一个对象让它去读文件
            cXiang = in.readLine();
        }
        fileVC.close();

    }

    QFile file_byqread1("/home/data/cspz_byqset.txt");
    if(file_byqread1.open(QIODevice::ReadOnly | QIODevice::Text))
    {
        QTextStream byqin(&file_byqread1);//创建一个对象让它去读文件
        byqin.readLine();
        QString one = byqin.readLine();

        if(one == "100")//电压互感器未连接
        {
            label_disconnect_voltage->hide();
        }
        else
        {
            if(aXiang != "" && bXiang != "" && cXiang != "")
            {
                if(aXiang == "1" && bXiang == "0" && cXiang == "0")
                {
                    label_disconnect_voltage->setText("A相电压互感器未连接");
                    label_disconnect_voltage->show();
                    label_disconnect_voltage->raise();
                }else if(aXiang == "1" && bXiang == "1" && cXiang == "0")
                {
                    label_disconnect_voltage->setText("AB相电压互感器未连接");
                    label_disconnect_voltage->show();
                    label_disconnect_voltage->raise();
                }else if(aXiang == "1" && bXiang == "0" && cXiang == "1")
                {
                    label_disconnect_voltage->setText("AC相电压互感器未连接");
                    label_disconnect_voltage->show();
                    label_disconnect_voltage->raise();
                }else if(aXiang == "1" && bXiang == "1" && cXiang == "1")
                {
                    label_disconnect_voltage->setText("ABC相电压互感器未连接");
                    label_disconnect_voltage->show();
                    label_disconnect_voltage->raise();
                }else if(aXiang == "0" && bXiang == "1" && cXiang == "0")
                {
                    label_disconnect_voltage->setText("B相电压互感器未连接");
                    label_disconnect_voltage->show();
                    label_disconnect_voltage->raise();
                }else if(aXiang == "0" && bXiang == "1" && cXiang == "1")
                {
                    label_disconnect_voltage->setText("BC相电压互感器未连接");
                    label_disconnect_voltage->show();
                    label_disconnect_voltage->raise();
                }else if(aXiang == "0" && bXiang == "0" && cXiang == "1")
                {
                    label_disconnect_voltage->setText("C相电压互感器未连接");
                    label_disconnect_voltage->show();
                    label_disconnect_voltage->raise();
                }else{
                    label_disconnect_voltage->hide();
                }
            }else if(aXiang != "" && bXiang != "" && cXiang == "")
            {
                if(aXiang == "1" && bXiang == "1")
                {
                    label_disconnect_voltage->setText("AB相电压互感器未连接");
                    label_disconnect_voltage->show();
                    label_disconnect_voltage->raise();
                }else if(aXiang == "1" && bXiang != "1")
                {
                    label_disconnect_voltage->setText("A相电压互感器未连接");
                    label_disconnect_voltage->show();
                    label_disconnect_voltage->raise();
                }else if(aXiang != "1" && bXiang == "1")
                {
                    label_disconnect_voltage->setText("B相电压互感器未连接");
                    label_disconnect_voltage->show();
                    label_disconnect_voltage->raise();
                }else{
                    label_disconnect_voltage->hide();
                }
            }else if(aXiang != "" && bXiang == "" && cXiang != "")
            {
                if(aXiang == "1" && cXiang == "1")
                {
                    label_disconnect_voltage->setText("AC相电压互感器未连接");
                    label_disconnect_voltage->show();
                    label_disconnect_voltage->raise();
                }else if(aXiang == "1" && cXiang != "1")
                {
                    label_disconnect_voltage->setText("A相电压互感器未连接");
                    label_disconnect_voltage->show();
                    label_disconnect_voltage->raise();
                }else if(aXiang != "1" && cXiang == "1")
                {
                    label_disconnect_voltage->setText("C相电压互感器未连接");
                    label_disconnect_voltage->show();
                    label_disconnect_voltage->raise();
                }else{
                    label_disconnect_voltage->hide();
                }
            }else if(aXiang == "" && bXiang != "" && cXiang != "")
            {
                if(bXiang == "1" && cXiang == "1")
                {
                    label_disconnect_voltage->setText("BC相电压互感器未连接");
                    label_disconnect_voltage->show();
                    label_disconnect_voltage->raise();
                }else if(bXiang == "1" && cXiang != "1")
                {
                    label_disconnect_voltage->setText("B相电压互感器未连接");
                    label_disconnect_voltage->show();
                    label_disconnect_voltage->raise();
                }else if(bXiang != "1" && cXiang == "1")
                {
                    label_disconnect_voltage->setText("C相电压互感器未连接");
                    label_disconnect_voltage->show();
                    label_disconnect_voltage->raise();
                }else{
                    label_disconnect_voltage->hide();
                }
            }else if(aXiang != "" && bXiang == "" && cXiang == "")
            {
                if(aXiang == "1")
                {
                    label_disconnect_voltage->setText("A相电压互感器未连接");
                    label_disconnect_voltage->show();
                    label_disconnect_voltage->raise();
                }else{
                    label_disconnect_voltage->hide();
                }
            }else if(aXiang == "" && bXiang != "" && cXiang == "")
            {
                if(bXiang == "1")
                {
                    label_disconnect_voltage->setText("B相电压互感器未连接");
                    label_disconnect_voltage->show();
                    label_disconnect_voltage->raise();
                }else{
                    label_disconnect_voltage->hide();
                }
            }else if(aXiang == "" && bXiang == "" && cXiang != "")
            {
                if(cXiang == "1")
                {
                    label_disconnect_voltage->setText("C相电压互感器未连接");
                    label_disconnect_voltage->show();
                    label_disconnect_voltage->raise();
                }else{
                    label_disconnect_voltage->hide();
                }
            }else{
                label_disconnect_voltage->hide();
            }
        }
        file_byqread1.close();
    }
}

void mainpage_A_V::onPageOneDataUpdate(float A_Current,float B_Current,float C_Current)
{
    bool ok;
    bool state = false;
    auto get_right = [&](QByteArray &array, int rig,int rems,int remd,int q = 10)->float{
        QString A1_temp = array.toHex();
        QString A1_temp1=A1_temp.right(rig);
        QString A1_temp2=A1_temp1.remove(rems, remd);
        return (float)A1_temp2.toInt(&ok,16)*1.0/q;
    };

    auto get_right_minus = [&](QByteArray &array,int q = 10)->float{
        QString transition1 = array.toHex();
        int transition2 = transition1.toUInt(&ok,16);
        return (float)transition2*1.0/q;
    };

    /*ABC相电压显示*/
    float C1_vol3 = get_right(SEC.SecFanStartTempeture,24,8,16,1000);
    ANA.C_vol = C1_vol3;
    label_3_2->setText("A相电压："+QString::number(C1_vol3,'f',3)+"V");//10进制转字符串,保留3位小数
    qDebug()<<"a xiang dian ya:"<<C1_vol3<<endl;

    float B1_vol3 = get_right(SEC.SecFanStartTempeture,16,8,8,1000);
    ANA.B_vol = B1_vol3;
    label_3_3->setText("B相电压："+QString::number(B1_vol3,'f',3)+"V");//10进制转字符串
    qDebug()<<"b xiang dian ya:"<<B1_vol3<<endl;

    QString A1_vol = SEC.SecFanStartTempeture.toHex();
    QString A1_vol1=A1_vol.right(8);
    float A1_vol2 = A1_vol1.toInt(&ok,16)*1.000/1000;//16进制转十进制
    ANA.A_vol = A1_vol2;
    label_3_4->setText("C相电压："+QString::number(A1_vol2,'f',3)+"V");//10进制转字符串
    qDebug()<<"c xiang dian ya:"<<A1_vol2<<endl;    

    QFileInfo fileall_v("/home/data/V_0_state.txt");
    if(fileall_v.exists())
    {
        QFile fileall2("/home/data/V_0_state.txt");
        if(fileall2.open(QIODevice::ReadOnly | QIODevice::Text))
        {
            QTextStream in(&fileall2);
            if(in.readLine()=="0" && ANA.A_vol < 10 && ANA.B_vol < 10 && ANA.C_vol < 10)
            {
                state = true;
            }else{
                state = false;
            }
            fileall2.close();
        }
    }else{
        state = false;
    }
    emit sigDispByqAlarm(state);

    /*线电压显示*/
    float AB_vol3 = get_right_minus(SEC.WheelLineVAB,100);
    label_line_vol_AB->setText("AB线电压："+QString::number(AB_vol3,'f',3)+"V");//10进制转字符串,保留3位小数
    qDebug()<<"ab xian dian ya:"<<AB_vol3<<endl;

    float BC_vol3 = get_right_minus(SEC.WheelLineVBC,100);
    label_line_vol_BC->setText("BC线电压："+QString::number(BC_vol3,'f',3)+"V");//10进制转字符串
    qDebug()<<"bc xian dian ya:"<<BC_vol3<<endl;

    float CA_vol2 = get_right_minus(SEC.WheelLineVCA,100);
    label_line_vol_CA->setText("CA线电压："+QString::number(CA_vol2,'f',3)+"V");//10进制转字符串
    qDebug()<<"ca xian dian ya:"<<CA_vol2<<endl;

    /*轮显电流显示*/
    label_3_6->setText("A相电流："+QString::number(A_Current,'f',3)+"A");//10进制转字符串
    qDebug()<<"a xiang dian liu:"<<A_Current<<endl;
    label_3_7->setText("B相电流："+QString::number(B_Current,'f',3)+"A");//10进制转字符串
    qDebug()<<"b xiang dian liu:"<<B_Current<<endl;
    label_3_8->setText("C相电流："+QString::number(C_Current,'f',3)+"A");//10进制转字符串
    qDebug()<<"c xiang dian liu:"<<C_Current<<endl;
}

void mainpage_A_V::onPageOneDataLabelUpdate()
{
    if(ANA.A_vol < 10 || ANA.B_vol < 10 || ANA.C_vol < 10)
    {
        labelShow();
    }
}
