added back trigger
This commit is contained in:
parent
49bfa0775a
commit
742499d0fa
@ -63,6 +63,16 @@ DeviceManager::DeviceError DeviceManager::addConfiguredDevice(const QUuid &devic
|
||||
return DeviceErrorNoError;
|
||||
}
|
||||
|
||||
Device *DeviceManager::findConfiguredDevice(const QUuid &id) const
|
||||
{
|
||||
foreach (Device *device, m_configuredDevices) {
|
||||
if (device->id() == id) {
|
||||
return device;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
QList<Device *> DeviceManager::configuredDevices() const
|
||||
{
|
||||
return m_configuredDevices;
|
||||
|
||||
@ -35,6 +35,7 @@ public:
|
||||
QList<Device*> configuredDevices() const;
|
||||
DeviceError addConfiguredDevice(const QUuid &deviceClassId, const QVariantMap ¶ms);
|
||||
|
||||
Device* findConfiguredDevice(const QUuid &id) const;
|
||||
QList<Device*> findConfiguredDevices(const QUuid &deviceClassId);
|
||||
DeviceClass findDeviceClass(const QUuid &deviceClassId);
|
||||
|
||||
|
||||
@ -1,7 +1,8 @@
|
||||
#include "trigger.h"
|
||||
|
||||
Trigger::Trigger(const QUuid &triggerTypeId, const QVariantMap ¶ms):
|
||||
Trigger::Trigger(const QUuid &triggerTypeId, const QUuid &deviceId, const QVariantMap ¶ms):
|
||||
m_triggerTypeId(triggerTypeId),
|
||||
m_deviceId(deviceId),
|
||||
m_params(params)
|
||||
{
|
||||
}
|
||||
@ -11,6 +12,11 @@ QUuid Trigger::triggerTypeId() const
|
||||
return m_triggerTypeId;
|
||||
}
|
||||
|
||||
QUuid Trigger::deviceId() const
|
||||
{
|
||||
return m_deviceId;
|
||||
}
|
||||
|
||||
QVariantMap Trigger::params() const
|
||||
{
|
||||
return m_params;
|
||||
@ -20,3 +26,10 @@ void Trigger::setParams(const QVariantMap ¶ms)
|
||||
{
|
||||
m_params = params;
|
||||
}
|
||||
|
||||
bool Trigger::operator ==(const Trigger &other) const
|
||||
{
|
||||
return m_triggerTypeId == other.triggerTypeId()
|
||||
&& m_deviceId == other.deviceId()
|
||||
&& m_params == other.params();
|
||||
}
|
||||
|
||||
@ -8,15 +8,19 @@
|
||||
class Trigger
|
||||
{
|
||||
public:
|
||||
Trigger(const QUuid &triggerTypeId, const QVariantMap ¶ms);
|
||||
Trigger(const QUuid &triggerTypeId, const QUuid &deviceId, const QVariantMap ¶ms);
|
||||
|
||||
QUuid triggerTypeId() const;
|
||||
QUuid deviceId() const;
|
||||
|
||||
QVariantMap params() const;
|
||||
void setParams(const QVariantMap ¶ms);
|
||||
|
||||
bool operator ==(const Trigger &other) const;
|
||||
|
||||
private:
|
||||
QUuid m_triggerTypeId;
|
||||
QUuid m_deviceId;
|
||||
QVariantMap m_params;
|
||||
};
|
||||
|
||||
|
||||
@ -359,7 +359,7 @@ void DevicePluginElro::receiveData(QList<int> rawData)
|
||||
DeviceClass deviceClass = supportedDevices().first();
|
||||
foreach (const TriggerType &triggerType, deviceClass.triggers()) {
|
||||
if (triggerType.name() == button) {
|
||||
Trigger trigger = Trigger(triggerType.id(), params);
|
||||
Trigger trigger = Trigger(triggerType.id(), device->id(), params);
|
||||
emit emitTrigger(trigger);
|
||||
return;
|
||||
}
|
||||
|
||||
@ -410,7 +410,7 @@ void DevicePluginIntertechno::receiveData(QList<int> rawData)
|
||||
foreach (const TriggerType &triggerType, deviceClass.triggers()) {
|
||||
if (triggerType.name() == buttonCode) {
|
||||
//qDebug() << "emit trigger " << triggerType.name();
|
||||
Trigger trigger = Trigger(triggerType.id(), params);
|
||||
Trigger trigger = Trigger(triggerType.id(), device->id(), params);
|
||||
emit emitTrigger(trigger);
|
||||
return;
|
||||
}
|
||||
|
||||
@ -63,7 +63,7 @@ void JsonRPCServer::processData(int clientId, const QByteArray &jsonData)
|
||||
sendResponse(clientId, commandId, params);
|
||||
} else if (method == "AddConfiguredDevice") {
|
||||
QUuid deviceClass = params.value("deviceClass").toUuid();
|
||||
QVariantMap deviceParams = params.value("deviceParams").toMap();
|
||||
QVariantMap deviceParams = params.value("params").toMap();
|
||||
DeviceManager::DeviceError status = HiveCore::instance()->deviceManager()->addConfiguredDevice(deviceClass, deviceParams);
|
||||
switch(status) {
|
||||
case DeviceManager::DeviceErrorNoError:
|
||||
@ -103,7 +103,7 @@ void JsonRPCServer::handleRulesMessage(int clientId, int commandId, const QStrin
|
||||
foreach (const Rule &rule, HiveCore::instance()->ruleEngine()->rules()) {
|
||||
QVariantMap ruleMap;
|
||||
ruleMap.insert("id", rule.id());
|
||||
ruleMap.insert("triggerId", rule.triggerTypeId());
|
||||
ruleMap.insert("trigger", packTrigger(rule.trigger()));
|
||||
ruleMap.insert("action", packAction(rule.action()));
|
||||
rulesList.append(ruleMap);
|
||||
}
|
||||
@ -111,20 +111,26 @@ void JsonRPCServer::handleRulesMessage(int clientId, int commandId, const QStrin
|
||||
rspParams.insert("rules", rulesList);
|
||||
sendResponse(clientId, commandId, rspParams);
|
||||
} else if (method == "AddRule") {
|
||||
QUuid triggerTypeId = params.value("triggerTypeId").toUuid();
|
||||
QVariantMap triggerMap = params.value("trigger").toMap();
|
||||
|
||||
QUuid triggerTypeId = triggerMap.value("triggerTypeId").toUuid();
|
||||
QUuid triggerDeviceId = triggerMap.value("deviceId").toUuid();
|
||||
QVariantMap triggerParams = triggerMap.value("params").toMap();
|
||||
Trigger trigger(triggerTypeId, triggerDeviceId, triggerParams);
|
||||
|
||||
Action action(params.value("deviceId").toString());
|
||||
action.setName(params.value("name").toString());
|
||||
action.setParams(params.value("actionParams").toList());
|
||||
action.setParams(params.value("params").toList());
|
||||
|
||||
switch(HiveCore::instance()->ruleEngine()->addRule(triggerTypeId, action)) {
|
||||
switch(HiveCore::instance()->ruleEngine()->addRule(trigger, action)) {
|
||||
case RuleEngine::RuleErrorNoError:
|
||||
sendResponse(clientId, commandId);
|
||||
break;
|
||||
case RuleEngine::RuleErrorNoSuchTrigger:
|
||||
sendErrorResponse(clientId, commandId, "No such trigger");
|
||||
case RuleEngine::RuleErrorDeviceNotFound:
|
||||
sendErrorResponse(clientId, commandId, "No such device.");
|
||||
break;
|
||||
case RuleEngine::RuleErrorNoSuchAction:
|
||||
sendErrorResponse(clientId, commandId, "No such action");
|
||||
case RuleEngine::RuleErrorTriggerTypeNotFound:
|
||||
sendErrorResponse(clientId, commandId, "Device does not have such a trigger type.");
|
||||
break;
|
||||
}
|
||||
}
|
||||
@ -184,6 +190,15 @@ QVariantMap JsonRPCServer::packDevice(Device *device)
|
||||
return variant;
|
||||
}
|
||||
|
||||
QVariantMap JsonRPCServer::packTrigger(const Trigger &trigger)
|
||||
{
|
||||
QVariantMap variant;
|
||||
variant.insert("id", trigger.triggerTypeId());
|
||||
variant.insert("deviceId", trigger.deviceId());
|
||||
variant.insert("params", trigger.params());
|
||||
return variant;
|
||||
}
|
||||
|
||||
QVariantMap JsonRPCServer::packAction(const Action &action)
|
||||
{
|
||||
QVariantMap variant;
|
||||
|
||||
@ -3,6 +3,7 @@
|
||||
|
||||
#include "deviceclass.h"
|
||||
#include "action.h"
|
||||
#include "trigger.h"
|
||||
|
||||
#include <QObject>
|
||||
#include <QVariantMap>
|
||||
@ -29,6 +30,7 @@ private:
|
||||
|
||||
QVariantMap packDeviceClass(const DeviceClass &deviceClass);
|
||||
QVariantMap packDevice(Device *device);
|
||||
QVariantMap packTrigger(const Trigger &action);
|
||||
QVariantMap packAction(const Action &action);
|
||||
|
||||
void sendResponse(int clientId, int commandId, const QVariantMap ¶ms = QVariantMap());
|
||||
|
||||
@ -1,8 +1,8 @@
|
||||
#include "rule.h"
|
||||
|
||||
Rule::Rule(const QUuid &id, const QUuid &triggerTypeId, const Action &action):
|
||||
Rule::Rule(const QUuid &id, const Trigger &trigger, const Action &action):
|
||||
m_id(id),
|
||||
m_triggerTypeId(triggerTypeId),
|
||||
m_trigger(trigger),
|
||||
m_action(action)
|
||||
{
|
||||
}
|
||||
@ -12,9 +12,9 @@ QUuid Rule::id() const
|
||||
return m_id;
|
||||
}
|
||||
|
||||
QUuid Rule::triggerTypeId() const
|
||||
Trigger Rule::trigger() const
|
||||
{
|
||||
return m_triggerTypeId;
|
||||
return m_trigger;
|
||||
}
|
||||
|
||||
Action Rule::action() const
|
||||
|
||||
@ -2,21 +2,22 @@
|
||||
#define RULE_H
|
||||
|
||||
#include "action.h"
|
||||
#include "trigger.h"
|
||||
|
||||
#include <QUuid>
|
||||
|
||||
class Rule
|
||||
{
|
||||
public:
|
||||
Rule(const QUuid &id, const QUuid &triggerTypeId, const Action &action);
|
||||
Rule(const QUuid &id, const Trigger &trigger, const Action &action);
|
||||
|
||||
QUuid id() const;
|
||||
QUuid triggerTypeId() const;
|
||||
Trigger trigger() const;
|
||||
Action action() const;
|
||||
|
||||
private:
|
||||
QUuid m_id;
|
||||
QUuid m_triggerTypeId;
|
||||
Trigger m_trigger;
|
||||
Action m_action;
|
||||
};
|
||||
|
||||
|
||||
@ -2,6 +2,7 @@
|
||||
|
||||
#include "hivecore.h"
|
||||
#include "devicemanager.h"
|
||||
#include "device.h"
|
||||
|
||||
#include <QSettings>
|
||||
#include <QDebug>
|
||||
@ -19,6 +20,11 @@ RuleEngine::RuleEngine(QObject *parent) :
|
||||
qDebug() << "found rule" << idString;
|
||||
|
||||
settings.beginGroup(idString);
|
||||
|
||||
settings.beginGroup("trigger");
|
||||
Trigger trigger(settings.value("triggerTypeId").toUuid(), settings.value("deviceId").toUuid(), settings.value("params").toMap());
|
||||
settings.endGroup();
|
||||
|
||||
settings.beginGroup("action");
|
||||
Action action = Action(settings.value("deviceId").toUuid(), settings.value("id").toUuid());
|
||||
action.setName(settings.value("name").toString());
|
||||
@ -26,7 +32,7 @@ RuleEngine::RuleEngine(QObject *parent) :
|
||||
settings.endGroup();
|
||||
settings.endGroup();
|
||||
|
||||
Rule rule = Rule(QUuid(idString), settings.value("triggerTypeId").toUuid(), action);
|
||||
Rule rule = Rule(QUuid(idString), trigger, action);
|
||||
m_rules.append(rule);
|
||||
}
|
||||
|
||||
@ -36,22 +42,44 @@ QList<Action> RuleEngine::evaluateTrigger(const Trigger &trigger)
|
||||
{
|
||||
QList<Action> actions;
|
||||
for (int i = 0; i < m_rules.count(); ++i) {
|
||||
if (m_rules.at(i).triggerTypeId() == trigger.triggerTypeId()) {
|
||||
if (m_rules.at(i).trigger() == trigger) {
|
||||
actions << m_rules.at(i).action();
|
||||
}
|
||||
}
|
||||
return actions;
|
||||
}
|
||||
|
||||
RuleEngine::RuleError RuleEngine::addRule(const QUuid &triggerTypeId, const Action &action)
|
||||
RuleEngine::RuleError RuleEngine::addRule(const Trigger &trigger, const Action &action)
|
||||
{
|
||||
|
||||
Rule rule = Rule(QUuid::createUuid(), triggerTypeId, action);
|
||||
Device *device = HiveCore::instance()->deviceManager()->findConfiguredDevice(trigger.deviceId());
|
||||
if (!device) {
|
||||
qWarning() << "Cannot create rule. No configured device for triggerTypeId" << trigger.triggerTypeId();
|
||||
return RuleErrorDeviceNotFound;
|
||||
}
|
||||
DeviceClass deviceClass = HiveCore::instance()->deviceManager()->findDeviceClass(device->deviceClassId());
|
||||
|
||||
bool triggerTypeFound = false;
|
||||
foreach (const TriggerType &triggerType, deviceClass.triggers()) {
|
||||
if (triggerType.id() == trigger.triggerTypeId()) {
|
||||
triggerTypeFound = true;
|
||||
}
|
||||
}
|
||||
if (!triggerTypeFound) {
|
||||
qWarning() << "Cannot create rule. Device " + device->name() + " has no trigger type:" << trigger.triggerTypeId();
|
||||
return RuleErrorTriggerTypeNotFound;
|
||||
}
|
||||
|
||||
Rule rule = Rule(QUuid::createUuid(), trigger, action);
|
||||
m_rules.append(rule);
|
||||
|
||||
QSettings settings(rulesFileName);
|
||||
settings.beginGroup(rule.id().toString());
|
||||
settings.setValue("triggerTypeId", rule.triggerTypeId());
|
||||
settings.beginGroup("trigger");
|
||||
settings.setValue("triggerTypeId", trigger.triggerTypeId());
|
||||
settings.setValue("deviceId", trigger.deviceId());
|
||||
settings.setValue("params", trigger.params());
|
||||
settings.endGroup();
|
||||
settings.beginGroup("action");
|
||||
settings.setValue("id", rule.action().id());
|
||||
settings.setValue("deviceId", rule.action().deviceId());
|
||||
|
||||
@ -14,15 +14,15 @@ class RuleEngine : public QObject
|
||||
public:
|
||||
enum RuleError {
|
||||
RuleErrorNoError,
|
||||
RuleErrorNoSuchTrigger,
|
||||
RuleErrorNoSuchAction
|
||||
RuleErrorDeviceNotFound,
|
||||
RuleErrorTriggerTypeNotFound
|
||||
};
|
||||
|
||||
explicit RuleEngine(QObject *parent = 0);
|
||||
|
||||
QList<Action> evaluateTrigger(const Trigger &trigger);
|
||||
|
||||
RuleError addRule(const QUuid &triggerTypeId, const Action &action);
|
||||
RuleError addRule(const Trigger &trigger, const Action &action);
|
||||
QList<Rule> rules() const;
|
||||
|
||||
private:
|
||||
|
||||
Reference in New Issue
Block a user