//
// Created by xin on 24-12-12.
//

#include "TemperaTureWoker.h"
#include "fstream"
#include <iostream>
#include <qthread.h>
#include <QDebug>
#include <logout.h>
TemperaTureWoker::TemperaTureWoker() {

}
//
// TemperaTureWoker::TemperaTureWoker(json jsonconfig) {
//
//     if(jsonconfig.contains("portname")&&jsonconfig["portname"].is_string())
//     {
//
//     }else {
//         jsonconfig["portname"] = "ttyS2";
//     }
//     if(jsonconfig.contains("baudrate")&&jsonconfig["baudrate"].is_number()) {
//     }
//     else {
//         jsonconfig["baudrate"] = 115200;
//     }
//     m_SerialPort->setPortName(jsonconfig["portname"].get<std::string>().c_str());
//     m_SerialPort->setBaudRate(jsonconfig["baudrate"].get<int>());
//     if (!m_SerialPort->open(QIODevice::ReadWrite))
//     {
//         std::cout << "Error open serial port" << std::endl;
//         m_isInit = false;
//         return;
//     }
//     else {
//         std::cout << "Open serial port success" << std::endl;
//         m_isInit = true;
//     }
//     tempera_group temp1;
//     tempera_group temp2;
//     tempera_group temp3;
//     tempera_group temp4;
//     m_Tempreaturegroups.append(temp1);
//     m_Tempreaturegroups.append(temp2);
//     m_Tempreaturegroups.append(temp3);
//     m_Tempreaturegroups.append(temp4);
//
//
//     ReadTempreature();
// }

void TemperaTureWoker::OpenCom(QString portname, int baudrate) {
    if (m_SerialPort== nullptr)
    {
        m_SerialPort=new QSerialPort();
    }
    m_SerialPort->setPortName(portname);
    m_SerialPort->setBaudRate(baudrate);
    if (!m_SerialPort->open(QIODevice::ReadWrite))
    {
       // std::cout << "Error open serial port" << std::endl;
        qDebug()<< "Error open serial port";
        m_isInit = false;
        return;
    }
    else {
        //std::cout << "Open serial port success" << std::endl;
        qDebug()<< "Open serial port success";
        m_isInit = true;
    }
    tempera_group temp1;
    tempera_group temp2;
    tempera_group temp3;
    tempera_group temp4;
    m_Tempreaturegroups.append(temp1);
    m_Tempreaturegroups.append(temp2);
    m_Tempreaturegroups.append(temp3);
    m_Tempreaturegroups.append(temp4);


    ReadTempreature();
}

void TemperaTureWoker::ReadTempreature() {
    if (m_isInit) {
        m_SerialPort->write("{\"command\":\"get_temp\"}");
        m_SerialPort->waitForReadyRead(1000);
        //QThread::msleep(100);
        QByteArray data = m_SerialPort->read(1000);
        int lenth = data.size();
        //QThread::msleep(100);
        int lenthofnow=0;
        while (lenthofnow != lenth)
        {
            lenthofnow = lenth;
            m_SerialPort->waitForReadyRead(200);
            data.append(m_SerialPort->readAll());
            lenth = data.size();
            //QThread::msleep(100);
        }

        if (lenth==0) {
            //std::cout << "No data" << std::endl;
            qDebug()<< "No data";
            logout("TemperaSersor","no data from tempera sensor",10);

            return;
        }
       std::cout << data.toStdString()<< std::endl;
        logout("TemperaSersor",data.toHex(),5);
        qDebug() << data;
        try {
            json::parse(data.toStdString());
        }
        catch (...) {
            logout("TemperaSersor","data from tempera sensor parse error",3);
            return;

        }
        json jsondata = json::parse(data.toStdString());
        for (int j = 0; j < 4; ++j) {
            if(jsondata.contains("Temperature"+std::to_string(j+1))) {
                int number = jsondata["Temperature"+std::to_string(j+1)]["number"].get<int>();
                for (int i = 1; i < number+1; ++i) {
                    QString index = "A"+QString::number(i);
                    float temp=jsondata["Temperature"+std::to_string(j+1)][index.toStdString()].get<float>();
                    m_Tempreaturegroups[j].tempera[i-1]=temp;

                }

            }
        }
        //��ӡm_Tempreaturegroups
        for (int i = 0; i < 4; ++i) {
            std::cout << "Group " << i << ":\t|";
            for (int j = 0; j < 8; ++j) {
                // ���ÿ��Ϊ 10��ʹ�ÿո���䣬��ͨ�� std::internal ʵ�����־���
                //std::cout << std::setw(10) << std::setfill(' ') << std::internal
                // qDebug().noquote().nospace()<< m_Tempreaturegroups[i].tempera[j] << ":\t|";
                std::cout << std::setw(10) << std::setfill(' ') << std::internal
                          << m_Tempreaturegroups[i].tempera[j] << "\t|";
            }
            //qDebug() <<"\n";
            std::cout << std::endl;
        }
       std::cout << std::endl;

    } else {
        //std::cout << "Serial port is not init" << std::endl;
        qDebug()<< "Serial port is not init";

    }
}
