[update] add condition & first task issue & add boardcast
This commit is contained in:
@@ -42,7 +42,7 @@ class Condition():
|
||||
self._value = new_value
|
||||
|
||||
def compareWith(self, operator, x, y) -> bool:
|
||||
print(operator, x, y)
|
||||
# print(operator, x, y)
|
||||
cases = {
|
||||
"equal": lambda a, b: a == b,
|
||||
"bigger": lambda a, b: a > b,
|
||||
@@ -52,7 +52,7 @@ class Condition():
|
||||
return cases[operator](x, y)
|
||||
|
||||
def match_or_not(self, **kwargs):
|
||||
print(self._type, self._comparsion, self._value)
|
||||
# print(self._type, self._comparsion, self._value)
|
||||
return getattr(self, self.type)(**kwargs)
|
||||
|
||||
def absolute_time(self, **kwargs):
|
||||
@@ -61,21 +61,29 @@ class Condition():
|
||||
|
||||
return self.compareWith(self.comparsion, now, time_condition)
|
||||
|
||||
def relative_time(self, **kwargs):
|
||||
now = time()
|
||||
def after_project_run(self, **kwargs):
|
||||
project_start_time = kwargs['project_start_time']
|
||||
task_start_time = kwargs['task_start_time']
|
||||
delay_time = kwargs['delay_time']
|
||||
|
||||
time_diff = int(now - project_start_time - delay_time)
|
||||
time_diff = int(time() - project_start_time - delay_time)
|
||||
return self.compareWith(self.comparsion, time_diff, int(self._value))
|
||||
|
||||
def after_task_run(self, **kwargs):
|
||||
# print('relative_time_from_task', kwargs,kwargs['task_start_time'],kwargs['delay_time'])
|
||||
if len(kwargs['task_start_time']) == 0:
|
||||
return False
|
||||
task_start_time = kwargs['task_start_time'][-1]
|
||||
delay_time = kwargs['delay_time']
|
||||
|
||||
time_diff = int(time() - task_start_time - delay_time)
|
||||
# print('time_diff', time_diff)
|
||||
return self.compareWith(self.comparsion, time_diff, int(self._value))
|
||||
|
||||
def device(self, **kwargs):
|
||||
print('device')
|
||||
|
||||
def task(self, **kwargs):
|
||||
def previous_task_done(self, **kwargs):
|
||||
running_task = kwargs['running_task']
|
||||
previous_task = kwargs['previous_task']
|
||||
if running_task.status == 2 and self._active == False:
|
||||
self._active = True
|
||||
return True
|
||||
|
||||
@@ -109,53 +109,64 @@ class Project(threading.Thread):
|
||||
def task_list(self):
|
||||
return self.task_manager.task_list
|
||||
|
||||
|
||||
@property
|
||||
def mqtt_thread(self):
|
||||
return self._mqtt_thread
|
||||
|
||||
def run(self):
|
||||
self._start_time = time()
|
||||
|
||||
self.mqtt_thread.broadcast_command('project:' + self._name + ' start_at ' + datetime.now().strftime('%Y-%m-%d %H:%M:%S.%f')[:-3])
|
||||
while not self._stop_flag :
|
||||
# check running task first
|
||||
delay_time = 0
|
||||
check_list = copy(self._task_manager.check_list)
|
||||
|
||||
for task in check_list:
|
||||
now = time()
|
||||
# print('round task', task.name)
|
||||
# print('running task', self._task_manager.running_task.name, self._task_manager.running_task.status)
|
||||
# if self._task_manager.prev_task != None:
|
||||
# print('previous_task', self._task_manager.prev_task.name, self._task_manager.prev_task.status)
|
||||
if task != None:
|
||||
now = time()
|
||||
# print('round task', task.name)
|
||||
# print('running task', self._task_manager.running_task.name, self._task_manager.running_task.status)
|
||||
# if self._task_manager.prev_task != None:
|
||||
# print('previous_task', self._task_manager.prev_task.name, self._task_manager.prev_task.status)
|
||||
|
||||
match_condition_list = task.check_condition(
|
||||
project_start_time = self._start_time,
|
||||
delay_time = delay_time,
|
||||
running_task= self._task_manager.running_task,
|
||||
previous_task = self._task_manager.prev_task
|
||||
)
|
||||
# print('match_condition_list', match_condition_list)
|
||||
for condition in match_condition_list:
|
||||
match_action_list = task.get_match_action(condition.id)
|
||||
for action in match_action_list:
|
||||
now = time()
|
||||
# print('match_action', action.type, action.target)
|
||||
if action.type == 'start' and task.status != 1:
|
||||
self._task_manager.set_running_task(task)
|
||||
match_condition_list = task.check_condition(
|
||||
project_start_time = self._start_time,
|
||||
delay_time = delay_time,
|
||||
running_task= self._task_manager.running_task,
|
||||
previous_task = self._task_manager.prev_task
|
||||
)
|
||||
# print('match_condition_list', match_condition_list)
|
||||
for condition in match_condition_list:
|
||||
match_action_list = task.get_match_action(condition.id)
|
||||
for action in match_action_list:
|
||||
# print('match_action', action.type, action.target)
|
||||
if action.type == 'start' and task.status != 1:
|
||||
self._task_manager.set_running_task(task)
|
||||
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]
|
||||
task_info = task.get_task_info(action)
|
||||
instruction_set = getattr(self._instruction_set, action.type, None)
|
||||
# print('instruction_set',instruction_set)
|
||||
|
||||
if instruction_set != None:
|
||||
for instruction in instruction_set:
|
||||
args = list(map(lambda arg: task_info[arg], instruction['arguments']))
|
||||
threading.Thread(target=getattr(device, instruction['method'])(*args))
|
||||
|
||||
delay_time += (time() - now)
|
||||
device = self._complete_device[action.target]
|
||||
task_info = task.get_task_info(action)
|
||||
instruction_set = getattr(self._instruction_set, action.type, None)
|
||||
# print('instruction_set',instruction_set)
|
||||
|
||||
if instruction_set != None:
|
||||
for instruction in instruction_set:
|
||||
args = list(map(lambda arg: task_info[arg], instruction['arguments']))
|
||||
threading.Thread(target=getattr(device, instruction['method'])(*args))
|
||||
|
||||
delay_time += (time() - now)
|
||||
|
||||
# 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.running_task.stop()
|
||||
|
||||
# check project done then close project
|
||||
if self.check_project_done() == True:
|
||||
self.mqtt_thread.broadcast_command('project:project ' + str(self._name) + ' stop at ' + datetime.now().strftime('%Y-%m-%d %H:%M:%S.%f')[:-3])
|
||||
print('project stop at', datetime.now())
|
||||
self.close()
|
||||
|
||||
if self._time_interval - delay_time > 0:
|
||||
@@ -177,6 +188,9 @@ class Project(threading.Thread):
|
||||
return True
|
||||
|
||||
def check_running_task_not_run(self):
|
||||
# if no running task
|
||||
if self._task_manager.running_task == None:
|
||||
return False
|
||||
device_list = self._task_manager.running_task.device
|
||||
for device in device_list:
|
||||
if self._complete_device[device].status == 1:
|
||||
|
||||
@@ -2,6 +2,7 @@ from typing import List
|
||||
from .condition import Condition
|
||||
from .action import Action
|
||||
from time import time
|
||||
from datetime import datetime
|
||||
|
||||
key_list = {
|
||||
'deviceList': 'device',
|
||||
@@ -45,7 +46,7 @@ class Task:
|
||||
self.update_condition_list(value)
|
||||
if key == 'action':
|
||||
self.update_action_list(value)
|
||||
print(key, value)
|
||||
# print(key, value)
|
||||
setattr(self, key, value)
|
||||
|
||||
def update_condition_list(self, condition_dict):
|
||||
@@ -197,6 +198,7 @@ class Task:
|
||||
return '|'.join(name_list) if name_or_value == 'name' else '|'.join(value_list)
|
||||
|
||||
def run(self):
|
||||
print(self._name, 'run at', datetime.now())
|
||||
self._status = 1
|
||||
self._start_time.append(time())
|
||||
|
||||
|
||||
@@ -20,8 +20,9 @@ class TaskManager():
|
||||
|
||||
self.load_task_list(task_list)
|
||||
|
||||
self.set_running_task(self._task_list[0])
|
||||
self._running_task.run()
|
||||
# self.set_running_task(self._task_list[0])
|
||||
# self._running_task.run()
|
||||
self._next_task.append(self._task_list[0])
|
||||
|
||||
@property
|
||||
def task_list(self):
|
||||
|
||||
Reference in New Issue
Block a user