168 lines
3.9 KiB
Python
168 lines
3.9 KiB
Python
from .condition import Condition
|
|
from .action import Action
|
|
from time import time
|
|
|
|
class Task:
|
|
def __init__(self, task):
|
|
self._task = task
|
|
|
|
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._instruction_list = []
|
|
|
|
self.setup()
|
|
|
|
def setup(self) -> None:
|
|
# add condition
|
|
for key, condition in self._task['condition'].items():
|
|
new_condition = Condition(key, condition)
|
|
self._condition_list.append(new_condition)
|
|
|
|
# add action
|
|
for key, action in self._task['action'].items():
|
|
new_action = Action(self, key, action)
|
|
self._action_list.append(new_action)
|
|
|
|
@property
|
|
def id(self) -> str:
|
|
return self._task['id']
|
|
|
|
@property
|
|
def file_name(self):
|
|
return self._task['name']
|
|
|
|
@property
|
|
def parent(self):
|
|
return self._task.get('parent', {"folder": [2]})
|
|
|
|
@property
|
|
def instruction_list(self):
|
|
return self._instruction_list
|
|
|
|
@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_set(self) -> list:
|
|
return self._task['parameterSet']
|
|
|
|
@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._task['status']
|
|
|
|
@property
|
|
def uuid(self) -> str:
|
|
return self._task['uuid']
|
|
|
|
@property
|
|
def next(self) -> str:
|
|
return self._task['next']
|
|
|
|
def get_parameter_set_by_device(self, device):
|
|
return [parameter_set for parameter_set in self.parameter_set.values() if device == parameter_set['target']]
|
|
|
|
def get_parameter(self, name_or_value, device):
|
|
name_list = []
|
|
value_list = []
|
|
for parameter in self.get_parameter_set_by_device(device):
|
|
for key, value in parameter.items():
|
|
if key != 'target':
|
|
name_list.append(key)
|
|
value_list.append(str(value))
|
|
|
|
return '|'.join(name_list) if name_or_value == 'name' else '|'.join(value_list)
|
|
|
|
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 set_status(self, status):
|
|
self._status = status
|
|
|
|
def start(self):
|
|
self.set_status('running')
|
|
|
|
def stop(self):
|
|
if self.status == 'running':
|
|
self.do_action('stop')
|
|
self.set_status('idle')
|
|
|
|
def done(self):
|
|
self.set_status('done')
|
|
|
|
def end(self):
|
|
self.set_status('end')
|
|
|
|
def get_match_action_list(self, match_condition_list):
|
|
return map(lambda condition: [x for x in self._action_list if condition.id in x.get_condition_list()], match_condition_list)
|
|
|
|
def do_action(self, action):
|
|
# # match_action = [x for x in self._action_list if condition_id in x.get_condition_list()]
|
|
|
|
# for action in match_action:
|
|
for instruction in action.get_instruction_list():
|
|
self._instruction_list.append(instruction)
|
|
|
|
def status_mapping(self, status):
|
|
statuses = {
|
|
"start": self.task_start,
|
|
"done": self.task_done,
|
|
}
|
|
return statuses[status]
|
|
|
|
def check_condition(self, type):
|
|
return [condition for condition in self._condition_list if condition.match_or_not()] |