[update] fix stop error & relative_time
This commit is contained in:
@@ -9,7 +9,6 @@ class Action():
|
||||
self._duration = action.get('duration', None)
|
||||
self._goto = action.get('goto', None)
|
||||
self._cycle = action.get('cycle', None)
|
||||
|
||||
self._instruction = None
|
||||
|
||||
self.update_instruction()
|
||||
@@ -19,12 +18,16 @@ class Action():
|
||||
return self._type
|
||||
|
||||
def update_instruction(self):
|
||||
instruction_type = self._type
|
||||
if instruction_type == 'stop':
|
||||
instruction_type = 'interrupt'
|
||||
|
||||
self._instruction = {
|
||||
"device_instruction": {
|
||||
"header": "call_instruction",
|
||||
"device": self._target,
|
||||
"arguments": {
|
||||
"instruction": self._type,
|
||||
"instruction": instruction_type,
|
||||
}
|
||||
},
|
||||
"device_parameter": {
|
||||
|
||||
@@ -3,7 +3,6 @@ from time import time
|
||||
|
||||
class Condition():
|
||||
def __init__(self, id, condition):
|
||||
print('condition', id, condition)
|
||||
self._id = id
|
||||
self._type = condition['type']
|
||||
self._comparsion = condition['comparsion']
|
||||
@@ -12,12 +11,15 @@ class Condition():
|
||||
@property
|
||||
def id(self):
|
||||
return self._id
|
||||
|
||||
@property
|
||||
def type(self):
|
||||
return self._type
|
||||
|
||||
@property
|
||||
def comparsion(self):
|
||||
return self._comparsion
|
||||
|
||||
@property
|
||||
def value(self):
|
||||
return self._value
|
||||
@@ -38,11 +40,11 @@ class Condition():
|
||||
"smaller": lambda a, b: a < b,
|
||||
}
|
||||
|
||||
# print('x','y',x, y,type(x),type(y))
|
||||
return cases[operator](x, y)
|
||||
|
||||
def match_or_not(self, **kwargs):
|
||||
return self.method_mapping(self.type)()
|
||||
# print(self._type, self._comparsion, self._value)
|
||||
return getattr(self, self.type)(**kwargs)
|
||||
|
||||
def absolute_time(self, **kwargs):
|
||||
now = time()
|
||||
@@ -51,7 +53,13 @@ class Condition():
|
||||
return self.compareWith(self.comparsion, int(now), int(time_condition))
|
||||
|
||||
def relative_time(self, **kwargs):
|
||||
project_start_time = kwargs['project_start_time']
|
||||
task_start_time = kwargs['task_start_time']
|
||||
delay_time = kwargs['delay_time']
|
||||
now = time()
|
||||
|
||||
time_diff = int(now - project_start_time - delay_time)
|
||||
return self.compareWith(self.comparsion, time_diff, int(self._value))
|
||||
|
||||
def device(self, **kwargs):
|
||||
print('device')
|
||||
|
||||
@@ -8,6 +8,7 @@ from datetime import datetime
|
||||
from collections import deque
|
||||
from biopro.device.manager import DeviceManager
|
||||
from biopro.text import *
|
||||
from copy import copy
|
||||
|
||||
class Project(threading.Thread):
|
||||
def __init__(self, project, device_manager: DeviceManager, mqttThread = None, name="project"):
|
||||
@@ -19,9 +20,8 @@ class Project(threading.Thread):
|
||||
self._start_time = None
|
||||
|
||||
self._task_manager = TaskManager(self._project['taskList'])
|
||||
self._stop = False
|
||||
|
||||
self._start_time = None
|
||||
|
||||
@property
|
||||
def desc(self) -> str:
|
||||
return self._project['desc']
|
||||
@@ -45,34 +45,38 @@ class Project(threading.Thread):
|
||||
def run(self):
|
||||
self._start_time = time.time()
|
||||
|
||||
while True:
|
||||
while not self._stop:
|
||||
# current task
|
||||
for task in self._task_manager.get_check_task_list():
|
||||
task_check_list = copy(self._task_manager.get_check_task_list())
|
||||
delay_time = 0
|
||||
for task in task_check_list:
|
||||
# [<match_condition>, ...]
|
||||
match_condition_list = task.check_condition('time')
|
||||
match_condition_list = task.check_condition(project_start_time = self._start_time, delay_time = delay_time)
|
||||
# [[<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:
|
||||
if action.type == 'start':
|
||||
task.reset_start_time()
|
||||
self._task_manager.set_current_task(task)
|
||||
task.do_action(action)
|
||||
|
||||
|
||||
now = time.time()
|
||||
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'])
|
||||
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())
|
||||
if device is None:
|
||||
raise RuntimeError(DEVICE_NOT_FOUND, device)
|
||||
|
||||
getattr(device, instruction['header'])(*instruction['arguments'].values())
|
||||
delay_time += (time.time() - now)
|
||||
time.sleep(self._time_interval)
|
||||
|
||||
def stop(self):
|
||||
return
|
||||
self._stop = True
|
||||
|
||||
def close(self):
|
||||
return
|
||||
self._stop = True
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
from typing import List
|
||||
from .condition import Condition
|
||||
from .action import Action
|
||||
from time import time
|
||||
@@ -6,8 +7,8 @@ class Task:
|
||||
def __init__(self, task):
|
||||
self._task = task
|
||||
|
||||
self._condition_list = []
|
||||
self._action_list = []
|
||||
self._condition_list:List[Condition] = []
|
||||
self._action_list:List[Action] = []
|
||||
|
||||
self._start_time = time()
|
||||
self._idle_time = []
|
||||
@@ -16,7 +17,6 @@ class Task:
|
||||
self._last_time = None
|
||||
|
||||
self._instruction_list = []
|
||||
|
||||
self.setup()
|
||||
|
||||
def setup(self) -> None:
|
||||
@@ -105,6 +105,9 @@ class Task:
|
||||
@property
|
||||
def next(self) -> str:
|
||||
return self._task['next']
|
||||
|
||||
def reset_start_time(self):
|
||||
self._start_time = time()
|
||||
|
||||
def get_parameter_set_by_device(self, device):
|
||||
return [parameter_set for parameter_set in self.parameter_set.values() if device == parameter_set['target']]
|
||||
@@ -151,9 +154,6 @@ class Task:
|
||||
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)
|
||||
|
||||
@@ -164,5 +164,6 @@ class Task:
|
||||
}
|
||||
return statuses[status]
|
||||
|
||||
def check_condition(self, type):
|
||||
return [condition for condition in self._condition_list if condition.match_or_not()]
|
||||
def check_condition(self, **kwargs):
|
||||
kwargs['task_start_time'] = self._start_time
|
||||
return [condition for condition in self._condition_list if condition.match_or_not(**kwargs)]
|
||||
@@ -1,5 +1,5 @@
|
||||
from json import loads as json_parse, dumps as _json_stringify
|
||||
from typing import Dict, Optional, Any
|
||||
from typing import Dict, List, Optional, Any
|
||||
from xml.dom.expatbuilder import parseString
|
||||
import paho.mqtt.client as mqtt
|
||||
from biopro.text import *
|
||||
@@ -29,7 +29,7 @@ class TaskManager():
|
||||
def get_task(self, task_id):
|
||||
return self._task_list[task_id]
|
||||
|
||||
def get_check_task_list(self):
|
||||
def get_check_task_list(self) -> List[Task]:
|
||||
return self._need_to_check_task_list
|
||||
|
||||
def get_current_task(self) -> Task:
|
||||
|
||||
Reference in New Issue
Block a user