#include "mcu_arm1.h"
#include "uart1.h"
#include "QFile"
#include "net_thread.h"
#include "QTime"
#include <QFileDialog>
extern QSerialPort SerialPort1;
UART1 *uart_mcu1;
extern uint16_t CRC1;
bool tag1 = true;

mcu_arm1::mcu_arm1():m_serialPort()
{
}

void mcu_delay(unsigned int msec)
{
    QTime _Timer = QTime::currentTime().addMSecs(msec);
    while( QTime::currentTime() < _Timer )
        QCoreApplication::processEvents(QEventLoop::AllEvents, 100);
}

void mcu_arm1::GetRtcTime(){
    QByteArray data;
    uint8_t CRC_lowData;
    data[0] = 0xAA;
    data[1] = 0x55;
    data[2] = 0x17;
    data[3] = 0x01;
    data[4] = 0x00;
    data[5] = 0x01;
    data[6] = 0x00;
    uart_mcu1->ModBusCRC16(data);
    CRC_lowData = CRC1;
    CRC1 = CRC1>>8;
    data[7] = CRC_lowData;
    data[8] = CRC1;
    uart_mcu1->ShuJu_Write(data);
}

void mcu_arm1::setIorn(uint8_t state){
    QByteArray data;
    uint8_t CRC_lowData;
    data[0] = 0xAA;
    data[1] = 0x55;
    data[2] = 0x16;
    data[3] = 0x01;
    data[4] = 0x00;
    data[5] = 0x01;
    data[6] = state;
    uart_mcu1->ModBusCRC16(data);
    CRC_lowData = CRC1;
    CRC1 = CRC1>>8;
    data[7] = CRC_lowData;
    data[8] = CRC1;
    uart_mcu1->ShuJu_Write(data);
}

void mcu_arm1::setBuzzerState(uint8_t state){
    QByteArray data;
    uint8_t CRC_lowData;

    data[0] = 0xAA;
    data[1] = 0x55;
    data[2] = 0x25;
    data[3] = 0x01;
    data[4] = 0x00;
    data[5] = 0x01;
    data[6] = state;
    uart_mcu1->ModBusCRC16(data);
    CRC_lowData = CRC1;
    CRC1 = CRC1>>8;
    data[7] = CRC_lowData;
    data[8] = CRC1;
    uart_mcu1->ShuJu_Write(data);
}


//热像仪状态    23.8.16
void mcu_arm1::rxy_state()
{
    QByteArray data;
    uint8_t CRC_lowData;

    data[0] = 0xAA;
    data[1] = 0x55;
    data[2] = 0x30;
    data[3] = 0x01;
    data[4] = 0x00;
    data[5] = 0x01;

    uint8_t state = 0x00;
    //热像仪报警状态部分
    QFileInfo file_alarm6_1("/home/data/Pop_rxy1.txt");
    QFileInfo file_alarm6_2("/home/data/Pop_rxy2.txt");

    if(file_alarm6_2.exists())
    {
        state += 1;             //末位至1并左移一位
        state = state << 1;
    }
    else
    {
        state = state << 1;
    }

    if(file_alarm6_1.exists()) //热像仪掉线
    {
        state += 1;
        state = state << 1;
    }
    else
    {
        state = state << 1;
    }


    //热像仪连接状态部分
    QFile file_readr2("/home/data/connect_rxy2.txt");
    if(file_readr2.exists()){
        if(file_readr2.open(QIODevice::ReadOnly | QIODevice::Text))
        {
            QTextStream in(&file_readr2);//创建一个对象让它去读文件
            if(in.readLine()=="1")
            {
                state += 1;
            }
            else
            {

            }
        }
        file_readr2.close();
    }else {
        QFile file_write_rxy2("/home/data/connect_rxy2.txt");//文件路径
        if(file_write_rxy2.open(QFile::WriteOnly))
        {
            QTextStream setWIFI_data(&file_write_rxy2);
            setWIFI_data<<"0"<<endl;
        }
        file_write_rxy2.close();
    }

    state = state << 1;

    QFile file_readr("/home/data/connect_rxy1.txt");
    if(file_readr.exists()){
        if(file_readr.open(QIODevice::ReadOnly | QIODevice::Text))
        {
            QTextStream in(&file_readr);//创建一个对象让它去读文件
            if(in.readLine()=="1")    //代表用户需求开启热像仪1
            {
                state += 1;
            }
            else
            {

            }
        }
        file_readr.close();
        }else{
        QFile file_write_rxy1("/home/data/connect_rxy1.txt");//文件路径
        if(file_write_rxy1.open(QFile::WriteOnly))
        {
            QTextStream setWIFI_data(&file_write_rxy1);
            setWIFI_data<<"0"<<endl;
        }
        file_write_rxy1.close();
        }

    data[6] = state;

    uart_mcu1->ModBusCRC16(data);
    CRC_lowData = CRC1;
    CRC1 = CRC1>>8;
    data[7] = CRC_lowData;
    data[8] = CRC1;
    uart_mcu1->ShuJu_Write(data);
}


