diff --git a/python/biopro/project/condition.py b/python/biopro/project/condition.py index 4c03e30..eaee6ec 100644 --- a/python/biopro/project/condition.py +++ b/python/biopro/project/condition.py @@ -82,11 +82,21 @@ class Condition(): def device(self, **kwargs): print('device') - def previous_task_done(self, **kwargs): + """ def previous_task_done(self, **kwargs): running_task = kwargs['running_task'] if running_task.status == 2 and self._active == False: self._active = True return True + return False """ + + def previous_task_done(self, **kwargs): + running_task = kwargs['running_task'] + self_task = kwargs['self_task'] + # print('\nprevious_task_done: ', running_task.status, ' ', self._active) + if running_task != self_task: + if running_task.status == 2 and self._active == False: + # self._active = True + return True return False def str_to_datetime(self, time_str): diff --git a/python/biopro/project/project.py b/python/biopro/project/project.py index 77a09fd..cada67d 100644 --- a/python/biopro/project/project.py +++ b/python/biopro/project/project.py @@ -127,8 +127,14 @@ class Project(threading.Thread): delay_time = 0 check_list = copy(self._task_manager.check_list) + # print('!@#len: ', len(check_list)) + """ if len(check_list) == 2: + if check_list[0] != None: + print('\n##0: ', check_list[0].name) + print('\n##1: ', check_list[1].name) """ for task in check_list: if task != None: + # print('\n!@#task: ', task.name) now = time() # print('round task', task.name) # print('running task', self._task_manager.running_task.name, self._task_manager.running_task.status) @@ -139,15 +145,20 @@ class Project(threading.Thread): project_start_time = self._start_time, delay_time = delay_time, running_task= self._task_manager.running_task, - previous_task = self._task_manager.prev_task + previous_task = self._task_manager.prev_task, + self_task = task ) # print('match_condition_list', match_condition_list) for condition in match_condition_list: + # print('\ncondition: ', condition, '\n') match_action_list = task.get_match_action(condition.id) for action in match_action_list: # print('match_action', action.type, action.target) + # print('\n!@#action type:task status', ' => ', action.type, ':', task.status) + # if action.type == 'start' and task.status == 0: if action.type == 'start' and task.status != 1: self._task_manager.set_running_task(task) + # Trace self.mqtt_thread.broadcast_command('project:task ' + task.name + ' start at ' + datetime.now().strftime('%Y-%m-%d %H:%M:%S.%f')[:-3]) device = self._complete_device[action.target] @@ -165,7 +176,11 @@ class Project(threading.Thread): # check task not running then stop if self.check_running_task_not_run() == True: self.mqtt_thread.broadcast_command('project:task ' + str(self._task_manager.running_task.name) + ' stop at ' + datetime.now().strftime('%Y-%m-%d %H:%M:%S.%f')[:-3]) + # self._task_manager.set_cycle_task() self._task_manager.running_task.stop() + # if self._task_manager.running_task.cycle_count < self._task_manager.running_task.cycle and self._task_manager.running_task.cycle > 0: + # print('\n\n\n!@#cycle: ', self._task_manager.running_task.cycle_count, self._task_manager.running_task.cycle) + # self._task_manager.set_running_task(self._task_manager.next_task[0]) # check project done then close project if self.check_project_done() == True: diff --git a/python/biopro/project/task.py b/python/biopro/project/task.py index fdaa808..2b7adb0 100644 --- a/python/biopro/project/task.py +++ b/python/biopro/project/task.py @@ -15,6 +15,8 @@ class Task: self._name = None self._parent = None self._cycle = None + self._cycle_count = 0 + self._cycle_next = None self._device = None self._event = None self._trigger = None @@ -100,6 +102,23 @@ class Task: @cycle.setter def cycle(self, new_cycle): self._cycle = new_cycle + + # TODO: not sure needed? + @property + def cycle_count(self) -> int: + return self._cycle_count + + @cycle_count.setter + def cycle_count(self, new_cycle_count): + self._cycle_count = new_cycle_count + + @property + def cycle_next(self) -> str: + return self._cycle_next + + @cycle_next.setter + def cycle_next(self, new_cycle_next): + self._cycle_next = new_cycle_next @property def device(self) -> dict: @@ -207,6 +226,10 @@ class Task: def stop(self): self.status = 2 + + def reset(self): + self.status = -1 + # self.status = 0 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) @@ -241,6 +264,8 @@ class Task: 'name': self.name, 'parent': self.parent, 'cycle': self.cycle, + 'cycle_count': self.cycle_count, + 'cycle_next': self.cycle_next, 'device': self.device, 'event': self.event, 'trigger': self.trigger, diff --git a/python/biopro/project/task_manager.py b/python/biopro/project/task_manager.py index c732147..6916365 100644 --- a/python/biopro/project/task_manager.py +++ b/python/biopro/project/task_manager.py @@ -5,6 +5,7 @@ from xml.dom.expatbuilder import parseString import paho.mqtt.client as mqtt from biopro.text import * from .task import Task +from copy import copy, deepcopy _RUNTIME_COMPILE = False @@ -57,7 +58,8 @@ class TaskManager(): task = Task(task) self._task_list.append(task) - def set_running_task(self, task): + # original version + """ def set_running_task(self, task): try: # if there is task running & same task active ,then reject if self._running_task != None and self._running_task.uuid == task.uuid: @@ -80,8 +82,75 @@ class TaskManager(): _task = next((task for task in self._task_list if task.uuid == task_uuid), None) if _task != None: self._next_task.append(_task) + except RuntimeError as e: + print(e) """ + + def set_running_task(self, task): + try: + # if there is task running & same task active ,then reject + if self._running_task != None and self._running_task.uuid == task.uuid: + # print('set_running_task return false: ', self._running_task.name) + return False + + # save running task + self._prev_task = self._running_task + # clear next task list + self._next_task.clear() + self._running_task = task + self._running_task.run() + + if self._prev_task != None: + # if previous task is still running, then need to close + if self._prev_task.status == 1: + self._prev_task.stop() + print('prev', 'run', self._prev_task.name, self._running_task.name) + + # print(f'!!@#!set_running_task\n\n\n{task._cycle_count}\n{task._cycle}\n\n\n') + if task._cycle_count < task._cycle: + task._cycle_count += 1 + cycle_target = deepcopy(self._task_list[task._cycle_next]) # TODO: not sure deep or shallow + cycle_target.reset() + # print('!!@#!status: ', cycle_target.status) + # self._task_list[task._cycle_next].status = 0 + self._next_task.append(cycle_target) + # print('!!@#!append what: ', cycle_target) + else: + task._cycle_count = 0 + for task_uuid in self._running_task.next: + _task = next((task for task in self._task_list if task.uuid == task_uuid), None) + if _task != None: + _task.reset() + # print('!@#WATCH', _task.status) + # print('!@#_task._cycle_count: ', _task._cycle_count) + _task_copy = deepcopy(_task) # TODO: here fix one bug, and make another, due to the second time "stop" condition will be trigger in this way, while the number of cycle will be reset + self._next_task.append(_task_copy) except RuntimeError as e: print(e) + + """ def set_cycle_task(self, task): + try: + # if there is task running & same task active ,then reject + if self._running_task != None and self._running_task.uuid == task.uuid: + return False + + print(f'!!@#!\n\n\n{task._cycle_count}\n{task._cycle}\n\n\n') + if task._cycle_count < task._cycle: + task._cycle_count += 1 + self._next_task.append(self._task_list[task._cycle_next]) + else: + task._cycle_count = 0 + except RuntimeError as e: + print(e) """ + """ def set_cycle_task(self): + try: + print(f'!!@#!set_cycle_task\n\n\n{self._running_task._cycle_count}\n{self._running_task._cycle}\n\n\n') + if self._running_task._cycle_count < self._running_task._cycle: + self._running_task._cycle_count += 1 + self._next_task.append(self._task_list[self._running_task._cycle_next]) + else: + self._running_task._cycle_count = 0 + except RuntimeError as e: + print(e) """ def get_task(self, task_id): return self._task_list[task_id] diff --git a/python/biopro/server/main.py b/python/biopro/server/main.py index f68ee14..4e81219 100644 --- a/python/biopro/server/main.py +++ b/python/biopro/server/main.py @@ -1015,6 +1015,10 @@ class ControlServer(SocketServer, ControlServerAPI): @logging_info def run_project(self, project) -> bool: if project is not None: + # print('!@#', project) + # print('\n\n\n!!@#') + # print(project) + # print('\n\n\n') project = self._project_manager.create(project) self._project_manager.run_project(project) return project.as_json()