75 lines
2.0 KiB
Python
75 lines
2.0 KiB
Python
from datetime import datetime
|
|
from time import time
|
|
|
|
class Condition():
|
|
def __init__(self, id, condition):
|
|
print('condition', id, condition)
|
|
self._id = id
|
|
self._type = condition['type']
|
|
self._comparsion = condition['comparsion']
|
|
self._value = condition['value']
|
|
|
|
@property
|
|
def id(self):
|
|
return self._id
|
|
@property
|
|
def type(self):
|
|
return self._type
|
|
@property
|
|
def comparsion(self):
|
|
return self._comparsion
|
|
@property
|
|
def value(self):
|
|
return self._value
|
|
|
|
def set_comparsion(self, comparsion):
|
|
self._comparsion = comparsion
|
|
|
|
def set_type(self, type):
|
|
self._type = type
|
|
|
|
def set_value(self, value):
|
|
self._value = value
|
|
|
|
def compareWith(self, operator:str, x, y) -> bool:
|
|
cases = {
|
|
"equal": lambda a, b: a == b,
|
|
"bigger": lambda a, b: a > b,
|
|
"smaller": lambda a, b: a < b,
|
|
}
|
|
|
|
# print('x','y',x, y,type(x),type(y))
|
|
return cases[operator](x, y)
|
|
|
|
def match_or_not(self, **kwargs):
|
|
return self.method_mapping(self.type)()
|
|
|
|
def absolute_time(self, **kwargs):
|
|
now = time()
|
|
time_condition = self.datetime_to_timestamp(self.str_to_datetime(self._value))
|
|
|
|
return self.compareWith(self.comparsion, int(now), int(time_condition))
|
|
|
|
def relative_time(self, **kwargs):
|
|
now = time()
|
|
|
|
def device(self, **kwargs):
|
|
print('device')
|
|
|
|
def task_status(self, **kwargs):
|
|
print('task_status')
|
|
|
|
def method_mapping(self, method_name):
|
|
methods = {
|
|
"relative_time": self.relative_time,
|
|
"absolute_time": self.absolute_time,
|
|
"device": self.device,
|
|
"task_status": self.task_status,
|
|
}
|
|
return methods[method_name]
|
|
|
|
def str_to_datetime(self, time_str):
|
|
return datetime.strptime(time_str,'%Y-%m-%dT%H:%M')
|
|
|
|
def datetime_to_timestamp(self, date):
|
|
return datetime.timestamp(date) |