-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathtaskinfo.cpp
More file actions
213 lines (184 loc) · 5.97 KB
/
taskinfo.cpp
File metadata and controls
213 lines (184 loc) · 5.97 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
#include "taskinfo.h"
#include "macros.h"
#include "magprofile.h"
#include <QNetworkRequest>
#include <QNetworkReply>
using namespace yasem;
TaskInfo::TaskInfo(MagProfile* profile, int id, const QString &url, const QString &fileName,
const QDateTime &startTime, const QDateTime &endTime,
QObject* parent):
QObject(parent),
m_profile(profile),
m_id(id),
m_url(url),
m_file_name(fileName),
m_start_time(startTime),
m_end_time(endTime),
m_state(STATE_COMPLETED),
m_error_code(ERROR_SUCCESSFUL),
m_manager(NULL),
m_reply(NULL),
m_file(NULL),
m_request_aborted(false)
{
m_task_timer.setSingleShot(true);
connect(this, &TaskInfo::errorHappened, this, &TaskInfo::onError);
}
TaskInfo::~TaskInfo()
{
STUB();
if(m_reply)
{
m_reply->deleteLater();
m_reply = 0;
}
}
QJsonObject TaskInfo::toJson() const
{
QJsonObject result;
result.insert("id", m_id);
result.insert("state", m_state);
result.insert("errorCode", m_error_code);
result.insert("fileName", m_file_name);
result.insert("url", m_url);
result.insert("startTime", m_start_time.toMSecsSinceEpoch() / 1000);
result.insert("endTime", m_end_time.toMSecsSinceEpoch() / 1000);
return result;
}
TaskInfo *TaskInfo::fromJson(MagProfile* profile, const QJsonObject& json, QObject* parent)
{
STUB() << json;
if(json.contains("id")
&& json.contains("state")
&& json.contains("errorCode")
&& json.contains("fileName")
&& json.contains("url")
&& json.contains("startTime")
&& json.contains("endTime"))
{
TaskInfo* task = new TaskInfo(profile,
json.value("id").toInt(),
json.value("url").toString(),
json.value("fileName").toString(),
QDateTime::fromMSecsSinceEpoch(json.value("startTime").toVariant().toLongLong()),
QDateTime::fromMSecsSinceEpoch(json.value("endTime").toVariant().toLongLong()),
parent);
task->m_state = (TaskState)json.value("state").toInt();
task->m_error_code = (ErrorCode)json.value("errorCode").toInt();
if(task->m_end_time > QDateTime::currentDateTime())
{
DEBUG() << "Task was already finished";
return NULL;
}
DEBUG() << "Task loaded";
return task;
}
else
{
WARN() << "Incorrect json data" << QString(QJsonDocument(json).toJson(QJsonDocument::Compact));
return NULL;
}
}
TaskInfo::ErrorCode TaskInfo::scheduleStart()
{
m_state = STATE_WAITING_FOR_START;
qint64 timeout = m_start_time.toMSecsSinceEpoch() - QDateTime::currentMSecsSinceEpoch();
STUB() << "Scheduling task start in" << timeout / 1000 << "sec";
if(timeout <= 0)
{
return startRecord();
}
else
{
disconnect(&m_task_timer);
connect(&m_task_timer, &QTimer::timeout, this, &TaskInfo::startRecord);
m_task_timer.start(timeout);
}
return ERROR_SUCCESSFUL;
}
TaskInfo::ErrorCode TaskInfo::scheduleStop()
{
qint64 timeout = m_end_time.toMSecsSinceEpoch() - QDateTime::currentMSecsSinceEpoch();
STUB() << "Scheduling task stop in" << timeout / 1000 << "sec";
disconnect(&m_task_timer);
connect(&m_task_timer, &QTimer::timeout, this, &TaskInfo::stopRecord);
if(timeout > 0)
m_task_timer.start(timeout);
else
stopRecord();
return ERROR_SUCCESSFUL;
}
TaskInfo::ErrorCode TaskInfo::startRecord()
{
STUB();
m_state = STATE_RECORDING;
scheduleStop();
const QString real_file_name = m_profile->translateStbPathToLocal(m_file_name);
DEBUG() << "Writing video to" << real_file_name;
m_file = new QFile(real_file_name, this);
if(!m_file->open(QIODevice::WriteOnly /* | QIODevice::Append */))
{
emit errorHappened(ERROR_CANNOT_OPEN_OUTPUT_FILE);
return ERROR_CANNOT_OPEN_OUTPUT_FILE;
}
m_manager = new QNetworkAccessManager(this);
m_reply = m_manager->get(QNetworkRequest(m_url));
connect(m_reply, &QNetworkReply::readyRead, this, &TaskInfo::httpReadyRead);
connect(m_reply, &QNetworkReply::downloadProgress, [=](qint64 bytesReceived, qint64 bytesTotal){
/*DEBUG() << qPrintable(
QString("Recording task %1: downloaded %2 bytes of %3")
.arg(m_id)
.arg(bytesReceived)
.arg(bytesTotal)
);
*/
});
connect(m_reply,
static_cast<void (QNetworkReply::*)(QNetworkReply::NetworkError)>(&QNetworkReply::error),
this, &TaskInfo::onNetworkError);
return ERROR_SUCCESSFUL;
}
TaskInfo::ErrorCode TaskInfo::stopRecord()
{
STUB();
stopDownloading();
m_state = STATE_COMPLETED;
return ERROR_SUCCESSFUL;
}
void TaskInfo::httpReadyRead()
{
// this slot gets called every time the QNetworkReply has new data.
// We read all of its new data and write it into the file.
// That way we use less RAM than when reading it at the finished()
// signal of the QNetworkReply
if (m_file)
{
qint64 len = m_file->write(m_reply->readAll());
if(len < 0)
emit errorHappened(ERROR_CANNOT_WRITE_FILE);
}
}
void TaskInfo::onNetworkError(QNetworkReply::NetworkError error)
{
WARN() << error;
if(error != QNetworkReply::OperationCanceledError)
emit errorHappened(ERROR_STREAM_OPENING_ERROR);
}
void TaskInfo::onError(TaskInfo::ErrorCode code)
{
STUB() << code;
m_state = STATE_ERROR;
m_error_code = code;
stopDownloading();
}
void TaskInfo::stopDownloading()
{
STUB();
if(m_reply && m_reply->isOpen())
m_reply->abort();
if(m_file && m_file->isOpen())
{
m_file->flush();
m_file->close();
}
}