-
Notifications
You must be signed in to change notification settings - Fork 0
/
restwidget.cpp
353 lines (247 loc) · 9.93 KB
/
restwidget.cpp
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
#include "restwidget.h"
#include <QPushButton>
#include <QVBoxLayout>
#include <QApplication>
#include <QLabel>
#include <QString>
#include <QTextDocument>
#include <QtNetwork/QNetworkAccessManager>
#include <QDateTime>
#include <QDebug>
RestWidget::RestWidget(QWidget *parent,QListWidget *output) :
QWidget(parent), xmloutput(output)
{
//server_url = "http://httpbin.org/get";
server_url = "http://192.168.0.100";
user = "team1";
status = "idle";
password = "nothing";
currentTicket = "none";
serverLabel = new QLabel;
userLabel = new QLabel;
statusLabel = new QLabel;
orderIDs = new QVector<QString>(100);
currentOrder = new QVector<int>(10);
//currentOrder = new std::vector<int>;
serverLabel->setText("ServerURL: " + server_url);
userLabel->setText("Current User: " + user);
statusLabel->setText("Status: " + status);
//currentOrder = new std::vector<int>;
//test area:
QPushButton *TestButton = new QPushButton(QApplication::translate("PackML","Get Orders"));
QPushButton *handleOrderTest = new QPushButton(QApplication::translate("PackML","Handle Order"));
QPushButton *testLog = new QPushButton(QApplication::translate("PackMl", "Log"));
QObject::connect(TestButton, SIGNAL(pressed()),this,SLOT(getOrderUrls()));
QObject::connect(handleOrderTest,SIGNAL(pressed()),this,SLOT(getNewOrder()));
QObject::connect(testLog,SIGNAL(pressed()),this,SLOT(testLog()));
//DEFINE THE OUTPUT AREA:
//xmloutput = new QListWidget();
xmloutput->addItem("ready");
xmloutput->addItem("for");
xmloutput->addItem("Action...");
dataOutput = new QTextEdit;
dataOutput->setText("Nothing to see here!");
//SET UP THE NETWORK CONNECTION:
netManager2 = new QNetworkAccessManager(this);
//SET THE LAYOUT:
QGridLayout *layout = new QGridLayout();
layout->addWidget(serverLabel,0,0);
layout->addWidget(userLabel,0,1);
layout->addWidget(statusLabel,0,2);
layout->addWidget(TestButton,2,0);
layout->addWidget(xmloutput,1,0,1,3);
layout->addWidget(dataOutput,3,0,3,3);
layout->addWidget(handleOrderTest,2,1);
layout->addWidget(testLog,2,2);
setLayout(layout);
}
//SIGNAL FUNCTIONS:
//start by retrieving the order list:
void RestWidget::getOrderUrls(){
QObject::connect(netManager2, SIGNAL(finished(QNetworkReply *)),this,
SLOT(parseOrderUrls(QNetworkReply *)));
orderIDs->clear();
//First define the request:
QNetworkRequest request;
request.setRawHeader("Authorization", "Basic " +
QByteArray(QString("%1:%2").arg(user).arg(password).toAscii().toBase64())
);
request.setRawHeader("Accept", "application/xml");
request.setRawHeader("Content-Type", "application/xml");
request.setUrl(QUrl(server_url+"/orders/"));
//Wait for a reply:
QNetworkReply *reply = netManager2->get(request);
}
void RestWidget::parseOrderUrls(QNetworkReply *reply){
if(reply->error()>0){
xmloutput->addItem(QTime::currentTime().toString() +" : " + reply->errorString());
emit connectionError();
} else{
QByteArray data = reply->readAll();
QDomDocument doc;
dataOutput->setText(data);
doc.setContent(data);
QDomNodeList orderNodes = doc.elementsByTagName("order");
for(int i = 0; i < orderNodes.size(); i++){
xmloutput->addItem(orderNodes.at(i).toElement().text());
orderIDs->append(orderNodes.at(i).toElement().text());
}
order_ptr = 0; //reset the order pointer.
}
QObject::disconnect(netManager2, SIGNAL(finished(QNetworkReply*)),this, SLOT(parseOrderUrls(QNetworkReply*)));
}
//orderhandling stuff
void RestWidget::getNewOrder(){
xmloutput->addItem("getting new order:");
//First define the request:
QNetworkRequest request;
if(!orderIDs->empty()){
order_url = orderIDs->at(order_ptr);
request.setUrl(QUrl(order_url));
QObject::connect(netManager2, SIGNAL(finished(QNetworkReply *)),this,
SLOT(parseOrderInfo(QNetworkReply *)));
order_ptr++;
if(order_ptr >= orderIDs->size()){
order_ptr = 0; //if the order ptr moves out of bounds start over.
}
//Wait for a reply:
QNetworkReply *reply = netManager2->get(request);
} else{
emit noOrderError();
xmloutput->addItem("No order urls present");
}
}
void RestWidget::parseOrderInfo(QNetworkReply *reply){
QObject::disconnect(netManager2, SIGNAL(finished(QNetworkReply*)),this, SLOT(parseOrderInfo(QNetworkReply*)));
xmloutput->addItem("Parsing order info");
if(reply->error()>0){
xmloutput->addItem(QTime::currentTime().toString() +" : " + reply->errorString());
emit connectionError();
} else{
QByteArray data = reply->readAll();
QDomDocument doc;
dataOutput->setText(data);
xmloutput->addItem(data);
doc.setContent(data);
QString ready = doc.elementsByTagName("status").item(0).toElement().text();
if(QString::compare(ready,"ready",Qt::CaseInsensitive) == 0){
if(getThisOrder(reply->url())){
QString reds = doc.elementsByTagName("red").item(0).toElement().text();
currentOrder->append(reds.toInt());
QString blues = doc.elementsByTagName("blue").item(0).toElement().text();
currentOrder->append(blues.toInt());
QString yellows = doc.elementsByTagName("yellow").item(0).toElement().text();
currentOrder->append(yellows.toInt());
xmloutput->addItem(yellows);
}
else{
emit noTicketError();
}
}
}
}
//test if the order is availible (yeah its synchronous!):
bool RestWidget::getThisOrder(QUrl url){
QByteArray putData;
putData.append("taking&");
putData.append("ticket");
QNetworkRequest request;
request.setUrl(url);
QNetworkReply *reply = netManager2->put( request,putData );
QEventLoop loop;
QObject::connect(reply, SIGNAL(readyRead()), &loop, SLOT(quit()));
// Execute the event loop here, now we will wait here until readyRead() signal is emitted
// which in turn will trigger event loop quit.
loop.exec();
currentTicket = "none";
if(reply->error()>0){
xmloutput->addItem(QTime::currentTime().toString() +" : " + reply->errorString());
emit connectionError();
} else{
QByteArray data = reply->readAll();
QDomDocument doc;
dataOutput->setText(data);
doc.setContent(data);
xmloutput->addItem(data);
currentTicket = doc.elementsByTagName("ticket").item(0).toElement().text();
if(QString::compare("none",currentTicket) == 0){
emit noTicketError();
return false;
}
}
}
void RestWidget::orderDone(){
if(QString::compare("none",currentTicket)== 0){
emit noTicketToDeleteError();
qDebug() << "Emiting no ticket to delete error";
return;
} else{
//first to the delete on the server:
qDebug() << "deleting order to:" << order_url << "/" << currentTicket;
QNetworkRequest request;
request.setUrl(QUrl(order_url + "/" + currentTicket));
netManager2->deleteResource(request);
qDebug() << "posting order log to" << order_url;
//then log the order:
}
emit orderReportDone();
}
void RestWidget::updateLog(QString comment, QString event){
//QNetworkRequest request;
//request.setUrl(QUrl(server_url +"/log"));
//make the log xml:
QDomDocument doc;
QDomProcessingInstruction instr = doc.createProcessingInstruction("xml","version=\"1.0\" encoding=\"UTF-8\"");
doc.appendChild(instr);
QDomElement root = doc.createElement( "log_entry" );
doc.appendChild( root );
QDomElement timeNode = doc.createElement("time");
//timeNode.setNodeValue(QDateTime::currentDateTime().toString());
QDate date = QDate::currentDate();
QTime time = QTime::currentTime();
timeNode.appendChild(doc.createTextNode(date.toString("yyyy-MM-dd") + " " + time.toString()));
root.appendChild(timeNode);
QDomElement eventNode = doc.createElement("event");
eventNode.appendChild(doc.createTextNode(event));
root.appendChild(eventNode);
QDomElement cell_idNode = doc.createElement("cell_id");
cell_idNode.appendChild(doc.createTextNode("1"));
root.appendChild(cell_idNode);
QDomElement commentNode = doc.createElement("comment");
commentNode.appendChild(doc.createTextNode(comment));
root.appendChild(commentNode);
qDebug() << doc.toString();
QByteArray putData;
putData.append(doc.toString());
QNetworkRequest request;
request.setUrl(QUrl(server_url +"/log"));
QObject::connect(netManager2, SIGNAL(finished(QNetworkReply *)),this,
SLOT(receiveLogReply(QNetworkReply*)));
QNetworkReply *reply = netManager2->post( request,putData );
}
void RestWidget::receiveLogReply(QNetworkReply *reply){
QObject::disconnect(netManager2, SIGNAL(finished(QNetworkReply*)),this, SLOT(receiveLogReply(QNetworkReply*)));
if(reply->error()>0){
xmloutput->addItem(QTime::currentTime().toString() +" : " + reply->errorString());
qDebug() << "no connection";
emit connectionError();
} else{
qDebug() << "receiving response";
QByteArray data = reply->readAll();
QDomDocument doc;
dataOutput->setText(data);
doc.setContent(data);
xmloutput->addItem(data);
QString response = data;
if(QString::compare("Created",response) == 0){
qDebug()<<"not log received";
emit logNotReceived();
}
}
qDebug() << "logging done";
emit loggingdone();
}
void RestWidget::testLog(){
qDebug() << "testing the log function";
updateLog("testing the log please ignore", "PML_Execute");
}