//热像仪1实时温度  23.8.16
void mcu_arm1::Realtime_temperature(int max,int min,int average)
{
    QByteArray data;
    uint8_t CRC_lowData;

    data[0] = 0xAA;
    data[1] = 0x55;
    data[2] = 0x26;
    data[3] = 0x01;
    data[4] = 0x00;
    data[5] = 0x0c;
    data[6] = max>>24;
    data[7] = max>>16;
    data[8] = max>>8;
    data[9] = max;
    data[10] = min>>24;
    data[11] = min>>16;
    data[12] = min>>8;
    data[13] = min;
    data[14] = average>>24;
    data[15] = average>>16;
    data[16] = average>>8;
    data[17] = average;

    uart_mcu1->ModBusCRC16(data);
    CRC_lowData = CRC1;
    CRC1 = CRC1>>8;
    data[18] = CRC_lowData;
    data[19] = CRC1;
    uart_mcu1->ShuJu_Write(data);

}


//热像仪2实时温度  23.8.16
void mcu_arm1::Realtime_temperature1(int max, int min, int average)
{
    QByteArray data;
    uint8_t CRC_lowData;

    data[0] = 0xAA;
    data[1] = 0x55;
    data[2] = 0x29;
    data[3] = 0x01;
    data[4] = 0x00;
    data[5] = 0x0c;
    data[6] = max>>24;
    data[7] = max>>16;
    data[8] = max>>8;
    data[9] = max;
    data[10] = min>>24;
    data[11] = min>>16;
    data[12] = min>>8;
    data[13] = min;
    data[14] = average>>24;
    data[15] = average>>16;
    data[16] = average>>8;
    data[17] = average;

    uart_mcu1->ModBusCRC16(data);
    CRC_lowData = CRC1;
    CRC1 = CRC1>>8;
    data[18] = CRC_lowData;
    data[19] = CRC1;
    uart_mcu1->ShuJu_Write(data);
}

//23.8.24 断电数据发送完成标志
void mcu_arm1::Powerlost_datapost(bool isok)
{
    QByteArray data;
    uint8_t CRC_lowData;

    data[0] = 0xAA;
    data[1] = 0x55;
    data[2] = 0x32;
    data[3] = 0x01;
    data[4] = 0x00;
    data[5] = 0x01;
    if(isok)
        data[6] = 0x01;
    else
        data[6] = 0x00;

    uart_mcu1->ModBusCRC16(data);
    CRC_lowData = CRC1;
    CRC1 = CRC1>>8;
    data[7] = CRC_lowData;
    data[8] = CRC1;
    uart_mcu1->ShuJu_Write(data);
}


void mcu_arm1::setTime()
{
    QByteArray data;
    uint8_t CRC_lowData;
    QDateTime nowtime = QDateTime::currentDateTime();
    QString year = nowtime.currentDateTime().toString("yyyy");
    QString month = nowtime.currentDateTime().toString("MM");
    QString day = nowtime.currentDateTime().toString("dd");
    QString hour = nowtime.currentDateTime().toString("hh");
    QString minute = nowtime.currentDateTime().toString("mm");
    QString second = nowtime.currentDateTime().toString("ss");
    uint16_t num = year.toInt();
    QString str = QString::number(num,16);
    QByteArray yeardata =QByteArray::fromHex(str.toLatin1());
    uint16_t num1 = month.toInt();
    QString str1 = QString::number(num1,16);
    QByteArray monthdata =QByteArray::fromHex(str1.toLatin1());
    uint16_t num2 = day.toInt();
    QString str2 = QString::number(num2,16);
    QByteArray daydata =QByteArray::fromHex(str2.toLatin1());
    uint16_t num3 = hour.toInt();
    QString str3 = QString::number(num3,16);
    QByteArray hourdata =QByteArray::fromHex(str3.toLatin1());
    uint16_t num4 = minute.toInt();
    QString str4 = QString::number(num4,16);
    QByteArray minutedata =QByteArray::fromHex(str4.toLatin1());
    uint16_t num5 = second.toInt();
    QString str5 = QString::number(num5,16);
    QByteArray seconddata =QByteArray::fromHex(str5.toLatin1());
    data[0]  = 0xAA;
    data[1]  = 0x55;
    data[2]  = 0x14;
    data[3]  = 0x01;
    data[4]  = 0x00;
    data[5]  = 0x07;
    data[6]  = yeardata.at(0);
    data[7]  = yeardata.at(1);
    data[8]  = monthdata.at(0);
    data[9]  = daydata.at(0);
    data[10] = hourdata.at(0);
    data[11] = minutedata.at(0);
    data[12] = seconddata.at(0);
    uart_mcu1->ModBusCRC16(data);
    CRC_lowData = CRC1;
    CRC1 = CRC1>>8;
    data[13] = CRC_lowData;
    data[14] = CRC1;
    uart_mcu1->ShuJu_Write(data);
}

