wanshenmean
2026-03-11 a8f45091019012eeafec563913dee71cda3d9790
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
using WIDESEA_Core;
using WIDESEAWCS_Common.TaskEnum;
using WIDESEAWCS_ITaskInfoService;
using WIDESEAWCS_Model.Models;
using WIDESEAWCS_Tasks.Workflow.Abstractions;
 
namespace WIDESEAWCS_Tasks.Workflow
{
    /// <summary>
    /// RobotJob Á÷³Ì±àÅÅÆ÷£ºÇ¨ÒÆÔ­ RobotJob ×´Ì¬»ú·ÖÖ§£¬½µµÍ Job ÀิÔÓ¶È¡£
    /// </summary>
    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.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);
            }
        }
    }
}