266 lines
7.3 KiB
C++
266 lines
7.3 KiB
C++
#include "replay.h"
|
|
|
|
|
|
Replay::Replay(QObject *parent) : ThreadTemplet(parent)
|
|
{
|
|
setRunFrq(5);//100
|
|
|
|
logFile.clear();
|
|
|
|
Parser2_init(&parser);
|
|
Packer2_init(&packer);
|
|
|
|
}
|
|
|
|
Replay::~Replay()
|
|
{
|
|
qDebug() << "stop replay" << QThread::currentThreadId();
|
|
}
|
|
|
|
void Replay::process()//线程函数
|
|
{
|
|
QThread::msleep(5000);//5s后再发送
|
|
quint64 lastTimestamp = 0;
|
|
quint64 currentTimestamp = 0;
|
|
|
|
quint64 timeStamp = 0;
|
|
|
|
while (true)
|
|
{
|
|
if(isplaying)
|
|
{
|
|
if(!ispause)//不暂停
|
|
{
|
|
if(file)//已经有文件,那么可以读取
|
|
{
|
|
if(position <= file->size())
|
|
{
|
|
QByteArray data;
|
|
file->seek(position);
|
|
|
|
//data = file->readAll();
|
|
data = file->read(255);
|
|
|
|
for (QByteArray::const_iterator i = data.cbegin(); i != data.cend(); ++i)
|
|
{
|
|
position ++;
|
|
|
|
if(0x01 == Parser2_char(&parser,*i))
|
|
{
|
|
QByteArray raw;
|
|
QByteArray time;
|
|
|
|
raw.setRawData((const char*)parser.buff,parser.len);
|
|
|
|
/*
|
|
QString hex;
|
|
for (int var = 0; var < raw.size(); ++var) {
|
|
hex.append(QString::number((uint8_t)raw[var],16).toUpper() + " ");
|
|
}
|
|
qDebug() << hex << parser.len << endl;
|
|
*/
|
|
|
|
time = raw.left(8);
|
|
|
|
union {uint8_t B[8];uint64_t DW;}src;
|
|
|
|
src.B[0] = time.data()[7];
|
|
src.B[1] = time.data()[6];
|
|
src.B[2] = time.data()[5];
|
|
src.B[3] = time.data()[4];
|
|
src.B[4] = time.data()[3];
|
|
src.B[5] = time.data()[2];
|
|
src.B[6] = time.data()[1];
|
|
src.B[7] = time.data()[0];
|
|
|
|
currentTimestamp = src.DW;
|
|
|
|
if(lastTimestamp == 0)
|
|
{
|
|
lastTimestamp = currentTimestamp - 50;
|
|
}
|
|
|
|
if(((currentTimestamp - lastTimestamp) >0)&&((currentTimestamp - lastTimestamp) < 5000000))
|
|
{
|
|
timeStamp = currentTimestamp - lastTimestamp;
|
|
}
|
|
|
|
if((currentTimestamp - lastTimestamp) >0)
|
|
{
|
|
|
|
if((((double)timeStamp)/ multiple) > 50)
|
|
{
|
|
QThread::usleep(uint64_t(timeStamp / multiple));
|
|
}
|
|
else
|
|
{
|
|
QThread::usleep(1000);
|
|
}
|
|
|
|
if(timeStamp < 50)
|
|
{
|
|
timeStamp = 50;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
buff.clear();
|
|
buff = raw.mid(8);
|
|
|
|
//qDebug() << QString::number((uint8_t)buff[0],16).toUpper();
|
|
|
|
//读取一帧数据
|
|
//将百分比增加,或者位置增加
|
|
percentage = (float)position / file->size();
|
|
emit currentPercentage(percentage);
|
|
emit readReady();
|
|
|
|
lastTimestamp = currentTimestamp;
|
|
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
else
|
|
{
|
|
emit replayComplete();
|
|
isplaying = false;
|
|
}
|
|
}
|
|
}
|
|
else
|
|
{
|
|
QThread::msleep(1000);//如果没有播放,那么每秒钟检测一下是否要播放
|
|
}
|
|
}
|
|
else
|
|
{
|
|
QThread::msleep(1000);//如果没有播放,那么每秒钟检测一下是否要播放
|
|
}
|
|
|
|
if(isInterruptionRequested())//退出
|
|
{
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
//类相关函数
|
|
QByteArray Replay::readAll(void)
|
|
{
|
|
//把buff里面的数据返回
|
|
QByteArray data;
|
|
|
|
data.clear();
|
|
if(!buff.isEmpty())
|
|
{
|
|
data.append(buff);
|
|
buff.clear();
|
|
}
|
|
return data;
|
|
}
|
|
|
|
|
|
void Replay::setLogfile(const QString name, float percent)
|
|
{
|
|
if(name != logFile)
|
|
{
|
|
logFile = name;
|
|
//重置各种参数
|
|
percentage = 0;
|
|
ispause = false;
|
|
isplaying = false;
|
|
position = 0;
|
|
emit currentPercentage(percentage);
|
|
emit readReady();
|
|
emit replayComplete();
|
|
if(!file)
|
|
{
|
|
file = new QFile();
|
|
}
|
|
|
|
file->setFileName(logFile);
|
|
if(!file->open(QIODevice::ReadOnly))
|
|
{
|
|
if(!logFile.isEmpty())
|
|
{
|
|
QMessageBox::warning(nullptr,
|
|
tr("Error"),
|
|
tr("%1").arg(file->errorString()));
|
|
}
|
|
delete file;
|
|
file = nullptr;
|
|
}
|
|
|
|
}
|
|
else
|
|
{
|
|
logFile = name;
|
|
//重置各种参数
|
|
percentage = percent;
|
|
ispause = false;
|
|
isplaying = false;
|
|
}
|
|
|
|
qDebug() << name << percent;
|
|
}
|
|
|
|
void Replay::setPercentage(float value)
|
|
{
|
|
percentage = value;
|
|
|
|
qDebug() << "percent"<< value;
|
|
|
|
if(file)//已经有文件,那么可以读取
|
|
{
|
|
position = percentage * file->size();
|
|
}
|
|
|
|
}
|
|
|
|
void Replay::startReplay(bool flag)
|
|
{
|
|
qDebug() << "play flag" << flag;
|
|
if(flag)
|
|
{
|
|
if(ispause == true)
|
|
{
|
|
ispause = false;
|
|
}
|
|
|
|
if(isplaying == false)
|
|
{
|
|
isplaying = true;
|
|
}
|
|
|
|
//根据百分比设置位置
|
|
if(file)//已经有文件,那么可以读取
|
|
{
|
|
position = percentage * file->size();
|
|
}
|
|
//启动线程
|
|
start();
|
|
}
|
|
else
|
|
{
|
|
ispause = true;
|
|
}
|
|
}
|
|
|
|
|
|
void Replay::setMultiple(double value)
|
|
{
|
|
qDebug() << "multiple";
|
|
if(value != 0)
|
|
{
|
|
multiple = value;
|
|
qDebug() << "multiple" << multiple;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|