-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCWriteToBinaryThread.cpp
More file actions
220 lines (169 loc) · 4.42 KB
/
CWriteToBinaryThread.cpp
File metadata and controls
220 lines (169 loc) · 4.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
#include "CWriteToBinaryThread.h"
#include <QAbstractItemModel>
#include <QDataStream>
#include <QDateTime>
#include <QDebug>
#include "DataModel.h"
#include <QFile>
#include <QMutexLocker>
#include "DataEntry.h"
#include <QDir>
CWriteToBinaryThread::CWriteToBinaryThread(QAbstractItemModel* model, QQueue<DataEntry>* dataQueue, QObject* parent) :
QThread(parent),
m_model(model),
m_dataQueue(dataQueue)
{
}
void CWriteToBinaryThread::start(qint64 num)
{
QMutexLocker locker(&m_mutex);
m_stop = false;
m_num = num;
QThread::start();
}
CWriteToBinaryThread::~CWriteToBinaryThread()
{
}
void CWriteToBinaryThread::saveDataToFile(QFile& file)
{
QMutexLocker locker(&m_mutex);
QDataStream out(&file);
qint64 nStartTimeStamp = m_dataQueue->at(0).getTime();
//TODO 设置文件区分间隔的时间单位
qint64 nEndTimeStamp = getEndOfTimerstamp(nStartTimeStamp, EDEVIDE_TIMEUNIT::HOUR);
DataEntry dataEntry;
while(nStartTimeStamp <= nEndTimeStamp)
{
if (m_dataQueue->isEmpty())
{
return;
}
dataEntry = m_dataQueue->dequeue();
nStartTimeStamp = dataEntry.getTime();
out << dataEntry;
m_num--;
//TODO 判断测试数据的间隔时间来决定需要自增多少,如下以毫秒为间隔
}
}
void CWriteToBinaryThread::readDataFromFile(QFile& file)
{
QDataStream in(&file);
QList<DataEntry> entries; // 用于存储读取的所有数据条目
//TODO optimize this
while (!in.atEnd())
{
DataEntry entry;
in >> entry;
// 将读取的条目添加到列表中
entries.append(entry);
}
m_stop = true;
emit signalFinishReadData(entries);
}
qint64 CWriteToBinaryThread::getEndOfTimerstamp(qint64 currentTimestamp, EDEVIDE_TIMEUNIT divideUnit)
{
QDateTime dateTime = QDateTime::fromMSecsSinceEpoch(currentTimestamp);
// 获取当前分钟的年、月、日、小时和分钟值
int year = dateTime.date().year();
int month = dateTime.date().month();
int day = dateTime.date().day();
int hour = dateTime.time().hour();
int minute = dateTime.time().minute();
int second = dateTime.time().second();
QDateTime endOfTime;
switch (divideUnit)
{
case EDEVIDE_TIMEUNIT::YEAR:
endOfTime = QDateTime(QDate(year, 12, 31), QTime(23, 59, 59, 999));
break;
case EDEVIDE_TIMEUNIT::MONTH:
{
QDate lastDayOfMonth(year, month, QDate(year, month, 1).daysInMonth());
endOfTime = QDateTime(lastDayOfMonth, QTime(23, 59, 59, 999));
}
break;
case EDEVIDE_TIMEUNIT::DAY:
endOfTime = QDateTime(QDate(year, month, day), QTime(23, 59, 59, 999));
break;
case EDEVIDE_TIMEUNIT::HOUR:
endOfTime = QDateTime(QDate(year, month, day), QTime(hour, 59, 59, 999));
break;
case EDEVIDE_TIMEUNIT::MINUTE:
endOfTime = QDateTime(QDate(year, month, day), QTime(hour, minute, 59, 999));
break;
case EDEVIDE_TIMEUNIT::SECOND:
endOfTime = QDateTime(QDate(year, month, day), QTime(hour, minute, second, 999));
break;
default:
qWarning() << "未知的时间单位";
return -1; // 返回一个无效的时间戳
}
return endOfTime.toMSecsSinceEpoch();
}
void CWriteToBinaryThread::run()
{
while(m_num)
{
if (m_stop)
{
break;
}
#if 1
qint64 nTime = m_dataQueue->at(0).getTime();
QDateTime dateTime = QDateTime::fromMSecsSinceEpoch(nTime);
// 按年月日构建目录路径
QString yearPath = "data/" + dateTime.toString("yyyy");
QString monthPath = yearPath + "/" + dateTime.toString("MM");
QString dayPath = monthPath + "/" + dateTime.toString("dd");
QString hourPath = dayPath + "/" + dateTime.toString("hh");
// 构建文件名格式为yyyy-MM-dd-hh
//QString fileName = dateTime.toString("yyyy-MM-dd-hh-mm") + ".bin";
QString fileName = dateTime.toString("yyyy-MM-dd-hh") + ".bin";
QString fullPath = hourPath + "/" + fileName;
QDir dir;
if (!dir.exists(hourPath)) {
if (!dir.mkpath(hourPath)) {
qWarning() << "无法创建目录:" << hourPath;
break;
}
}
QFile file(fullPath);
if (file.open(QIODevice::WriteOnly | QIODevice::Append))
{
// Save all data to the file
saveDataToFile(file);
}
file.close();
m_stop = true;
#endif
#if 0
//QString fileName = "data/2024-09-25-08-12.bin";
QFile file(m_strFilePath);
if (file.open(QIODevice::ReadOnly))
{
readDataFromFile(file);
}
file.close();
m_stop = true;
#endif
}
}
void CWriteToBinaryThread::stop()
{
QMutexLocker locker(&m_mutex);
m_stop = true;
}
void CWriteToBinaryThread::start()
{
QMutexLocker locker(&m_mutex);
m_stop = false;
QThread::start();
}
bool CWriteToBinaryThread::isRunning() const
{
return !m_stop;
}
void CWriteToBinaryThread::setFilePath(const QString& filePath)
{
m_strFilePath = filePath;
}