Added a TagWatcher component

This commit is contained in:
Michael Zanetti 2021-04-09 23:39:30 +02:00
parent 3f65cffc81
commit dc041ca939
7 changed files with 180 additions and 3 deletions

View File

@ -128,6 +128,7 @@
#include "zigbee/zigbeenetwork.h"
#include "zigbee/zigbeenetworks.h"
#include "applogcontroller.h"
#include "tagwatcher.h"
#include <QtQml/qqml.h>
@ -266,6 +267,7 @@ void registerQmlTypes() {
qmlRegisterType<TagsProxyModel>(uri, 1, 0, "TagsProxyModel");
qmlRegisterType<TagListModel>(uri, 1, 0, "TagListModel");
qmlRegisterType<TagListProxyModel>(uri, 1, 0, "TagListProxyModel");
qmlRegisterType<TagWatcher>(uri, 1, 0, "TagWatcher");
qmlRegisterType<BtWiFiSetup>(uri, 1, 0, "BtWiFiSetup");
qmlRegisterType<BluetoothDiscovery>(uri, 1, 0, "BluetoothDiscovery");

View File

@ -21,6 +21,7 @@ INCLUDEPATH += \
SOURCES += \
$$PWD/models/scriptsproxymodel.cpp \
$$PWD/tagwatcher.cpp \
$${PWD}/logging.cpp \
$${PWD}/applogcontroller.cpp \
$${PWD}/wifisetup/btwifisetup.cpp \
@ -167,6 +168,7 @@ SOURCES += \
HEADERS += \
$$PWD/models/scriptsproxymodel.h \
$$PWD/tagwatcher.h \
$${PWD}/logging.h \
$${PWD}/applogcontroller.h \
$${PWD}/wifisetup/btwifisetup.h \

View File

@ -34,9 +34,6 @@
#include <QJsonDocument>
#include "logging.h"
NYMEA_LOGGING_CATEGORY(dcTags, "Tags")
TagsManager::TagsManager(JsonRpcClient *jsonClient, QObject *parent):
JsonHandler(parent),
m_jsonClient(jsonClient),

117
libnymea-app/tagwatcher.cpp Normal file
View File

@ -0,0 +1,117 @@
#include "tagwatcher.h"
#include "types/tag.h"
TagWatcher::TagWatcher(QObject *parent) : QObject(parent)
{
}
Tags *TagWatcher::tags() const
{
return m_tags;
}
void TagWatcher::setTags(Tags *tags)
{
if (m_tags != tags) {
if (m_tags) {
disconnect(m_tags, &Tags::countChanged, this, &TagWatcher::update);
}
m_tags = tags;
emit tagsChanged();
if (m_tags) {
connect(m_tags, &Tags::countChanged, this, &TagWatcher::update);
}
update();
}
}
QUuid TagWatcher::thingId() const
{
return m_thingId;
}
void TagWatcher::setThingId(const QUuid &thingId)
{
if (m_thingId != thingId) {
m_thingId = thingId;
emit thingIdChanged();
update();
}
}
QUuid TagWatcher::ruleId() const
{
return m_ruleId;
}
void TagWatcher::setRuleId(const QUuid &ruleId)
{
if (m_ruleId != ruleId) {
m_ruleId = ruleId;
emit ruleIdChanged();
update();
}
}
QString TagWatcher::tagId() const
{
return m_tagId;
}
void TagWatcher::setTagId(const QString &tagId)
{
if (m_tagId != tagId) {
m_tagId = tagId;
emit tagIdChanged();
update();
}
}
Tag *TagWatcher::tag() const
{
return m_tag;
}
void TagWatcher::update()
{
qCDebug(dcTags) << "Updating tag for watcher:" << m_tags << m_thingId << m_tagId;
if (!m_tags) {
updateTag(nullptr);
return;
}
if (m_thingId.isNull() && m_ruleId.isNull()) {
updateTag(nullptr);
return;
}
if (m_tagId.isEmpty()) {
updateTag(nullptr);
return;
}
Tag *tag = nullptr;
for (int i = 0; i < m_tags->rowCount(); i++) {
Tag *t = m_tags->get(i);
if (t->tagId() != m_tagId) {
continue;
}
if (t->thingId() != m_thingId && t->ruleId() != m_ruleId) {
continue;
}
tag = t;
break;
}
updateTag(tag);
}
void TagWatcher::updateTag(Tag *tag)
{
if (m_tag != tag) {
m_tag = tag;
emit tagChanged();
}
}

53
libnymea-app/tagwatcher.h Normal file
View File

@ -0,0 +1,53 @@
#ifndef TAGWATCHER_H
#define TAGWATCHER_H
#include <QObject>
#include <QUuid>
#include "types/tags.h"
class TagWatcher : public QObject
{
Q_OBJECT
Q_PROPERTY(Tags* tags READ tags WRITE setTags NOTIFY tagsChanged)
Q_PROPERTY(QUuid thingId READ thingId WRITE setThingId NOTIFY thingIdChanged)
Q_PROPERTY(QUuid ruleId READ ruleId WRITE setRuleId NOTIFY ruleIdChanged)
Q_PROPERTY(QString tagId READ tagId WRITE setTagId NOTIFY tagIdChanged)
Q_PROPERTY(Tag* tag READ tag NOTIFY tagChanged)
public:
explicit TagWatcher(QObject *parent = nullptr);
Tags* tags() const;
void setTags(Tags *tags);
QUuid thingId() const;
void setThingId(const QUuid &thingId);
QUuid ruleId() const;
void setRuleId(const QUuid &ruleId);
QString tagId() const;
void setTagId(const QString &tagId);
Tag* tag() const;
signals:
void tagsChanged();
void tagIdChanged();
void thingIdChanged();
void ruleIdChanged();
void tagChanged();
private slots:
void update();
void updateTag(Tag *tag);
private:
Tags* m_tags = nullptr;
QUuid m_thingId;
QUuid m_ruleId;
QString m_tagId;
Tag *m_tag = nullptr;
};
#endif // TAGWATCHER_H

View File

@ -32,6 +32,9 @@
#include <QDebug>
#include "logging.h"
NYMEA_LOGGING_CATEGORY(dcTags, "Tags")
Tag::Tag(const QString &tagId, const QString &value, QObject *parent):
QObject(parent),
m_tagId(tagId),

View File

@ -33,6 +33,9 @@
#include <QObject>
#include <QUuid>
#include <QLoggingCategory>
Q_DECLARE_LOGGING_CATEGORY(dcTags)
class Tag : public QObject
{