void mcu_arm1::Set_Init()
{
    setWirelessT();
    mcu_delay(500);
    setoverheat();
    mcu_delay(500);
    setFanStartT();
    mcu_delay(500);
}

uint16_t ct;
int ct_u;
uint8_t wendua;
uint8_t wendub;
uint8_t wenduc;
uint8_t wendut;
void mcu_arm1::setCT(){

    QFile file("/home/data/cspz_byqset.txt");
    if(file.open(QIODevice::ReadOnly | QIODevice::Text))
    {
        QTextStream in(&file);
        ct = in.readLine().toInt();
        ct_u = in.readLine().toInt();
        wendua = in.readLine().toInt();
        wendub = in.readLine().toInt();
        wenduc = in.readLine().toInt();
        wendut = in.readLine().toInt();
    }
    file.close();
    QByteArray data;
    uint8_t CRC_lowData;

    data[0] = 0xAA;
    data[1] = 0x55;
    data[2] = 0x28;
    data[3] = 0x01;
    data[4] = 0x00;
    data[5] = 0x0A;
    data[6] = ct>>8;
    data[7] = ct;
    data[8] = ct_u>>24;
    data[9] = ct_u>>16;
    data[10] = ct_u>>8;
    data[11] = ct_u;

    data[12] = wendua;
    data[13] = wendub;
    data[14] = wenduc;
    data[15] = wendut;

    uart_mcu1->ModBusCRC16(data);
    CRC_lowData = CRC1;
    CRC1 = CRC1>>8;
    data[16] = CRC_lowData;
    data[17] = CRC1;
    uart_mcu1->ShuJu_Write(data);
}

uint16_t hour;
uint16_t minute;
uint16_t keeptime;

void mcu_arm1::setFanTiming(uint8_t state){
    QByteArray time;

    QFile file("/home/data/FanTiming.txt");
    if(file.open(QIODevice::ReadOnly | QIODevice::Text))
    {
        QTextStream in(&file);
        hour = in.readLine().toInt();
        minute = in.readLine().toInt();
        keeptime = in.readLine().toInt();
    }
    file.close();
    QByteArray data;
    uint8_t CRC_lowData;

    data[0] = 0xAA;
    data[1] = 0x55;
    data[2] = 0x24;
    data[3] = 0x01;
    data[4] = 0x00;
    data[5] = 0x04;
    data[6] = hour;
    data[7] = minute;
    data[8] = keeptime;
    data[9] = state;

    uart_mcu1->ModBusCRC16(data);
    CRC_lowData = CRC1;
    CRC1 = CRC1>>8;
    data[10] = CRC_lowData;
    data[11] = CRC1;
    uart_mcu1->ShuJu_Write(data);
}

void mcu_arm1::setFanState(uint8_t state){
    QByteArray data;
    uint8_t CRC_lowData;

    data[0] = 0xAA;
    data[1] = 0x55;
    data[2] = 0x23;
    data[3] = 0x01;
    data[4] = 0x00;
    data[5] = 0x01;
    data[6] = state;
    uart_mcu1->ModBusCRC16(data);
    CRC_lowData = CRC1;
    CRC1 = CRC1>>8;
    data[7] = CRC_lowData;
    data[8] = CRC1;
    uart_mcu1->ShuJu_Write(data);
}

