Files
controller-wisetopdataserver/python/biopro/project/task_manager.py
T

76 lines
2.4 KiB
Python
Raw Normal View History

2022-02-07 20:27:30 +08:00
from json import loads as json_parse, dumps as _json_stringify
from typing import Dict, Optional, Any
2022-02-10 00:49:49 +08:00
from xml.dom.expatbuilder import parseString
2022-02-07 20:27:30 +08:00
import paho.mqtt.client as mqtt
2022-02-25 20:54:58 +08:00
from python.biopro.text import *
2022-02-07 20:27:30 +08:00
from .task import Task
_RUNTIME_COMPILE = False
def json_stringify(o: Any) -> str:
return _json_stringify(o, separators=(',', ':'))
class TaskManager():
2022-02-10 00:49:49 +08:00
def __init__(self, task_list):
2022-02-17 20:35:36 +08:00
self._current_task = None
2022-02-12 00:21:27 +08:00
self._task_list = []
2022-02-17 20:35:36 +08:00
self._need_to_check_task_list = []
2022-02-12 00:21:27 +08:00
for task in task_list:
2022-02-25 20:54:58 +08:00
task = Task(task)
2022-02-17 20:35:36 +08:00
self._task_list.append(task)
self.set_current_task(self._task_list[0])
2022-02-14 19:19:54 +08:00
self._current_task.run()
2022-02-07 20:27:30 +08:00
def remove(self, index):
2022-02-08 20:12:44 +08:00
pass
2022-02-07 20:27:30 +08:00
2022-02-25 20:54:58 +08:00
def get_task(self, task_id):
return self._task_list[task_id]
2022-02-17 20:35:36 +08:00
def get_check_task_list(self):
return self._need_to_check_task_list
2022-02-07 20:27:30 +08:00
2022-02-10 00:49:49 +08:00
def get_current_task(self):
2022-02-12 00:21:27 +08:00
return self._current_task
2022-02-17 20:35:36 +08:00
def get_next_task(self):
return self._current_task.next
def set_current_task(self, task):
self._current_task = task
self._need_to_check_task_list = []
# append current task
self._need_to_check_task_list.append(self._current_task)
# append next task
for task_uuid in self._current_task.next:
task = next((task for task in self._task_list if task.uuid == task_uuid), None)
self._need_to_check_task_list.append(task)
2022-02-25 20:54:58 +08:00
def check_condition(self):
for task in self.get_check_task_list():
# [<match_condition>, ...]
match_condition_list = task.check_time_condition()
# [[<match_action>, ...], ...]
match_action_list = task.get_match_action_list(match_condition_list)
for action_list in match_action_list:
for action in action_list:
task.do_action(action)
while len(task.instruction_list) > 0:
instruction = task.instruction_list.pop(0)
if instruction.get('device', None) != None:
device = self._device_manager.get_device(self.device[instruction['device']]['connectDevice']['device_address'])
if device is None:
raise RuntimeError(DEVICE_NOT_FOUND, device)
print(instruction['header'], *instruction['arguments'].values())
getattr(device, instruction['header'])(*instruction['arguments'].values())