| 1 | // Copyright (C) 2024 The Qt Company Ltd. |
|---|---|
| 2 | // Copyright (C) 2024 Florian Richer <florian.richer@protonmail.com> |
| 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 "iiosensorproxyproximitysensor.h" |
| 6 | #include "sensorproxy_interface.h" |
| 7 | |
| 8 | #include <QtDBus/QDBusPendingReply> |
| 9 | |
| 10 | using namespace QtSensorsPrivate; |
| 11 | |
| 12 | char const * const IIOSensorProxyProximitySensor::id("iio-sensor-proxy.proximitysensor"); |
| 13 | |
| 14 | static inline QString dbusPath() { return QStringLiteral("/net/hadess/SensorProxy"); } |
| 15 | |
| 16 | IIOSensorProxyProximitySensor::IIOSensorProxyProximitySensor(QSensor *sensor) |
| 17 | : IIOSensorProxySensorBase(dbusPath(), NetHadessSensorProxyInterface::staticInterfaceName(), sensor) |
| 18 | { |
| 19 | setReading<QProximityReading>(&m_reading); |
| 20 | m_sensorProxyInterface = new NetHadessSensorProxyInterface(serviceName(), dbusPath(), |
| 21 | QDBusConnection::systemBus(), this); |
| 22 | } |
| 23 | |
| 24 | IIOSensorProxyProximitySensor::~IIOSensorProxyProximitySensor() |
| 25 | { |
| 26 | } |
| 27 | |
| 28 | void IIOSensorProxyProximitySensor::start() |
| 29 | { |
| 30 | if (isServiceRunning()) { |
| 31 | if (m_sensorProxyInterface->hasProximity()) { |
| 32 | QDBusPendingReply<> reply = m_sensorProxyInterface->ClaimProximity(); |
| 33 | reply.waitForFinished(); |
| 34 | if (!reply.isError()) { |
| 35 | updateProximityNear(proximityNear: m_sensorProxyInterface->proximityNear()); |
| 36 | return; |
| 37 | } |
| 38 | } |
| 39 | } |
| 40 | sensorStopped(); |
| 41 | } |
| 42 | |
| 43 | void IIOSensorProxyProximitySensor::stop() |
| 44 | { |
| 45 | if (isServiceRunning()) { |
| 46 | QDBusPendingReply<> reply = m_sensorProxyInterface->ReleaseProximity(); |
| 47 | reply.waitForFinished(); |
| 48 | } |
| 49 | sensorStopped(); |
| 50 | } |
| 51 | |
| 52 | void IIOSensorProxyProximitySensor::updateProperties(const QVariantMap &changedProperties) |
| 53 | { |
| 54 | if (changedProperties.contains(key: "ProximityNear")) { |
| 55 | bool proximityNear = changedProperties.value(key: "ProximityNear").toBool(); |
| 56 | updateProximityNear(proximityNear); |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | void IIOSensorProxyProximitySensor::updateProximityNear(bool proximityNear) |
| 61 | { |
| 62 | m_reading.setClose(proximityNear); |
| 63 | m_reading.setTimestamp(produceTimestamp()); |
| 64 | newReadingAvailable(); |
| 65 | } |
| 66 |
