#include "mainpage_rxy.h"

#include <QMessageBox>
#include "mcu_arm1.h"

mcu_arm1 *ARM_2; //调用串口通信


void Delay_MSec_1(unsigned int msec)
{
    QEventLoop eventloop;
    QTimer::singleShot((int)msec, &eventloop, SLOT(quit()));
    eventloop.exec();
}

mainpage_Rxy::mainpage_Rxy(QWidget *parent) : QWidget(parent)
{
    button9_1=new QPushButton(this);
    button9_2=new QPushButton(this);
    label_9_1=new QLabel(this);
    label_9_2=new QLabel(this);

    label_disconnect_rxy1=new QLabel(this);
    label_disconnect_rxy1->setGeometry(10,10,370,100);
    label_disconnect_rxy1->setAlignment(Qt::AlignCenter);
    label_disconnect_rxy1->setStyleSheet("background: rgb(145, 145, 145);font: 12pt 黑体 bold;color: rgb(240, 0, 0)");
    label_disconnect_rxy2=new QLabel(this);
    label_disconnect_rxy2->setGeometry(380,10,370,100);
    label_disconnect_rxy2->setAlignment(Qt::AlignCenter);
    label_disconnect_rxy2->setStyleSheet("background: rgb(145, 145, 145);font: 12pt 黑体 bold;color: rgb(240, 0, 0)");

    button9_1->setGeometry(150,50,100,30);
    button9_1->setStyleSheet("background: rgb(0, 171, 171);font: 12pt 黑体 bold;color: rgb(255, 255, 255)");
    button9_1->setText("热像仪1拍摄");
    label_9_1->setGeometry(150,100,200,30);
    label_9_1->setStyleSheet("background: rgb(145, 145, 145);font: 12pt 黑体 bold;color: rgb(0, 0, 0)");

    button9_2->setGeometry(550,50,100,30);
    button9_2->setStyleSheet("background: rgb(0, 171, 171);font: 12pt 黑体 bold;color: rgb(255, 255, 255)");
    button9_2->setText("热像仪2拍摄");
    label_9_2->setGeometry(550,100,200,30);
    label_9_2->setStyleSheet("background: rgb(145, 145, 145);font: 12pt 黑体 bold;color: rgb(0, 0, 0)");

    rxy_show_picture=new QLabel(this);
    rxy_show_picture->setGeometry(0,1,380,200);
    rxy_show_picture->hide();
    rxy_show_picture->installEventFilter(this);//安装鼠标事件
    rxy_show_picture1=new QLabel(this);
    rxy_show_picture1->setGeometry(380,1,380,200);
    rxy_show_picture1->hide();
    rxy_show_picture1->installEventFilter(this);//安装鼠标事件

    label_9_3=new QLabel(this);
    label_9_3->setGeometry(300,30,250,30);
    label_9_3->setAlignment(Qt::AlignCenter);
    label_9_3->setStyleSheet("background: rgb(145, 145, 145);font: 12pt 黑体 bold;color: rgb(0, 0, 0)");
    label_9_3->setText("热像仪未准备就绪，请稍候...");

    connect(button9_1,SIGNAL(clicked()),this,SLOT(button9_1_clicked()));
    connect(button9_2,SIGNAL(clicked()),this,SLOT(button9_2_clicked()));
}

bool mainpage_Rxy::eventFilter(QObject *watch, QEvent *evn)
{
    if(watch == rxy_show_picture && evn->type() == QEvent::MouseButtonPress)
    {
        emit sigPageEightSend();
        rxy_show_picture->close();
    }

    if(watch == rxy_show_picture1 && evn->type() == QEvent::MouseButtonPress)
    {
        emit sigPageEightSend();
        rxy_show_picture1->close();
    }

    return QWidget::eventFilter(watch,evn);
}

