This repository has been archived by the owner on Sep 19, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
currentobservation.cpp
565 lines (503 loc) · 16.8 KB
/
currentobservation.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
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
/****************************************************************************
** Copyright (C) 2014-2014 52°North Initiative for Geospatial Open Source
** Software GmbH
**
** This program is free software; you can redistribute it and/or modify it
** under the terms of the GNU General Public License version 2 as published
** by the Free Software Foundation.
**
** If the program is linked with libraries which are licensed under one of
** the following licenses, the combination of the program with the linked
** library is not considered a "derivative work" of the program:
**
** - Apache License, version 2.0
** - Apache Software License, version 1.0
** - GNU Lesser General Public License, version 3
** - Mozilla Public License, versions 1.0, 1.1 and 2.0
** - Common Development and Distribution License (CDDL), version 1.0.
**
** Therefore the distribution of the program linked with libraries licensed
** under the aforementioned licenses, is permitted by the copyright holders
** if the distribution is compliant with both the GNU General Public
** icense version 2 and the aforementioned licenses.
**
** This program is distributed in the hope that it will be useful, but
** WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
** Public License for more details.
****************************************************************************/
// Author: B.P. Ottow
// Date: August 2014
// GSoC Project: Gatherer, ILWIS Mobile. Hosted by 52 North and ITC Enschede.
//
// This is the source file for the class CurrentObservation which handles everything about the current observation(s).
// Saving it, uploading it, deleting it, showing observations.
#include "currentobservation.h"
#include "dataobject.h"
#include <QNetworkReply>
#include <QFile>
#include <QTextStream>
#include <QImage>
#include <QXmlStreamReader>
CurrentObservation::CurrentObservation(QObject *parent) :
QObject(parent)
{
}
CurrentObservation::~CurrentObservation()
{
delete nam;
}
/// To get the location of the current QML template
QString CurrentObservation::url() const
{
return m_url;
}
/// To set the location of the current QML template
/// It also reads if there is a basemap, if so the map will be set up
void CurrentObservation::setUrl(const QString url)
{
m_url = url;
QStringList dirs = m_url.split("/");
QStringList last = dirs[dirs.length() - 1].split(".");
m_name = last[0];
m_map = "";
for (int i = 0;i<dirs.length() - 1;i++)
m_map += dirs[i] + "/";
m_map += m_name + ".png";
QString filename= "/data/data/org.qtproject.example.Gatherer/files/" + m_name + ".gatherer";
QString content;
QFile file( filename );
if ( file.open(QIODevice::ReadWrite) )
{
QTextStream stream( &file );
content = stream.readAll();
}
file.close();
QStringList metadata = content.split( "\n" );
for (int i=1;i<metadata.length();i++){
QStringList line = metadata[i].split("|");
if (line.length() > 2)
if (line[1] == "BaseMap"){
QString mapUrl = line[2].trimmed();
QStringList request = mapUrl.split("&");
for (int j=0;j<request.length();j++){
if (request[j].startsWith("BBOX")){
QStringList parts = request[j].split("=");
QStringList coords = parts[1].split(",");
m_xmin = coords[0];
m_ymin = coords[1];
m_xmax = coords[2];
m_ymax = coords[3];
}
if (request[j].startsWith("WIDTH")){
QStringList parts = request[j].split("=");
m_width = parts[1];
}
if (request[j].startsWith("HEIGHT")){
QStringList parts = request[j].split("=");
m_height = parts[1];
}
}
}
}
}
/// To get the name of the current template
QString CurrentObservation::name() const
{
return m_name;
}
/// To set the name of the current template
void CurrentObservation::setName(const QString name)
{
m_name = name;
}
/// To get the time of the current observation
QString CurrentObservation::time() const
{
return m_time;
}
/// To set the time of the current observation
void CurrentObservation::setTime(const QString time)
{
m_time = time;
}
/// To get the coordinates of the current observation
QString CurrentObservation::location() const
{
return m_location;
}
/// To set the coordinates of the current observation
void CurrentObservation::setLocation(const QString location)
{
m_location = location;
}
/// To get the name of the observer of the current observation
QString CurrentObservation::observer() const
{
return m_observer;
}
/// to set the name of the observer of the current observation
void CurrentObservation::setObserver(const QString observer)
{
m_observer = observer;
}
/// To get the data fields of the current observation
QString CurrentObservation::data() const
{
return m_data;
}
/// To set the data fields of the current observation
void CurrentObservation::setData(const QString data)
{
m_data = data;
}
/// To get the server url of the current observation
QString CurrentObservation::server() const
{
return m_server;
}
/// To set the server url of the current observation
void CurrentObservation::setServer(const QString server)
{
m_server = server;
}
/// To get the response of the server after the last upload
QString CurrentObservation::response() const
{
return m_response;
}
/// To get the location of the image of the map of the current observation
QString CurrentObservation::map() const
{
return m_map;
}
/// To get the xmin of the map of the current template
QString CurrentObservation::xmin() const
{
return m_xmin;
}
/// To get the xmax of the map of the current template
QString CurrentObservation::xmax() const
{
return m_xmax;
}
/// To get the ymin of the map of the current template
QString CurrentObservation::ymin() const
{
return m_ymin;
}
/// To get the ymax of the map of the current template
QString CurrentObservation::ymax() const
{
return m_ymax;
}
/// To get the width of the map of the current template
QString CurrentObservation::width() const
{
return m_width;
}
/// To get the height of the map of the current template
QString CurrentObservation::height() const
{
return m_height;
}
/// To upload the selected observation to the server.
void CurrentObservation::upload(int i)
{
m_i = i;
/// Reading the saved observations
QFile saveFile(m_filename);
bool ret = saveFile.open(QIODevice::ReadWrite | QIODevice::Text);
QString content;
if( ret )
{
QTextStream stream(&saveFile);
content = stream.readAll();
}
saveFile.close();
QStringList observations = content.split("\n");
/// Uploading the selected observation
if ( nam)
delete nam;
nam = new QNetworkAccessManager();
QObject::connect(nam, SIGNAL(finished(QNetworkReply*)),
this, SLOT(finishedUploadiSlot(QNetworkReply*)));
qDebug() << observations[i];
QUrl url(observations[i]);
nam->get(QNetworkRequest(url));
}
/// The corresponding slot
void CurrentObservation::finishedUploadiSlot(QNetworkReply *reply)
{
QString replyContent = reply->readAll();
/// Saving the response
if (replyContent.length() > 100) { /// if succes
QStringList replyPieces = replyContent.split(">");
QStringList response = replyPieces[3].split("<");
m_response = response[0];
/// deleting the uploaded observation
QFile saveFile(m_filename);
bool ret = saveFile.open(QIODevice::ReadWrite | QIODevice::Text);
QString content;
if( ret )
{
QTextStream stream(&saveFile);
content = stream.readAll();
}
saveFile.close();
QStringList observations = content.split("\n");
QFile file(m_filename);
file.open(QIODevice::WriteOnly | QIODevice::Truncate);
QTextStream stream(&file);
for (int j = 0; j<observations.length()-1;j++){
if (m_i != j)
stream << observations[j] + "\n";
}
file.close();
}
else
m_response = "upload failed";
emit responseReady();
}
/// For saving the current observation
void CurrentObservation::save()
{
/// Opening the savefile
QFile saveFile(m_filename);
bool ret = saveFile.open(QIODevice::ReadWrite | QIODevice::Text);
QString content;
if( ret )
{
QString urlString = m_server + "?subjectobservation&location=" + m_location + "&time=" + m_time + "&subjectname=" + m_name + "&observer=" + m_observer + m_data + "\n";
QTextStream stream(&saveFile);
content = stream.readAll();
/// Adding the observation at the end
stream << urlString;
}
saveFile.close();
}
/// For uploading all the saved observations
void CurrentObservation::uploadSaved()
{
m_i = 0;
m_response = "";
m_failed.clear();
QFile saveFile(m_filename);
bool ret = saveFile.open(QIODevice::ReadWrite | QIODevice::Text);
QString content;
if( ret )
{
QTextStream stream(&saveFile);
content = stream.readAll();
}
saveFile.close();
QStringList observations = content.split("\n");
if ( nam)
delete nam;
nam = new QNetworkAccessManager();
QObject::connect(nam, SIGNAL(finished(QNetworkReply*)),
this, SLOT(finishedUploadallSlot(QNetworkReply*)));
qDebug() << observations[m_i];
QUrl url(observations[m_i]);
nam->get(QNetworkRequest(url));
}
/// The corresponding slot
void CurrentObservation::finishedUploadallSlot(QNetworkReply *reply)
{
QString replyContent = reply->readAll();
/// Saving response
if (replyContent.length() > 100) {
QStringList replyPieces = replyContent.split(">");
QStringList response = replyPieces[3].split("<");
m_response += response[0] + "\n";
}
else {
m_response += "upload failed\n";
m_failed.append(m_i);
}
//qDebug()<<replyContent;
m_i++;
/// loading in observations
QFile saveFile(m_filename);
bool ret = saveFile.open(QIODevice::ReadWrite | QIODevice::Text);
QString content;
if( ret )
{
QTextStream stream(&saveFile);
content = stream.readAll();
}
saveFile.close();
QStringList observations = content.split("\n");
if (m_i == observations.length() - 1){ /// last observation already uploaded, delete succesfull uploaded observations
QFile file(m_filename);
file.open(QIODevice::WriteOnly | QIODevice::Truncate);
QTextStream stream(&file);
for (int j = 0; j<observations.length()-1;j++){
if (m_failed.contains(j))
stream << observations[j] + "\n";
}
file.close();
emit responseReady();
}
else { /// upload next observation
if ( nam)
delete nam;
nam = new QNetworkAccessManager();
QObject::connect(nam, SIGNAL(finished(QNetworkReply*)),
this, SLOT(finishedUploadallSlot(QNetworkReply*)));
qDebug() << observations[m_i];
QUrl url(observations[m_i]);
nam->get(QNetworkRequest(url));
}
}
/// For loading in the saved observations for the listmodel
void CurrentObservation::loadSaved()
{
QFile saveFile(m_filename);
bool ret = saveFile.open(QIODevice::ReadWrite | QIODevice::Text);
QString content;
if( ret )
{
QTextStream stream(&saveFile);
content = stream.readAll();
}
saveFile.close();
QStringList observations = content.split("\n");
m_model.clear();
for (int i = 0; i<observations.length()-1;i++){
QStringList data= observations[i].split("&");
QStringList time = data[2].split("=");
QStringList subjectname = data[3].split("=");
QStringList input = data[5].split("=");
m_model.append(new DataObject(time[1],subjectname[1],input[1]));
}
}
/// Deleting the selected observation
void CurrentObservation::deleteObservation(int i)
{
QFile saveFile(m_filename);
bool ret = saveFile.open(QIODevice::ReadWrite | QIODevice::Text);
QString content;
if( ret )
{
QTextStream stream(&saveFile);
content = stream.readAll();
}
saveFile.close();
QStringList observations = content.split("\n");
QFile file(m_filename);
file.open(QIODevice::WriteOnly | QIODevice::Truncate);
if( ret )
{
QTextStream stream(&file);
for (int j = 0; j<observations.length()-1;j++){
if (i != j)
stream << observations[j] + "\n";
}
}
file.close();
}
/// Check if there is a map downloaded
QString CurrentObservation::mapAvailable()
{
QStringList filename = m_map.split("//");
QFile file(filename[1]);
QString result = "false";
if (file.exists()) {
result = "true";
return result;
}
else
return result;
}
/// To show the historical observations on the map.
void CurrentObservation::showObservations(const QString from, const QString to)
{
/// Parse xml file
QString filename= "/data/data/org.qtproject.example.Gatherer/files/" + m_name + ".xml";
QFile file( filename );
QDateTime fromDate = QDateTime::fromString(from, "yyyy-MM-dd");
QDateTime toDate = QDateTime::fromString(to, "yyyy-MM-dd");
QString time = "";
QString observation = "";
QString observer = "";
QStringList coords = {"",""};
if ( file.open(QIODevice::ReadWrite) )
{
QXmlStreamReader xmlReader;
xmlReader.setDevice(&file);
xmlReader.readNext();
bool firstObservation = false;
bool secondObservation = false;
while (!xmlReader.atEnd())
{
if (xmlReader.isStartElement())
{
QString name = xmlReader.name().toString();
//qDebug()<< "start-tag: " + name;
if (name == "Observation") {
if (firstObservation) {
observation = xmlReader.readElementText();
//qDebug()<< observation;
}
else
firstObservation = true;
} else if (name == "Location") {
QString text = xmlReader.readElementText();
QString coord = text.mid(6, text.length() - 7);
coords = coord.split(" ");
} else if (name == "TimeStamp") {
QString text = xmlReader.readElementText();
time = text.left(10);
} else if (name == "Observer") {
observer = xmlReader.readElementText();
}
} else if (xmlReader.isEndElement()) {
QString name = xmlReader.name().toString();
if (name == "Observation")
{
if (secondObservation)
secondObservation = false;
else {
firstObservation = false;
QDateTime timeDate = QDateTime::fromString(time, "yyyy-MM-dd");
if (timeDate <= toDate && timeDate >= fromDate)
emit addPoint(coords[0].toDouble(), coords[1].toDouble(), observation, time, observer, false);
}
}
}
xmlReader.readNext();
}
if (xmlReader.hasError())
{
qDebug() << "XML error: " << xmlReader.errorString();
qDebug() << firstObservation + " " + secondObservation;
}
}
/// Parse new observations
QFile saveFile(m_filename);
bool ret = saveFile.open(QIODevice::ReadWrite | QIODevice::Text);
QString content;
if( ret )
{
QTextStream stream(&saveFile);
content = stream.readAll();
saveFile.close();
QStringList observations = content.split("\n");
if (observations[0].startsWith("http"))
for (int i = 0; i<observations.length()-1;i++){
QStringList data= observations[i].split("&");
QStringList coordData = data[1].split("=");
QString coord = coordData[1].mid(6, coordData[1].length() - 7);
coords = coord.split(" ");
QStringList timeList = data[2].split("=");
QStringList observerList = data[4].split("=");
QStringList input = data[5].split("=");
emit addPoint(coords[0].toDouble(), coords[1].toDouble(), input[1], timeList[1], observerList[1], true);
}
}
}
QQmlListProperty<DataObject> CurrentObservation::model()
{
return QQmlListProperty<DataObject>(this,m_model);
}