374 lines
9.4 KiB
C++
374 lines
9.4 KiB
C++
#include "replay.h"
|
|
|
|
|
|
Replay::Replay(QObject *parent) : QObject(parent)
|
|
{
|
|
//main thread
|
|
running_flag = false;
|
|
thread = new QThread();
|
|
this->moveToThread(thread);
|
|
connect(thread, SIGNAL(started()), this, SLOT(process()));
|
|
setRunFrq(5);//100
|
|
|
|
logFile.clear();
|
|
}
|
|
|
|
Replay::~Replay()
|
|
{
|
|
if(thread->isRunning())
|
|
{
|
|
thread->quit();
|
|
thread->wait();
|
|
}
|
|
delete thread;
|
|
thread = nullptr;
|
|
}
|
|
|
|
//线程相关函数
|
|
void Replay::setRunFrq(uint32_t frq)
|
|
{
|
|
if((frq != 0)||(frq <= 1000))
|
|
{
|
|
running_frq = frq;
|
|
qDebug() << "set thread running frquency:" <<frq <<"Hz";
|
|
}
|
|
}
|
|
|
|
void Replay::start()
|
|
{
|
|
if(!thread)
|
|
{
|
|
thread = new QThread();
|
|
this->moveToThread(thread);
|
|
connect(thread, SIGNAL(started()), this, SLOT(process()));
|
|
connect(thread, &QThread::finished,thread,&QObject::deleteLater);
|
|
}
|
|
|
|
if(!thread->isRunning())
|
|
{
|
|
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
|
|
{
|
|
running_flag = true;
|
|
thread->start();
|
|
qDebug() << "thread start" << running_flag;
|
|
}
|
|
|
|
}
|
|
else
|
|
{
|
|
running_flag = true;
|
|
thread->start();
|
|
qDebug() << "thread start" << running_flag;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
qDebug() << "thread has started";
|
|
}
|
|
}
|
|
|
|
|
|
void Replay::stop()
|
|
{
|
|
if(thread->isRunning())
|
|
{
|
|
running_flag = false;
|
|
|
|
disconnect(thread, nullptr, nullptr, nullptr);
|
|
|
|
thread->quit();
|
|
thread->deleteLater();
|
|
}
|
|
else
|
|
{
|
|
qDebug() << "thread is not running";
|
|
}
|
|
}
|
|
|
|
void Replay::process()//线程函数
|
|
{
|
|
quint64 lastTimestamp = 0;
|
|
quint64 currentTimestamp = 0;
|
|
|
|
quint64 timeStamp = 0;
|
|
|
|
while (running_flag)
|
|
{
|
|
//mutex.lock();
|
|
if(isplaying)
|
|
{
|
|
if(!ispause)//不暂停
|
|
{
|
|
if(file)//已经有文件,那么可以读取
|
|
{
|
|
if(position <= file->size())
|
|
{
|
|
QByteArray raw;
|
|
QByteArray time;
|
|
|
|
file->seek(position);
|
|
|
|
raw = file->read(MAVLINK_MAX_PACKET_LEN + sizeof(quint64));
|
|
|
|
if(raw.indexOf(0xFD) >= 8)
|
|
{
|
|
time = raw.mid(raw.indexOf(0xFD) - 8,8);
|
|
}
|
|
else
|
|
{
|
|
position -= 8 - raw.indexOf(0xFD);
|
|
|
|
file->seek(position);
|
|
|
|
raw = file->read(MAVLINK_MAX_PACKET_LEN + sizeof(quint64));
|
|
|
|
time = raw.mid(raw.indexOf(0xFD) - 8,8);
|
|
}
|
|
|
|
|
|
//qDebug() << raw.indexOf(0xFD);
|
|
|
|
|
|
|
|
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));
|
|
//qDebug() << "us" << uint64_t(timeStamp / multiple) << timeStamp << multiple;
|
|
}
|
|
else
|
|
{
|
|
QThread::usleep(1000);
|
|
//qDebug() << "1000 us";
|
|
}
|
|
|
|
//QThread::msleep(timeStamp * (multiple / 1000.0));
|
|
|
|
|
|
if(timeStamp < 50)
|
|
{
|
|
timeStamp = 50;
|
|
}
|
|
|
|
//QThread::msleep(timeStamp);
|
|
|
|
|
|
/*
|
|
if((timeStamp * (multiple / 1000.0)) > 50)
|
|
{
|
|
QThread::msleep(timeStamp * (multiple / 1000.0));
|
|
}
|
|
else
|
|
{
|
|
QThread::msleep(50);
|
|
}
|
|
*/
|
|
|
|
}
|
|
|
|
|
|
position += (uint8_t)raw.at(raw.indexOf(0xFD)+1) + 20;
|
|
|
|
buff.clear();
|
|
buff = raw.mid(raw.indexOf(0xFD),(uint8_t)raw.at(raw.indexOf(0xFD) +1) + 12);
|
|
|
|
//读取一帧数据
|
|
//将百分比增加,或者位置增加
|
|
percentage = (float)position / file->size();
|
|
emit currentPercentage(percentage);
|
|
emit readReady();
|
|
|
|
|
|
/*
|
|
qDebug() << position
|
|
<< currentTimestamp
|
|
<< lastTimestamp
|
|
<< currentTimestamp - lastTimestamp;
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
lastTimestamp = currentTimestamp;
|
|
}
|
|
else
|
|
{
|
|
emit replayComplete();
|
|
isplaying = false;
|
|
}
|
|
}
|
|
}
|
|
else
|
|
{
|
|
QThread::msleep(1000);//如果没有播放,那么每秒钟检测一下是否要播放
|
|
}
|
|
}
|
|
else
|
|
{
|
|
QThread::msleep(1000);//如果没有播放,那么每秒钟检测一下是否要播放
|
|
}
|
|
//mutex.unlock();
|
|
}
|
|
//退出线程
|
|
//disconnect(thread, nullptr, nullptr, nullptr);
|
|
//thread->quit();
|
|
//thread->deleteLater();
|
|
//thread = nullptr;
|
|
|
|
}
|
|
|
|
//类相关函数
|
|
|
|
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;
|
|
|
|
//emit currentPercentage(percentage);
|
|
//emit readReady();
|
|
|
|
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;
|
|
|
|
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)
|
|
{
|
|
if(value != 0)
|
|
{
|
|
multiple = value;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|