diff --git a/python/biopro/api/controller.py b/python/biopro/api/controller.py index 7a645c5..47a4f52 100644 --- a/python/biopro/api/controller.py +++ b/python/biopro/api/controller.py @@ -18,9 +18,10 @@ class ControllerAPI(): @staticmethod def getByMac(mac_address): try: - ret = requests.get(API_URL + 'api/controller/get_by_mac/' + mac_address, headers= AuthAPI.get_key()) - if ret.status_code == 200: - return ret.json() + if mac_address != None: + ret = requests.get(API_URL + 'api/controller/get_by_mac/' + mac_address, headers= AuthAPI.get_key()) + if ret.status_code == 200: + return ret.json() except (requests.exceptions.ConnectionError, json.decoder.JSONDecodeError) as e: print('get controller fail', e) return [] diff --git a/python/biopro/project/action.py b/python/biopro/project/action.py index 92ba565..5c7971c 100644 --- a/python/biopro/project/action.py +++ b/python/biopro/project/action.py @@ -6,9 +6,17 @@ class Action(): self._type:str = action['type'] self._target:str = action['target'] self._condition_list:list[str] = action['condition'] + self._duration = action.get('duration', None) + self._goto = action.get('goto', None) + self._cycle = action.get('cycle', None) + self._instruction = None - + self.update_instruction() + + @property + def type(self): + return self._type def update_instruction(self): self._instruction = { diff --git a/python/biopro/project/condition.py b/python/biopro/project/condition.py index 20b910a..44b4302 100644 --- a/python/biopro/project/condition.py +++ b/python/biopro/project/condition.py @@ -38,7 +38,7 @@ class Condition(): "smaller": lambda a, b: a < b, } - print('x','y',x, y,type(x),type(y)) + # print('x','y',x, y,type(x),type(y)) return cases[operator](x, y) def match_or_not(self, **kwargs): @@ -51,7 +51,7 @@ class Condition(): return self.compareWith(self.comparsion, int(now), int(time_condition)) def relative_time(self, **kwargs): - print('relative_time') + now = time() def device(self, **kwargs): print('device') diff --git a/python/biopro/project/project.py b/python/biopro/project/project.py index 2d4af21..dfe3a93 100644 --- a/python/biopro/project/project.py +++ b/python/biopro/project/project.py @@ -20,7 +20,7 @@ class Project(threading.Thread): self._task_manager = TaskManager(self._project['taskList']) - self._start_time = time.time() + self._start_time = None @property def desc(self) -> str: @@ -41,8 +41,10 @@ class Project(threading.Thread): @property def get_current_task(self) -> str: return self._task_manager.get_current_task() - + def run(self): + self._start_time = time.time() + while True: # current task for task in self._task_manager.get_check_task_list(): @@ -53,6 +55,8 @@ class Project(threading.Thread): for action_list in match_action_list: for action in action_list: + if action.type == 'start': + self._task_manager.set_current_task(task) task.do_action(action) while len(task.instruction_list) > 0: @@ -63,8 +67,7 @@ class Project(threading.Thread): if device is None: raise RuntimeError(DEVICE_NOT_FOUND, device) - print(instruction['header'], *instruction['arguments'].values()) - + # print(instruction['header'], *instruction['arguments'].values()) getattr(device, instruction['header'])(*instruction['arguments'].values()) time.sleep(self._time_interval) diff --git a/python/biopro/project/project_manager.py b/python/biopro/project/project_manager.py index 84cb41c..3745c81 100644 --- a/python/biopro/project/project_manager.py +++ b/python/biopro/project/project_manager.py @@ -28,3 +28,6 @@ class ProjectManager(): def run(self, project): project.start() + + def stop(self, project): + project.stop() diff --git a/python/biopro/project/task.py b/python/biopro/project/task.py index 51b057c..d9e2d83 100644 --- a/python/biopro/project/task.py +++ b/python/biopro/project/task.py @@ -105,12 +105,6 @@ class Task: @property def next(self) -> str: return self._task['next'] - - def start(self) -> str: - self._status = 'start' - - def stop(self) -> str: - self._status = 'stop' def get_parameter_set_by_device(self, device): return [parameter_set for parameter_set in self.parameter_set.values() if device == parameter_set['target']] @@ -141,6 +135,11 @@ class Task: 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') @@ -151,7 +150,7 @@ class Task: 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_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: @@ -165,5 +164,5 @@ class Task: } return statuses[status] - def check_time_condition(self): + def check_condition(self, type): return [condition for condition in self._condition_list if condition.match_or_not()] \ No newline at end of file diff --git a/python/biopro/project/task_manager.py b/python/biopro/project/task_manager.py index 07f850f..2c4bef6 100644 --- a/python/biopro/project/task_manager.py +++ b/python/biopro/project/task_manager.py @@ -32,44 +32,25 @@ class TaskManager(): def get_check_task_list(self): return self._need_to_check_task_list - def get_current_task(self): + def get_current_task(self) -> Task: return self._current_task 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) + try: + if self._current_task != None and self._current_task.id != task.id: + self._current_task.stop() - # 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) - - - - def check_condition(self): - for task in self.get_check_task_list(): - # [, ...] - match_condition_list = task.check_time_condition() - # [[, ...], ...] - 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']) + self._current_task = task + self._need_to_check_task_list = [] + # append current task + self._need_to_check_task_list.append(self._current_task) - if device is None: - raise RuntimeError(DEVICE_NOT_FOUND, device) - - print(instruction['header'], *instruction['arguments'].values()) - - getattr(device, instruction['header'])(*instruction['arguments'].values()) + # 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) + except Exception as e: + print(e)