#include "mainpage_power.h"

mainpage_Power::mainpage_Power(QWidget *parent) : QWidget(parent)
{
    label_4_1=new QLabel(this);
    label_4_2=new QLabel(this);
    label_4_3=new QLabel(this);
    label_4_4=new QLabel(this);
    label_4_5=new QLabel(this);
    label_4_6=new QLabel(this);
    label_4_7=new QLabel(this);
    label_4_8=new QLabel(this);
    label_4_9=new QLabel(this);
    label_4_10=new QLabel(this);
    label_4_11=new QLabel(this);
    label_4_12=new QLabel(this);
    label_4_13=new QLabel(this);
    label_4_14=new QLabel(this);
    label_4_15=new QLabel(this);
    label_4_16=new QLabel(this);
    label_4_17=new QLabel(this);
    label_4_18=new QLabel(this);
    label_4_19=new QLabel(this);

    label_3_10=new QLabel(this);
    label_3_11=new QLabel(this);
    line3_2=new QFrame(this);

    line4_1=new QFrame(this);
    line4_3=new QFrame(this);

    label_4_1->setGeometry(30,10,200,20);
    label_4_1->setStyleSheet("background: rgb(145, 145, 145);font: 12pt 黑体 bold;color: rgb(255, 255, 255)");
    label_4_1->setText("有功功率：");
    label_4_2->setGeometry(30,30,220,20);
    label_4_2->setStyleSheet("background: rgb(145, 145, 145);font: 12pt 黑体 bold;color: rgb(0, 0, 0)");
    label_4_3->setGeometry(30,50,220,20);
    label_4_3->setStyleSheet("background: rgb(145, 145, 145);font: 12pt 黑体 bold;color: rgb(0, 0, 0)");
    label_4_4->setGeometry(30,70,220,20);
    label_4_4->setStyleSheet("background: rgb(145, 145, 145);font: 12pt 黑体 bold;color: rgb(0, 0, 0)");
    label_4_5->setGeometry(30,90,220,20);
    label_4_5->setStyleSheet("background: rgb(145, 145, 145);font: 12pt 黑体 bold;color: rgb(0, 0, 0)");
    label_4_6->setGeometry(250,10,220,20);
    label_4_6->setStyleSheet("background: rgb(145, 145, 145);font: 12pt 黑体 bold;color: rgb(255, 255, 255)");
    label_4_6->setText("无功功率：");
    label_4_7->setGeometry(250,30,220,20);
    label_4_7->setStyleSheet("background: rgb(145, 145, 145);font: 12pt 黑体 bold;color: rgb(0, 0, 0)");
    label_4_8->setGeometry(250,50,220,20);
    label_4_8->setStyleSheet("background: rgb(145, 145, 145);font: 12pt 黑体 bold;color: rgb(0, 0, 0)");
    label_4_9->setGeometry(250,70,220,20);
    label_4_9->setStyleSheet("background: rgb(145, 145, 145);font: 12pt 黑体 bold;color: rgb(0, 0, 0)");
    label_4_10->setGeometry(250,90,220,20);
    label_4_10->setStyleSheet("background: rgb(145, 145, 145);font: 12pt 黑体 bold;color: rgb(0, 0, 0)");

    label_4_11->setGeometry(470,10,100,20);
    label_4_11->setStyleSheet("background: rgb(145, 145, 145);font: 12pt 黑体 bold;color: rgb(255, 255, 255)");
    label_4_11->setText("功率因数：");
    label_4_12->setGeometry(470,30,150,20);
    label_4_12->setStyleSheet("background: rgb(145, 145, 145);font: 12pt 黑体 bold;color: rgb(0, 0, 0)");
    label_4_13->setGeometry(470,50,150,20);
    label_4_13->setStyleSheet("background: rgb(145, 145, 145);font: 12pt 黑体 bold;color: rgb(0, 0, 0)");
    label_4_14->setGeometry(470,70,150,20);
    label_4_14->setStyleSheet("background: rgb(145, 145, 145);font: 12pt 黑体 bold;color: rgb(0, 0, 0)");
    label_4_15->setGeometry(470,90,150,20);
    label_4_15->setStyleSheet("background: rgb(145, 145, 145);font: 12pt 黑体 bold;color: rgb(0, 0, 0)");

    label_3_10->setGeometry(640,10,140,20);
    label_3_10->setStyleSheet("background: rgb(145, 145, 145);font: 12pt 黑体 bold;color: rgb(255, 255, 255)");
    label_3_10->setText("视在功率：");
    label_3_11->setGeometry(640,30,140,20);
    label_3_11->setStyleSheet("background: rgb(145, 145, 145);font: 12pt 黑体 bold;color: rgb(0, 0, 0)");

    label_4_16->setGeometry(640,50,150,20);
    label_4_16->setStyleSheet("background: rgb(145, 145, 145);font: 12pt 黑体 bold;color: rgb(255, 255, 255)");
    label_4_16->setText("有功需量：");
    label_4_17->setGeometry(640,70,150,20);
    label_4_17->setStyleSheet("background: rgb(145, 145, 145);font: 12pt 黑体 bold;color: rgb(0, 0, 0)");

    label_4_18->setGeometry(640,90,150,20);
    label_4_18->setStyleSheet("background: rgb(145, 145, 145);font: 12pt 黑体 bold;color: rgb(255, 255, 255)");
    label_4_18->setText("无功需量：");
    label_4_19->setGeometry(640,110,150,20);
    label_4_19->setStyleSheet("background: rgb(145, 145, 145);font: 12pt 黑体 bold;color: rgb(0, 0, 0)");

    line3_2->setGeometry(460,10,1,150);
    line3_2->setFrameShape(QFrame::VLine);

    line4_1->setGeometry(240,10,1,150);
    line4_1->setFrameShape(QFrame::VLine);
    line4_3->setGeometry(630,10,1,150);
    line4_3->setFrameShape(QFrame::VLine);
}

