-
Notifications
You must be signed in to change notification settings - Fork 0
/
rfadevicequery.cpp
201 lines (157 loc) · 5.8 KB
/
rfadevicequery.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
#include <QtCore>
#include <QtWidgets>
#include <QtNetwork>
#include <QUrl>
#include "rfa-default-creds.h"
#include "rfadevicequery.h"
#include "ui_authenticationdialog.h"
const QString rfa_url = "http://10.4.23.76/";
const QString win_title = "Rainforest EAGLE Local API Device List (discovery)";
const int winsz_x = 800;
const int winsz_y = 600;
// HttpWindow::HttpWindow(QWidget *parent):
// QDialog(parent),
// statusLabel(new QLabel(tr("Please enter the URL of a file you want to download.\n\n"), this)),
// urlLineEdit(new QLineEdit(defaultUrl)),
// downloadButton(new QPushButton(tr("Download"))),
// launchCheckBox(new QCheckBox("Launch file")),
// defaultFileLineEdit(new QLineEdit(defaultFileName)),
// downloadDirectoryLineEdit(new QLineEdit),
// reply(Q_NULLPTR),
// file(Q_NULLPTR),
// httpRequestAborted(false) {
RFADeviceQuery::RFADeviceQuery(QWidget *parent): QDialog(parent), reply(0) {
lineEdit = new QLineEdit(this);
fetchButton = new QPushButton(tr("Get Devices"), this);
treeWidget = new QTreeWidget(this);
lineEdit->setText(rfa_url);
connect(treeWidget, SIGNAL(itemActivated(QTreeWidgetItem*, int)), this, SLOT(itemActivated(QTreeWidgetItem*)));
QStringList headerLabels;
headerLabels << tr("HardwareAddress") << tr("Manufacturer") << tr("ModelId") << tr("Protocol") << tr("LastContact") << tr("ConnectionStatus") << tr("NetworkAddress");
treeWidget->setHeaderLabels(headerLabels);
treeWidget->header()->setSectionResizeMode(QHeaderView::ResizeToContents);
connect(&qnam, &QNetworkAccessManager::authenticationRequired, this, &RFADeviceQuery::slotAuthRequired);
connect(&qnam, SIGNAL(finished(QNetworkReply*)), this, SLOT(finished(QNetworkReply*)));
connect(lineEdit, SIGNAL(returnPressed()), this, SLOT(fetch()));
connect(fetchButton, SIGNAL(clicked()), this, SLOT(fetch()));
QVBoxLayout *layout = new QVBoxLayout(this);
QHBoxLayout *hboxLayout = new QHBoxLayout;
hboxLayout->addWidget(lineEdit);
hboxLayout->addWidget(fetchButton);
layout->addLayout(hboxLayout);
layout->addWidget(treeWidget);
setWindowTitle(win_title);
resize(winsz_x, winsz_y);
}
void RFADeviceQuery::get(const QUrl &url) {
QNetworkRequest request(url);
QByteArray reqData("<Command><Name>device_list</Name></Command>\n");
if (reply) {
reply->disconnect(this);
reply->deleteLater();
}
request.setHeader(QNetworkRequest::ContentTypeHeader, "text/xml");
// reply = qnam.get(request);
reply = qnam.post(request, reqData);
connect(reply, SIGNAL(readyRead()), this, SLOT(readyRead()));
connect(reply, SIGNAL(metaDataChanged()), this, SLOT(metaDataChanged()));
connect(reply, SIGNAL(error(QNetworkReply::NetworkError)), this, SLOT(error(QNetworkReply::NetworkError)));
}
void RFADeviceQuery::fetch() {
lineEdit->setReadOnly(true);
fetchButton->setEnabled(false);
treeWidget->clear();
xml.clear();
QUrl url(lineEdit->text());
get(url);
}
void RFADeviceQuery::metaDataChanged() {
QUrl redirTarget;
redirTarget = reply->attribute(QNetworkRequest::RedirectionTargetAttribute).toUrl();
if (redirTarget.isValid()) {
get(redirTarget);
}
}
void RFADeviceQuery::readyRead() {
int statusCode;
statusCode = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
if ((statusCode >= 200) && (statusCode < 300)) {
QByteArray data = reply->readAll();
xml.addData(data);
parseXml();
}
}
void RFADeviceQuery::finished(QNetworkReply *reply) {
Q_UNUSED(reply);
lineEdit->setReadOnly(false);
fetchButton->setEnabled(true);
}
void RFADeviceQuery::parseXml() {
while (!xml.atEnd()) {
xml.readNext();
if (xml.isStartElement()) {
currentTag = xml.name().toString();
} else if (xml.isEndElement()) {
if (xml.name() == "Device") {
QTreeWidgetItem *item = new QTreeWidgetItem;
item->setText(0, hardwareAddressString);
item->setText(1, manufacturerString);
item->setText(2, modelIdString);
item->setText(3, protocolString);
item->setText(4, lastContactString);
item->setText(5, connectionStatusString);
item->setText(6, networkAddressString);
treeWidget->addTopLevelItem(item);
hardwareAddressString.clear();
manufacturerString.clear();
modelIdString.clear();
protocolString.clear();
lastContactString.clear();
connectionStatusString.clear();
networkAddressString.clear();
}
} else if (xml.isCharacters() && !xml.isWhitespace()) {
if (currentTag == "HardwareAddress") {
hardwareAddressString += xml.text();
} else if (currentTag == "Manufacturer") {
manufacturerString += xml.text();
} else if (currentTag == "ModelId") {
modelIdString += xml.text();
} else if (currentTag == "Protocol") {
protocolString += xml.text();
} else if (currentTag == "LastContact") {
lastContactString += xml.text();
} else if (currentTag == "ConnectionStatus") {
connectionStatusString += xml.text();
} else if (currentTag == "NetworkAddress") {
networkAddressString += xml.text();
}
}
}
if (xml.error() && (xml.error() != QXmlStreamReader::PrematureEndOfDocumentError)) {
qWarning() << "XML ERROR:" << xml.lineNumber() << ": " << xml.errorString();
}
}
QString RFADeviceQuery::itemActivated(QTreeWidgetItem *item) {
return (item->text(0));
}
void RFADeviceQuery::error(QNetworkReply::NetworkError) {
qWarning("Error retreiving device list");
reply->disconnect(this);
reply->deleteLater();
reply = 0;
}
void RFADeviceQuery::slotAuthRequired(QNetworkReply*, QAuthenticator *authenticator) {
QDialog authDialog;
Ui::AuthenticationDialog ui;
ui.setupUi(&authDialog);
authDialog.adjustSize();
ui.siteDesc->setText(tr("%1 at %2").arg(authenticator->realm(), url.host()));
/* prefill the dialog */
ui.httpUser->setText(RFA_CREDS_USER);
ui.httpPass->setText(RFA_CREDS_PASS);
if (authDialog.exec() == QDialog::Accepted) {
authenticator->setUser(ui.httpUser->text());
authenticator->setPassword(ui.httpPass->text());
}
}