using WIDESEA_Core; using WIDESEAWCS_Common.TaskEnum; using WIDESEAWCS_ITaskInfoService; using WIDESEAWCS_Model.Models; using WIDESEAWCS_Tasks.Workflow.Abstractions; namespace WIDESEAWCS_Tasks.Workflow { /// /// RobotJob Á÷³Ì±àÅÅÆ÷£ºÇ¨ÒÆÔ­ RobotJob ״̬»ú·ÖÖ§£¬½µµÍ Job ÀิÔÓ¶È¡£ /// public class RobotWorkflowOrchestrator : IRobotWorkflowOrchestrator { private readonly RobotStateManager _stateManager; private readonly RobotClientManager _clientManager; private readonly RobotTaskProcessor _taskProcessor; private readonly IRobotTaskService _robotTaskService; public RobotWorkflowOrchestrator( RobotStateManager stateManager, RobotClientManager clientManager, RobotTaskProcessor taskProcessor, IRobotTaskService robotTaskService) { _stateManager = stateManager; _clientManager = clientManager; _taskProcessor = taskProcessor; _robotTaskService = robotTaskService; } public async Task ExecuteAsync(RobotSocketState latestState, Dt_RobotTask task, string ipAddress) { // ±£³ÖÔ­ÓзÖÖ§Åж¨Ìõ¼þ²»±ä£¬È·±£ÐÐΪһÖ¡£ if (latestState.RobotRunMode == 2 && latestState.RobotControlMode == 1 && latestState.OperStatus != "Running") { if ((latestState.CurrentAction == "PickFinished" || latestState.CurrentAction == "AllPickFinished") && latestState.RobotArmObject == 1 && task.RobotTaskState == TaskRobotStatusEnum.RobotPickFinish.GetHashCode()) { await HandlePickFinishedStateAsync(task, ipAddress); } else if ((latestState.CurrentAction == "PutFinished" || latestState.CurrentAction == "AllPutFinished" || latestState.CurrentAction == null) && latestState.OperStatus == "Homed" && latestState.RobotArmObject == 0 && (task.RobotTaskState == TaskRobotStatusEnum.RobotPutFinish.GetHashCode() || task.RobotTaskState != TaskRobotStatusEnum.RobotExecuting.GetHashCode())) { await HandlePutFinishedStateAsync(task, ipAddress); } } } private async Task HandlePickFinishedStateAsync(Dt_RobotTask task, string ipAddress) { string taskString = $"Putbattery,{task.RobotTargetAddress}"; bool result = await _clientManager.SendToClientAsync(ipAddress, taskString); if (result) { task.RobotTaskState = TaskRobotStatusEnum.RobotExecuting.GetHashCode(); var stateToUpdate = _stateManager.GetState(ipAddress); if (stateToUpdate != null) { stateToUpdate.CurrentTask = task; if (_stateManager.TryUpdateStateSafely(ipAddress, stateToUpdate)) { await _robotTaskService.UpdateRobotTaskAsync(task); } } } } private async Task HandlePutFinishedStateAsync(Dt_RobotTask task, string ipAddress) { var stateForUpdate = _stateManager.GetState(ipAddress); if (stateForUpdate == null) { return; } if (!stateForUpdate.IsSplitPallet && !stateForUpdate.IsGroupPallet) { stateForUpdate.IsSplitPallet = task.RobotTaskType == RobotTaskTypeEnum.SplitPallet.GetHashCode(); stateForUpdate.IsGroupPallet = task.RobotTaskType == RobotTaskTypeEnum.GroupPallet.GetHashCode() || task.RobotTaskType == RobotTaskTypeEnum.ChangePallet.GetHashCode(); } if (task.RobotTaskType == RobotTaskTypeEnum.GroupPallet.GetHashCode()) { const string prefix = "TRAY"; string trayBarcode1 = RobotBarcodeGenerator.GenerateTrayBarcode(prefix); string trayBarcode2 = RobotBarcodeGenerator.GenerateTrayBarcode(prefix); if (!string.IsNullOrEmpty(trayBarcode1) && !string.IsNullOrEmpty(trayBarcode2)) { stateForUpdate.CellBarcode.Add(trayBarcode1); stateForUpdate.CellBarcode.Add(trayBarcode2); await _taskProcessor.SendSocketRobotPickAsync(task, stateForUpdate); } } else { await _taskProcessor.SendSocketRobotPickAsync(task, stateForUpdate); } } } }