-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrestdialog.cpp
156 lines (130 loc) · 5.2 KB
/
restdialog.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
#include "restdialog.h"
#include "ui_restdialog.h"
#include <QDomDocument>
#include <QNetworkAccessManager>
#include <QNetworkReply>
#include <QNetworkRequest>
#include <QUrl>
RESTDialog::RESTDialog(QWidget *parent) :
QWidget(parent),
m_manager(nullptr),
ui(new Ui::RESTDialog)
{
ui->setupUi(this);
ui->siteLineEdit->setText("394329104490101"); // TOLL GATE CREEK ABOVE 6TH AVE AT AURORA, CO
ui->startDateTimeEdit->setDateTime(QDateTime::fromString("2017-06-01T06:00", "yyyy-MM-ddThh:mm"));
ui->endDateTimeEdit->setDateTime(QDateTime::fromString("2017-06-01T10:00", "yyyy-MM-ddThh:mm"));
m_manager = new QNetworkAccessManager(this);
}
RESTDialog::~RESTDialog()
{
delete ui;
}
void RESTDialog::on_genURLButton_clicked()
{
static const char *formats[] = {"waterml,2.0", "waterml,2.0", "rdb", "json"};
char buffer[32767];
QApplication::setOverrideCursor(Qt::WaitCursor);
/*
68478 00060 00003 69928 Discharge, cubic feet per second (Mean)
68511 00400 00008 69941 pH, water, unfiltered, field, standard units (Median), From multiparameter sonde
00060 Discharge, cubic feet per second
00065 Gage height, feet
30207 Gage height, above datum, meters
30208 Discharge, cubic meters per second
*/
qsnprintf(buffer, sizeof(buffer)/sizeof(buffer[0]),
"https://waterservices.usgs.gov/nwis/iv/?format=%s&sites=%s&startDT=%s&endDT=%s¶meterCd=00060,00065,30207,30208&siteStatus=all",
formats[ui->formatComboBox->currentIndex()],
ui->siteLineEdit->text().toStdString().c_str(),
ui->startDateTimeEdit->dateTime().toString(Qt::ISODate).toStdString().c_str(),
ui->endDateTimeEdit->dateTime().toString(Qt::ISODate).toStdString().c_str()
);
ui->urlEdit->setText(buffer);
ui->urlEdit->selectAll();
QUrl url(buffer);
QNetworkRequest request;
QNetworkReply *reply;
while (url.toString().size() != 0) {
// make request
request.setUrl(url);
reply = m_manager->get(request);
// wait until finished
QEventLoop loop;
connect(reply, SIGNAL(finished()), &loop, SLOT(quit()));
loop.exec();
// HTTP status code
// see https://en.wikipedia.org/wiki/List_of_HTTP_status_codes
// see https://waterservices.usgs.gov/rest/IV-Service.html#Error
int status = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
QString reason = reply->attribute(QNetworkRequest::HttpReasonPhraseAttribute).toString();
qDebug() << status << reason.toStdString().c_str();
// check for errors
if (reply->error() != 0) break;
// get redirect url (if any)
url = reply->attribute(QNetworkRequest::RedirectionTargetAttribute).toUrl();
}
if (reply->error() == 0) {
if (ui->formatComboBox->currentIndex() != 0) {
ui->outputPlainTextEdit->document()->setPlainText(reply->readAll());
}
else {
QDomDocument doc;
doc.setContent(reply->readAll());
Q_ASSERT(!doc.isNull());
QString lines;
lines += "*******************\n";
lines += "Parsed WaterML, 2.0\n";
lines += "*******************\n";
QDomNodeList list = doc.elementsByTagName("wml2:observationMember");
for (int i = 0; i < list.count(); ++i) {
QDomElement oMemb = list.at(i).toElement();
QDomNodeList oProp = oMemb.elementsByTagName("om:observedProperty");
Q_ASSERT(oProp.size() == 1);
// property
QString property;
if (oProp.at(0).attributes().contains("xlink:title")) {
property = oProp.at(0).attributes().namedItem("xlink:title").nodeValue();
Q_ASSERT(property == "Discharge");
}
// property units
QString units;
QDomNodeList uom = oMemb.elementsByTagName("wml2:uom");
if (uom.at(0).attributes().contains("xlink:title")) {
units = uom.at(0).attributes().namedItem("xlink:title").nodeValue();
Q_ASSERT(units == "ft3/s");
}
// headings
lines += "Date/Time\t";
lines += "Time Diff(s)\t";
lines += property + "(" + units + ")\t";
lines += property + "(m3/s)\n";
// result
QDomElement result = oMemb.elementsByTagName("om:result").at(0).toElement();
// points
QDomNodeList pts = result.elementsByTagName("wml2:point");
QDateTime first, current;
for (int p = 0; p < pts.count(); ++p) {
// time
QString time = pts.at(p).toElement().elementsByTagName("wml2:time").at(0).childNodes().at(0).toText().data();
current = QDateTime::fromString(time, Qt::ISODate);
if (p == 0) first = current;
qint64 seconds = first.msecsTo(current) / 1000;
// value
QString value = pts.at(p).toElement().elementsByTagName("wml2:value").at(0).childNodes().at(0).toText().data();
double ft3_per_sec = value.toDouble();
double m3_per_sec = ft3_per_sec * 0.0283168;
lines += time + "\t";
lines += QString::number(seconds) + "\t";
lines += value + "\t";
lines += QString::number(m3_per_sec, 'g', 3) + "\n";
}
}
ui->outputPlainTextEdit->document()->setPlainText(lines);
}
}
else {
ui->outputPlainTextEdit->document()->setPlainText(reply->errorString());
}
QApplication::restoreOverrideCursor();
}