| 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 | |
| 10 | char const * const IIOSensorProxyCompass::id("iio-sensor-proxy.compass"); |
| 11 | |
| 12 | static inline QString dbusPath() { return QStringLiteral("/net/hadess/SensorProxy/Compass"); } |
| 13 | |
| 14 | IIOSensorProxyCompass::IIOSensorProxyCompass(QSensor *sensor) |
| 15 | : IIOSensorProxySensorBase(dbusPath(), NetHadessSensorProxyCompassInterface::staticInterfaceName(), sensor) |
| 16 | { |
| 17 | setReading<QCompassReading>(&m_reading); |
| 18 | m_sensorProxyInterface = new NetHadessSensorProxyCompassInterface(serviceName(), dbusPath(), |
| 19 | QDBusConnection::systemBus(), this); |
| 20 | } |
| 21 | |
| 22 | IIOSensorProxyCompass::~IIOSensorProxyCompass() |
| 23 | { |
| 24 | } |
| 25 | |
| 26 | void IIOSensorProxyCompass::start() |
| 27 | { |
| 28 | if (isServiceRunning()) { |
| 29 | if (m_sensorProxyInterface->hasCompass()) { |
| 30 | QDBusPendingReply<> reply = m_sensorProxyInterface->ClaimCompass(); |
| 31 | reply.waitForFinished(); |
| 32 | if (!reply.isError()) { |
| 33 | double azimuth = m_sensorProxyInterface->compassHeading(); |
| 34 | updateAzimuth(azimuth); |
| 35 | return; |
| 36 | } |
| 37 | } |
| 38 | } |
| 39 | sensorStopped(); |
| 40 | } |
| 41 | |
| 42 | void IIOSensorProxyCompass::stop() |
| 43 | { |
| 44 | if (isServiceRunning()) { |
| 45 | QDBusPendingReply<> reply = m_sensorProxyInterface->ReleaseCompass(); |
| 46 | reply.waitForFinished(); |
| 47 | } |
| 48 | sensorStopped(); |
| 49 | } |
| 50 | |
| 51 | void IIOSensorProxyCompass::updateProperties(const QVariantMap &changedProperties) |
| 52 | { |
| 53 | if (changedProperties.contains(key: "CompassHeading")) { |
| 54 | double azimuth = changedProperties.value(key: "CompassHeading").toDouble(); |
| 55 | updateAzimuth(azimuth); |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | void IIOSensorProxyCompass::updateAzimuth(double azimuth) |
| 60 | { |
| 61 | m_reading.setAzimuth(azimuth); |
| 62 | m_reading.setTimestamp(produceTimestamp()); |
| 63 | newReadingAvailable(); |
| 64 | } |
| 65 |
