qt: add readable proxy

This commit is contained in:
Jussi Kuokkanen 2020-06-03 23:20:14 +03:00
parent 0b7fdd8bc1
commit 38217e1a26
2 changed files with 72 additions and 0 deletions

View File

@ -0,0 +1,52 @@
#include "DynamicReadableProxy.hpp"
#include <DBusTypes.hpp>
#include <QDBusMetaType>
#include <QDBusReply>
#include <QDebug>
namespace TCD = TuxClocker::DBus;
using namespace TuxClocker::Device;
Q_DECLARE_METATYPE(TCD::Result<QDBusVariant>)
Q_DECLARE_METATYPE(ReadableValue)
Q_DECLARE_METATYPE(ReadError)
Q_DECLARE_METATYPE(ReadResult)
ReadResult toTCResult(TCD::Result<QDBusVariant> res) {
if (res.error)
return static_cast<ReadError>(res.value.variant().toInt());
auto type = static_cast<QMetaType::Type>(res.value.variant().type());
auto v = res.value.variant();
switch (type) {
case QMetaType::Int:
return v.value<int>();
case QMetaType::UInt:
return v.value<uint>();
case QMetaType::Double:
return v.value<double>();
default:
// TODO: indicate unhandled value
return ReadError::UnknownError;
}
}
DynamicReadableProxy::DynamicReadableProxy(QString path, QDBusConnection conn,
QObject *parent) : QObject(parent),
m_iface("org.tuxclocker", path, "org.tuxclocker.DynamicReadable", conn) {
qDBusRegisterMetaType<TCD::Result<QDBusVariant>>();
m_timer.start(1000);
connect(&m_timer, &QTimer::timeout, [=] {
QDBusReply<TCD::Result<QDBusVariant>> reply = m_iface.call("value");
if (!reply.isValid())
emit valueChanged(ReadError::UnknownError);
else
//qDebug() << QVariant::fromStdVariant(toTCResult(reply.value()));
emit valueChanged(toTCResult(reply.value()));
});
}

View File

@ -0,0 +1,20 @@
#pragma once
#include <Device.hpp>
#include <QDBusConnection>
#include <QDBusInterface>
#include <QObject>
#include <QTimer>
class DynamicReadableProxy : public QObject {
public:
DynamicReadableProxy(QString path, QDBusConnection conn,
QObject *parent = nullptr);
signals:
void valueChanged(TuxClocker::Device::ReadResult val);
private:
Q_OBJECT
QDBusInterface m_iface;
QTimer m_timer; // Emits latest value
};