diff --git a/libnymea-core/nymeacore.cpp b/libnymea-core/nymeacore.cpp index 878d5573..a69e15c2 100644 --- a/libnymea-core/nymeacore.cpp +++ b/libnymea-core/nymeacore.cpp @@ -444,7 +444,7 @@ void NymeaCore::executeRuleActions(const QList ruleActions) qCWarning(dcRuleEngine()) << "Not executing rule action"; continue; } - Action action(actionTypeId, thing->id()); + Action action(actionTypeId, thing->id(), Action::TriggeredByRule); action.setParams(params); actions.append(action); } else if (ruleAction.type() == RuleAction::TypeBrowser) { @@ -497,7 +497,7 @@ void NymeaCore::executeRuleActions(const QList ruleActions) continue; } - Action action = Action(actionType.id(), thing->id()); + Action action = Action(actionType.id(), thing->id(), Action::TriggeredByRule); action.setParams(params); actions.append(action); } diff --git a/libnymea-core/ruleengine/ruleaction.cpp b/libnymea-core/ruleengine/ruleaction.cpp index 29b3fc77..168ff4ca 100644 --- a/libnymea-core/ruleengine/ruleaction.cpp +++ b/libnymea-core/ruleengine/ruleaction.cpp @@ -108,7 +108,7 @@ bool RuleAction::isStateBased() const Action RuleAction::toAction() const { - Action action(m_actionTypeId, m_thingId); + Action action(m_actionTypeId, m_thingId, Action::TriggeredByRule); ParamList params; foreach (const RuleActionParam &ruleActionParam, m_ruleActionParams) { params.append(Param(ruleActionParam.paramTypeId(), ruleActionParam.value())); diff --git a/libnymea-core/scriptengine/scriptaction.cpp b/libnymea-core/scriptengine/scriptaction.cpp index 991edb1e..293b0283 100644 --- a/libnymea-core/scriptengine/scriptaction.cpp +++ b/libnymea-core/scriptengine/scriptaction.cpp @@ -111,9 +111,7 @@ void ScriptAction::execute(const QVariantMap ¶ms) qCWarning(dcScriptEngine()) << "Either a valid actionTypeId or actionName is required"; return; } - Action action; - action.setActionTypeId(actionType.id()); - action.setThingId(ThingId(m_thingId)); + Action action(actionType.id(), ThingId(m_thingId), Action::TriggeredByScript); ParamList paramList; foreach (const QString ¶mNameOrId, params.keys()) { ParamType paramType; diff --git a/libnymea-core/scriptengine/scriptstate.cpp b/libnymea-core/scriptengine/scriptstate.cpp index da14e223..12001abe 100644 --- a/libnymea-core/scriptengine/scriptstate.cpp +++ b/libnymea-core/scriptengine/scriptstate.cpp @@ -168,9 +168,7 @@ void ScriptState::setValue(const QVariant &value) return; } - Action action; - action.setThingId(ThingId(m_thingId)); - action.setActionTypeId(ActionTypeId(actionTypeId)); + Action action(ActionTypeId(actionTypeId), ThingId(m_thingId), Action::TriggeredByScript); ParamList params = ParamList() << Param(ParamTypeId(actionTypeId), value); action.setParams(params); diff --git a/libnymea/types/action.cpp b/libnymea/types/action.cpp index 7df1246f..d9c43a2e 100644 --- a/libnymea/types/action.cpp +++ b/libnymea/types/action.cpp @@ -46,9 +46,10 @@ #include "action.h" /*! Construct an Action with the given \a deviceId and \a actionTypeId. */ -Action::Action(const ActionTypeId &actionTypeId, const ThingId &thingId) : +Action::Action(const ActionTypeId &actionTypeId, const ThingId &thingId, TriggeredBy triggeredBy) : m_actionTypeId(actionTypeId), - m_thingId(thingId) + m_thingId(thingId), + m_triggeredBy(triggeredBy) { } @@ -112,6 +113,24 @@ Param Action::param(const ParamTypeId ¶mTypeId) const return Param(ParamTypeId(), QString()); } +/*! Gives an indication of the origin of this action. + Normally a plugin should treat all actions the same. There might be + rare exceptions tho: + + - This might be important to know for some devices, e.g. garage doors which are required to + not close in some circumstances. I.e. if a garage door does not have a light sensor bar, it + must never close automatically by a rule to prevent damage and fulfill legal requirements. + + - Other use cases might be to prioritize a single request by a user over a queue of automated + ones. I.e. if a script animation animates a color light, the plugin might build up a queue of + pending commands. To still react timely to a user interaction (e.g. power off), this + information might be of use to a plugin. +*/ +Action::TriggeredBy Action::triggeredBy() const +{ + return m_triggeredBy; +} + /*! Copy the data to an \l{Action} from an \a other action. */ void Action::operator =(const Action &other) { diff --git a/libnymea/types/action.h b/libnymea/types/action.h index 358d3c87..6e6cef58 100644 --- a/libnymea/types/action.h +++ b/libnymea/types/action.h @@ -46,7 +46,12 @@ class LIBNYMEA_EXPORT Action Q_PROPERTY(ParamList params READ params WRITE setParams USER true) public: - explicit Action(const ActionTypeId &actionTypeId = ActionTypeId(), const ThingId &thingId = ThingId()); + enum TriggeredBy { + TriggeredByUser, + TriggeredByRule, + TriggeredByScript + }; + explicit Action(const ActionTypeId &actionTypeId = ActionTypeId(), const ThingId &thingId = ThingId(), TriggeredBy triggeredBy = TriggeredByUser); Action(const Action &other); bool isValid() const; @@ -60,11 +65,14 @@ public: void setParams(const ParamList ¶ms); Param param(const ParamTypeId ¶mTypeId) const; + TriggeredBy triggeredBy() const; + void operator=(const Action &other); private: ActionTypeId m_actionTypeId; ThingId m_thingId; ParamList m_params; + TriggeredBy m_triggeredBy = TriggeredByUser; }; #endif // ACTION_H