From a7f8a7574e1fa6fd2422d151a3d5e3fc94586d5b Mon Sep 17 00:00:00 2001 From: Dirk Hohndel Date: Thu, 7 Jan 2016 00:08:00 -0800 Subject: Create GpsListModel in order to be able to display GPS fixes This will allow us to visualize the GPS fixes that are currently stored in the QML UI. Signed-off-by: Dirk Hohndel --- subsurface-core/gpslocation.cpp | 7 +------ subsurface-core/gpslocation.h | 7 +++++++ 2 files changed, 8 insertions(+), 6 deletions(-) (limited to 'subsurface-core') diff --git a/subsurface-core/gpslocation.cpp b/subsurface-core/gpslocation.cpp index 9019f59c7..6106dd358 100644 --- a/subsurface-core/gpslocation.cpp +++ b/subsurface-core/gpslocation.cpp @@ -1,4 +1,5 @@ #include "gpslocation.h" +#include "gpslistmodel.h" #include "pref.h" #include "dive.h" #include "helpers.h" @@ -195,12 +196,6 @@ int GpsLocation::getGpsNum() const return geoSettings->value("count", 0).toInt(); } -struct gpsTracker { - degrees_t latitude; - degrees_t longitude; - time_t when; -}; - static void copy_gps_location(struct gpsTracker *gps, struct dive *d) { struct dive_site *ds = get_dive_site_by_uuid(d->dive_site_uuid); diff --git a/subsurface-core/gpslocation.h b/subsurface-core/gpslocation.h index 94706f611..d595c877e 100644 --- a/subsurface-core/gpslocation.h +++ b/subsurface-core/gpslocation.h @@ -9,6 +9,13 @@ #include #include +struct gpsTracker { + degrees_t latitude; + degrees_t longitude; + time_t when; + QString name; +}; + class GpsLocation : QObject { Q_OBJECT -- cgit v1.2.3-70-g09d2 From 1c66e399fa1f3549a9c3349d3a3f13000bb0d353 Mon Sep 17 00:00:00 2001 From: Dirk Hohndel Date: Thu, 7 Jan 2016 21:32:24 -0800 Subject: Download gps locations in the mobile app This is not the same as the existing download to apply the gps fixes to the dive list. This allows us to download and store the GPS fixes in the settings. I may end up changing things around to have a shared implementation for downloading the GPS fixes, but for now this seemed easier. Signed-off-by: Dirk Hohndel --- subsurface-core/gpslocation.cpp | 83 +++++++++++++++++++++++++++++++++++++++++ subsurface-core/gpslocation.h | 1 + 2 files changed, 84 insertions(+) (limited to 'subsurface-core') diff --git a/subsurface-core/gpslocation.cpp b/subsurface-core/gpslocation.cpp index 6106dd358..c2f8b0e2f 100644 --- a/subsurface-core/gpslocation.cpp +++ b/subsurface-core/gpslocation.cpp @@ -10,8 +10,12 @@ #include #include #include +#include +#include +#include #define GPS_FIX_ADD_URL "http://api.subsurface-divelog.org/api/dive/add/" +#define GPS_FIX_DOWNLOAD_URL "http://api.subsurface-divelog.org/api/dive/get/" #define GET_WEBSERVICE_UID_URL "https://cloud.subsurface-divelog.org/webuserid/" GpsLocation *GpsLocation::m_Instance = NULL; @@ -396,3 +400,82 @@ void GpsLocation::uploadToServer() geoSettings->setValue(QString("gpsFix%1_uploaded").arg(i), 1); } } + +void GpsLocation::downloadFromServer() +{ + QEventLoop loop; + QTimer timer; + timer.setSingleShot(true); + QNetworkAccessManager *manager = new QNetworkAccessManager(qApp); + QUrl url(QString(GPS_FIX_DOWNLOAD_URL "?login=%1").arg(prefs.userid)); + QNetworkRequest request; + request.setUrl(url); + request.setRawHeader("User-Agent", getUserAgent().toUtf8()); + request.setRawHeader("Accept", "text/json"); + request.setRawHeader("Content-type", "text/html"); + qDebug() << "downloadFromServer accessing" << url; + reply = manager->get(request); + connect(&timer, &QTimer::timeout, &loop, &QEventLoop::quit); + connect(reply, &QNetworkReply::finished, &loop, &QEventLoop::quit); + connect(reply, SIGNAL(error(QNetworkReply::NetworkError)), + this, SLOT(getUseridError(QNetworkReply::NetworkError))); + timer.start(10000); + loop.exec(); + if (timer.isActive()) { + timer.stop(); + if (!reply->error()) { + QString response = reply->readAll(); + QJsonDocument json = QJsonDocument::fromJson(response.toLocal8Bit()); + QJsonObject object = json.object(); + if (object.value("download").toString() != "ok") { + qDebug() << "problems downloading GPS fixes"; + return; + } + // create a table with the GPS information + QHash gpsFixes; + int existing = geoSettings->value("count", 0).toInt(); + for (int i = 0; i < existing; i++) { + struct gpsTracker gt; + gt.latitude.udeg = geoSettings->value(QString("gpsFix%1_lat").arg(i)).toInt(); + gt.longitude.udeg = geoSettings->value(QString("gpsFix%1_lon").arg(i)).toInt(); + gt.when = geoSettings->value(QString("gpsFix%1_time").arg(i)).toULongLong(); + gpsFixes.insert(gt.when, gt); + } + qDebug() << "already have" << gpsFixes.count() << "GPS fixes"; + QJsonArray dives = object.value("dives").toArray(); + qDebug() << dives.count() << "GPS fixes downloaded"; + for (int i = 0; i < dives.count(); i++) { + QJsonObject fix = dives[i].toObject(); + QString date = fix.value("date").toString(); + QString time = fix.value("time").toString(); + QString name = fix.value("name").toString(); + QString latitude = fix.value("latitude").toString(); + QString longitude = fix.value("longitude").toString(); + QDateTime timestamp = QDateTime::fromString(date + " " + time, "yyyy-M-d hh:m:s"); + + struct gpsTracker gt; + gt.when = timestamp.toMSecsSinceEpoch() / 1000 + gettimezoneoffset(timestamp.toMSecsSinceEpoch() / 1000); + gt.latitude.udeg = latitude.toDouble() * 1000000; + gt.longitude.udeg = longitude.toDouble() * 1000000; + gt.name = name; + gpsFixes.insert(gt.when, gt); + } + QList keys = gpsFixes.keys(); + qSort(keys); + for (int i = 0; i < keys.count(); i++) { + struct gpsTracker gt = gpsFixes.value(keys[i]); + geoSettings->setValue(QString("gpsFix%1_time").arg(i), (uint64_t)gt.when); + geoSettings->setValue(QString("gpsFix%1_name").arg(i), gt.name); + geoSettings->setValue(QString("gpsFix%1_lat").arg(i), gt.latitude.udeg); + geoSettings->setValue(QString("gpsFix%1_lon").arg(i), gt.longitude.udeg); + } + geoSettings->setValue("count", keys.count()); + } else { + qDebug() << "network error" << reply->error() << reply->errorString() << reply->readAll(); + } + } else { + qDebug() << "download timed out"; + status("Download from server timed out"); + } + reply->deleteLater(); +} diff --git a/subsurface-core/gpslocation.h b/subsurface-core/gpslocation.h index d595c877e..2d2d915d0 100644 --- a/subsurface-core/gpslocation.h +++ b/subsurface-core/gpslocation.h @@ -46,6 +46,7 @@ public slots: void newPosition(QGeoPositionInfo pos); void updateTimeout(); void uploadToServer(); + void downloadFromServer(); void postError(QNetworkReply::NetworkError error); void getUseridError(QNetworkReply::NetworkError error); void clearGpsData(); -- cgit v1.2.3-70-g09d2 From 06dcc9ab8d6d7ca7d9967fa9db4c8e87ae376dec Mon Sep 17 00:00:00 2001 From: Dirk Hohndel Date: Thu, 7 Jan 2016 21:34:21 -0800 Subject: Populate the model with the GPS fixes Signed-off-by: Dirk Hohndel --- subsurface-core/gpslocation.cpp | 26 ++++++++++++++++++++++++++ subsurface-core/gpslocation.h | 1 + 2 files changed, 27 insertions(+) (limited to 'subsurface-core') diff --git a/subsurface-core/gpslocation.cpp b/subsurface-core/gpslocation.cpp index c2f8b0e2f..cbcb6910b 100644 --- a/subsurface-core/gpslocation.cpp +++ b/subsurface-core/gpslocation.cpp @@ -324,6 +324,32 @@ void GpsLocation::applyLocations() mark_divelist_changed(true); } +void GpsLocation::updateModel() +{ + GpsListModel *gpsListModel = GpsListModel::instance(); + if (!gpsListModel) { + qDebug() << "no gpsListModel"; + return; + } + int cnt = geoSettings->value("count", 0).toInt(); + if (cnt == 0) { + qDebug() << "no gps fixes"; + gpsListModel->clear(); + return; + } + + // create a table with the GPS information + struct gpsTracker gt; + for (int i = 0; i < cnt; i++) { + gt.latitude.udeg = geoSettings->value(QString("gpsFix%1_lat").arg(i)).toInt(); + gt.longitude.udeg = geoSettings->value(QString("gpsFix%1_lon").arg(i)).toInt(); + gt.when = geoSettings->value(QString("gpsFix%1_time").arg(i)).toULongLong(); + gt.name = geoSettings->value(QString("gpsFix%1_name").arg(i)).toString(); + gpsListModel->addGpsFix(>); + } + qDebug() << "added" << cnt << "gps fixes to model"; +} + void GpsLocation::clearGpsData() { geoSettings->clear(); diff --git a/subsurface-core/gpslocation.h b/subsurface-core/gpslocation.h index 2d2d915d0..3dbe48320 100644 --- a/subsurface-core/gpslocation.h +++ b/subsurface-core/gpslocation.h @@ -49,6 +49,7 @@ public slots: void downloadFromServer(); void postError(QNetworkReply::NetworkError error); void getUseridError(QNetworkReply::NetworkError error); + void updateModel(); void clearGpsData(); }; -- cgit v1.2.3-70-g09d2