
#include <QThread>
#include <QtDebug>
#include <QSignalSpy>
#include <QMutexLocker>
#include "serialui.h"

SerialUi::SerialUi(QObject *parent) : QObject(parent)
{
    static QThread* thread = new QThread;
    mSerialPort = new SerialPort();
    mSerialPort->moveToThread(thread);

    //connect(this,&SerialUi::sendConnect,mSerialPort,&SerialPort::receiveConnect);
    connect(this,&SerialUi::sendWrite,mSerialPort,&SerialPort::receiveWrite);
    connect(this,&SerialUi::sendOnlyWrite,mSerialPort,&SerialPort::receiveOnlyWrite);


    connect(this,&SerialUi::sendClose,mSerialPort,&SerialPort::reveiveClose);
    connect(this,&SerialUi::sendInit,mSerialPort,&SerialPort::receiveInit);


    connect(mSerialPort,&SerialPort::sendPortStatus,this,&SerialUi::receivePortStatus);
    connect(mSerialPort,&SerialPort::sendCrcError,this,&SerialUi::receiveCrcError);
    connect(mSerialPort,&SerialPort::sendTimeout,this,&SerialUi::receiveTimeout);

    thread->start();
    emit sendInit();

}

void SerialUi::serialWrite(QByteArray sendData)
{
    //qDebug() << QThread::currentThreadId() << "SerialUi";
    if(mSerialPort->getOpenStatus() == true)
        emit sendWrite(sendData);

    //    QString s;

    //    s += QString::number(0x10,16)+" ";
    //    s += QString::number(0x11,16)+" ";
    //    s += QString::number(0x12,16)+" ";
    //    qDebug()<<s;
    //QByteArray::toHex ()
}

void SerialUi::onlyWrite(QByteArray sendData)
{
    emit sendOnlyWrite(sendData);
}

void SerialUi::closeConnect()
{
    emit sendClose();

    //mSerialPort->closeConnect();
}

QByteArray SerialUi::serialWriteReponse(QByteArray sendData)
{
    QMutexLocker locker(&mutex);

    //qDebug() << __TIME__;
    //qDebug("serialWriteReponse enter addr:0x%x ", sendData.at(0));

    QByteArray data;
    if(mSerialPort->getOpenStatus() == false)
        return  data;

    emit sendWrite(sendData);
    QSignalSpy spy(mSerialPort, SIGNAL(sendReceiveData()));
    bool ret = spy.wait(500);
    if(ret == true)
        data = mSerialPort->serialRead();
    else{
        for(int i = 0; i<sendData.length(); i++)
            qDebug("timeout :0x%x ", sendData.at(i));
    }

    //qDebug() << __TIME__;
    //qDebug("serialWriteReponse leave data.size: %d ", data.size());

    return  data;
}

void SerialUi::receivePortStatus(bool status)
{
    emit portStatusChange(status);
}

void SerialUi::receiveData(QByteArray data)
{
    QList<int> rawData;
    rawData.clear();
    for(int i=0; i<data.length();i++){
        uint8_t value =data.at(i);
        rawData.append(value);
    }
    emit receiveRawDataChange(rawData);

    QString s;
    s.clear();
    for(int i=0; i<data.length();i++){
        uint8_t value =data.at(i);
        if(value < 0x10){
            s += "0"+QString::number(data.at(i),16)+" ";
        }
        else{
            s += QString::number(value,16)+" ";
        }
    }
    s.remove(s.length()-1,1);
    emit receiveDataChange(s);
}

void SerialUi::receiveCrcError()
{
    emit checkError();
}

void SerialUi::receiveTimeout()
{
    emit timeout();
}

bool SerialUi::serialConnect(QString port, QString baudrate, QString databits, QString parity, QString stopbits)
{
    // emit sendConnect( port,  baudrate,  databits,  parity,  stopbits);
    bool ret =  mSerialPort->serialConnect(port,  baudrate,  databits,  parity,  stopbits);
    // qDebug()<<ret;
    return  ret;
}