void mainpage_Rxy::button9_1_clicked()//热像仪1按钮槽函数
{
    button9_1->hide();

    button9_2->setEnabled(false);    //防止传输数据粘包    23.8.30
    label_9_2->setText("等待获取热像仪1图片...");

    emit sigPageEightSend();

    QString Thermal_imager_Alarm_file_rxy1;
    label_9_1->setText("正在获取热像仪1图片...");
    QFile file_write1("/home/data/rxy_show1.txt");//文件路径
    if(file_write1.open(QFile::WriteOnly))
    {
        QTextStream setWIFI_data(&file_write1);
    }
    file_write1.close();
    QFileInfo file("/home/data/rxy_can_show1.txt");
    for(int i =0;i<10;i++)
    {
        Delay_MSec_1(1000);

        if(file.exists())
        {
            break;
        }
        else
        {
            QFile file_write1("/home/data/rxy_show1.txt");//文件路径

            if(file_write1.exists())
            {
            }
            else
            {
                if(file_write1.open(QFile::WriteOnly))
                {
                    QTextStream setWIFI_data(&file_write1);
                }
                file_write1.close();
            }
        }
    }

    if(file.exists())
    {
        std::system("rm -rf /home/data/rxy_can_show1.txt");
    }

    QFile file_read("/home/data/rxy_1.txt");
    if(file_read.open(QIODevice::ReadOnly | QIODevice::Text))
    {
        QTextStream in(&file_read);//创建一个对象让它去读文件
        Thermal_imager_Alarm_file_rxy1=in.readLine();
    }
    file_read.close();
    label_9_1->clear();
    button9_1->show();
    button9_1->raise();
    QPixmap *pixmap = nullptr;
    pixmap = new QPixmap(Thermal_imager_Alarm_file_rxy1);
    pixmap->scaled(rxy_show_picture->size(), Qt::KeepAspectRatio);//设置图片适应lable大小
    rxy_show_picture->setScaledContents(true);
    rxy_show_picture->setPixmap(*pixmap);
    rxy_show_picture->show();
    rxy_show_picture->raise();
    QFile fileTemp("/home/data/rxy_1.txt");//删除热像仪报警文件
    fileTemp.remove();
    if(pixmap != nullptr)
    {
        delete pixmap;
        pixmap = nullptr;
    }
    QFileInfo file_image("/home/data/IMAGE/");//删文件夹IMAGE
    if(file_image.exists() == true)//检测是否存在IMAGE文件
    {
        std::system("rm -rf /home/data/IMAGE/");
    }
    std::system("mkdir /home/data/IMAGE/");

    //实时温度部分   数据正常读取，已处理
    QFile file_temperature("/home/data/Real-TimeTemperature.txt");
    if(file_temperature.exists())
    {
        if(file_temperature.open(QIODevice::ReadOnly))
        {
            QTextStream in(&file_temperature);
            QString maxTemperature = in.readLine();
            QString minTemperature = in.readLine();
            QString averageTemperature = in.readLine();
            qDebug() << maxTemperature << endl
                     <<minTemperature << endl
                    <<averageTemperature;

            int max = maxTemperature.toFloat() * 100;
            int min = minTemperature.toFloat() * 100;
            int ave = averageTemperature.toFloat() * 100;
            ARM_2->Realtime_temperature(max,min,ave);


        }
        file_temperature.close();
    }

    label_9_2->clear();
    button9_2->setEnabled(true);    //防止传输数据粘包    23.8.30


    QFile file_temperature_temp("/home/data/Real-TimeTemperature.txt");//删除温度存储部分
    file_temperature_temp.remove();

}

