2015-11-11 18:54:36 +00:00
|
|
|
#include "qt-mobile/gpslocation.h"
|
2015-11-11 19:16:59 +00:00
|
|
|
#include "qt-mobile/qmlmanager.h"
|
2015-11-11 23:19:09 +00:00
|
|
|
#include "pref.h"
|
|
|
|
#include "dive.h"
|
2015-11-12 22:57:27 +00:00
|
|
|
#include "helpers.h"
|
2015-11-11 23:19:09 +00:00
|
|
|
#include <time.h>
|
2015-11-11 18:52:52 +00:00
|
|
|
#include <QDebug>
|
2015-11-11 23:19:09 +00:00
|
|
|
#include <QVariant>
|
2015-11-11 18:50:55 +00:00
|
|
|
|
2015-11-11 18:52:52 +00:00
|
|
|
GpsLocation::GpsLocation(QObject *parent)
|
|
|
|
{
|
2015-11-12 21:04:23 +00:00
|
|
|
// create a QSettings object that's separate from the main application settings
|
|
|
|
QSettings *geoSettings = new QSettings(QSettings::NativeFormat, QSettings::UserScope,
|
|
|
|
QString("org.subsurfacedivelog"), QString("subsurfacelocation"), this);
|
2015-11-11 20:32:54 +00:00
|
|
|
gpsSource = QGeoPositionInfoSource::createDefaultSource(parent);
|
2015-11-11 18:52:52 +00:00
|
|
|
if (gpsSource != 0) {
|
2015-11-11 19:16:59 +00:00
|
|
|
QString msg = QString("have position source %1").arg(gpsSource->sourceName());
|
2015-11-11 18:52:52 +00:00
|
|
|
connect(gpsSource, SIGNAL(positionUpdated(QGeoPositionInfo)), this, SLOT(newPosition(QGeoPositionInfo)));
|
|
|
|
connect(gpsSource, SIGNAL(updateTimeout()), this, SLOT(updateTimeout()));
|
|
|
|
} else {
|
2015-11-11 19:16:59 +00:00
|
|
|
status("don't have GPS source");
|
2015-11-11 18:52:52 +00:00
|
|
|
}
|
|
|
|
}
|
2015-11-11 20:34:56 +00:00
|
|
|
|
|
|
|
void GpsLocation::serviceEnable(bool toggle)
|
|
|
|
{
|
2015-11-11 23:19:09 +00:00
|
|
|
if (!gpsSource) {
|
|
|
|
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();
|
|
|
|
status("Starting Subsurface GPS service");
|
|
|
|
} else {
|
|
|
|
gpsSource->stopUpdates();
|
|
|
|
status("Stopping Subsurface GPS service");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-11 23:44:58 +00:00
|
|
|
#define MINTIME 600
|
|
|
|
#define MINDIST 200
|
|
|
|
|
2015-11-11 18:52:52 +00:00
|
|
|
void GpsLocation::newPosition(QGeoPositionInfo pos)
|
|
|
|
{
|
2015-11-11 23:44:58 +00:00
|
|
|
time_t lastTime;
|
|
|
|
QGeoCoordinate lastCoord;
|
2015-11-11 19:16:59 +00:00
|
|
|
QString msg("received new position %1");
|
|
|
|
status(qPrintable(msg.arg(pos.coordinate().toString())));
|
2015-11-11 23:19:09 +00:00
|
|
|
int nr = geoSettings.value("count", 0).toInt();
|
2015-11-11 23:44:58 +00:00
|
|
|
if (nr) {
|
|
|
|
lastCoord.setLatitude(geoSettings.value(QString("gpsFix%1_lat").arg(nr)).toInt() / 1000000.0);
|
|
|
|
lastCoord.setLongitude(geoSettings.value(QString("gpsFix%1_lon").arg(nr)).toInt() / 1000000.0);
|
|
|
|
time_t lastTime = geoSettings.value(QString("gpsFix%1_time").arg(nr)).toULongLong();
|
|
|
|
}
|
|
|
|
// if we have no record stored or if at least 10 minutes have passed or we moved at least 200m
|
|
|
|
if (!nr || pos.timestamp().toTime_t() > lastTime + MINTIME || lastCoord.distanceTo(pos.coordinate()) > MINDIST) {
|
|
|
|
geoSettings.setValue("count", nr + 1);
|
|
|
|
geoSettings.setValue(QString("gpsFix%1_time").arg(nr), pos.timestamp().toTime_t());
|
|
|
|
geoSettings.setValue(QString("gpsFix%1_lat").arg(nr), rint(pos.coordinate().latitude() * 1000000));
|
|
|
|
geoSettings.setValue(QString("gpsFix%1_lon").arg(nr), rint(pos.coordinate().longitude() * 1000000));
|
|
|
|
geoSettings.sync();
|
|
|
|
}
|
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");
|
|
|
|
}
|
|
|
|
|
|
|
|
void GpsLocation::status(QString msg)
|
|
|
|
{
|
|
|
|
qDebug() << msg;
|
|
|
|
qmlUiShowMessage(qPrintable(msg));
|
2015-11-11 18:52:52 +00:00
|
|
|
}
|
2015-11-12 22:57:27 +00:00
|
|
|
|
|
|
|
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);
|
|
|
|
ds->latitude = gps->latitude;
|
|
|
|
ds->longitude = gps->longitude;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define SAME_GROUP 6 * 3600 /* six hours */
|
|
|
|
bool GpsLocation::applyLocations()
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
bool changed = false;
|
|
|
|
int last = 0;
|
|
|
|
int cnt = geoSettings.value("count", 0).toInt();
|
|
|
|
if (cnt == 0)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// create a table with the GPS information
|
|
|
|
struct gpsTracker *gpsTable = (struct gpsTracker *)calloc(cnt, sizeof(struct gpsTracker));
|
|
|
|
for (int i = 0; i < cnt; i++) {
|
|
|
|
gpsTable[i].latitude.udeg = geoSettings.value(QString("gpsFix%1_lat").arg(i)).toInt();
|
|
|
|
gpsTable[i].longitude.udeg = geoSettings.value(QString("gpsFix%1_lon").arg(i)).toInt();
|
|
|
|
gpsTable[i].when = geoSettings.value(QString("gpsFix%1_time").arg(i)).toULongLong();
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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)
|
|
|
|
qDebug() << "processing gpsfix @" << get_dive_date_string(gpsTable[j].when) <<
|
|
|
|
"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)
|
|
|
|
qDebug() << "gpsfix is during the dive, pick that one";
|
|
|
|
copy_gps_location(gpsTable + j, d);
|
|
|
|
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";
|
|
|
|
copy_gps_location(gpsTable + j + 1, d);
|
|
|
|
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";
|
|
|
|
copy_gps_location(gpsTable + j, d);
|
|
|
|
changed = true;
|
|
|
|
last = j;
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
/* ok, gpsfix is before, nextgpsfix is after */
|
|
|
|
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";
|
|
|
|
copy_gps_location(gpsTable + j, d);
|
|
|
|
changed = true;
|
|
|
|
last = j;
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
if (verbose)
|
|
|
|
qDebug() << "pick the one after as it's closer to the start";
|
|
|
|
copy_gps_location(gpsTable + j + 1, d);
|
|
|
|
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";
|
|
|
|
copy_gps_location(gpsTable + j, d);
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|