void mcu_arm1::setEnable(uint8_t enable){
    QByteArray data;
    uint8_t CRC_lowData;

    data[0] = 0xAA;
    data[1] = 0x55;
    data[2] = 0x12;
    data[3] = 0x01;
    data[4] = 0x00;
    data[5] = 0x01;
    data[6] = enable;
    uart_mcu1->ModBusCRC16(data);
    CRC_lowData = CRC1;//6225
    CRC1 = CRC1>>8;
    data[7] = CRC_lowData;
    data[8] = CRC1;
    qDebug()<<"1111111111111111111"<<data<<endl;
    uart_mcu1->ShuJu_Write(data);
}
uint16_t start_temperature;
uint16_t stop_temperature;
void mcu_arm1::setFanStartT()
{

    QFile file("/home/data/FanTemperature.txt");
    if(file.open(QIODevice::ReadOnly | QIODevice::Text))
    {
        QTextStream in(&file);
        start_temperature = in.readLine().toInt();
        stop_temperature = in.readLine().toInt();
    }
    file.close();
    QByteArray data;
    uint8_t lowData;
    uint8_t CRC_lowData;

    data[0] = 0xAA;
    data[1] = 0x55;
    data[2] = 0x22;
    data[3] = 0x01;
    data[4] = 0x00;
    data[5] = 0x04;
    lowData = start_temperature;
    start_temperature = start_temperature >> 8;
    data[6] = start_temperature;
    data[7] = lowData;
    lowData = stop_temperature;
    stop_temperature = stop_temperature >> 8;
    data[8] = stop_temperature;
    data[9] = lowData;
    uart_mcu1->ModBusCRC16(data);
    CRC_lowData = CRC1;
    CRC1 = CRC1>>8;
    data[10] = CRC_lowData;
    data[11] = CRC1;
    uart_mcu1->ShuJu_Write(data);
}
uint16_t alarm_temperature;
uint16_t trip_temperature;

void mcu_arm1::setoverheat()
{

    QFile file("/home/data/overheat.txt");
    if(file.open(QIODevice::ReadOnly | QIODevice::Text))
    {
        QTextStream in(&file);
        alarm_temperature = in.readLine().toInt();
        trip_temperature = in.readLine().toInt();
    }
    file.close();
    QByteArray data;
    uint8_t lowData;
    uint8_t CRC_lowData;

    data[0] = 0xAA;
    data[1] = 0x55;
    data[2] = 0x27;
    data[3] = 0x01;
    data[4] = 0x00;
    data[5] = 0x04;
    lowData = alarm_temperature;
    alarm_temperature = alarm_temperature >> 8;
    data[6] = alarm_temperature;
    data[7] = lowData;
    lowData = trip_temperature;
    trip_temperature = trip_temperature >> 8;
    data[8] = trip_temperature;
    data[9] = lowData;
    uart_mcu1->ModBusCRC16(data);
    CRC_lowData = CRC1;
    CRC1 = CRC1>>8;
    data[10] = CRC_lowData;
    data[11] = CRC1;
    uart_mcu1->ShuJu_Write(data);
}

void mcu_arm1::setWirelessT()
{
        QString line1;
        uint8_t cout = 0;
        uint8_t len_cout = 0;
        QByteArray data;
        uint8_t CRC_lowData;
        bool ok;
        QFile file("/home/data/wireless_adress.txt");
        if(file.open(QIODevice::ReadOnly | QIODevice::Text))
        {
        QTextStream in(&file);
        while (!in.atEnd())
        {
        line1= in.readLine();
        if(line1!=nullptr)
        {
        qlonglong hexnum = line1.toLongLong(&ok,10);
        QString str=QString::number(hexnum,16);
        QByteArray finish =QByteArray::fromHex(str.toLatin1());
        data[cout+7] = finish[0];
        data[cout+8] = finish[1];
        data[cout+9] = finish[2];
        data[cout+10] = finish[3];
        finish = 0;
        cout+=4;
        len_cout++;
        }
        }
        data[0] = 0xAA;
        data[1] = 0x55;
        data[2] = 0x21;
        data[3] = 0x01;
        data[4] = (len_cout*4+1)/256;
        data[5] = (len_cout*4+1)%256;
        data[6] = len_cout;
        uart_mcu1->ModBusCRC16(data);
        CRC_lowData = CRC1;
        CRC1 = CRC1>>8;
        data[7+4*len_cout] = CRC_lowData;
        data[8+4*len_cout] = CRC1;
        uart_mcu1->ShuJu_Write(data);
        if(in.atEnd())
        {
        cout = 0;
        len_cout = 0;
        }
        }
        file.close();
}

void mcu_arm1::testSerialPort(uint8_t uart){
    QByteArray data;
    uint8_t CRC_lowData;

    data[0] = 0xAA;
    data[1] = 0x55;
    if(uart == 1)
    {
    data[2] = 0x10;
    }
    else if(uart == 2)
    {
    data[2] = 0x11;
    }
//    if(tag1){
//        data[3] = 0x01;
//    } else {
//        data[3] = 0x03;
//    }
    data[3] = 0x03;
    tag1 = !tag1;
    data[4] = 0x00;
    data[5] = 0x01;
    data[6] = 0x0;
    uart_mcu1->ModBusCRC16(data);
    CRC_lowData = CRC1;
    CRC1 = CRC1>>8;
    data[7] = CRC_lowData;
    data[8] = CRC1;
    qDebug()<<"111111111111111111111111data:"<<data<<endl;
    uart_mcu1->ShuJu_Write(data);
}

mcu_arm1::~mcu_arm1(){
}