void mainpage_Power::onPageTwoDataUpdate(float A_PowerFactor3,float B_PowerFactor3,float C_PowerFactor2)
{
    bool ok;

    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;
    };

    /*有功功率显示*/
    float A_Active_Power3 = get_right_minus(SEC.SecActivePowerAA,1000);
    ACC.A_Active_Power = A_Active_Power3;
    label_4_2->setText("A相有功功率："+QString::number(A_Active_Power3,'f',2)+"kW");//10进制转字符串
    qDebug()<<"a xiang you gong:"<<A_Active_Power3<<endl;

    float B_Active_Power3 = get_right_minus(SEC.SecActivePowerBB,1000);
    ACC.B_Active_Power = B_Active_Power3;
    label_4_3->setText("B相有功功率："+QString::number(B_Active_Power3,'f',2)+"kW");//10进制转字符串
    qDebug()<<"b xiang you gong:"<<B_Active_Power3<<endl;

    float C_Active_Power2 = get_right_minus(SEC.SecActivePowerCC,1000);
    ACC.C_Active_Power = C_Active_Power2;
    label_4_4->setText("C相有功功率："+QString::number(C_Active_Power2,'f',2)+"kW");//10进制转字符串
    qDebug()<<"c xiang you gong:"<<C_Active_Power2<<endl;

    /*ABC相有功功率显示*/
    float Com_Active_Power2 = get_right_minus(SEC.SecActivePowerCom,1000);
    ACC.Com_Active_Power = Com_Active_Power2;
    label_4_5->setText("总有功功率："+QString::number(Com_Active_Power2,'f',2)+"kW");//10进制转字符串
    qDebug()<<"zong you gong:"<<Com_Active_Power2<<endl;

    /*无功功率显示*/
    QString A_Reactive_PowerAA1 = SEC.SecReactivePowerAA.toHex();
    int A_Reactive_PowerAA2 = A_Reactive_PowerAA1.toUInt(&ok,16);
    float A_Reactive_PowerAA3 = A_Reactive_PowerAA2*1.00/1000;
    ACC.A_Reactive_Power = A_Reactive_PowerAA3;
    label_4_7->setText("A相无功功率："+QString::number(A_Reactive_PowerAA3,'f',2)+"kVar");//10进制转字符串
    qDebug()<<"a xiang wu gong:"<<A_Reactive_PowerAA3<<endl;

    QString A_Reactive_PowerBB1 = SEC.SecReactivePowerBB.toHex();
    int A_Reactive_PowerBB2 = A_Reactive_PowerBB1.toUInt(&ok,16);
    float A_Reactive_PowerBB3 = A_Reactive_PowerBB2*1.00/1000;
    ACC.B_Reactive_Power = A_Reactive_PowerBB3;
    label_4_8->setText("B相无功功率："+QString::number(A_Reactive_PowerBB3,'f',2)+"kVar");//10进制转字符串
    qDebug()<<"b xiang wu gong:"<<A_Reactive_PowerBB3<<endl;

    QString A_Reactive_PowerCC1 = SEC.SecReactivePowerCC.toHex();
    int A_Reactive_PowerCC2 = A_Reactive_PowerCC1.toUInt(&ok,16);
    float A_Reactive_PowerCC3 = A_Reactive_PowerCC2*1.00/1000;
    ACC.C_Reactive_Power = A_Reactive_PowerCC3;
    label_4_9->setText("C相无功功率："+QString::number(A_Reactive_PowerCC3,'f',2)+"kVar");//10进制转字符串
    qDebug()<<"c xiang wu gong:"<<A_Reactive_PowerCC3<<endl;

    /*ABC相无功功率显示*/
    QString Com_Reactive_Power = SEC.SecReactivePowerCom.toHex();
    int Com_Reactive_Power1=Com_Reactive_Power.toUInt(&ok,16);
    float Com_Reactive_Power2 = Com_Reactive_Power1*1.00/1000;//16进制转十进制
    ACC.Com_Reactive_Power = Com_Reactive_Power2;
    label_4_10->setText("总无功功率："+QString::number(Com_Reactive_Power2,'f',2)+"kVar");//10进制转字符串
    qDebug()<<"zong wu gong:"<<Com_Reactive_Power2<<endl;

    /*功率因数*/
    label_4_12->setText("A相功率因数："+QString::number(A_PowerFactor3,'f',2));//10进制转字符串
    qDebug()<<"a xiang PowerFactor:"<<A_PowerFactor3<<endl;
    label_4_13->setText("B相功率因数："+QString::number(B_PowerFactor3,'f',2));//10进制转字符串
    qDebug()<<"b xiang PowerFactor:"<<B_PowerFactor3<<endl;
    label_4_14->setText("C相功率因数："+QString::number(C_PowerFactor2,'f',2));//10进制转字符串
    qDebug()<<"c xiang PowerFactor:"<<C_PowerFactor2<<endl;

    /*功率因数和*/
    QString Com_PowerFactor = SEC.SecWheelPowerFactorCom.toHex();
    int Com_PowerFactor1=Com_PowerFactor.toUInt(&ok,16);
    float Com_PowerFactor2 = Com_PowerFactor1*1.0000/10000;//16进制转十进制
    ACC.Com_PowerFactor = Com_PowerFactor2;
    label_4_15->setText("总功率因数："+QString::number(Com_PowerFactor2,'f',2));//10进制转字符串
    qDebug()<<"zong PowerFactor:"<<Com_PowerFactor2<<endl;

    /*视在功率显示*/
    float shizai = Com_Active_Power2*Com_Active_Power2 + Com_Reactive_Power2*Com_Reactive_Power2;
    float Electroplate_Active_Power = sqrt(shizai);
    //float Electroplate_Active_Power=Com_Active_Power2+Com_Reactive_Power2;
    label_3_11->setText(QString::number(Electroplate_Active_Power,'f',2)+"kVA");//10进制转字符串
    qDebug()<<"shizai Power:"<<Electroplate_Active_Power<<endl;

    /*有/无功需量*/
    float Demand3 = get_right_minus(SEC.Demandhave,1000);
    label_4_17->setText(QString::number(Demand3,'f',2)+"kW");//10进制转字符串
    qDebug()<<"Demand have:"<<Demand3<<endl;

    float Demand_N2 = get_right_minus(SEC.Demandwu,1000);
    label_4_19->setText(QString::number(Demand_N2,'f',2)+"kVar");//10进制转字符串
    qDebug()<<"Demand wu:"<<Demand_N2<<endl;
}
