Align RuleManager api with other apis and add more signals
This commit is contained in:
parent
ab65f1a484
commit
844a9959bd
@ -76,7 +76,7 @@ void RuleManager::init()
|
|||||||
{
|
{
|
||||||
m_fetchingData = true;
|
m_fetchingData = true;
|
||||||
emit fetchingDataChanged();
|
emit fetchingDataChanged();
|
||||||
m_jsonClient->sendCommand("Rules.GetRules", this, "getRulesReply");
|
m_jsonClient->sendCommand("Rules.GetRules", this, "getRulesResponse");
|
||||||
}
|
}
|
||||||
|
|
||||||
bool RuleManager::fetchingData() const
|
bool RuleManager::fetchingData() const
|
||||||
@ -96,44 +96,44 @@ Rule *RuleManager::createNewRule()
|
|||||||
|
|
||||||
int RuleManager::addRule(const QVariantMap params)
|
int RuleManager::addRule(const QVariantMap params)
|
||||||
{
|
{
|
||||||
return m_jsonClient->sendCommand("Rules.AddRule", params, this, "onAddRuleReply");
|
return m_jsonClient->sendCommand("Rules.AddRule", params, this, "addRuleResponse");
|
||||||
}
|
}
|
||||||
|
|
||||||
int RuleManager::addRule(Rule *rule)
|
int RuleManager::addRule(Rule *rule)
|
||||||
{
|
{
|
||||||
QVariantMap params = packRule(rule);
|
QVariantMap params = packRule(rule);
|
||||||
qCDebug(dcRuleManager) << "packed rule:" << qUtf8Printable(QJsonDocument::fromVariant(params).toJson(QJsonDocument::Indented));
|
qCDebug(dcRuleManager) << "packed rule:" << qUtf8Printable(QJsonDocument::fromVariant(params).toJson(QJsonDocument::Indented));
|
||||||
return m_jsonClient->sendCommand("Rules.AddRule", params, this, "onAddRuleReply");
|
return m_jsonClient->sendCommand("Rules.AddRule", params, this, "addRuleResponse");
|
||||||
}
|
}
|
||||||
|
|
||||||
int RuleManager::removeRule(const QUuid &ruleId)
|
int RuleManager::removeRule(const QUuid &ruleId)
|
||||||
{
|
{
|
||||||
QVariantMap params;
|
QVariantMap params;
|
||||||
params.insert("ruleId", ruleId);
|
params.insert("ruleId", ruleId);
|
||||||
return m_jsonClient->sendCommand("Rules.RemoveRule", params, this, "removeRuleReply");
|
return m_jsonClient->sendCommand("Rules.RemoveRule", params, this, "removeRuleResponse");
|
||||||
}
|
}
|
||||||
|
|
||||||
int RuleManager::editRule(Rule *rule)
|
int RuleManager::editRule(Rule *rule)
|
||||||
{
|
{
|
||||||
QVariantMap params = packRule(rule);
|
QVariantMap params = packRule(rule);
|
||||||
qWarning() << "Packed rule:" << qUtf8Printable(QJsonDocument::fromVariant(params).toJson(QJsonDocument::Indented));
|
qCDebug(dcRuleManager) << "Packed rule:" << qUtf8Printable(QJsonDocument::fromVariant(params).toJson(QJsonDocument::Indented));
|
||||||
return m_jsonClient->sendCommand("Rules.EditRule", params, this, "onEditRuleReply");
|
return m_jsonClient->sendCommand("Rules.EditRule", params, this, "editRuleResponse");
|
||||||
}
|
}
|
||||||
|
|
||||||
int RuleManager::executeActions(const QString &ruleId)
|
int RuleManager::executeActions(const QString &ruleId)
|
||||||
{
|
{
|
||||||
QVariantMap params;
|
QVariantMap params;
|
||||||
params.insert("ruleId", ruleId);
|
params.insert("ruleId", ruleId);
|
||||||
return m_jsonClient->sendCommand("Rules.ExecuteActions", params, this, "onExecuteRuleActionsReply");
|
return m_jsonClient->sendCommand("Rules.ExecuteActions", params, this, "executeRuleActionsResponse");
|
||||||
}
|
}
|
||||||
|
|
||||||
void RuleManager::handleRulesNotification(const QVariantMap ¶ms)
|
void RuleManager::handleRulesNotification(const QVariantMap ¶ms)
|
||||||
{
|
{
|
||||||
qDebug() << "Rules notification received:" << qUtf8Printable(QJsonDocument::fromVariant(params).toJson(QJsonDocument::Indented));
|
qCDebug(dcRuleManager) << "Rules notification received:" << qUtf8Printable(QJsonDocument::fromVariant(params).toJson(QJsonDocument::Indented));
|
||||||
if (params.value("notification").toString() == "Rules.RuleAdded") {
|
if (params.value("notification").toString() == "Rules.RuleAdded") {
|
||||||
QVariantMap ruleMap = params.value("params").toMap().value("rule").toMap();
|
QVariantMap ruleMap = params.value("params").toMap().value("rule").toMap();
|
||||||
Rule *rule = parseRule(ruleMap);
|
Rule *rule = parseRule(ruleMap);
|
||||||
qDebug() << "Rule added:" << rule;
|
qCDebug(dcRuleManager) << "Rule added:" << rule;
|
||||||
m_rules->insert(rule);
|
m_rules->insert(rule);
|
||||||
} else if (params.value("notification").toString() == "Rules.RuleRemoved") {
|
} else if (params.value("notification").toString() == "Rules.RuleRemoved") {
|
||||||
QUuid ruleId = params.value("params").toMap().value("ruleId").toUuid();
|
QUuid ruleId = params.value("params").toMap().value("ruleId").toUuid();
|
||||||
@ -143,26 +143,26 @@ void RuleManager::handleRulesNotification(const QVariantMap ¶ms)
|
|||||||
QUuid ruleId = ruleMap.value("id").toUuid();
|
QUuid ruleId = ruleMap.value("id").toUuid();
|
||||||
Rule *rule = m_rules->getRule(ruleId);
|
Rule *rule = m_rules->getRule(ruleId);
|
||||||
if (!rule) {
|
if (!rule) {
|
||||||
qWarning() << "Got a rule update notification for a rule we don't know" << ruleId;
|
qCWarning(dcRuleManager) << "Got a rule update notification for a rule we don't know" << ruleId;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
m_rules->remove(ruleId);
|
m_rules->remove(ruleId);
|
||||||
Rule *newRule = parseRule(ruleMap);
|
Rule *newRule = parseRule(ruleMap);
|
||||||
m_rules->insert(newRule);
|
m_rules->insert(newRule);
|
||||||
qDebug() << "Rule changed:" << newRule;
|
qCDebug(dcRuleManager) << "Rule changed:" << newRule;
|
||||||
} else if (params.value("notification").toString() == "Rules.RuleActiveChanged") {
|
} else if (params.value("notification").toString() == "Rules.RuleActiveChanged") {
|
||||||
Rule *rule = m_rules->getRule(params.value("params").toMap().value("ruleId").toUuid());
|
Rule *rule = m_rules->getRule(params.value("params").toMap().value("ruleId").toUuid());
|
||||||
if (!rule) {
|
if (!rule) {
|
||||||
qWarning() << "Got a rule active notification for a rule we don't know";
|
qCWarning(dcRuleManager) << "Got a rule active notification for a rule we don't know";
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
rule->setActive(params.value("params").toMap().value("active").toBool());
|
rule->setActive(params.value("params").toMap().value("active").toBool());
|
||||||
} else {
|
} else {
|
||||||
qWarning() << "Unhandled rule notification" << params;
|
qCWarning(dcRuleManager) << "Unhandled rule notification" << params;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void RuleManager::getRulesReply(int /*commandId*/, const QVariantMap ¶ms)
|
void RuleManager::getRulesResponse(int /*commandId*/, const QVariantMap ¶ms)
|
||||||
{
|
{
|
||||||
// qDebug() << "Get Rules reply" << params;
|
// qDebug() << "Get Rules reply" << params;
|
||||||
foreach (const QVariant &ruleDescriptionVariant, params.value("ruleDescriptions").toList()) {
|
foreach (const QVariant &ruleDescriptionVariant, params.value("ruleDescriptions").toList()) {
|
||||||
@ -181,19 +181,19 @@ void RuleManager::getRulesReply(int /*commandId*/, const QVariantMap ¶ms)
|
|||||||
|
|
||||||
QVariantMap requestParams;
|
QVariantMap requestParams;
|
||||||
requestParams.insert("ruleId", rule->id());
|
requestParams.insert("ruleId", rule->id());
|
||||||
m_jsonClient->sendCommand("Rules.GetRuleDetails", requestParams, this, "getRuleDetailsReply");
|
m_jsonClient->sendCommand("Rules.GetRuleDetails", requestParams, this, "getRuleDetailsResponse");
|
||||||
}
|
}
|
||||||
m_fetchingData = false;
|
m_fetchingData = false;
|
||||||
emit fetchingDataChanged();
|
emit fetchingDataChanged();
|
||||||
}
|
}
|
||||||
|
|
||||||
void RuleManager::getRuleDetailsReply(int commandId, const QVariantMap ¶ms)
|
void RuleManager::getRuleDetailsResponse(int commandId, const QVariantMap ¶ms)
|
||||||
{
|
{
|
||||||
Q_UNUSED(commandId)
|
Q_UNUSED(commandId)
|
||||||
QVariantMap ruleMap = params.value("rule").toMap();
|
QVariantMap ruleMap = params.value("rule").toMap();
|
||||||
Rule* rule = m_rules->getRule(ruleMap.value("id").toUuid());
|
Rule* rule = m_rules->getRule(ruleMap.value("id").toUuid());
|
||||||
if (!rule) {
|
if (!rule) {
|
||||||
qWarning() << "Got rule details for a rule we don't know";
|
qCWarning(dcRuleManager) << "Got rule details for a rule we don't know";
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
parseEventDescriptors(ruleMap.value("eventDescriptors").toList(), rule);
|
parseEventDescriptors(ruleMap.value("eventDescriptors").toList(), rule);
|
||||||
@ -205,33 +205,40 @@ void RuleManager::getRuleDetailsReply(int commandId, const QVariantMap ¶ms)
|
|||||||
// qDebug() << "Rule JSON:" << qUtf8Printable(QJsonDocument::fromVariant(ruleMap).toJson());
|
// qDebug() << "Rule JSON:" << qUtf8Printable(QJsonDocument::fromVariant(ruleMap).toJson());
|
||||||
}
|
}
|
||||||
|
|
||||||
void RuleManager::onAddRuleReply(int commandId, const QVariantMap ¶ms)
|
void RuleManager::addRuleResponse(int commandId, const QVariantMap ¶ms)
|
||||||
{
|
{
|
||||||
if (params.value("ruleError").toString() != "RuleErrorNoError") {
|
if (params.value("ruleError").toString() != "RuleErrorNoError") {
|
||||||
qWarning() << "Failed to add rule:" << qUtf8Printable(QJsonDocument::fromVariant(params).toJson());
|
qCWarning(dcRuleManager) << "Failed to add rule:" << qUtf8Printable(QJsonDocument::fromVariant(params).toJson());
|
||||||
} else {
|
} else {
|
||||||
qDebug() << "Rule added successfully. Rule ID:" << params.value("ruleId").toString();
|
qCDebug(dcRuleManager) << "Rule added successfully. Rule ID:" << params.value("ruleId").toString();
|
||||||
}
|
}
|
||||||
emit addRuleReply(commandId, params.value("ruleError").toString(), params.value("ruleId").toString());
|
QMetaEnum metaEnum = QMetaEnum::fromType<RuleError>();
|
||||||
|
RuleError ruleError = static_cast<RuleError>(metaEnum.keyToValue(params.value("ruleError").toByteArray()));
|
||||||
|
emit addRuleReply(commandId, ruleError, params.value("ruleId").toUuid());
|
||||||
}
|
}
|
||||||
|
|
||||||
void RuleManager::removeRuleReply(int commandId, const QVariantMap ¶ms)
|
void RuleManager::removeRuleResponse(int commandId, const QVariantMap ¶ms)
|
||||||
{
|
{
|
||||||
qDebug() << "Have remove rule reply" << commandId << params;
|
qCDebug(dcRuleManager) << "Have remove rule reply" << commandId << params;
|
||||||
|
QMetaEnum metaEnum = QMetaEnum::fromType<RuleError>();
|
||||||
|
RuleError ruleError = static_cast<RuleError>(metaEnum.keyToValue(params.value("ruleError").toByteArray()));
|
||||||
|
qCritical() << "DAFUQQQ" << commandId << ruleError;
|
||||||
|
emit removeRuleReply(commandId, ruleError);
|
||||||
}
|
}
|
||||||
|
|
||||||
void RuleManager::onEditRuleReply(int commandId, const QVariantMap ¶ms)
|
void RuleManager::editRuleResponse(int commandId, const QVariantMap ¶ms)
|
||||||
{
|
{
|
||||||
if (params.value("ruleError").toString() != "RuleErrorNoError") {
|
if (params.value("ruleError").toString() != "RuleErrorNoError") {
|
||||||
qDebug() << "Bad rule:" << params.value("ruleError").toString();
|
qCDebug(dcRuleManager) << "Bad rule:" << params.value("ruleError").toString();
|
||||||
}
|
}
|
||||||
emit editRuleReply(commandId, params.value("ruleError").toString());
|
QMetaEnum metaEnum = QMetaEnum::fromType<RuleError>();
|
||||||
|
RuleError ruleError = static_cast<RuleError>(metaEnum.keyToValue(params.value("ruleError").toByteArray()));
|
||||||
|
emit editRuleReply(commandId, ruleError);
|
||||||
}
|
}
|
||||||
|
|
||||||
void RuleManager::onExecuteRuleActionsReply(int commandId, const QVariantMap ¶ms)
|
void RuleManager::executeRuleActionsResponse(int commandId, const QVariantMap ¶ms)
|
||||||
{
|
{
|
||||||
qDebug() << "Execute rule actions reply:" << commandId << params;
|
qCDebug(dcRuleManager) << "Execute rule actions reply:" << commandId << params;
|
||||||
}
|
}
|
||||||
|
|
||||||
Rule *RuleManager::parseRule(const QVariantMap &ruleMap)
|
Rule *RuleManager::parseRule(const QVariantMap &ruleMap)
|
||||||
|
|||||||
@ -53,6 +53,33 @@ class RuleManager : public JsonHandler
|
|||||||
Q_PROPERTY(bool fetchingData READ fetchingData NOTIFY fetchingDataChanged)
|
Q_PROPERTY(bool fetchingData READ fetchingData NOTIFY fetchingDataChanged)
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
|
||||||
|
enum RuleError {
|
||||||
|
RuleErrorNoError,
|
||||||
|
RuleErrorInvalidRuleId,
|
||||||
|
RuleErrorRuleNotFound,
|
||||||
|
RuleErrorThingNotFound,
|
||||||
|
RuleErrorEventTypeNotFound,
|
||||||
|
RuleErrorStateTypeNotFound,
|
||||||
|
RuleErrorActionTypeNotFound,
|
||||||
|
RuleErrorInvalidParameter,
|
||||||
|
RuleErrorInvalidRuleFormat,
|
||||||
|
RuleErrorMissingParameter,
|
||||||
|
RuleErrorInvalidRuleActionParameter,
|
||||||
|
RuleErrorInvalidStateEvaluatorValue,
|
||||||
|
RuleErrorTypesNotMatching,
|
||||||
|
RuleErrorNotExecutable,
|
||||||
|
RuleErrorInvalidTimeDescriptor,
|
||||||
|
RuleErrorInvalidRepeatingOption,
|
||||||
|
RuleErrorInvalidCalendarItem,
|
||||||
|
RuleErrorInvalidTimeEventItem,
|
||||||
|
RuleErrorContainsEventBasesAction,
|
||||||
|
RuleErrorNoExitActions,
|
||||||
|
RuleErrorInterfaceNotFound
|
||||||
|
};
|
||||||
|
Q_ENUM(RuleError)
|
||||||
|
|
||||||
|
|
||||||
explicit RuleManager(JsonRpcClient *jsonClient, QObject *parent = nullptr);
|
explicit RuleManager(JsonRpcClient *jsonClient, QObject *parent = nullptr);
|
||||||
|
|
||||||
QString nameSpace() const override;
|
QString nameSpace() const override;
|
||||||
@ -71,14 +98,20 @@ public:
|
|||||||
Q_INVOKABLE int editRule(Rule *rule);
|
Q_INVOKABLE int editRule(Rule *rule);
|
||||||
Q_INVOKABLE int executeActions(const QString &ruleId);
|
Q_INVOKABLE int executeActions(const QString &ruleId);
|
||||||
|
|
||||||
|
signals:
|
||||||
|
void addRuleReply(int commandId, RuleError ruleError, const QUuid &ruleId);
|
||||||
|
void editRuleReply(int commandId, RuleError ruleError);
|
||||||
|
void removeRuleReply(int commandId, RuleError ruleError);
|
||||||
|
void fetchingDataChanged();
|
||||||
|
|
||||||
private slots:
|
private slots:
|
||||||
void handleRulesNotification(const QVariantMap ¶ms);
|
void handleRulesNotification(const QVariantMap ¶ms);
|
||||||
void getRulesReply(int commandId, const QVariantMap ¶ms);
|
void getRulesResponse(int commandId, const QVariantMap ¶ms);
|
||||||
void getRuleDetailsReply(int commandId, const QVariantMap ¶ms);
|
void getRuleDetailsResponse(int commandId, const QVariantMap ¶ms);
|
||||||
void onAddRuleReply(int commandId, const QVariantMap ¶ms);
|
void addRuleResponse(int commandId, const QVariantMap ¶ms);
|
||||||
void removeRuleReply(int commandId, const QVariantMap ¶ms);
|
void removeRuleResponse(int commandId, const QVariantMap ¶ms);
|
||||||
void onEditRuleReply(int commandId, const QVariantMap ¶ms);
|
void editRuleResponse(int commandId, const QVariantMap ¶ms);
|
||||||
void onExecuteRuleActionsReply(int commandId, const QVariantMap ¶ms);
|
void executeRuleActionsResponse(int commandId, const QVariantMap ¶ms);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Rule *parseRule(const QVariantMap &ruleMap);
|
Rule *parseRule(const QVariantMap &ruleMap);
|
||||||
@ -98,11 +131,6 @@ private:
|
|||||||
QVariantList packRuleActions(RuleActions *ruleActions);
|
QVariantList packRuleActions(RuleActions *ruleActions);
|
||||||
QVariantMap packStateEvaluator(StateEvaluator *stateEvaluator);
|
QVariantMap packStateEvaluator(StateEvaluator *stateEvaluator);
|
||||||
|
|
||||||
signals:
|
|
||||||
void addRuleReply(int commandId, const QString &ruleError, const QString &ruleId);
|
|
||||||
void editRuleReply(int commandId, const QString &ruleError);
|
|
||||||
void fetchingDataChanged();
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
JsonRpcClient *m_jsonClient;
|
JsonRpcClient *m_jsonClient;
|
||||||
Rules* m_rules;
|
Rules* m_rules;
|
||||||
|
|||||||
@ -102,7 +102,7 @@ Page {
|
|||||||
Connections {
|
Connections {
|
||||||
target: engine.ruleManager
|
target: engine.ruleManager
|
||||||
onAddRuleReply: {
|
onAddRuleReply: {
|
||||||
if (ruleError == "RuleErrorNoError") {
|
if (ruleError == RuleManager.RuleErrorNoError) {
|
||||||
// print("should tag rule now:", d.editRulePage.rule.id, d.editRulePage.ruleIcon, d.editRulePage.ruleColor)
|
// print("should tag rule now:", d.editRulePage.rule.id, d.editRulePage.ruleIcon, d.editRulePage.ruleColor)
|
||||||
// engine.tagsManager.tagRule(ruleId, "color", d.editRulePage.ruleColor)
|
// engine.tagsManager.tagRule(ruleId, "color", d.editRulePage.ruleColor)
|
||||||
// engine.tagsManager.tagRule(ruleId, "icon", d.editRulePage.ruleIcon)
|
// engine.tagsManager.tagRule(ruleId, "icon", d.editRulePage.ruleIcon)
|
||||||
@ -115,7 +115,7 @@ Page {
|
|||||||
}
|
}
|
||||||
|
|
||||||
onEditRuleReply: {
|
onEditRuleReply: {
|
||||||
if (ruleError == "RuleErrorNoError") {
|
if (ruleError == RuleManager.RuleErrorNoError) {
|
||||||
// print("should tag rule now:", d.editRulePage.ruleIcon, d.editRulePage.ruleColor)
|
// print("should tag rule now:", d.editRulePage.ruleIcon, d.editRulePage.ruleColor)
|
||||||
engine.tagsManager.tagRule(d.editRulePage.rule.id, "color", d.editRulePage.ruleColor)
|
engine.tagsManager.tagRule(d.editRulePage.rule.id, "color", d.editRulePage.ruleColor)
|
||||||
engine.tagsManager.tagRule(d.editRulePage.rule.id, "icon", d.editRulePage.ruleIcon)
|
engine.tagsManager.tagRule(d.editRulePage.rule.id, "icon", d.editRulePage.ruleIcon)
|
||||||
|
|||||||
@ -111,7 +111,7 @@ ThingPageBase {
|
|||||||
onAddRuleReply: {
|
onAddRuleReply: {
|
||||||
if (commandId == d.pendingRuleCreationId) {
|
if (commandId == d.pendingRuleCreationId) {
|
||||||
d.pendingRuleCreationId = -1
|
d.pendingRuleCreationId = -1
|
||||||
if (ruleError != "RuleErrorNoError") {
|
if (ruleError != RuleManager.RuleErrorNoError) {
|
||||||
var comp = Qt.createComponent("../components/ErrorDialog.qml")
|
var comp = Qt.createComponent("../components/ErrorDialog.qml")
|
||||||
var popup = comp.createObject(app, {errorCode: ruleError})
|
var popup = comp.createObject(app, {errorCode: ruleError})
|
||||||
popup.open();
|
popup.open();
|
||||||
|
|||||||
@ -105,7 +105,7 @@ Page {
|
|||||||
Connections {
|
Connections {
|
||||||
target: engine.ruleManager
|
target: engine.ruleManager
|
||||||
onAddRuleReply: {
|
onAddRuleReply: {
|
||||||
if (ruleError == "RuleErrorNoError") {
|
if (ruleError == RuleManager.RuleErrorNoError) {
|
||||||
pageStack.pop(root);
|
pageStack.pop(root);
|
||||||
} else {
|
} else {
|
||||||
var errorDialog = Qt.createComponent(Qt.resolvedUrl("../components/ErrorDialog.qml"));
|
var errorDialog = Qt.createComponent(Qt.resolvedUrl("../components/ErrorDialog.qml"));
|
||||||
@ -116,7 +116,7 @@ Page {
|
|||||||
}
|
}
|
||||||
|
|
||||||
onEditRuleReply: {
|
onEditRuleReply: {
|
||||||
if (ruleError == "RuleErrorNoError") {
|
if (ruleError == RuleManager.RuleErrorNoError) {
|
||||||
pageStack.pop(root);
|
pageStack.pop(root);
|
||||||
} else {
|
} else {
|
||||||
var errorDialog = Qt.createComponent(Qt.resolvedUrl("../components/ErrorDialog.qml"));
|
var errorDialog = Qt.createComponent(Qt.resolvedUrl("../components/ErrorDialog.qml"));
|
||||||
|
|||||||
Reference in New Issue
Block a user