136 lines
5.9 KiB
Python
136 lines
5.9 KiB
Python
|
|
#!/opt/homebrew/bin/python3
|
||
|
|
# -*- coding:utf-8 -*-
|
||
|
|
import json
|
||
|
|
from PyQt6 import *
|
||
|
|
from PyQt6.QtCore import *
|
||
|
|
from logs import log
|
||
|
|
from typing import Union
|
||
|
|
from models import Task
|
||
|
|
from taskModel.taskActuator import defaultTaskActuator
|
||
|
|
from taskModel.taskActuator import TaskActuator
|
||
|
|
from common import common
|
||
|
|
from taskModel.taskManager import taskManager
|
||
|
|
from taskInstructionModel.taskInstructionManager import taskInstructionManager
|
||
|
|
class TaskActuatorManager(QObject):
|
||
|
|
logMsg = pyqtSignal(dict)
|
||
|
|
taskStart = pyqtSignal(str)
|
||
|
|
taskStop = pyqtSignal(str)
|
||
|
|
executeFinished = pyqtSignal(str)
|
||
|
|
updateProgress = pyqtSignal(str, int, int)
|
||
|
|
updateInstructProgress = pyqtSignal(str, int, int)
|
||
|
|
updateDetails = pyqtSignal(str, int, int)
|
||
|
|
def __init__(self, parent=None):
|
||
|
|
super().__init__()
|
||
|
|
self.taskProgressInfo = {}
|
||
|
|
self.instructProgressInfo = {}
|
||
|
|
self.taskActuatorDict = {}
|
||
|
|
@pyqtSlot(str,QVariant)
|
||
|
|
def startInstruction(self, instructionId, attr):
|
||
|
|
try:
|
||
|
|
if not isinstance(attr, dict):
|
||
|
|
attr = attr.toVariant()
|
||
|
|
taskInstruction = common.getInstructionDetail(instructionId, attr)
|
||
|
|
|
||
|
|
if taskInstruction is None:
|
||
|
|
return
|
||
|
|
if taskInstruction["instructionInfo"] is None:
|
||
|
|
return
|
||
|
|
if instructionId in self.taskActuatorDict:
|
||
|
|
if not self.taskActuatorDict[instructionId].isRunning():
|
||
|
|
self.taskActuatorDict[instructionId].execute(taskInstruction)
|
||
|
|
else:
|
||
|
|
self.taskActuatorDict[instructionId] = TaskActuator(instructionId)
|
||
|
|
self.taskActuatorDict[instructionId].updateInstructProgress.connect(self.onUpdateInstructProgress)
|
||
|
|
self.taskActuatorDict[instructionId].executeFinished.connect(self.onExecuteFinished)
|
||
|
|
self.taskActuatorDict[instructionId].execute(taskInstruction)
|
||
|
|
except Exception as e:
|
||
|
|
print(e)
|
||
|
|
|
||
|
|
@pyqtSlot(str)
|
||
|
|
def stopInstruction(self, instructionId):
|
||
|
|
if instructionId in self.taskActuatorDict:
|
||
|
|
if self.taskActuatorDict[instructionId].isRunning():
|
||
|
|
self.taskActuatorDict[instructionId].stop()
|
||
|
|
|
||
|
|
@pyqtSlot(QVariant, list)
|
||
|
|
def startVirtual(self, taskInfo, taskInstructions):
|
||
|
|
try:
|
||
|
|
if not isinstance(taskInfo, dict):
|
||
|
|
taskInfo = taskInfo.toVariant()
|
||
|
|
|
||
|
|
taskId = taskInfo["id"]
|
||
|
|
if taskId in self.taskActuatorDict:
|
||
|
|
if not self.taskActuatorDict[taskId].isRunning():
|
||
|
|
common.getTaskDetails(taskInstructions)
|
||
|
|
self.taskActuatorDict[taskId].execute(taskInstructions,taskInfo)
|
||
|
|
else:
|
||
|
|
self.taskActuatorDict[taskId] = TaskActuator(taskId)
|
||
|
|
self.taskActuatorDict[taskId].executeFinished.connect(self.onExecuteFinished)
|
||
|
|
common.getTaskDetails(taskInstructions)
|
||
|
|
self.taskActuatorDict[taskId].execute(taskInstructions,taskInfo)
|
||
|
|
except Exception as e:
|
||
|
|
print(e)
|
||
|
|
@pyqtSlot(str,QVariant)
|
||
|
|
def start(self, taskId, params = None):
|
||
|
|
try:
|
||
|
|
if not isinstance(params, dict) and params is not None:
|
||
|
|
params = params.toVariant()
|
||
|
|
taskInfo = taskManager.getInfo(taskId)
|
||
|
|
taskInfo["params"] = params
|
||
|
|
if taskId in self.taskActuatorDict:
|
||
|
|
if not self.taskActuatorDict[taskId].isRunning():
|
||
|
|
taskInstructions = taskInstructionManager.getInfo(taskId)
|
||
|
|
common.getTaskDetails(taskInstructions)
|
||
|
|
self.taskActuatorDict[taskId].execute(taskInstructions,taskInfo)
|
||
|
|
self.taskStart.emit(taskId)
|
||
|
|
else:
|
||
|
|
taskInstructions = taskInstructionManager.getInfo(taskId)
|
||
|
|
self.taskActuatorDict[taskId] = TaskActuator(taskId)
|
||
|
|
self.taskActuatorDict[taskId].updateProgress.connect(self.onUpdateProgress)
|
||
|
|
self.taskActuatorDict[taskId].updateDetails.connect(self.onUpdateDetails)
|
||
|
|
common.getTaskDetails(taskInstructions)
|
||
|
|
self.taskActuatorDict[taskId].execute(taskInstructions,taskInfo)
|
||
|
|
self.taskStart.emit(taskId)
|
||
|
|
except Exception as e:
|
||
|
|
print(e)
|
||
|
|
|
||
|
|
def onExecuteFinished(self, taskId):
|
||
|
|
self.executeFinished.emit(taskId)
|
||
|
|
|
||
|
|
def onUpdateInstructProgress(self,id, value, maxValue):
|
||
|
|
if id in self.instructProgressInfo:
|
||
|
|
self.instructProgressInfo[id]["value"] = value
|
||
|
|
self.instructProgressInfo[id]["maxValue"] = maxValue
|
||
|
|
else:
|
||
|
|
self.instructProgressInfo[id] = {"value": value, "maxValue": maxValue}
|
||
|
|
self.updateInstructProgress.emit(id, value, maxValue)
|
||
|
|
|
||
|
|
def onUpdateProgress(self,taskId, value, maxValue):
|
||
|
|
if taskId in self.taskProgressInfo:
|
||
|
|
self.taskProgressInfo[taskId]["value"] = value
|
||
|
|
self.taskProgressInfo[taskId]["maxValue"] = maxValue
|
||
|
|
else:
|
||
|
|
self.taskProgressInfo[taskId] = {"value": value, "maxValue": maxValue}
|
||
|
|
self.updateProgress.emit(taskId, value, maxValue)
|
||
|
|
|
||
|
|
def onUpdateDetails(self,detailId, value, maxValue):
|
||
|
|
self.updateDetails.emit(detailId, value, maxValue)
|
||
|
|
|
||
|
|
@pyqtSlot(str)
|
||
|
|
def stop(self, taskId):
|
||
|
|
if taskId in self.taskActuatorDict:
|
||
|
|
if self.taskActuatorDict[taskId].isRunning():
|
||
|
|
self.taskActuatorDict[taskId].stop()
|
||
|
|
self.taskStop.emit(taskId)
|
||
|
|
@pyqtSlot()
|
||
|
|
def stopAll(self):
|
||
|
|
for taskId in self.taskActuatorDict:
|
||
|
|
if self.taskActuatorDict[taskId].isRunning():
|
||
|
|
self.taskActuatorDict[taskId].stop()
|
||
|
|
self.taskStop.emit(taskId)
|
||
|
|
|
||
|
|
def isRunning(self, taskId):
|
||
|
|
if taskId in self.taskActuatorDict:
|
||
|
|
return self.taskActuatorDict[taskId].isRunning()
|
||
|
|
|
||
|
|
taskActuatorManager = TaskActuatorManager()
|