2016-04-05 05:02:03 +00:00
|
|
|
#include "core/gpslocation.h"
|
|
|
|
#include "qt-models/gpslistmodel.h"
|
|
|
|
#include "core/pref.h"
|
|
|
|
#include "core/dive.h"
|
|
|
|
#include "core/helpers.h"
|
2015-11-11 23:19:09 +00:00
|
|
|
#include <time.h>
|
2015-11-14 01:21:43 +00:00
|
|
|
#include <unistd.h>
|
2015-11-11 18:52:52 +00:00
|
|
|
#include <QDebug>
|
2015-11-11 23:19:09 +00:00
|
|
|
#include <QVariant>
|
2015-11-14 01:21:43 +00:00
|
|
|
#include <QUrlQuery>
|
|
|
|
#include <QApplication>
|
|
|
|
#include <QTimer>
|
2016-01-08 05:32:24 +00:00
|
|
|
#include <QJsonDocument>
|
|
|
|
#include <QJsonObject>
|
|
|
|
#include <QJsonArray>
|
2015-11-11 18:50:55 +00:00
|
|
|
|
2015-11-18 21:14:19 +00:00
|
|
|
#define GPS_FIX_ADD_URL "http://api.subsurface-divelog.org/api/dive/add/"
|
2016-01-11 01:39:13 +00:00
|
|
|
#define GPS_FIX_DELETE_URL "http://api.subsurface-divelog.org/api/dive/delete/"
|
2016-01-08 05:32:24 +00:00
|
|
|
#define GPS_FIX_DOWNLOAD_URL "http://api.subsurface-divelog.org/api/dive/get/"
|
2015-11-18 21:14:19 +00:00
|
|
|
#define GET_WEBSERVICE_UID_URL "https://cloud.subsurface-divelog.org/webuserid/"
|
|
|
|
|
2015-12-02 22:30:47 +00:00
|
|
|
GpsLocation *GpsLocation::m_Instance = NULL;
|
|
|
|
|
2016-04-18 05:41:30 +00:00
|
|
|
GpsLocation::GpsLocation(void (*showMsgCB)(const char *), QObject *parent) :
|
|
|
|
QObject(parent),
|
|
|
|
m_GpsSource(0),
|
|
|
|
waitingForPosition(false),
|
|
|
|
haveSource(UNKNOWN)
|
2015-11-11 18:52:52 +00:00
|
|
|
{
|
2015-12-02 22:30:47 +00:00
|
|
|
Q_ASSERT_X(m_Instance == NULL, "GpsLocation", "GpsLocation recreated");
|
|
|
|
m_Instance = this;
|
2015-11-19 02:10:58 +00:00
|
|
|
showMessageCB = showMsgCB;
|
2015-11-12 21:04:23 +00:00
|
|
|
// create a QSettings object that's separate from the main application settings
|
2015-11-13 19:55:39 +00:00
|
|
|
geoSettings = new QSettings(QSettings::NativeFormat, QSettings::UserScope,
|
|
|
|
QString("org.subsurfacedivelog"), QString("subsurfacelocation"), this);
|
2015-11-18 22:44:07 +00:00
|
|
|
userAgent = getUserAgent();
|
2016-04-18 05:41:30 +00:00
|
|
|
(void)getGpsSource();
|
2016-01-11 01:22:20 +00:00
|
|
|
loadFromStorage();
|
2015-11-11 18:52:52 +00:00
|
|
|
}
|
2015-11-11 20:34:56 +00:00
|
|
|
|
2015-12-02 22:30:47 +00:00
|
|
|
GpsLocation *GpsLocation::instance()
|
|
|
|
{
|
|
|
|
Q_ASSERT(m_Instance != NULL);
|
|
|
|
|
|
|
|
return m_Instance;
|
|
|
|
}
|
|
|
|
|
|
|
|
GpsLocation::~GpsLocation()
|
|
|
|
{
|
|
|
|
m_Instance = NULL;
|
|
|
|
}
|
|
|
|
|
2015-11-30 18:58:18 +00:00
|
|
|
QGeoPositionInfoSource *GpsLocation::getGpsSource()
|
|
|
|
{
|
2016-04-18 05:41:30 +00:00
|
|
|
if (haveSource == NOGPS)
|
|
|
|
return 0;
|
|
|
|
|
2016-01-02 01:19:28 +00:00
|
|
|
if (!m_GpsSource) {
|
|
|
|
m_GpsSource = QGeoPositionInfoSource::createDefaultSource(this);
|
|
|
|
if (m_GpsSource != 0) {
|
2016-04-18 05:50:33 +00:00
|
|
|
#if defined(Q_OS_IOS)
|
|
|
|
// at least Qt 5.6.0 isn't doing things right on iOS - it MUST check for permission before
|
|
|
|
// accessing the position source
|
|
|
|
// I have a hacked version of Qt 5.6.0 that I will build the iOS binaries with for now;
|
|
|
|
// this test below righ after creating the source checks if the location service is disabled
|
|
|
|
// and set's an error right when the position source is created to indicate this
|
|
|
|
if (m_GpsSource->error() == QGeoPositionInfoSource::AccessError) {
|
|
|
|
haveSource = NOGPS;
|
|
|
|
emit haveSourceChanged();
|
|
|
|
m_GpsSource = NULL;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
#endif
|
2016-04-18 05:41:30 +00:00
|
|
|
haveSource = (m_GpsSource->supportedPositioningMethods() & QGeoPositionInfoSource::SatellitePositioningMethods) ? HAVEGPS : NOGPS;
|
|
|
|
emit haveSourceChanged();
|
2015-12-02 22:30:47 +00:00
|
|
|
#ifndef SUBSURFACE_MOBILE
|
|
|
|
if (verbose)
|
|
|
|
#endif
|
2016-03-24 15:49:45 +00:00
|
|
|
status(QString("Created position source %1").arg(m_GpsSource->sourceName()));
|
2016-01-02 01:19:28 +00:00
|
|
|
connect(m_GpsSource, SIGNAL(positionUpdated(QGeoPositionInfo)), this, SLOT(newPosition(QGeoPositionInfo)));
|
|
|
|
connect(m_GpsSource, SIGNAL(updateTimeout()), this, SLOT(updateTimeout()));
|
2016-04-18 05:47:53 +00:00
|
|
|
connect(m_GpsSource, SIGNAL(error(QGeoPositionInfoSource::Error)), this, SLOT(positionSourceError(QGeoPositionInfoSource::Error)));
|
2016-01-02 01:19:28 +00:00
|
|
|
m_GpsSource->setUpdateInterval(5 * 60 * 1000); // 5 minutes so the device doesn't drain the battery
|
2015-11-30 18:58:18 +00:00
|
|
|
} else {
|
2015-12-18 22:37:01 +00:00
|
|
|
#ifdef SUBSURFACE_MOBILE
|
2015-11-30 18:58:18 +00:00
|
|
|
status("don't have GPS source");
|
2015-12-18 22:37:01 +00:00
|
|
|
#endif
|
2016-04-18 05:41:30 +00:00
|
|
|
haveSource = NOGPS;
|
|
|
|
emit haveSourceChanged();
|
2015-11-30 18:58:18 +00:00
|
|
|
}
|
|
|
|
}
|
2016-01-02 01:19:28 +00:00
|
|
|
return m_GpsSource;
|
2015-11-30 18:58:18 +00:00
|
|
|
}
|
|
|
|
|
2015-11-19 02:56:50 +00:00
|
|
|
bool GpsLocation::hasLocationsSource()
|
|
|
|
{
|
2016-04-18 05:41:30 +00:00
|
|
|
(void)getGpsSource();
|
|
|
|
return haveSource == HAVEGPS;
|
2015-11-19 02:56:50 +00:00
|
|
|
}
|
|
|
|
|
2015-11-11 20:34:56 +00:00
|
|
|
void GpsLocation::serviceEnable(bool toggle)
|
|
|
|
{
|
2015-11-30 18:58:18 +00:00
|
|
|
QGeoPositionInfoSource *gpsSource = getGpsSource();
|
2016-04-18 05:41:30 +00:00
|
|
|
if (haveSource != HAVEGPS) {
|
2015-11-11 23:19:09 +00:00
|
|
|
if (toggle)
|
|
|
|
status("Can't start location service, no location source available");
|
2015-11-11 20:34:56 +00:00
|
|
|
return;
|
2015-11-11 23:19:09 +00:00
|
|
|
}
|
2015-11-11 20:34:56 +00:00
|
|
|
if (toggle) {
|
|
|
|
gpsSource->startUpdates();
|
2016-03-24 15:49:45 +00:00
|
|
|
status(QString("Starting Subsurface GPS service with update interval %1").arg(gpsSource->updateInterval()));
|
2015-11-11 20:34:56 +00:00
|
|
|
} else {
|
|
|
|
gpsSource->stopUpdates();
|
|
|
|
status("Stopping Subsurface GPS service");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-02 01:21:30 +00:00
|
|
|
QString GpsLocation::currentPosition()
|
|
|
|
{
|
|
|
|
if (!hasLocationsSource())
|
|
|
|
return tr("Unknown GPS location");
|
2016-01-09 22:07:58 +00:00
|
|
|
if (m_trackers.count() && m_trackers.lastKey() + 300 >= QDateTime::currentMSecsSinceEpoch() / 1000) {
|
|
|
|
// we can simply use the last position that we tracked
|
|
|
|
gpsTracker gt = m_trackers.last();
|
|
|
|
QString gpsString = printGPSCoords(gt.latitude.udeg, gt.longitude.udeg);
|
|
|
|
qDebug() << "returning last position" << gpsString;
|
|
|
|
return gpsString;
|
2016-01-02 01:21:30 +00:00
|
|
|
}
|
|
|
|
qDebug() << "requesting new GPS position";
|
|
|
|
m_GpsSource->requestUpdate();
|
|
|
|
// ok, we need to get the current position and somehow in the callback update the location in the QML UI
|
|
|
|
// punting right now
|
|
|
|
waitingForPosition = true;
|
QML UI: rewrite the commitChanges function
I couldn't figure out how to break this down into small, useful commits.
Part of the problem is that I kept going while working on this and as you
can see from looking at the commit, diff tries so hard to find small code
fragments that moved around, that the diff overall becomes quite
unreadable and it seemed impossible to recreate the sequence of steps
after the fact.
It all started with adding the parsing for the GPS coordinates. But while
testing that code I found several issues with the rest of the function.
Most importantly it seemed ridiculous that we carefully tried to match the
texts that the DiveObjectHelper would create for the various fields,
instead of just using the DiveObjectHelper to do just that. And once I had
converted that I once again realized just how long and hard to understand
that function was getting and decided to break out some of the more
complex parts into their own helper functions.
But of course all this didn't happen in this logical, structured, ordered
way. Instead I did all of these things at the same time, testing,
rearranging, etc.
So in the end I went with one BIG commit that does all of this in one fell
swoop.
This adds four helper functions to deal with start time/date, duration,
location and gps coordinates, and depth of the dive.
To avoid mistakes when dealing with the GPS coordinates, there's another
helper to encapsulate the creation of the dive site and we switched to a
current GPS location.
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2016-04-15 13:01:14 +00:00
|
|
|
return GPS_CURRENT_POS;
|
2016-01-02 01:21:30 +00:00
|
|
|
}
|
|
|
|
|
2015-11-11 18:52:52 +00:00
|
|
|
void GpsLocation::newPosition(QGeoPositionInfo pos)
|
|
|
|
{
|
2016-03-24 18:50:32 +00:00
|
|
|
int64_t lastTime = 0;
|
2015-11-11 23:44:58 +00:00
|
|
|
QGeoCoordinate lastCoord;
|
2016-01-09 22:07:58 +00:00
|
|
|
int nr = m_trackers.count();
|
2015-11-11 23:44:58 +00:00
|
|
|
if (nr) {
|
2016-01-09 22:07:58 +00:00
|
|
|
gpsTracker gt = m_trackers.last();
|
|
|
|
lastCoord.setLatitude(gt.latitude.udeg / 1000000.0);
|
|
|
|
lastCoord.setLongitude(gt.longitude.udeg / 1000000.0);
|
|
|
|
lastTime = gt.when;
|
2015-11-11 23:44:58 +00:00
|
|
|
}
|
2016-01-02 01:20:30 +00:00
|
|
|
// if we are waiting for a position update or
|
2015-11-13 20:01:01 +00:00
|
|
|
// if we have no record stored or if at least the configured minimum
|
|
|
|
// time has passed or we moved at least the configured minimum distance
|
2016-03-24 18:50:32 +00:00
|
|
|
int64_t delta = (int64_t)pos.timestamp().toTime_t() + gettimezoneoffset() - lastTime;
|
|
|
|
if (!nr || waitingForPosition || delta > prefs.time_threshold ||
|
2015-11-14 17:10:06 +00:00
|
|
|
lastCoord.distanceTo(pos.coordinate()) > prefs.distance_threshold) {
|
2016-03-24 18:50:32 +00:00
|
|
|
QString msg("received new position %1 after delta %2 threshold %3");
|
|
|
|
status(qPrintable(msg.arg(pos.coordinate().toString()).arg(delta).arg(prefs.time_threshold)));
|
2016-01-02 01:20:30 +00:00
|
|
|
waitingForPosition = false;
|
2016-01-09 22:07:58 +00:00
|
|
|
gpsTracker gt;
|
|
|
|
gt.when = pos.timestamp().toTime_t();
|
|
|
|
gt.when += gettimezoneoffset(gt.when);
|
|
|
|
gt.latitude.udeg = rint(pos.coordinate().latitude() * 1000000);
|
|
|
|
gt.longitude.udeg = rint(pos.coordinate().longitude() * 1000000);
|
|
|
|
addFixToStorage(gt);
|
2015-11-11 23:44:58 +00:00
|
|
|
}
|
2015-11-11 18:50:55 +00:00
|
|
|
}
|
|
|
|
|
2015-11-11 18:52:52 +00:00
|
|
|
void GpsLocation::updateTimeout()
|
|
|
|
{
|
2015-11-11 19:16:59 +00:00
|
|
|
status("request to get new position timed out");
|
|
|
|
}
|
|
|
|
|
2016-04-18 05:47:53 +00:00
|
|
|
void GpsLocation::positionSourceError(QGeoPositionInfoSource::Error)
|
|
|
|
{
|
|
|
|
status("error receiving a GPS location");
|
|
|
|
haveSource = NOGPS;
|
|
|
|
emit haveSourceChanged();
|
|
|
|
}
|
|
|
|
|
2015-11-11 19:16:59 +00:00
|
|
|
void GpsLocation::status(QString msg)
|
|
|
|
{
|
|
|
|
qDebug() << msg;
|
2015-11-19 02:10:58 +00:00
|
|
|
if (showMessageCB)
|
|
|
|
(*showMessageCB)(qPrintable(msg));
|
2015-11-11 18:52:52 +00:00
|
|
|
}
|
2015-11-12 22:57:27 +00:00
|
|
|
|
2015-11-18 21:14:19 +00:00
|
|
|
QString GpsLocation::getUserid(QString user, QString passwd)
|
|
|
|
{
|
|
|
|
qDebug() << "called getUserid";
|
|
|
|
QEventLoop loop;
|
|
|
|
QTimer timer;
|
|
|
|
timer.setSingleShot(true);
|
|
|
|
|
|
|
|
QNetworkAccessManager *manager = new QNetworkAccessManager(qApp);
|
|
|
|
QUrl url(GET_WEBSERVICE_UID_URL);
|
|
|
|
QString data;
|
|
|
|
data = user + " " + passwd;
|
|
|
|
QNetworkRequest request;
|
|
|
|
request.setUrl(url);
|
2015-11-18 22:44:07 +00:00
|
|
|
request.setRawHeader("User-Agent", getUserAgent().toUtf8());
|
2015-11-18 21:14:19 +00:00
|
|
|
request.setRawHeader("Accept", "text/html");
|
|
|
|
request.setRawHeader("Content-type", "application/x-www-form-urlencoded");
|
|
|
|
reply = manager->post(request, data.toUtf8());
|
|
|
|
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() == QNetworkReply::NoError) {
|
|
|
|
QString result = reply->readAll();
|
|
|
|
status(QString("received ") + result);
|
|
|
|
result.remove("WebserviceID:");
|
|
|
|
reply->deleteLater();
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
status("Getting Web service ID timed out");
|
|
|
|
}
|
|
|
|
reply->deleteLater();
|
|
|
|
return QString();
|
|
|
|
}
|
|
|
|
|
2015-11-13 17:17:13 +00:00
|
|
|
int GpsLocation::getGpsNum() const
|
|
|
|
{
|
2016-01-09 22:07:58 +00:00
|
|
|
return m_trackers.count();
|
2015-11-13 17:17:13 +00:00
|
|
|
}
|
|
|
|
|
2016-01-09 22:07:58 +00:00
|
|
|
static void copy_gps_location(struct gpsTracker &gps, struct dive *d)
|
2015-11-12 22:57:27 +00:00
|
|
|
{
|
|
|
|
struct dive_site *ds = get_dive_site_by_uuid(d->dive_site_uuid);
|
2016-01-11 18:02:58 +00:00
|
|
|
if (!ds) {
|
|
|
|
d->dive_site_uuid = create_dive_site(qPrintable(gps.name), gps.when);
|
|
|
|
ds = get_dive_site_by_uuid(d->dive_site_uuid);
|
|
|
|
}
|
2016-01-09 22:07:58 +00:00
|
|
|
ds->latitude = gps.latitude;
|
|
|
|
ds->longitude = gps.longitude;
|
2015-11-12 22:57:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#define SAME_GROUP 6 * 3600 /* six hours */
|
2016-01-11 03:34:21 +00:00
|
|
|
bool GpsLocation::applyLocations()
|
2015-11-12 22:57:27 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
bool changed = false;
|
|
|
|
int last = 0;
|
2016-01-09 22:07:58 +00:00
|
|
|
int cnt = m_trackers.count();
|
2015-11-12 22:57:27 +00:00
|
|
|
if (cnt == 0)
|
2016-01-11 03:34:21 +00:00
|
|
|
return false;
|
2015-11-12 22:57:27 +00:00
|
|
|
|
|
|
|
// create a table with the GPS information
|
2016-01-09 22:07:58 +00:00
|
|
|
QList<struct gpsTracker> gpsTable = m_trackers.values();
|
2015-11-12 22:57:27 +00:00
|
|
|
|
|
|
|
// now walk the dive table and see if we can fill in missing gps data
|
|
|
|
struct dive *d;
|
|
|
|
for_each_dive(i, d) {
|
|
|
|
if (dive_has_gps_location(d))
|
|
|
|
continue;
|
|
|
|
for (int j = last; j < cnt; j++) {
|
|
|
|
if (time_during_dive_with_offset(d, gpsTable[j].when, SAME_GROUP)) {
|
|
|
|
if (verbose)
|
2015-11-14 01:21:43 +00:00
|
|
|
qDebug() << "processing gpsFix @" << get_dive_date_string(gpsTable[j].when) <<
|
2015-11-12 22:57:27 +00:00
|
|
|
"which is withing six hours of dive from" <<
|
|
|
|
get_dive_date_string(d->when) << "until" <<
|
|
|
|
get_dive_date_string(d->when + d->duration.seconds);
|
|
|
|
/*
|
|
|
|
* If position is fixed during dive. This is the good one.
|
|
|
|
* Asign and mark position, and end gps_location loop
|
|
|
|
*/
|
|
|
|
if (time_during_dive_with_offset(d, gpsTable[j].when, 0)) {
|
|
|
|
if (verbose)
|
2015-11-14 01:21:43 +00:00
|
|
|
qDebug() << "gpsFix is during the dive, pick that one";
|
2016-01-09 22:07:58 +00:00
|
|
|
copy_gps_location(gpsTable[j], d);
|
2015-11-12 22:57:27 +00:00
|
|
|
changed = true;
|
|
|
|
last = j;
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* If it is not, check if there are more position fixes in SAME_GROUP range
|
|
|
|
*/
|
|
|
|
if (j + 1 < cnt && time_during_dive_with_offset(d, gpsTable[j+1].when, SAME_GROUP)) {
|
|
|
|
if (verbose)
|
|
|
|
qDebug() << "look at the next gps fix @" << get_dive_date_string(gpsTable[j+1].when);
|
|
|
|
/* first let's test if this one is during the dive */
|
|
|
|
if (time_during_dive_with_offset(d, gpsTable[j+1].when, 0)) {
|
|
|
|
if (verbose)
|
|
|
|
qDebug() << "which is during the dive, pick that one";
|
2016-01-09 22:07:58 +00:00
|
|
|
copy_gps_location(gpsTable[j+1], d);
|
2015-11-12 22:57:27 +00:00
|
|
|
changed = true;
|
|
|
|
last = j + 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
/* we know the gps fixes are sorted; if they are both before the dive, ignore the first,
|
|
|
|
* if theay are both after the dive, take the first,
|
|
|
|
* if the first is before and the second is after, take the closer one */
|
|
|
|
if (gpsTable[j+1].when < d->when) {
|
|
|
|
if (verbose)
|
|
|
|
qDebug() << "which is closer to the start of the dive, do continue with that";
|
|
|
|
continue;
|
|
|
|
} else if (gpsTable[j].when > d->when + d->duration.seconds) {
|
|
|
|
if (verbose)
|
|
|
|
qDebug() << "which is even later after the end of the dive, so pick the previous one";
|
2016-01-09 22:07:58 +00:00
|
|
|
copy_gps_location(gpsTable[j], d);
|
2015-11-12 22:57:27 +00:00
|
|
|
changed = true;
|
|
|
|
last = j;
|
|
|
|
break;
|
|
|
|
} else {
|
2015-11-14 01:21:43 +00:00
|
|
|
/* ok, gpsFix is before, nextgpsFix is after */
|
2015-11-12 22:57:27 +00:00
|
|
|
if (d->when - gpsTable[j].when <= gpsTable[j+1].when - (d->when + d->duration.seconds)) {
|
|
|
|
if (verbose)
|
|
|
|
qDebug() << "pick the one before as it's closer to the start";
|
2016-01-09 22:07:58 +00:00
|
|
|
copy_gps_location(gpsTable[j], d);
|
2015-11-12 22:57:27 +00:00
|
|
|
changed = true;
|
|
|
|
last = j;
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
if (verbose)
|
|
|
|
qDebug() << "pick the one after as it's closer to the start";
|
2016-01-09 22:07:58 +00:00
|
|
|
copy_gps_location(gpsTable[j+1], d);
|
2015-11-12 22:57:27 +00:00
|
|
|
changed = true;
|
|
|
|
last = j + 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* If no more positions in range, the actual is the one. Asign, mark and end loop.
|
|
|
|
*/
|
|
|
|
} else {
|
|
|
|
if (verbose)
|
|
|
|
qDebug() << "which seems to be the best one for this dive, so pick it";
|
2016-01-09 22:07:58 +00:00
|
|
|
copy_gps_location(gpsTable[j], d);
|
2015-11-12 22:57:27 +00:00
|
|
|
changed = true;
|
|
|
|
last = j;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* If position is out of SAME_GROUP range and in the future, mark position for
|
|
|
|
* next dive iteration and end the gps_location loop
|
|
|
|
*/
|
|
|
|
if (gpsTable[j].when >= d->when + d->duration.seconds + SAME_GROUP) {
|
|
|
|
last = j;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
2015-11-19 03:19:56 +00:00
|
|
|
if (changed)
|
|
|
|
mark_divelist_changed(true);
|
2016-01-11 03:34:21 +00:00
|
|
|
return changed;
|
2015-11-12 22:57:27 +00:00
|
|
|
}
|
2015-11-14 01:20:45 +00:00
|
|
|
|
2016-01-09 22:07:58 +00:00
|
|
|
QMap<qint64, gpsTracker> GpsLocation::currentGPSInfo() const
|
2016-01-09 07:18:41 +00:00
|
|
|
{
|
2016-01-09 22:07:58 +00:00
|
|
|
return m_trackers;
|
2016-01-09 07:18:41 +00:00
|
|
|
}
|
|
|
|
|
2016-01-11 01:22:20 +00:00
|
|
|
void GpsLocation::loadFromStorage()
|
|
|
|
{
|
|
|
|
int nr = geoSettings->value(QString("count")).toInt();
|
|
|
|
for (int i = 0; i < nr; i++) {
|
|
|
|
struct gpsTracker gt;
|
|
|
|
gt.when = geoSettings->value(QString("gpsFix%1_time").arg(i)).toLongLong();
|
|
|
|
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.name = geoSettings->value(QString("gpsFix%1_name").arg(i)).toString();
|
|
|
|
gt.idx = i;
|
|
|
|
m_trackers.insert(gt.when, gt);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void GpsLocation::replaceFixToStorage(gpsTracker >)
|
|
|
|
{
|
|
|
|
if (!m_trackers.keys().contains(gt.when)) {
|
|
|
|
addFixToStorage(gt);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
gpsTracker replacedTracker = m_trackers.value(gt.when);
|
|
|
|
geoSettings->setValue(QString("gpsFix%1_time").arg(replacedTracker.idx), gt.when);
|
|
|
|
geoSettings->setValue(QString("gpsFix%1_lat").arg(replacedTracker.idx), gt.latitude.udeg);
|
|
|
|
geoSettings->setValue(QString("gpsFix%1_lon").arg(replacedTracker.idx), gt.longitude.udeg);
|
|
|
|
geoSettings->setValue(QString("gpsFix%1_name").arg(replacedTracker.idx), gt.name);
|
|
|
|
replacedTracker.latitude = gt.latitude;
|
|
|
|
replacedTracker.longitude = gt.longitude;
|
|
|
|
replacedTracker.name = gt.name;
|
|
|
|
}
|
|
|
|
|
2016-01-09 22:07:58 +00:00
|
|
|
void GpsLocation::addFixToStorage(gpsTracker >)
|
2016-01-08 05:34:21 +00:00
|
|
|
{
|
2016-01-09 22:07:58 +00:00
|
|
|
int nr = m_trackers.count();
|
|
|
|
geoSettings->setValue("count", nr + 1);
|
|
|
|
geoSettings->setValue(QString("gpsFix%1_time").arg(nr), gt.when);
|
|
|
|
geoSettings->setValue(QString("gpsFix%1_lat").arg(nr), gt.latitude.udeg);
|
|
|
|
geoSettings->setValue(QString("gpsFix%1_lon").arg(nr), gt.longitude.udeg);
|
|
|
|
geoSettings->setValue(QString("gpsFix%1_name").arg(nr), gt.name);
|
|
|
|
gt.idx = nr;
|
|
|
|
geoSettings->sync();
|
|
|
|
m_trackers.insert(gt.when, gt);
|
2016-01-08 05:34:21 +00:00
|
|
|
}
|
|
|
|
|
2016-01-09 22:07:58 +00:00
|
|
|
void GpsLocation::deleteFixFromStorage(gpsTracker >)
|
2016-01-09 07:18:41 +00:00
|
|
|
{
|
2016-01-09 22:07:58 +00:00
|
|
|
qint64 when = gt.when;
|
|
|
|
int cnt = m_trackers.count();
|
|
|
|
if (cnt == 0 || !m_trackers.keys().contains(when)) {
|
|
|
|
qDebug() << "no gps fix with timestamp" << when;
|
|
|
|
return;
|
|
|
|
}
|
2016-03-07 00:11:06 +00:00
|
|
|
if (geoSettings->value(QString("gpsFix%1_time").arg(gt.idx)).toLongLong() != when) {
|
2016-01-09 22:07:58 +00:00
|
|
|
qDebug() << "uh oh - index for tracker has gotten out of sync...";
|
|
|
|
return;
|
2016-01-09 07:18:41 +00:00
|
|
|
}
|
2016-01-09 22:07:58 +00:00
|
|
|
if (cnt > 1) {
|
|
|
|
// now we move the last tracker into that spot (so our settings stay consecutive)
|
|
|
|
// and delete the last settings entry
|
|
|
|
when = geoSettings->value(QString("gpsFix%1_time").arg(cnt - 1)).toLongLong();
|
|
|
|
struct gpsTracker movedTracker = m_trackers.value(when);
|
|
|
|
movedTracker.idx = gt.idx;
|
2016-01-11 01:39:13 +00:00
|
|
|
m_trackers.remove(movedTracker.when);
|
|
|
|
m_trackers.insert(movedTracker.when, movedTracker);
|
2016-01-09 22:07:58 +00:00
|
|
|
geoSettings->setValue(QString("gpsFix%1_time").arg(movedTracker.idx), when);
|
|
|
|
geoSettings->setValue(QString("gpsFix%1_lat").arg(movedTracker.idx), movedTracker.latitude.udeg);
|
|
|
|
geoSettings->setValue(QString("gpsFix%1_lon").arg(movedTracker.idx), movedTracker.longitude.udeg);
|
|
|
|
geoSettings->setValue(QString("gpsFix%1_name").arg(movedTracker.idx), movedTracker.name);
|
2016-01-09 07:18:41 +00:00
|
|
|
geoSettings->remove(QString("gpsFix%1_lat").arg(cnt - 1));
|
|
|
|
geoSettings->remove(QString("gpsFix%1_lon").arg(cnt - 1));
|
|
|
|
geoSettings->remove(QString("gpsFix%1_time").arg(cnt - 1));
|
|
|
|
geoSettings->remove(QString("gpsFix%1_name").arg(cnt - 1));
|
|
|
|
}
|
2016-01-09 22:07:58 +00:00
|
|
|
geoSettings->setValue("count", cnt - 1);
|
|
|
|
geoSettings->sync();
|
2016-01-11 01:39:13 +00:00
|
|
|
m_trackers.remove(gt.when);
|
2016-01-09 22:07:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void GpsLocation::deleteGpsFix(qint64 when)
|
|
|
|
{
|
2016-01-11 06:24:30 +00:00
|
|
|
struct gpsTracker defaultTracker;
|
|
|
|
defaultTracker.when = 0;
|
2016-01-09 22:07:58 +00:00
|
|
|
struct gpsTracker deletedTracker = m_trackers.value(when, defaultTracker);
|
|
|
|
if (deletedTracker.when != when) {
|
|
|
|
qDebug() << "can't find tracker for timestamp" << when;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
deleteFixFromStorage(deletedTracker);
|
2016-01-11 01:39:13 +00:00
|
|
|
m_deletedTrackers.append(deletedTracker);
|
2016-01-09 07:18:41 +00:00
|
|
|
}
|
|
|
|
|
2015-11-14 01:20:45 +00:00
|
|
|
void GpsLocation::clearGpsData()
|
|
|
|
{
|
2016-01-09 22:07:58 +00:00
|
|
|
m_trackers.clear();
|
2015-11-14 01:20:45 +00:00
|
|
|
geoSettings->clear();
|
|
|
|
geoSettings->sync();
|
|
|
|
}
|
2015-11-14 01:21:43 +00:00
|
|
|
|
|
|
|
void GpsLocation::postError(QNetworkReply::NetworkError error)
|
|
|
|
{
|
2016-03-07 00:11:06 +00:00
|
|
|
Q_UNUSED(error);
|
2015-11-14 01:21:43 +00:00
|
|
|
status(QString("error when sending a GPS fix: %1").arg(reply->errorString()));
|
|
|
|
}
|
|
|
|
|
2015-11-18 21:14:19 +00:00
|
|
|
void GpsLocation::getUseridError(QNetworkReply::NetworkError error)
|
|
|
|
{
|
2016-03-07 00:11:06 +00:00
|
|
|
Q_UNUSED(error);
|
2015-11-18 21:14:19 +00:00
|
|
|
status(QString("error when retrieving Subsurface webservice user id: %1").arg(reply->errorString()));
|
|
|
|
}
|
|
|
|
|
2016-01-11 01:39:13 +00:00
|
|
|
void GpsLocation::deleteFixesFromServer()
|
|
|
|
{
|
|
|
|
QEventLoop loop;
|
|
|
|
QTimer timer;
|
|
|
|
timer.setSingleShot(true);
|
|
|
|
|
|
|
|
QNetworkAccessManager *manager = new QNetworkAccessManager(qApp);
|
|
|
|
QUrl url(GPS_FIX_DELETE_URL);
|
|
|
|
QList<qint64> keys = m_trackers.keys();
|
|
|
|
while (!m_deletedTrackers.isEmpty()) {
|
|
|
|
gpsTracker gt = m_deletedTrackers.takeFirst();
|
2016-06-22 20:46:22 +00:00
|
|
|
QDateTime dt = QDateTime::fromTime_t(gt.when, Qt::UTC);
|
2016-01-11 01:39:13 +00:00
|
|
|
QUrlQuery data;
|
|
|
|
data.addQueryItem("login", prefs.userid);
|
|
|
|
data.addQueryItem("dive_date", dt.toString("yyyy-MM-dd"));
|
|
|
|
data.addQueryItem("dive_time", dt.toString("hh:mm"));
|
|
|
|
status(data.toString(QUrl::FullyEncoded).toUtf8());
|
|
|
|
QNetworkRequest request;
|
|
|
|
request.setUrl(url);
|
|
|
|
request.setRawHeader("User-Agent", getUserAgent().toUtf8());
|
|
|
|
request.setRawHeader("Accept", "text/json");
|
|
|
|
request.setRawHeader("Content-type", "application/x-www-form-urlencoded");
|
|
|
|
reply = manager->post(request, data.toString(QUrl::FullyEncoded).toUtf8());
|
|
|
|
connect(&timer, &QTimer::timeout, &loop, &QEventLoop::quit);
|
|
|
|
connect(reply, &QNetworkReply::finished, &loop, &QEventLoop::quit);
|
|
|
|
connect(reply, SIGNAL(error(QNetworkReply::NetworkError)),
|
|
|
|
this, SLOT(postError(QNetworkReply::NetworkError)));
|
|
|
|
timer.start(10000);
|
|
|
|
loop.exec();
|
|
|
|
if (timer.isActive()) {
|
|
|
|
timer.stop();
|
|
|
|
if (reply->error() != QNetworkReply::NoError) {
|
|
|
|
QString response = reply->readAll();
|
|
|
|
status(QString("Server response:") + reply->readAll());
|
|
|
|
}
|
|
|
|
} else {
|
2016-01-11 03:18:28 +00:00
|
|
|
status("Deleting on the server timed out - try again later");
|
|
|
|
m_deletedTrackers.prepend(gt);
|
2016-01-11 01:39:13 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
reply->deleteLater();
|
|
|
|
status(QString("completed deleting gps fix %1 - response: ").arg(gt.idx) + reply->readAll());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-14 01:21:43 +00:00
|
|
|
void GpsLocation::uploadToServer()
|
|
|
|
{
|
|
|
|
// we want to do this one at a time (the server prefers that)
|
|
|
|
QEventLoop loop;
|
|
|
|
QTimer timer;
|
|
|
|
timer.setSingleShot(true);
|
|
|
|
|
|
|
|
QNetworkAccessManager *manager = new QNetworkAccessManager(qApp);
|
2015-11-18 21:14:19 +00:00
|
|
|
QUrl url(GPS_FIX_ADD_URL);
|
2016-03-07 00:22:28 +00:00
|
|
|
Q_FOREACH(qint64 key, m_trackers.keys()) {
|
2016-01-09 22:07:58 +00:00
|
|
|
struct gpsTracker gt = m_trackers.value(key);
|
2016-06-22 20:46:22 +00:00
|
|
|
QDateTime dt = QDateTime::fromTime_t(gt.when, Qt::UTC);
|
2015-11-14 01:21:43 +00:00
|
|
|
QUrlQuery data;
|
|
|
|
data.addQueryItem("login", prefs.userid);
|
|
|
|
data.addQueryItem("dive_date", dt.toString("yyyy-MM-dd"));
|
|
|
|
data.addQueryItem("dive_time", dt.toString("hh:mm"));
|
2016-01-22 14:41:43 +00:00
|
|
|
data.addQueryItem("dive_latitude", QString::number(gt.latitude.udeg / 1000000.0, 'f', 9));
|
|
|
|
data.addQueryItem("dive_longitude", QString::number(gt.longitude.udeg / 1000000.0, 'f', 9));
|
2016-01-09 22:07:58 +00:00
|
|
|
if (gt.name.isEmpty())
|
|
|
|
gt.name = "Auto-created dive";
|
|
|
|
data.addQueryItem("dive_name", gt.name);
|
2015-11-14 01:21:43 +00:00
|
|
|
status(data.toString(QUrl::FullyEncoded).toUtf8());
|
|
|
|
QNetworkRequest request;
|
|
|
|
request.setUrl(url);
|
2015-11-18 22:44:07 +00:00
|
|
|
request.setRawHeader("User-Agent", getUserAgent().toUtf8());
|
2015-11-14 01:21:43 +00:00
|
|
|
request.setRawHeader("Accept", "text/json");
|
|
|
|
request.setRawHeader("Content-type", "application/x-www-form-urlencoded");
|
|
|
|
reply = manager->post(request, data.toString(QUrl::FullyEncoded).toUtf8());
|
|
|
|
connect(&timer, &QTimer::timeout, &loop, &QEventLoop::quit);
|
|
|
|
connect(reply, &QNetworkReply::finished, &loop, &QEventLoop::quit);
|
|
|
|
// somehoe I cannot get this to work with the new connect syntax:
|
|
|
|
// connect(reply, &QNetworkReply::error, this, &GpsLocation::postError);
|
|
|
|
connect(reply, SIGNAL(error(QNetworkReply::NetworkError)),
|
|
|
|
this, SLOT(postError(QNetworkReply::NetworkError)));
|
|
|
|
timer.start(10000);
|
|
|
|
loop.exec();
|
|
|
|
if (timer.isActive()) {
|
|
|
|
timer.stop();
|
|
|
|
if (reply->error() != QNetworkReply::NoError) {
|
|
|
|
QString response = reply->readAll();
|
|
|
|
if (!response.contains("Duplicate entry")) {
|
|
|
|
status(QString("Server response:") + reply->readAll());
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
status("Uploading to server timed out");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
reply->deleteLater();
|
2016-01-09 22:07:58 +00:00
|
|
|
status(QString("completed sending gps fix %1 - response: ").arg(gt.idx) + reply->readAll());
|
2015-11-14 01:21:43 +00:00
|
|
|
}
|
2016-01-11 01:39:13 +00:00
|
|
|
// and now remove the ones that were locally deleted
|
|
|
|
deleteFixesFromServer();
|
2015-11-14 01:21:43 +00:00
|
|
|
}
|
2016-01-08 05:32:24 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
2016-01-09 22:07:58 +00:00
|
|
|
qDebug() << "already have" << m_trackers.count() << "GPS fixes";
|
|
|
|
QJsonArray downloadedFixes = object.value("dives").toArray();
|
|
|
|
qDebug() << downloadedFixes.count() << "GPS fixes downloaded";
|
|
|
|
for (int i = 0; i < downloadedFixes.count(); i++) {
|
|
|
|
QJsonObject fix = downloadedFixes[i].toObject();
|
2016-06-22 20:46:22 +00:00
|
|
|
QDate date = QDate::fromString(fix.value("date").toString(), "yyy-M-d");
|
|
|
|
QTime time = QTime::fromString(fix.value("time").toString(), "hh:m:s");
|
2016-01-08 05:32:24 +00:00
|
|
|
QString name = fix.value("name").toString();
|
|
|
|
QString latitude = fix.value("latitude").toString();
|
|
|
|
QString longitude = fix.value("longitude").toString();
|
2016-06-22 20:46:22 +00:00
|
|
|
QDateTime timestamp;
|
|
|
|
timestamp.setTimeSpec(Qt::UTC);
|
|
|
|
timestamp.setDate(date);
|
|
|
|
timestamp.setTime(time);
|
2016-01-08 05:32:24 +00:00
|
|
|
|
|
|
|
struct gpsTracker gt;
|
2016-06-22 20:46:22 +00:00
|
|
|
gt.when = timestamp.toMSecsSinceEpoch() / 1000;
|
2016-01-08 05:32:24 +00:00
|
|
|
gt.latitude.udeg = latitude.toDouble() * 1000000;
|
|
|
|
gt.longitude.udeg = longitude.toDouble() * 1000000;
|
|
|
|
gt.name = name;
|
2016-01-09 22:07:58 +00:00
|
|
|
// add this GPS fix to the QMap and the settings (remove existing fix at the same timestamp first)
|
2016-01-11 01:22:20 +00:00
|
|
|
if (m_trackers.keys().contains(gt.when)) {
|
|
|
|
qDebug() << "already have a fix at time stamp" << gt.when;
|
|
|
|
replaceFixToStorage(gt);
|
|
|
|
} else {
|
|
|
|
addFixToStorage(gt);
|
|
|
|
}
|
2016-01-08 05:32:24 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
qDebug() << "network error" << reply->error() << reply->errorString() << reply->readAll();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
qDebug() << "download timed out";
|
|
|
|
status("Download from server timed out");
|
|
|
|
}
|
|
|
|
reply->deleteLater();
|
|
|
|
}
|