124 lines
2.7 KiB
Python
124 lines
2.7 KiB
Python
from .condition import Condition
|
|
from time import time
|
|
|
|
class Task:
|
|
def __init__(self, task):
|
|
self._task = task
|
|
self._status = self._task['status']
|
|
self._cycle = self._task['cycle']
|
|
# store the reference (next item)
|
|
self.next = []
|
|
|
|
self._condition_list = []
|
|
self._action_list = []
|
|
|
|
self._start_time = time()
|
|
self._idle_time = []
|
|
|
|
self._period = None
|
|
self._next_time = None
|
|
self._last_time = None
|
|
|
|
self.setup()
|
|
|
|
def setup(self) -> None:
|
|
for key, condition in self._task['condition'].items():
|
|
new_condition = Condition(key, condition)
|
|
self._condition_list.append(new_condition)
|
|
|
|
for action in self._task['action']:
|
|
self._action_list.append(action)
|
|
print('self._condition_list', self._condition_list)
|
|
|
|
@property
|
|
def task_id(self) -> str:
|
|
return self._id
|
|
|
|
@property
|
|
def cycle(self) -> int:
|
|
return self._task['cycle']
|
|
|
|
@property
|
|
def order(self) -> int:
|
|
return self._task['order']
|
|
|
|
@property
|
|
def max_cycle(self) -> int:
|
|
return self._task['max_cycle']
|
|
|
|
@property
|
|
def devices(self) -> dict:
|
|
return self._task['devices']
|
|
|
|
@property
|
|
def devices_id(self) -> list:
|
|
return [device_id for device_id in self.devices]
|
|
|
|
@property
|
|
def events(self) -> list:
|
|
return self._task['events']
|
|
|
|
@property
|
|
def triggers(self) -> list:
|
|
return self._task['triggers']
|
|
|
|
@property
|
|
def parameter_sets(self) -> list:
|
|
return self._task['parameter_sets']
|
|
|
|
@property
|
|
def instructions(self) -> list:
|
|
return self._task['instructions']
|
|
|
|
@property
|
|
def conditions(self) -> list:
|
|
return self._task['conditions']
|
|
|
|
@property
|
|
def actions(self) -> list:
|
|
return self._task['actions']
|
|
|
|
@property
|
|
def condition_list(self) -> list:
|
|
return self._condition_list
|
|
|
|
@property
|
|
def status(self) -> str:
|
|
return self._status
|
|
|
|
def run(self):
|
|
self._status = 'running'
|
|
self._start_time = time()
|
|
|
|
def pause(self):
|
|
self._status = 'pause'
|
|
|
|
def set_cycle(self, cycle):
|
|
self._cycle = cycle
|
|
|
|
def match_condition_actions(self, condition_id) -> list:
|
|
return [action_id for action_id in self.actions if condition_id in self.actions[action_id]['condition']]
|
|
|
|
def set_status(self, status):
|
|
self._status = status
|
|
|
|
def start(self):
|
|
self.set_status('running')
|
|
|
|
def done(self):
|
|
self.set_status('done')
|
|
|
|
def end(self):
|
|
self.set_status('end')
|
|
|
|
def status_mapping(self, status):
|
|
statuses = {
|
|
"start": self.task_start,
|
|
"done": self.task_done,
|
|
}
|
|
return statuses[status]
|
|
|
|
def check_time(self):
|
|
for condition in self._current_task.condition_list:
|
|
if condition.type == 'absolute_time':
|
|
condition.check_match(type, self.start_time) |