qt: add ReadableMasterObsevable

This commit is contained in:
Jussi Kuokkanen 2019-11-17 15:11:41 +02:00
parent 05b437e454
commit ed84e8bde4
2 changed files with 58 additions and 0 deletions

View File

@ -0,0 +1,32 @@
#include "ReadableMasterObservable.h"
ReadableMasterObservable::ReadableMasterObservable(ReadableData *data, QObject *parent) : QObject(parent) {
m_lowestInterval = std::chrono::milliseconds(-1);
m_readableData = data;
m_emitTimer = new QTimer;
}
void ReadableMasterObservable::notifyChangedInterval(std::chrono::milliseconds interval) {
// Interval hasn't been set yet or new one is in the range 0 < new < old
if ((m_lowestInterval < std::chrono::milliseconds(0) || interval < m_lowestInterval) && interval > std::chrono::milliseconds(0)) {
m_lowestInterval = interval;
}
m_emitTimer->start(m_lowestInterval);
connect(m_emitTimer, &QTimer::timeout, [=]() {
emit valueUpdated(m_readableData->value());
});
}
/*ReadableObservable *ReadableMasterObservable::observable(ReadableData *data, std::chrono::milliseconds updateInterval) {
QTimer *timer = new QTimer;
ReadableObservable *masterObservable = new ReadableObservable(data, this); // Create master observable for this node
connect(timer, &QTimer::timeout, [=]() {
});
}*/

View File

@ -0,0 +1,26 @@
#pragma once
#include <QObject>
#include <QHash>
#include <QTimer>
#include "ReadableManager.h"
#include "ReadableData.h"
#include "ReadableObservable.h"
// Class that updates readable values from nodes and emits signals for observers. This prevents CPU overhead when the same value is being observed in multiple places.
class ReadableMasterObservable : public QObject {
public:
ReadableMasterObservable(ReadableData *data, QObject *parent = nullptr);
void notifyChangedInterval(std::chrono::milliseconds interval); // Called when an observable changes its emission interval
//ReadableObservable *observable(ReadableData *data, std::chrono::milliseconds updateInterval);
signals:
void valueUpdated(tc_readable_result_t value);
private:
Q_OBJECT
ReadableData *m_readableData; // Node to update value from
std::chrono::milliseconds m_lowestInterval; // The lowest interval that an observer wants to update its value
QTimer *m_emitTimer; // Updates the value
};