#include "QSerialPortUtil.h"

#include <iostream>
#include <QTimer>
#include <QDateTime>

QSerialPortUtil::QSerialPortUtil(QObject *parent) : QObject(parent)
{
    // 其他默认配置
    serial.setDataBits(QSerialPort::Data8);
    serial.setParity(QSerialPort::EvenParity);
    serial.setStopBits(QSerialPort::OneStop);
    serial.setFlowControl(QSerialPort::NoFlowControl);
}

void QSerialPortUtil::openSerialPort(QString portName, int baudRate)
{
    serial.setPortName(portName); // 串口名
    serial.setBaudRate(baudRate); // 波特率

    open = serial.open(QIODevice::ReadWrite);

    std::cout << QString("%1, %2, %3").arg(portName).arg(baudRate).arg(open).toStdString() << std::endl;

//    if (open == true)
//    {
//        // 绑定信号与槽
//        connect(&serial, &QSerialPort::readyRead,
//                this, &QSerialPortUtil::readData);

        // mock data received per second
        QTimer * timer = new QTimer(this);
        connect(timer, &QTimer::timeout,
                this, &QSerialPortUtil::mockReceivData);
        timer->start(1000);
//    }
}

void QSerialPortUtil::sendData(QByteArray data)
{
    if (this->open == true)
    {
        std::cout << data.toStdString() << std::endl;
        serial.write(data);
    }
}

void QSerialPortUtil::readData()
{
    QByteArray buffer = serial.readAll();
    recvBuff.append(buffer);
    if (recvBuff.endsWith("\r\n"))
    {
        emit dataRecieved(recvBuff);
        recvBuff.clear();
    }
}

bool QSerialPortUtil::isOpen()
{
    return this->open;
}

void QSerialPortUtil::mockReceivData()
{
    QDateTime now = QDateTime::currentDateTime();
/*
    QByteArray buffer;
    buffer.append("$XHTS");
    buffer.append("03100010");
    buffer.append(now.toString("yyMMddHHmmss"));
    buffer.append("276");

    qsrand(QDateTime::currentDateTime().toMSecsSinceEpoch());

    QByteArray statusBuff;
    QByteArray dataBuff;

    QString validStr = "1111010111001011";

    statusBuff.append("|").append("1").append(validStr.toLocal8Bit()).append("01");
    dataBuff.append("|").append("+0.000000000112");

    for (int i = 1; i < 16; i++)
    {
        int valid = validStr.mid(i, 1).toInt();
        int zero = qrand() % 2;
        double data = qrand() % 200 * 1e-12;

        dataBuff.append("|");
        if (valid == 0)
        {
            dataBuff.append("+0.000000000000");
        } else
        {
            dataBuff.append(zero == 0 ? "+" : "-");
            dataBuff.append(QString::number(data, 'f', 12));
        }
    }

    buffer.append(statusBuff).append(dataBuff).append("0");

    emit dataRecieved(buffer);
    */

    QByteArray buffer;
//    for (int i = 1; i <= 8; i++)
//    {
//        QString channel = QString("%1").arg(i);
//        QString channelRef = "1";
//        QString dataValue = QString("%1").arg(qrand() % 400);
//        QString level = QString("%1").arg(qrand() % 4 / (double) 10);
//        QString frameId = QString("%1").arg(now.toSecsSinceEpoch() % 10000);

//        buffer.append("$GL,")
//              .append("0,").append(channel + ",").append("1,").append(channelRef + ",")
//              .append(dataValue + ",").append("1,").append(level + ",").append(frameId + "*")
//              .append("00").append("\r\n");
//    }

    buffer.append("$GL,0,1,1,1,10304181796,0,1.0,59354*1D").append("\r\n");
    buffer.append("$GL,0,2,1,1,10304180924,0,1.0,59341*1C").append("\r\n");
    buffer.append("$GL,0,3,1,1,10304181782,0,1.0,59371*1D").append("\r\n");
    buffer.append("$GL,0,4,1,1,10304182288,0,1.0,59874*18").append("\r\n");
    buffer.append("$GL,0,5,1,1,10304182136,0,1.0,59695*1E").append("\r\n");
    buffer.append("$GL,0,6,1,1,10304182466,0,1.0,60015*19").append("\r\n");
    buffer.append("$GL,0,7,1,1,10304175641,0,1.0,41447*17").append("\r\n");
    buffer.append("$GL,0,8,1,1,10304175969,0,1.0,32686*16").append("\r\n");
    buffer.append("$GL,0,9,1,1,25393441727,0,1.0,32979*16").append("\r\n");
    buffer.append("$GL,0,10,1,1,59027139401,0,1.0,64835*2E").append("\r\n");
    buffer.append("$GL,0,11,1,1,70478437959,0,1.0,54457*2A").append("\r\n");
    buffer.append("$GL,0,12,1,1,10304168666,0,1.0,41512*2B").append("\r\n");
    buffer.append("$GL,0,13,1,1,4194443548,0,1.0,31190*1E").append("\r\n");
    buffer.append("$GL,0,14,1,1,58426511854,0,1.0,31289*21").append("\r\n");
    buffer.append("$GL,0,15,0,1,0,0,1.0,0*21").append("\r\n");
    buffer.append("$GL,0,16,0,1,0,0,1.0,0*22").append("\r\n");
    buffer.append("$GLN,0,192.168.000.126,255.255.255.000,192.168.000.001,192.168.001.126,255.255.255.000,192.168.001.001,255.255.255.255,2000,3000,3001*74").append("\r\n");
    buffer.append("$GL,1,2,2,211123,210930*1D").append("\r\n");
    emit dataRecieved(buffer);
}
