From 2cdefcb1f20e6f9acde2fe8291c9fe26c62b573e Mon Sep 17 00:00:00 2001 From: Michael Zanetti Date: Thu, 17 Apr 2014 01:42:38 +0200 Subject: [PATCH] add some debug helpers --- libguh/plugin/deviceplugin.cpp | 36 ++++++++++++++++++++++++++++++++++ libguh/plugin/deviceplugin.h | 2 ++ libguh/types/event.cpp | 17 ++++++++++++++++ libguh/types/event.h | 3 +++ libguh/types/state.cpp | 16 +++++++++++++++ libguh/types/state.h | 6 ++++++ server/ruleengine.cpp | 5 ++++- 7 files changed, 84 insertions(+), 1 deletion(-) diff --git a/libguh/plugin/deviceplugin.cpp b/libguh/plugin/deviceplugin.cpp index 39291103..92fea89f 100644 --- a/libguh/plugin/deviceplugin.cpp +++ b/libguh/plugin/deviceplugin.cpp @@ -193,6 +193,42 @@ DeviceManager *DevicePlugin::deviceManager() const return m_deviceManager; } +/*! + Returns a list of all configured devices belonging to this plugin. + */ +QList DevicePlugin::myDevices() const +{ + QList myDeviceClassIds; + foreach (const DeviceClass &deviceClass, supportedDevices()) { + myDeviceClassIds.append(deviceClass.id()); + } + + QList ret; + foreach (Device *device, deviceManager()->configuredDevices()) { + if (myDeviceClassIds.contains(device->deviceClassId())) { + ret.append(device); + } + } + return ret; +} + +/*! + Find a certain device from myDevices() by its params. All parameters must + match or the device will not be found. Be prepared for nullptrs. + */ +Device *DevicePlugin::findDeviceByParams(const QVariantMap ¶ms) const +{ + foreach (Device *device, myDevices()) { + bool matching = true; + foreach (const QString ¶mName, device->params().keys()) { + if (device->params().value(paramName) == params.value(paramName)) { + return device; + } + } + } + return nullptr; +} + /*! Transmits data contained in \a rawData on the Radio433 or Radio868 devices, depending on the hardware requested by this plugin. diff --git a/libguh/plugin/deviceplugin.h b/libguh/plugin/deviceplugin.h index 8d97590c..1890e125 100644 --- a/libguh/plugin/deviceplugin.h +++ b/libguh/plugin/deviceplugin.h @@ -73,6 +73,8 @@ signals: protected: DeviceManager *deviceManager() const; + QList myDevices() const; + Device* findDeviceByParams(const QVariantMap ¶ms) const; void transmitData(QList rawData); diff --git a/libguh/types/event.cpp b/libguh/types/event.cpp index aaf6f815..6a80a4a2 100644 --- a/libguh/types/event.cpp +++ b/libguh/types/event.cpp @@ -75,3 +75,20 @@ bool Event::operator ==(const Event &other) const && m_deviceId == other.deviceId() && m_params == other.params(); } + +QDebug operator<<(QDebug dbg, const Event &event) +{ + dbg.nospace() << "Event(EventTypeId: " << event.eventTypeId().toString() << ", DeviceId" << event.deviceId() << ")"; + + return dbg.space(); +} + +QDebug operator<<(QDebug dbg, const QList &events) +{ + dbg.nospace() << "EventList (count:" << events.count() << ")"; + for (int i = 0; i < events.count(); i++ ) { + dbg.nospace() << " " << i << ": " << events.at(i); + } + + return dbg.space(); +} diff --git a/libguh/types/event.h b/libguh/types/event.h index 7faa172c..ac79a953 100644 --- a/libguh/types/event.h +++ b/libguh/types/event.h @@ -23,6 +23,7 @@ #include #include +#include class Event { @@ -42,5 +43,7 @@ private: DeviceId m_deviceId; QVariantMap m_params; }; +QDebug operator<<(QDebug dbg, const Event &event); +QDebug operator<<(QDebug dbg, const QList &events); #endif // EVENT_H diff --git a/libguh/types/state.cpp b/libguh/types/state.cpp index 5b8feae3..d6eeb764 100644 --- a/libguh/types/state.cpp +++ b/libguh/types/state.cpp @@ -62,3 +62,19 @@ void State::setValue(const QVariant &value) { m_value = value; } + +QDebug operator<<(QDebug dbg, const State &state) +{ + dbg.nospace() << "State(StateTypeId: " << state.stateTypeId().toString() << ", DeviceId:" << state.deviceId() << ", value:" << state.value() << ")"; + return dbg.space(); +} + +QDebug operator<<(QDebug dbg, const QList &states) +{ + dbg.nospace() << "StateList (count:" << states.count() << ")"; + for (int i = 0; i < states.count(); i++ ) { + dbg.nospace() << " " << i << ": " << states.at(i); + } + + return dbg.space(); +} diff --git a/libguh/types/state.h b/libguh/types/state.h index b9317265..853f3299 100644 --- a/libguh/types/state.h +++ b/libguh/types/state.h @@ -22,6 +22,7 @@ #include "typeutils.h" #include +#include class State { @@ -31,6 +32,7 @@ public: StateTypeId stateTypeId() const; DeviceId deviceId() const; + QStringList stateNames() const; QVariant value() const; void setValue(const QVariant &value); @@ -40,4 +42,8 @@ private: QVariant m_value; }; +QDebug operator<<(QDebug dbg, const State &event); +QDebug operator<<(QDebug dbg, const QList &events); + + #endif // STATE_H diff --git a/server/ruleengine.cpp b/server/ruleengine.cpp index 81ea4e56..60684302 100644 --- a/server/ruleengine.cpp +++ b/server/ruleengine.cpp @@ -119,11 +119,13 @@ RuleEngine::RuleEngine(QObject *parent) : list of all \l{Action}{Actions} that should be executed. */ QList RuleEngine::evaluateEvent(const Event &event) { + qDebug() << "got event:" << event; QList actions; for (int i = 0; i < m_rules.count(); ++i) { + qDebug() << "evaluating rule" << i << m_rules.at(i).events(); if (m_rules.at(i).events().contains(event)) { bool statesMatching = true; - qDebug() << "checking states"; + qDebug() << "checking states:" << m_rules.at(i).states(); foreach (const State &state, m_rules.at(i).states()) { Device *device = GuhCore::instance()->deviceManager()->findConfiguredDevice(state.deviceId()); if (!device) { @@ -131,6 +133,7 @@ QList RuleEngine::evaluateEvent(const Event &event) break; } if (state.value() != device->stateValue(state.stateTypeId())) { + qDebug() << "State value not matching:" << state.value() << device->stateValue(state.stateTypeId()); statesMatching = false; break; }