Skip to content

Commit

Permalink
stateengine plugin: introduce triggeredby condition functionality (eq…
Browse files Browse the repository at this point in the history
…uivalent to updatedby and changedby)
  • Loading branch information
onkelandy committed Aug 4, 2023
1 parent bed16a5 commit 2d83a58
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 4 deletions.
45 changes: 43 additions & 2 deletions stateengine/StateEngineCondition.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,10 @@ def __init__(self, abitem, name: str):
self.__agemax = StateEngineValue.SeValue(self._abitem, "agemax")
self.__changedby = StateEngineValue.SeValue(self._abitem, "changedby", True)
self.__updatedby = StateEngineValue.SeValue(self._abitem, "updatedby", True)
self.__triggeredby = StateEngineValue.SeValue(self._abitem, "triggeredby", True)
self.__changedbynegate = None
self.__updatedbynegate = None
self.__triggeredbynegate = None
self.__agenegate = None
self.__error = None
self.__itemClass = Item
Expand All @@ -65,7 +67,7 @@ def __repr__(self):

# set a certain function to a given value
# func: Function to set ('item', 'eval', 'value', 'min', 'max', 'negate', 'changedby', 'updatedby',
# 'changedbynegate', 'updatedbynegate', 'agemin', 'agemax' or 'agenegate')
# 'triggeredby','changedbynegate', 'updatedbynegate', 'triggeredbynegate','agemin', 'agemax' or 'agenegate')
# value: Value for function
def set(self, func, value):
if func == "se_item":
Expand Down Expand Up @@ -100,10 +102,14 @@ def set(self, func, value):
self.__changedby.set(value, self.__name)
elif func == "se_updatedby":
self.__updatedby.set(value, self.__name)
elif func == "se_triggeredby":
self.__triggeredby.set(value, self.__name)
elif func == "se_changedbynegate":
self.__changedbynegate = value
elif func == "se_updatedbynegate":
self.__updatedbynegate = value
elif func == "se_triggeredbynegate":
self.__triggeredbynegate = value
elif func == "se_negate":
self.__negate = value
elif func == "se_agenegate":
Expand Down Expand Up @@ -131,6 +137,7 @@ def get(self):
'max': str(self.__max), 'agemin': str(self.__agemin), 'agemax': str(self.__agemax),
'negate': str(self.__negate), 'agenegate': str(self.__agenegate),
'changedby': str(self.__changedby), 'updatedby': str(self.__updatedby),
'triggeredby': str(self.__triggeredby), 'triggeredbynegate': str(self.__triggeredbynegate),
'changedbynegate': str(self.__changedbynegate),
'updatedbynegate': str(self.__updatedbynegate)}
return result
Expand All @@ -142,7 +149,7 @@ def complete(self, item_state):
# check if it is possible to complete this condition
if self.__min.is_empty() and self.__max.is_empty() and self.__value.is_empty() \
and self.__agemin.is_empty() and self.__agemax.is_empty() \
and self.__changedby.is_empty() and self.__updatedby.is_empty():
and self.__changedby.is_empty() and self.__updatedby.is_empty() and self.__triggeredby.is_empty():
return False

# set 'eval' for some known conditions if item and eval are not set, yet
Expand Down Expand Up @@ -226,6 +233,9 @@ def complete(self, item_state):
if (self.__item is not None or self.__status is not None or self.__eval is not None)\
and not self.__updatedby.is_empty() and self.__updatedbynegate is None:
self.__updatedbynegate = False
if (self.__item is not None or self.__status is not None or self.__eval is not None)\
and not self.__triggeredby.is_empty() and self.__triggeredbynegate is None:
self.__triggeredbynegate = False

# cast stuff
try:
Expand Down Expand Up @@ -271,6 +281,9 @@ def check(self):
if not self.__check_updatedby():
self._log_decrease_indent()
return False
if not self.__check_triggeredby():
self._log_decrease_indent()
return False
if not self.__check_changedby():
self._log_decrease_indent()
return False
Expand Down Expand Up @@ -317,6 +330,9 @@ def write_to_logger(self, log_level=StateEngineDefaults.log_level):
self.__updatedby.write_to_logger(log_level)
if self.__updatedbynegate is not None and not self.__updatedby.is_empty():
self._log_info("updatedby negate: {0}", self.__updatedbynegate)
self.__triggeredby.write_to_logger(log_level)
if self.__updatedbynegate is not None and not self.__triggeredby.is_empty():
self._log_info("triggeredby negate: {0}", self.__triggeredbynegate)

# Cast 'value', 'min' and 'max' using given cast function
# cast_func: cast function to use
Expand All @@ -334,6 +350,9 @@ def __cast_all(self, cast_func):
self.__updatedby.set_cast(StateEngineTools.cast_str)
if self.__updatedbynegate is not None:
self.__updatedbynegate = StateEngineTools.cast_bool(self.__updatedbynegate)
self.__triggeredby.set_cast(StateEngineTools.cast_str)
if self.__triggeredbynegate is not None:
self.__triggeredbynegate = StateEngineTools.cast_bool(self.__triggeredbynegate)
if self.__agenegate is not None:
self.__agenegate = StateEngineTools.cast_bool(self.__agenegate)

Expand Down Expand Up @@ -376,9 +395,11 @@ def __convert(convert_value, convert_current):

current = self.__get_current(eval_type='changedby') if valuetype == "changedby" else\
self.__get_current(eval_type='updatedby') if valuetype == "updatedby" else\
self.__get_current(eval_type='triggeredby') if valuetype == "triggeredby" else\
self.__get_current(eval_type='value')
negate = self.__changedbynegate if valuetype == "changedby" else\
self.__updatedbynegate if valuetype == "updatedby" else\
self.__triggeredbynegate if valuetype == "triggeredby" else\
self.__negate

