2022-02-07 20:27:30 +08:00
|
|
|
from .task import Task
|
|
|
|
|
from .task_manager import TaskManager
|
|
|
|
|
import sys
|
|
|
|
|
import json
|
|
|
|
|
import threading
|
|
|
|
|
import time
|
|
|
|
|
|
|
|
|
|
class Project(threading.Thread):
|
|
|
|
|
def __init__(self, project, mqttThread = None, name="project"):
|
|
|
|
|
super(Project, self).__init__(name = name)
|
|
|
|
|
self._project = project
|
|
|
|
|
self._mqtt_thread = mqttThread
|
|
|
|
|
self._time_interval = 1
|
2022-02-08 20:12:44 +08:00
|
|
|
|
|
|
|
|
self._task_manager = TaskManager(self._project['taskObj'])
|
2022-02-07 20:27:30 +08:00
|
|
|
|
|
|
|
|
@property
|
|
|
|
|
def desc(self) -> str:
|
|
|
|
|
return self.project['desc']
|
2022-02-08 20:12:44 +08:00
|
|
|
|
|
|
|
|
@property
|
|
|
|
|
def status(self) -> str:
|
|
|
|
|
return self.project['status']
|
|
|
|
|
|
|
|
|
|
@property
|
|
|
|
|
def device(self) -> list:
|
|
|
|
|
return self.project['deviceList']
|
2022-02-07 20:27:30 +08:00
|
|
|
|
|
|
|
|
@property
|
2022-02-08 20:12:44 +08:00
|
|
|
def task(self) -> list:
|
2022-02-07 20:27:30 +08:00
|
|
|
return self._task_manager.get()
|
|
|
|
|
|
|
|
|
|
@property
|
2022-02-08 20:12:44 +08:00
|
|
|
def get_current_task(self) -> str:
|
|
|
|
|
return self._task_manager.get_current_task()
|
|
|
|
|
|
|
|
|
|
def get_instructions_match_condition(self) -> list:
|
|
|
|
|
return
|
2022-02-07 20:27:30 +08:00
|
|
|
|
2022-02-08 20:12:44 +08:00
|
|
|
def stop(self):
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
def close(self):
|
|
|
|
|
return
|
|
|
|
|
|
2022-02-07 20:27:30 +08:00
|
|
|
def run(self):
|
|
|
|
|
# each task condition
|
|
|
|
|
while True:
|
|
|
|
|
print('run')
|
|
|
|
|
time.sleep( self._time_interval)
|
|
|
|
|
# for task in self.tasks:
|
|
|
|
|
# print('task', task.name)
|
|
|
|
|
# check time
|
|
|
|
|
# match_condition_tuple = task.checkTimeCondition()
|
|
|
|
|
# match_conditions = task.conditions_meet()
|
|
|
|
|
|
|
|
|
|
# if len(match_conditions) > 0:
|
|
|
|
|
# for match_condition in match_conditions:
|
|
|
|
|
# task_id, condition_id = match_condition
|
|
|
|
|
# instructionList = self.doAction(task_id, condition_id)
|
|
|
|
|
# print(instructionList)
|
|
|
|
|
# for instruction in instructionList:
|
|
|
|
|
# self._mqtt_thread.publish(json.dumps(instruction))
|
|
|
|
|
# sys.stdout.flush()
|
|
|
|
|
# check device
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
# def doAction(self, task_id, condition_id) -> list:
|
|
|
|
|
# task = self.get_task(task_id)
|
|
|
|
|
# instruction_list = []
|
|
|
|
|
|
|
|
|
|
# for match_action_id in task.match_condition_actions(condition_id):
|
|
|
|
|
# print('match_action_id', match_action_id)
|
|
|
|
|
# # TODO check device alive or not
|
|
|
|
|
# action_content = task.actions[match_action_id]['content']
|
|
|
|
|
# # print(task.actions[action_id]['content'])
|
|
|
|
|
# print(action_content)
|
|
|
|
|
# for device in action_content:
|
|
|
|
|
# if int(device) == -1:
|
|
|
|
|
# print(action_content[device])
|
|
|
|
|
# task.status_mapping(action_content[device])()
|
|
|
|
|
# else:
|
|
|
|
|
# for step in action_content[device]:
|
|
|
|
|
# instruction_list.append(self.buildInstructions(task_id, step))
|
|
|
|
|
|
|
|
|
|
# return instruction_list
|
2022-02-08 20:12:44 +08:00
|
|
|
|
2022-02-07 20:27:30 +08:00
|
|
|
# def buildInstructions(self, task_id, params) -> dict:
|
|
|
|
|
# # instruction = {
|
|
|
|
|
# # 'header': targetTask.instructions[str(ins_type)]['inst'] + '/0',
|
|
|
|
|
# # 'device': device,
|
|
|
|
|
# # 'instruction': value
|
|
|
|
|
# # }
|
|
|
|
|
# ins_type = params[0]
|
|
|
|
|
# ins_params = params[1:]
|
|
|
|
|
# ins_message = {}
|
|
|
|
|
# separator = '|'
|
|
|
|
|
|
|
|
|
|
# targetTask = self.get_task(task_id)
|
|
|
|
|
# ins_message['header'] = targetTask.instructions[str(ins_type)]['inst'] + '/0'
|
|
|
|
|
# for x in targetTask.instructions[str(ins_type)]['param'].items():
|
|
|
|
|
# # print(targetTask.instructions[str(ins_type)])
|
|
|
|
|
# if x[1] == 'device':
|
|
|
|
|
# # print('device', int(targetTask.mapping_device_id(ins_params[int(x[0])])))
|
|
|
|
|
# ins_message[x[1]] = int(ins_params[int(x[0])])
|
|
|
|
|
# elif targetTask.instructions[str(ins_type)]['inst'] == 'device_parameter':
|
|
|
|
|
# if x[1] == 'parameter':
|
|
|
|
|
# # print(ins_params[int(x[0])])
|
|
|
|
|
# # print(targetTask.parameterSets[str(ins_params[int(x[0])])])
|
|
|
|
|
# ins_message[x[1]] = separator.join(targetTask.parameter_sets[str(ins_params[int(x[0])])]['recording'].keys())
|
|
|
|
|
# elif x[1] == 'content':
|
|
|
|
|
# # print(targetTask.parameterSets[str(ins_params[int(x[0]) - 1])].values())
|
|
|
|
|
# ins_message[x[1]] = separator.join(map(str,targetTask.parameter_sets[str(ins_params[int(x[0]) - 1])]['recording'].values()))
|
|
|
|
|
# else:
|
|
|
|
|
# ins_message[x[1]] = ins_params[int(x[0])]
|
|
|
|
|
# print('instruction', ins_message)
|
|
|
|
|
# return ins_message
|