void mainpage_Rxy::button9_2_clicked()//热像仪2按钮槽函数
{
    button9_2->hide();


    button9_1->setEnabled(false);    //防止传输数据粘包    23.8.30
    label_9_1->setText("等待获取热像仪2图片...");

    emit sigPageEightSend();

    QString Thermal_imager_Alarm_file_rxy2;
    label_9_2->setText("正在获取热像仪2图片...");
    QFile file_write1("/home/data/rxy_show2.txt");//文件路径
    if(file_write1.open(QFile::WriteOnly))
    {
        QTextStream setWIFI_data(&file_write1);
    }
    file_write1.close();
    QFileInfo file("/home/data/rxy_can_show2.txt");
    for(int i =0;i<10;i++)
    {
        Delay_MSec_1(1000);

        if(file.exists())
        {
            break;
        }
        else
        {
            QFile file_write1("/home/data/rxy_show2.txt");//文件路径

            if(file_write1.exists())
            {
            }
            else
            {
                if(file_write1.open(QFile::WriteOnly))
                {
                    QTextStream setWIFI_data(&file_write1);
                }
                file_write1.close();
            }
        }
    }
    if(file.exists())
    {
        std::system("rm -rf /home/data/rxy_can_show2.txt");
    }
    QFile file_read("/home/data/rxy_2.txt");
    if(file_read.open(QIODevice::ReadOnly | QIODevice::Text))
    {
        QTextStream in(&file_read);//创建一个对象让它去读文件
        Thermal_imager_Alarm_file_rxy2=in.readLine();
    }
    file_read.close();

    label_9_2->clear();
    button9_2->show();
    button9_2->raise();
    QPixmap *pixmap = nullptr;
    pixmap = new QPixmap(Thermal_imager_Alarm_file_rxy2);
    pixmap->scaled(rxy_show_picture1->size(), Qt::KeepAspectRatio);//设置图片适应lable大小
    rxy_show_picture1->setScaledContents(true);
    rxy_show_picture1->setPixmap(*pixmap);
    rxy_show_picture1->show();
    rxy_show_picture1->raise();
    QFile fileTemp("/home/data/rxy_2.txt");//删除热像仪报警文件
    fileTemp.remove();

    if(pixmap != nullptr)
    {
        delete pixmap;
        pixmap = nullptr;
    }
    QFileInfo file_iamge1("/home/data/IMAGE1/");//删文件夹IMAGE
    if(file_iamge1.exists() == true)//检测是否存在IMAGE文件
    {
        std::system("rm -rf /home/data/IMAGE1/");
    }
    std::system("mkdir /home/data/IMAGE1/");

    //实时温度部分   数据正常读取，串口传输数据
    QFile file_temperature("/home/data/Real-TimeTemperature1.txt");
    if(file_temperature.exists())
    {
        if(file_temperature.open(QIODevice::ReadOnly))
        {
            QTextStream in(&file_temperature);
            QString maxTemperature = in.readLine();
            QString minTemperature = in.readLine();
            QString averageTemperature = in.readLine();
            qDebug() << maxTemperature << endl
                     <<minTemperature << endl
                    <<averageTemperature;

            int max = maxTemperature.toFloat() * 100;
            int min = minTemperature.toFloat() * 100;
            int ave = averageTemperature.toFloat() * 100;
            ARM_2->Realtime_temperature1(max,min,ave);


        }
        file_temperature.close();
    }

    label_9_1->clear();
    button9_1->setEnabled(true);    //防止传输数据粘包    23.8.30

    QFile file_temperature_temp("/home/data/Real-TimeTemperature1.txt");//删除温度存储部分
    file_temperature_temp.remove();
}

void mainpage_Rxy::onPageEightDataUpdate()
{
    bool rxy1_state = false,rxy2_state = false;

    QFile file_read_con1("/home/data/connect_rxy1.txt");
    if(file_read_con1.open(QIODevice::ReadOnly | QIODevice::Text))
    {
        QTextStream in(&file_read_con1);//创建一个对象让它去读文件
        if(in.readLine()=="1")
        {
            rxy1_state=false;
            label_disconnect_rxy1->hide();
        }
        else
        {
            rxy1_state=true;
            label_disconnect_rxy1->setText("热像仪1未启用！");
            label_disconnect_rxy1->show();
            label_disconnect_rxy1->raise();
        }
    }
    file_read_con1.close();

    QFile file_read_con2("/home/data/connect_rxy2.txt");
    if(file_read_con2.open(QIODevice::ReadOnly | QIODevice::Text))
    {
        QTextStream in(&file_read_con2);//创建一个对象让它去读文件
        if(in.readLine()=="1")
        {
            rxy2_state=false;
            label_disconnect_rxy2->hide();
        }
        else
        {
            rxy2_state=true;
            label_disconnect_rxy2->setText("热像仪2未启用！");
            label_disconnect_rxy2->show();
            label_disconnect_rxy2->raise();
        }
    }
    file_read_con2.close();
    emit sigDispRxyAlarm(rxy1_state,rxy2_state);
}

void mainpage_Rxy::onRxyLabelSet()
{
    label_9_3->setText("");
}

void mainpage_Rxy::onRxyButtonShow()
{
    button9_1->show();
    button9_1->raise();
    button9_2->show();
    button9_2->raise();
}