if isinstance(value, list):
Expand Down Expand Up @@ -557,6 +578,23 @@ def __check_changedby(self):
finally:
self._log_decrease_indent()

# Check if triggeredby conditions match
def __check_triggeredby(self):
try:
if not self.__triggeredby.is_empty():
# 'updatedby' is given.
triggeredby = self.__triggeredby.get()
triggeredby = StateEngineTools.flatten_list(triggeredby)
return self.__change_update_value(triggeredby, "triggeredby")
else:
self._log_increase_indent()
return True

except Exception as ex:
self._log_warning("Problem checking triggeredby {}", ex)
finally:
self._log_decrease_indent()

# Check if updatedby conditions match
def __check_updatedby(self):
try:
Expand Down Expand Up @@ -663,13 +701,15 @@ def __get_current(self, eval_type='value'):
return self.__status.property.last_change_age if eval_type == 'age' else\
self.__status.property.last_change_by if eval_type == 'changedby' else\
self.__status.property.last_update_by if eval_type == 'updatedby' else\
self.__status.property.last_trigger_by if eval_type == 'triggeredby' else\
self.__status.property.value
elif self.__item is not None:
# noinspection PyUnusedLocal
self._log_debug("Trying to get {} of item {}", eval_type, self.__item)
return self.__item.property.last_change_age if eval_type == 'age' else\
self.__item.property.last_change_by if eval_type == 'changedby' else\
self.__item.property.last_update_by if eval_type == 'updatedby' else\
self.__item.property.last_trigger_by if eval_type == 'triggeredby' else\
self.__item.property.value
if self.__eval is not None:
# noinspection PyUnusedLocal
Expand All @@ -686,6 +726,7 @@ def __get_current(self, eval_type='value'):
value = eval(self.__eval).property.last_change_age if eval_type == 'age' else \
eval(self.__eval).property.last_change_by if eval_type == 'changedby' else \
eval(self.__eval).property.last_update_by if eval_type == 'updatedby' else \
eval(self.__eval).property.last_trigger_by if eval_type == 'triggeredby' else \
eval(self.__eval).property.value
else:
value = eval(self.__eval)
Expand Down
9 changes: 7 additions & 2 deletions stateengine/StateEngineWebif.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ def _conditionlabel(self, state, conditionset):
agemax_none = condition_dict.get('agemax') == 'None'
changedby_none = condition_dict.get('changedby') == 'None'
updatedby_none = condition_dict.get('updatedby') == 'None'
triggeredby_none = condition_dict.get('triggeredby') == 'None'

for compare in condition_dict:
cond1 = not condition_dict.get(compare) == 'None'
Expand All @@ -182,8 +183,9 @@ def _conditionlabel(self, state, conditionset):
cond5 = not compare == 'agenegate'
cond6 = not compare == 'changedbynegate'
cond7 = not compare == 'updatedbynegate'
cond8 = not compare == 'status'
if cond1 and cond2 and cond3 and cond4 and cond5 and cond6 and cond7 and cond8:
cond8 = not compare == 'triggeredbynegate'
cond9 = not compare == 'status'
if cond1 and cond2 and cond3 and cond4 and cond5 and cond6 and cond7 and cond8 and cond9:
conditionlist += '<tr><td align="center"><b>'.format(compare, condition_dict.get(compare))
if not status_none:
textlength = len(str(condition_dict.get('status')))
Expand Down Expand Up @@ -223,6 +225,9 @@ def _conditionlabel(self, state, conditionset):
else "not updated by" if (not updatedby_none and compare == "updatedby"
and condition_dict.get('updatedbynegate') == 'True')\
else "updated by" if not updatedby_none and compare == "updatedby"\
else "not triggered by" if (not triggeredby_none and compare == "triggeredby"
and condition_dict.get('triggeredbynegate') == 'True')\
else "triggered by" if not triggeredby_none and compare == "triggeredby"\
else "!=" if (not value_none and compare == "value"
and condition_dict.get('negate') == 'True')\
else "=="
Expand Down
22 changes: 22 additions & 0 deletions stateengine/user_doc/05_bedingungen.rst
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,28 @@ Die Werte(liste) kann auch durch ``se_changedbynegate_<Bedingungsname>`` negiert
se_changedbynegate_<Bedingungsname>: True|False
**Triggerung des Items durch**

.. code-block:: yaml
se_triggeredby_<Bedingungsname>: [Wert]
Die Bedingung ist erfüllt, wenn das Item durch den angegebenen Wert bzw.
einen der angegebenen Werte getriggert wurde. Dies kann relevant werden,
um herauszufinden, wodurch ein Item mit einem eval-Attribut getriggert wurde,
unabhängig davon, ob sich daraus eine Wertänderung ergibt oder nicht.
Hier bietet es sich an, den Wert als Regular Expression mittels
``se_triggeredby_<Bedingungsname>: regex:StateEngine Plugin`` zu definieren.
Die Werte(liste) kann auch durch ``se_triggeredbynegate_<Bedingungsname>`` negiert werden.

.. code-block:: yaml
se_triggeredby_<Bedingungsname>:
- [Wert1]
- [Wert2]
- regex:[WertN]
se_triggeredbynegate_<Bedingungsname>: True|False
**Mindestalter**

Expand Down

0 comments on commit 2d83a58

Please sign in to comment.