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

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