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 "iiosensorproxylightsensor.h"
6#include "sensorproxy_interface.h"
7
8#include <QtDBus/QDBusPendingReply>
9
10using namespace QtSensorsPrivate;
11
12char const * const IIOSensorProxyLightSensor::id("iio-sensor-proxy.lightsensor");
13
14static inline QString dbusPath() { return QStringLiteral("/net/hadess/SensorProxy"); }
15
16IIOSensorProxyLightSensor::IIOSensorProxyLightSensor(QSensor *sensor)
17 : IIOSensorProxySensorBase(dbusPath(), NetHadessSensorProxyInterface::staticInterfaceName(), sensor)
18{
19 setReading<QLightReading>(&m_reading);
20 m_sensorProxyInterface = new NetHadessSensorProxyInterface(serviceName(), dbusPath(),
21 QDBusConnection::systemBus(), this);
22}
23
24IIOSensorProxyLightSensor::~IIOSensorProxyLightSensor()
25{
26}
27
28void IIOSensorProxyLightSensor::start()
29{
30 if (isServiceRunning()) {
31 if (m_sensorProxyInterface->hasAmbientLight()
32 && m_sensorProxyInterface->lightLevelUnit() == QLatin1String("lux")) {
33 QDBusPendingReply<> reply = m_sensorProxyInterface->ClaimLight();
34 reply.waitForFinished();
35 if (!reply.isError()) {
36 updateLightLevel(lux: m_sensorProxyInterface->lightLevel());
37 return;
38 }
39 }
40 }
41 sensorStopped();
42}
43
44void IIOSensorProxyLightSensor::stop()
45{
46 if (isServiceRunning()) {
47 QDBusPendingReply<> reply = m_sensorProxyInterface->ReleaseLight();
48 reply.waitForFinished();
49 }
50 sensorStopped();
51}
52
53void IIOSensorProxyLightSensor::updateProperties(const QVariantMap &changedProperties)
54{
55 if (changedProperties.contains(key: "LightLevel")) {
56 double lux = changedProperties.value(key: "LightLevel").toDouble();
57 updateLightLevel(lux);
58 }
59}
60
61void IIOSensorProxyLightSensor::updateLightLevel(double lux)
62{
63 m_reading.setLux(lux);
64 m_reading.setTimestamp(produceTimestamp());
65 newReadingAvailable();
66}
67

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