mirror of
https://github.com/Lurkki14/tuxclocker.git
synced 2025-02-25 18:55:24 -06:00
qt: add readable proxy
This commit is contained in:
parent
0b7fdd8bc1
commit
38217e1a26
52
src/tuxclocker-qt/data/DynamicReadableProxy.cpp
Normal file
52
src/tuxclocker-qt/data/DynamicReadableProxy.cpp
Normal 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()));
|
||||||
|
});
|
||||||
|
}
|
20
src/tuxclocker-qt/data/DynamicReadableProxy.hpp
Normal file
20
src/tuxclocker-qt/data/DynamicReadableProxy.hpp
Normal 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
|
||||||
|
};
|
Loading…
Reference in New Issue
Block a user