65 lines
2.2 KiB
C++
65 lines
2.2 KiB
C++
|
#include <QDateTime>
|
|||
|
#include "DataCollectionThread.h"
|
|||
|
|
|||
|
DataCollectionThread::DataCollectionThread(QVector<QString> collectionDataNames,
|
|||
|
double collectionFrequency,
|
|||
|
double collectionTime,
|
|||
|
QString collectionPath) :
|
|||
|
m_CollectionFrequency(collectionFrequency),
|
|||
|
m_CollectionTime(collectionTime),
|
|||
|
m_CollectionPath(collectionPath) {
|
|||
|
for (const QString &dataName : collectionDataNames) {
|
|||
|
m_CollectionDataMap[dataName] = QVector<double>();
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
void DataCollectionThread::run() {
|
|||
|
double collectionPeriod = 1.0 / m_CollectionFrequency;
|
|||
|
for (double time = m_CollectionTime; time > 0; time -= collectionPeriod) {
|
|||
|
for (const QString &dataName : m_CollectionDataMap.keys()) {
|
|||
|
m_CollectionDataMap[dataName] << generateTestCollectionData<double>();
|
|||
|
}
|
|||
|
//如果time为整数,则更新时间
|
|||
|
if (time - static_cast<int>(time) < collectionPeriod) {
|
|||
|
emit UpdateDataCollectionTime(static_cast<unsigned int>(time));
|
|||
|
}
|
|||
|
msleep(collectionPeriod * 1000);
|
|||
|
}
|
|||
|
|
|||
|
QString timeStamp = QDateTime::currentDateTime().toString("yyyyMMddHHmmss");
|
|||
|
QString fileName = m_CollectionPath + "/data" + timeStamp + ".csv";
|
|||
|
|
|||
|
//保存数据
|
|||
|
QFile file(fileName);
|
|||
|
file.open(QIODevice::WriteOnly | QIODevice::Text);
|
|||
|
QTextStream out(&file);
|
|||
|
out << "Time";
|
|||
|
for (const QString &dataName : m_CollectionDataMap.keys()) {
|
|||
|
out << "," << dataName;
|
|||
|
}
|
|||
|
out << "\n";
|
|||
|
for (int i = 0; i < m_CollectionDataMap[m_CollectionDataMap.keys().first()].size(); i++) {
|
|||
|
out << i * collectionPeriod;
|
|||
|
for (const QString &dataName : m_CollectionDataMap.keys()) {
|
|||
|
out << "," << m_CollectionDataMap[dataName][i];
|
|||
|
}
|
|||
|
out << "\n";
|
|||
|
}
|
|||
|
file.close();
|
|||
|
emit DataCollectionStatus(true, fileName);
|
|||
|
}
|
|||
|
|
|||
|
void DataCollectionThread::onStopDataCollection() {
|
|||
|
quit();
|
|||
|
emit DataCollectionStatus(false, QString(""));
|
|||
|
}
|
|||
|
|
|||
|
template <typename T>
|
|||
|
T DataCollectionThread::generateTestCollectionData() {
|
|||
|
std::random_device rd; // 用于获取随机数种子
|
|||
|
std::mt19937 gen(rd()); // Mersenne Twister随机数生成器
|
|||
|
std::uniform_real_distribution<> dis(-1.0, 1.0); // [-1.0, 1.0)范围内的均匀分布
|
|||
|
return (T)(dis(gen) + 100);
|
|||
|
}
|
|||
|
|