2015-11-11 10:54:36 -08:00
|
|
|
#include "qt-mobile/gpslocation.h"
|
2015-11-11 11:16:59 -08:00
|
|
|
#include "qt-mobile/qmlmanager.h"
|
2015-11-11 15:19:09 -08:00
|
|
|
#include "pref.h"
|
|
|
|
#include "dive.h"
|
|
|
|
#include <time.h>
|
2015-11-11 10:52:52 -08:00
|
|
|
#include <QDebug>
|
2015-11-11 15:19:09 -08:00
|
|
|
#include <QVariant>
|
2015-11-11 10:50:55 -08:00
|
|
|
|
2015-11-11 10:52:52 -08:00
|
|
|
GpsLocation::GpsLocation(QObject *parent)
|
|
|
|
{
|
2015-11-11 15:19:09 -08:00
|
|
|
QSettings *geoSettings = new QSettings();
|
2015-11-11 12:32:54 -08:00
|
|
|
gpsSource = QGeoPositionInfoSource::createDefaultSource(parent);
|
2015-11-11 10:52:52 -08:00
|
|
|
if (gpsSource != 0) {
|
2015-11-11 11:16:59 -08:00
|
|
|
QString msg = QString("have position source %1").arg(gpsSource->sourceName());
|
2015-11-11 10:52:52 -08:00
|
|
|
connect(gpsSource, SIGNAL(positionUpdated(QGeoPositionInfo)), this, SLOT(newPosition(QGeoPositionInfo)));
|
|
|
|
connect(gpsSource, SIGNAL(updateTimeout()), this, SLOT(updateTimeout()));
|
|
|
|
} else {
|
2015-11-11 11:16:59 -08:00
|
|
|
status("don't have GPS source");
|
2015-11-11 10:52:52 -08:00
|
|
|
}
|
|
|
|
}
|
2015-11-11 12:34:56 -08:00
|
|
|
|
|
|
|
void GpsLocation::serviceEnable(bool toggle)
|
|
|
|
{
|
2015-11-11 15:19:09 -08:00
|
|
|
if (!gpsSource) {
|
|
|
|
if (toggle)
|
|
|
|
status("Can't start location service, no location source available");
|
2015-11-11 12:34:56 -08:00
|
|
|
return;
|
2015-11-11 15:19:09 -08:00
|
|
|
}
|
2015-11-11 12:34:56 -08:00
|
|
|
if (toggle) {
|
|
|
|
gpsSource->startUpdates();
|
|
|
|
status("Starting Subsurface GPS service");
|
|
|
|
} else {
|
|
|
|
gpsSource->stopUpdates();
|
|
|
|
status("Stopping Subsurface GPS service");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-11 15:44:58 -08:00
|
|
|
#define MINTIME 600
|
|
|
|
#define MINDIST 200
|
|
|
|
|
2015-11-11 10:52:52 -08:00
|
|
|
void GpsLocation::newPosition(QGeoPositionInfo pos)
|
|
|
|
{
|
2015-11-11 15:44:58 -08:00
|
|
|
time_t lastTime;
|
|
|
|
QGeoCoordinate lastCoord;
|
2015-11-11 11:16:59 -08:00
|
|
|
QString msg("received new position %1");
|
|
|
|
status(qPrintable(msg.arg(pos.coordinate().toString())));
|
2015-11-11 15:19:09 -08:00
|
|
|
geoSettings.beginGroup("locations");
|
|
|
|
int nr = geoSettings.value("count", 0).toInt();
|
2015-11-11 15:44:58 -08: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 10:50:55 -08:00
|
|
|
}
|
|
|
|
|
2015-11-11 10:52:52 -08:00
|
|
|
void GpsLocation::updateTimeout()
|
|
|
|
{
|
2015-11-11 11:16:59 -08:00
|
|
|
status("request to get new position timed out");
|
|
|
|
}
|
|
|
|
|
|
|
|
void GpsLocation::status(QString msg)
|
|
|
|
{
|
|
|
|
qDebug() << msg;
|
|
|
|
qmlUiShowMessage(qPrintable(msg));
|
2015-11-11 10:52:52 -08:00
|
|
|
}
|