1// Copyright (C) 2016 Alexander Volkov <a.volkov@rusbitech.ru>
2// Copyright (C) 2016 The Qt Company Ltd.
3// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
4
5#include "iiosensorproxycompass.h"
6#include "compass_interface.h"
7
8#include <QtDBus/QDBusPendingReply>
9
10using namespace QtSensorsPrivate;
11
12char const * const IIOSensorProxyCompass::id("iio-sensor-proxy.compass");
13
14static inline QString dbusPath() { return QStringLiteral("/net/hadess/SensorProxy/Compass"); }
15
16IIOSensorProxyCompass::IIOSensorProxyCompass(QSensor *sensor)
17 : IIOSensorProxySensorBase(dbusPath(), NetHadessSensorProxyCompassInterface::staticInterfaceName(), sensor)
18{
19 setReading<QCompassReading>(&m_reading);
20 m_sensorProxyInterface = new NetHadessSensorProxyCompassInterface(serviceName(), dbusPath(),
21 QDBusConnection::systemBus(), this);
22}
23
24IIOSensorProxyCompass::~IIOSensorProxyCompass()
25{
26}
27
28void IIOSensorProxyCompass::start()
29{
30 if (isServiceRunning()) {
31 if (m_sensorProxyInterface->hasCompass()) {
32 QDBusPendingReply<> reply = m_sensorProxyInterface->ClaimCompass();
33 reply.waitForFinished();
34 if (!reply.isError()) {
35 double azimuth = m_sensorProxyInterface->compassHeading();
36 updateAzimuth(azimuth);
37 return;
38 }
39 }
40 }
41 sensorStopped();
42}
43
44void IIOSensorProxyCompass::stop()
45{
46 if (isServiceRunning()) {
47 QDBusPendingReply<> reply = m_sensorProxyInterface->ReleaseCompass();
48 reply.waitForFinished();
49 }
50 sensorStopped();
51}
52
53void IIOSensorProxyCompass::updateProperties(const QVariantMap &changedProperties)
54{
55 if (changedProperties.contains(key: "CompassHeading")) {
56 double azimuth = changedProperties.value(key: "CompassHeading").toDouble();
57 updateAzimuth(azimuth);
58 }
59}
60
61void IIOSensorProxyCompass::updateAzimuth(double azimuth)
62{
63 m_reading.setAzimuth(azimuth);
64 m_reading.setTimestamp(produceTimestamp());
65 newReadingAvailable();
66}
67

source code of qtsensors/src/plugins/sensors/iio-sensor-proxy/iiosensorproxycompass.cpp