forked from foldynl/QLog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPropConditions.cpp
439 lines (353 loc) · 13.4 KB
/
PropConditions.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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
#include <QNetworkAccessManager>
#include <QUrl>
#include <QNetworkRequest>
#include <QNetworkReply>
#include <QTimer>
#include <QStandardPaths>
#include <QDir>
#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonArray>
#include <QDomDocument>
#include "PropConditions.h"
#include "debug.h"
#include "data/Data.h"
//#define FLUX_URL "https://services.swpc.noaa.gov/products/summary/10cm-flux.json"
#define K_INDEX_URL "https://www.hamqsl.com/solarxml.php"
#define SOLAR_SUMMARY_IMG "https://www.hamqsl.com/solar101vhf.php"
#define AURORA_MAP "https://services.swpc.noaa.gov/json/ovation_aurora_latest.json"
#define MUF_POINTS "https://prop.kc2g.com/api/stations.json?maxage=2700"
#define DXC_TRENDS "https://api.ure.es/v2/heatmap"
// the resend mechanism was implemented only because of a issue with prop.kc2g.com
// This site has IPv4 and IPv6 DNS record, and if the notebook is IPv4 only, QT uses an IPv6
// address for the first attempt and an IPv4 address for the second attempt.
// This resulted in a long interval before information was obtained from this server.
// Resend mechanism accelerates all this.
#define RESEND_ATTEMPTS 3
//intervals are defined in seconds
#define RESEND_BASE_INTERVAL 5
#define BASE_UPDATE_INTERVAL (15 * 60)
// in seconds
#define DXTRENDS_UPDATE_INTERVAL (5 * 60)
// in seconds
#define DXTRENDS_TIMEOUT 60
MODULE_IDENTIFICATION("qlog.core.conditions");
PropConditions::PropConditions(QObject *parent) : QObject(parent),
agentString(QString("QLog/%1").arg(VERSION).toUtf8())
{
FCT_IDENTIFICATION;
nam = new QNetworkAccessManager(this);
connect(nam, &QNetworkAccessManager::finished, this, &PropConditions::processReply);
QTimer *timer = new QTimer(this);
connect(timer, &QTimer::timeout, this, &PropConditions::update);
update();
timer->start(BASE_UPDATE_INTERVAL * 1000);
QTimer *timerTrends = new QTimer(this);
connect(timerTrends, &QTimer::timeout, this, &PropConditions::updateDxTrends);
updateDxTrends();
timerTrends->start(DXTRENDS_UPDATE_INTERVAL * 1000);
connect(&dxTrendTimeoutTimer, &QTimer::timeout, this, &PropConditions::dxTrendTimeout);
}
void PropConditions::update()
{
FCT_IDENTIFICATION;
nam->get(prepareRequest(QUrl(SOLAR_SUMMARY_IMG)));
nam->get(prepareRequest(QUrl(K_INDEX_URL)));
nam->get(prepareRequest(QUrl(AURORA_MAP)));
nam->get(prepareRequest(QUrl(MUF_POINTS)));
}
void PropConditions::updateDxTrends()
{
FCT_IDENTIFICATION;
dxTrendResult.clear();
dxTrendTimeoutTimer.stop();
dxTrendPendingConnections.clear(); // pending connections will be ignored.
for ( const QString& continent : Data::getContinentList() )
dxTrendPendingConnections << nam->get(prepareRequest(QUrl(DXC_TRENDS + QString("/%0/15").arg(continent))));
dxTrendTimeoutTimer.start(DXTRENDS_TIMEOUT * 1000);
}
void PropConditions::processReply(QNetworkReply* reply)
{
FCT_IDENTIFICATION;
QByteArray data = reply->readAll();
qCDebug(runtime) << data;
int replyStatusCode = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
qCDebug(runtime) << reply->error()
<< reply->attribute(QNetworkRequest::HttpStatusCodeAttribute)
<< reply->url();
if ( reply->error() == QNetworkReply::NoError
&& replyStatusCode >= 200 && replyStatusCode < 300 )
{
failedRequests[reply->url()] = 0;
const QUrl &replyURL = reply->url();
if (replyURL == QUrl(SOLAR_SUMMARY_IMG))
{
QFile file(solarSummaryFile());
if ( file.open(QIODevice::WriteOnly))
{
file.write(data);
file.flush();
file.close();
}
}
else if (replyURL == QUrl(K_INDEX_URL))
{
QDomDocument doc;
if ( !doc.setContent(data) )
{
qWarning() << "Cannot parse response from " << K_INDEX_URL;
return;
}
QDomNodeList solarData = doc.elementsByTagName("solardata");
QDomNode n = solarData.item(0);
if ( n.isNull() )
{
qWarning() << "Cannot find solardata in " << K_INDEX_URL;
return;
}
QDomElement aindex = n.firstChildElement("aindex");
QDomElement kindex = n.firstChildElement("kindex");
QDomElement solarflux = n.firstChildElement("solarflux");
if ( !aindex.isNull() )
{
a_index = aindex.text().toInt();
qCDebug(runtime) << "A-Index: " << a_index;
a_index_last_update = QDateTime::currentDateTime();
emit AIndexUpdated();
}
if ( !kindex.isNull() )
{
k_index = kindex.text().toDouble();
qCDebug(runtime) << "K-Index: " << k_index;
k_index_last_update = QDateTime::currentDateTime();
emit KIndexUpdated();
}
if ( !solarflux.isNull() )
{
flux = solarflux.text().toInt();
qCDebug(runtime) << "Flux: " << flux;
flux_last_update = QDateTime::currentDateTime();
emit fluxUpdated();
}
}
else if (replyURL == QUrl(AURORA_MAP))
{
auroraMap.clear();
QJsonDocument doc = QJsonDocument::fromJson(data);
if ( ! doc.isNull() )
{
double skipElement = 0.0;
qCDebug(runtime) << "Aurora forecast Time:" << doc["Forecast Time"].toString();
const QJsonArray &jsonArray = doc["coordinates"].toArray();
for (const QJsonValue &value : jsonArray)
{
QJsonArray obj = value.toArray();
if ( obj.size() == 3 )
{
double longitute = obj[0].toDouble();
double latitude = obj[1].toDouble();
double prob = obj[2].toDouble();
auroraMap.addPoint(longitute, latitude, prob, &skipElement);
}
}
auroraMap_last_update = QDateTime::currentDateTime();
emit auroraMapUpdated();
}
}
else if (replyURL == QUrl(MUF_POINTS))
{
mufMap.clear();
QJsonDocument doc = QJsonDocument::fromJson(data);
if ( ! doc.isNull() )
{
double skipElement = 0.0;
const QJsonArray &jsonArray = doc.array();
for (const QJsonValue &value : jsonArray)
{
QJsonObject obj = value.toObject();
QJsonObject station = obj["station"].toObject();
double longitute = station["longitude"].toString().toDouble();
double latitude = station["latitude"].toString().toDouble();
double muf = obj["mufd"].toDouble();
mufMap.addPoint(longitute, latitude, muf, &skipElement);
}
mufMap_last_update = QDateTime::currentDateTime();
emit mufMapUpdated();
}
}
else if ( dxTrendPendingConnections.contains(reply) )
{
dxTrendPendingConnections.remove(reply);
QJsonDocument doc = QJsonDocument::fromJson(data);
const QString &requestContinent = replyURL.path().section('/', -2, -2);
if ( ! doc.isNull() )
{
QJsonObject jsonObject = doc.object();
for ( auto continentIt = jsonObject.begin(); continentIt != jsonObject.end(); ++continentIt )
{
const QString &toContinent = continentIt.key(); // "AF", "AS", "EU"....
const QJsonObject &values = continentIt->toObject();
for ( auto valueIt = values.begin(); valueIt != values.end(); ++valueIt )
{
const QString &band = valueIt.key() + "m"; // "10", "12", "15" ...
int spotCount = valueIt->toString().toInt(); // Number of Spots
dxTrendResult[requestContinent][toContinent][band] = spotCount;
}
}
}
if ( dxTrendPendingConnections.isEmpty() )
{
dxTrendTimeoutTimer.stop();
qCDebug(runtime) << "DXTrend finalized";
emit dxTrendFinalized(dxTrendResult);
}
}
reply->deleteLater();
emit conditionsUpdated();
}
else
{
qCDebug(runtime) << "HTTP Status Code" << replyStatusCode;
dxTrendPendingConnections.remove(reply);
repeateRequest(reply->url());
reply->deleteLater();
}
}
void PropConditions::repeateRequest(const QUrl &url)
{
FCT_IDENTIFICATION;
failedRequests[url]++;
if ( failedRequests[url] <= RESEND_ATTEMPTS )
{
int resendInterval = RESEND_BASE_INTERVAL * failedRequests[url];
qCDebug(runtime) << "Scheduled URL request resend" << resendInterval << "; URL:" << url.toString();
QTimer::singleShot(1000 * RESEND_BASE_INTERVAL * failedRequests[url], this, [this, url]()
{
qCDebug(runtime) << "Resending request" << url.toString();
QNetworkReply *req = nam->get(prepareRequest(url));
if ( url.toString().contains(DXC_TRENDS))
dxTrendPendingConnections << req;
});
}
else
{
qCDebug(runtime) << "Propagation - detected consecutive errors from" << url.toString();
}
}
QNetworkRequest PropConditions::prepareRequest(const QUrl &url)
{
FCT_IDENTIFICATION;
QNetworkRequest req(url);
req.setRawHeader("User-Agent", agentString);
return req;
}
void PropConditions::dxTrendTimeout()
{
FCT_IDENTIFICATION;
dxTrendTimeoutTimer.stop();
for ( auto it = dxTrendPendingConnections.begin(); it != dxTrendPendingConnections.end(); )
{
QNetworkReply* reply = *it;
it = dxTrendPendingConnections.erase(it);
if ( reply )
{
reply->abort();
reply->deleteLater();
}
}
dxTrendPendingConnections.clear();
dxTrendResult.clear();
emit dxTrendFinalized(dxTrendResult); // emit empty result
}
PropConditions::~PropConditions()
{
dxTrendTimeout();
nam->deleteLater();
}
bool PropConditions::isFluxValid()
{
FCT_IDENTIFICATION;
bool ret = false;
qCDebug(runtime)<<"Date valid: " << flux_last_update.isValid() << " last_update: " << flux_last_update;
ret = (flux_last_update.isValid()
&& flux_last_update.secsTo(QDateTime::currentDateTime()) < 20 * 60);
qCDebug(runtime)<< "Result: " << ret;
return ret;
}
bool PropConditions::isKIndexValid()
{
FCT_IDENTIFICATION;
bool ret = false;
qCDebug(runtime)<<"Date valid: " << k_index_last_update.isValid() << " last_update: " << k_index_last_update;
ret = (k_index_last_update.isValid()
&& k_index_last_update.secsTo(QDateTime::currentDateTime()) < 20 * 60);
qCDebug(runtime)<< "Result: " << ret;
return ret;
}
bool PropConditions::isAIndexValid()
{
FCT_IDENTIFICATION;
bool ret = false;
qCDebug(runtime)<<"Date valid: " << a_index_last_update.isValid() << " last_update: " << a_index_last_update;
ret = (a_index_last_update.isValid()
&& a_index_last_update.secsTo(QDateTime::currentDateTime()) < 20 * 60);
qCDebug(runtime)<< "Result: " << ret;
return ret;
}
bool PropConditions::isAuroraMapValid()
{
FCT_IDENTIFICATION;
qCDebug(runtime)<<"Date valid: " << auroraMap_last_update.isValid()
<< " last_update: " << auroraMap_last_update
<< " aurora count: " << auroraMap.count();
bool ret = (auroraMap_last_update.isValid()
&& auroraMap_last_update.secsTo(QDateTime::currentDateTime()) < 20 * 60
&& auroraMap.count() > 0);
qCDebug(runtime)<< "Result: " << ret;
return ret;
}
bool PropConditions::isMufMapValid()
{
FCT_IDENTIFICATION;
qCDebug(runtime)<<"Date valid: " << mufMap_last_update.isValid()
<< " last_update: " << mufMap_last_update
<< " aurora count: " << mufMap.count();
bool ret = (mufMap_last_update.isValid()
&& mufMap_last_update.secsTo(QDateTime::currentDateTime()) < 20 * 60
&& mufMap.count() > 0);
qCDebug(runtime)<< "Result: " << ret;
return ret;
}
int PropConditions::getFlux()
{
FCT_IDENTIFICATION;
qCDebug(runtime)<<"Current Flux: " << flux << " last_update: " << flux_last_update;
return flux;
}
int PropConditions::getAIndex()
{
FCT_IDENTIFICATION;
qCDebug(runtime)<<"Current A-Index: " << a_index << " last_update: " << a_index_last_update;
return a_index;
}
double PropConditions::getKIndex()
{
FCT_IDENTIFICATION;
qCDebug(runtime)<<"Current K-Index: " << k_index << " last_update: " << k_index_last_update;
return k_index;
}
QList<GenericValueMap<double>::MapPoint> PropConditions::getAuroraPoints() const
{
FCT_IDENTIFICATION;
return auroraMap.getMap();
}
QList<GenericValueMap<double>::MapPoint> PropConditions::getMUFPoints() const
{
return mufMap.getMap();
}
QString PropConditions::solarSummaryFile()
{
FCT_IDENTIFICATION;
QDir dir(QStandardPaths::writableLocation(QStandardPaths::AppLocalDataLocation));
return dir.filePath("solar101vhf.gif");
}