-
Notifications
You must be signed in to change notification settings - Fork 0
/
rfadevicedetails.cpp
285 lines (219 loc) · 8.31 KB
/
rfadevicedetails.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
#include <QtCore>
#include <QtWidgets>
#include <QtNetwork>
#include <QUrl>
#include "rfa-default-creds.h"
#include "rfadevicedetails.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;
QString request_data = "";
// 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) {
RFADeviceDetails::RFADeviceDetails(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, &RFADeviceDetails::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);
}
RFADeviceDetails::RFADeviceDetails(QWidget *parent, QString rfa_devaddr): QDialog(parent), reply(0) {
lineEdit = new QLineEdit(this);
fetchButton = new QPushButton(tr("Get Details"), 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, &RFADeviceDetails::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);
}
RFADeviceDetails::~RFADeviceDetails() {
delete rfa_hardware_address;
}
void RFADeviceDetails::assembleRequest(const QString rfaDevAddr) {
QString reqdata;
xml_reqdata_buffer.setBuffer(&xml_reqdata_ba);
/* ------------------------------------------------------------------------------ *
*
* <Command>
* <Name>device_details</Name>
* <DeviceDetails>
* <HardwareAddress>0x000781000081fd0b</HardwareAddress>
* </DeviceDetails>
* </Command>
*
* ------------------------------------------------------------------------------ */
#if 0
reqdata.clear();
reqdata.append("<Command><Name>device_details</Name>");
reqdata.append("<DeviceDetails><HardwareAddress>");
reqdata.append(rfaDevAddr);
reqdata.append("</HardwareAddress></DeviceDetails>");
reqdata.append("</Command>\n");
#else
xmlreq.setDevice(xml_reqdata_buffer);
// xmlreq.writeStartDocument();
xmlreq.writeStartElement("Command");
xmlreq.writeTextElement("Name", "device_details");
xmlreq.writeStartElement("DeviceDetails");
xmlreq.writeTextElement("HardwareAddress", rfaDevAddr);
xmlreq.writeEndElement();
xmlreq.writeEndElement();
xmlreq.writeEndDocument();
#endif
#if 0
return (reqdata);
#endif
}
void RFADeviceDetails::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, xml_reqdata_ba);
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 RFADeviceDetails::fetch() {
lineEdit->setReadOnly(true);
fetchButton->setEnabled(false);
treeWidget->clear();
xml.clear();
QUrl url(lineEdit->text());
get(url);
}
void RFADeviceDetails::metaDataChanged() {
QUrl redirTarget;
redirTarget = reply->attribute(QNetworkRequest::RedirectionTargetAttribute).toUrl();
if (redirTarget.isValid()) {
get(redirTarget);
}
}
void RFADeviceDetails::readyRead() {
int statusCode;
statusCode = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
if ((statusCode >= 200) && (statusCode < 300)) {
QByteArray data = reply->readAll();
xml.addData(data);
parseXml();
}
}
void RFADeviceDetails::finished(QNetworkReply *reply) {
Q_UNUSED(reply);
lineEdit->setReadOnly(false);
fetchButton->setEnabled(true);
}
void RFADeviceDetails::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 RFADeviceDetails::itemActivated(QTreeWidgetItem *item) {
return (item->text(0));
}
void RFADeviceDetails::error(QNetworkReply::NetworkError) {
qWarning("Error retreiving device list");
reply->disconnect(this);
reply->deleteLater();
reply = 0;
}
void RFADeviceDetails::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());
}
}