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
|
// Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
#include <QtPositioning/qgeopositioninfosourcefactory.h>
#include <QtPositioning/qgeosatelliteinfosource.h>
#include <QObject>
#include <QtPlugin>
#include <QTimer>
QT_USE_NAMESPACE
using namespace Qt::StringLiterals;
class SatelliteSource : public QGeoSatelliteInfoSource
{
Q_OBJECT
public:
SatelliteSource(const QVariantMap ¶meters, QObject *parent=0);
~SatelliteSource();
void startUpdates() override;
void stopUpdates() override;
void requestUpdate(int timeout = 5000) override;
void setUpdateInterval(int msec) override;
int minimumUpdateInterval() const override;
Error error() const override;
bool setBackendProperty(const QString &name, const QVariant &value) override;
QVariant backendProperty(const QString &name) const override;
private slots:
void doTimeout();
void updateSatelliteInfo();
private:
QTimer *timer;
QTimer *timeoutTimer;
QTimer *singleUpdateTimer;
Error lastError = QGeoSatelliteInfoSource::NoError;
QList<QGeoSatelliteInfo> satellitesInView;
QList<QGeoSatelliteInfo> satellitesInUse;
qsizetype desiredInViewCount = 5;
qsizetype desiredInUseCount = 3;
bool useElevation = false;
bool useAzimuth = false;
QGeoSatelliteInfo::SatelliteSystem desiredSystem = QGeoSatelliteInfo::GPS;
};
SatelliteSource::SatelliteSource(const QVariantMap ¶meters, QObject *parent)
: QGeoSatelliteInfoSource(parent),
timer(new QTimer(this)),
timeoutTimer(new QTimer(this)),
singleUpdateTimer(new QTimer(this))
{
QVariant systemType = parameters.value("satellitesystem"_L1, QVariant());
if (systemType.isValid())
desiredSystem = systemType.value<QGeoSatelliteInfo::SatelliteSystem>();
timer->setInterval(200);
connect(timer, &QTimer::timeout, this, &SatelliteSource::updateSatelliteInfo);
connect(singleUpdateTimer, &QTimer::timeout, this, &SatelliteSource::updateSatelliteInfo);
connect(timeoutTimer, &QTimer::timeout, this, &SatelliteSource::doTimeout);
}
SatelliteSource::~SatelliteSource()
{
}
QGeoSatelliteInfoSource::Error SatelliteSource::error() const
{
return lastError;
}
bool SatelliteSource::setBackendProperty(const QString &name, const QVariant &value)
{
if (name == "desiredInViewCount"_L1)
desiredInViewCount = value.value<qsizetype>();
else if (name == "desiredInUseCount"_L1)
desiredInUseCount = value.value<qsizetype>();
else if (name == "useElevation"_L1)
useElevation = value.toBool();
else if (name == "useAzimuth"_L1)
useAzimuth = value.toBool();
else
return false;
return true;
}
QVariant SatelliteSource::backendProperty(const QString &name) const
{
if (name == "desiredInViewCount"_L1)
return desiredInViewCount;
else if (name == "desiredInUseCount"_L1)
return desiredInUseCount;
else if (name == "useElevation"_L1)
return useElevation;
else if (name == "useAzimuth"_L1)
return useAzimuth;
return {};
}
void SatelliteSource::setUpdateInterval(int msec)
{
const int minInterval = minimumUpdateInterval();
if (msec == 0) {
timer->setInterval(minInterval);
} else if (msec < minInterval) {
msec = minInterval;
timer->setInterval(msec);
} else {
timer->setInterval(msec);
}
QGeoSatelliteInfoSource::setUpdateInterval(msec);
}
int SatelliteSource::minimumUpdateInterval() const
{
return 200;
}
void SatelliteSource::startUpdates()
{
lastError = QGeoSatelliteInfoSource::NoError;
timer->start();
}
void SatelliteSource::stopUpdates()
{
timer->stop();
}
void SatelliteSource::requestUpdate(int timeout)
{
if (timeoutTimer->isActive())
return; // already requested an update
lastError = QGeoSatelliteInfoSource::NoError;
// Such logic for timeout handling is needed for tst_qgeosatelliteinfosource.
// It expects an immediate error in case of negative timeout.
if (timeout == 0)
timeout = 5000;
if (timeout < 0)
timeout = 0;
timeoutTimer->setInterval(timeout);
timeoutTimer->start();
if (timer->isActive()) {
timer->stop();
timer->start();
}
singleUpdateTimer->start(minimumUpdateInterval());
}
void SatelliteSource::doTimeout()
{
timeoutTimer->stop();
singleUpdateTimer->stop();
lastError = QGeoSatelliteInfoSource::UpdateTimeoutError;
emit errorOccurred(lastError);
}
void SatelliteSource::updateSatelliteInfo()
{
timeoutTimer->stop();
singleUpdateTimer->stop();
if (desiredInUseCount > desiredInViewCount)
desiredInUseCount = desiredInViewCount;
satellitesInView.clear();
satellitesInUse.clear();
for (qsizetype i = 0; i < desiredInViewCount; ++i) {
QGeoSatelliteInfo si;
si.setSatelliteSystem(desiredSystem);
si.setSatelliteIdentifier(i + 1);
si.setSignalStrength(5 * (i + 1));
if (useElevation)
si.setAttribute(QGeoSatelliteInfo::Elevation, 3.0 * (i + 1));
if (useAzimuth)
si.setAttribute(QGeoSatelliteInfo::Azimuth, 0.5 * (i + 1));
satellitesInView.push_back(si);
if (i < desiredInUseCount)
satellitesInUse.push_back(si);
}
emit satellitesInViewUpdated(satellitesInView);
emit satellitesInUseUpdated(satellitesInUse);
}
class QGeoPositionInfoSourceFactoryTest : public QObject, public QGeoPositionInfoSourceFactory
{
Q_OBJECT
Q_PLUGIN_METADATA(IID "org.qt-project.qt.position.sourcefactory/6.0"
FILE "plugin.json")
Q_INTERFACES(QGeoPositionInfoSourceFactory)
public:
QGeoPositionInfoSource *positionInfoSource(QObject *parent,
const QVariantMap ¶meters) override;
QGeoSatelliteInfoSource *satelliteInfoSource(QObject *parent,
const QVariantMap ¶meters) override;
QGeoAreaMonitorSource *areaMonitor(QObject *parent, const QVariantMap ¶meters) override;
};
QGeoPositionInfoSource *
QGeoPositionInfoSourceFactoryTest::positionInfoSource(QObject *parent, const QVariantMap ¶meters)
{
Q_UNUSED(parent);
Q_UNUSED(parameters);
return nullptr;
}
QGeoSatelliteInfoSource *
QGeoPositionInfoSourceFactoryTest::satelliteInfoSource(QObject *parent, const QVariantMap ¶meters)
{
return new SatelliteSource(parameters, parent);
}
QGeoAreaMonitorSource *
QGeoPositionInfoSourceFactoryTest::areaMonitor(QObject *parent, const QVariantMap ¶meters)
{
Q_UNUSED(parent);
Q_UNUSED(parameters);
return nullptr;
}
#include "plugin.moc"
|