wanshenmean
2026-03-06 aefdecd0aa3226b7d00d1dc764241b82658b3be8
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
using Newtonsoft.Json;
using WIDESEA_Core;
using WIDESEAWCS_Common;
using WIDESEAWCS_Common.HttpEnum;
using WIDESEAWCS_Common.TaskEnum;
using WIDESEAWCS_Core;
using WIDESEAWCS_Core.Helper;
using WIDESEAWCS_DTO.Stock;
using WIDESEAWCS_DTO.TaskInfo;
using WIDESEAWCS_ITaskInfoService;
using WIDESEAWCS_Model.Models;
using WIDESEAWCS_QuartzJob;
using WIDESEAWCS_Tasks.SocketServer;
 
namespace WIDESEAWCS_Tasks
{
    /// <summary>
    /// 机械手任务处理器 - 负责机械手任务执行和处理
    /// </summary>
    public class RobotTaskProcessor
    {
        private readonly TcpSocketServer _tcpSocket;
        private readonly RobotStateManager _stateManager;
        private readonly IRobotTaskService _robotTaskService;
        private readonly ITaskService _taskService;
        private readonly HttpClientHelper _httpClientHelper;
 
        public RobotTaskProcessor(
            TcpSocketServer tcpSocket,
            RobotStateManager stateManager,
            IRobotTaskService robotTaskService,
            ITaskService taskService,
            HttpClientHelper httpClientHelper)
        {
            _tcpSocket = tcpSocket;
            _stateManager = stateManager;
            _robotTaskService = robotTaskService;
            _taskService = taskService;
            _httpClientHelper = httpClientHelper;
        }
 
        /// <summary>
        /// 获取机械手任务
        /// </summary>
        public Dt_RobotTask? GetTask(RobotCraneDevice robotCrane)
        {
            return _robotTaskService.QueryRobotCraneTask(robotCrane.DeviceCode);
        }
 
        /// <summary>
        /// 获取机械手任务
        /// </summary>
        public bool? DeleteTask(int ID)
        {
            return _robotTaskService.Repository.DeleteDataById(ID);
        }
 
        /// <summary>
        /// 发送机械手取货命令
        /// </summary>
        public async Task SendSocketRobotPickAsync(Dt_RobotTask task, RobotSocketState state)
        {
            string taskString = $"Pickbattery,{task.RobotSourceAddress}";
            // 发送任务指令
            bool result = await _tcpSocket.SendToClientAsync(state.IPAddress, taskString);
            if (result)
            {
                task.RobotTaskState = TaskRobotStatusEnum.RobotExecuting.GetHashCode();
                state.CurrentTask = task;
 
                // 更新缓存中的状态(使用安全更新防止并发覆盖)
                if (_stateManager.TryUpdateStateSafely(state.IPAddress, state))
                    await _robotTaskService.UpdateRobotTaskAsync(task);
            }
        }
 
        /// <summary>
        /// 处理入库任务
        /// </summary>
        public async Task<bool> HandleInboundTaskAsync(RobotSocketState state, bool useSourceAddress)
        {
            var currentTask = state.CurrentTask;
            if (currentTask == null)
            {
                return false;
            }
 
            string roadway = currentTask.RobotSourceAddressLineCode;
            int warehouseId = currentTask.RobotRoadway == "ZYRB1" ? 1 : currentTask.RobotRoadway == "HPRB001" ? 2 : 3;
 
            int taskType = 0;
            string SourceAddress = currentTask.RobotTargetAddressLineCode;
            string TargetAddress = currentTask.RobotSourceAddressLineCode;
            string PalletCode = string.Empty;
            // 直接转换为枚举类型进行比较
            var robotTaskType = (RobotTaskTypeEnum)currentTask.RobotTaskType;
 
            if (useSourceAddress)
            {
                switch (robotTaskType)
                {
                    case RobotTaskTypeEnum.GroupPallet:
                        return false;
 
                    case RobotTaskTypeEnum.ChangePallet:
                    case RobotTaskTypeEnum.SplitPallet:
                        taskType = TaskTypeEnum.InEmpty.GetHashCode();
                        PalletCode = currentTask.RobotSourceAddressPalletCode;
                        break;
                }
            }
            else
            {
                switch (robotTaskType)
                {
                    case RobotTaskTypeEnum.ChangePallet:
                    case RobotTaskTypeEnum.GroupPallet:
                        taskType = TaskTypeEnum.Inbound.GetHashCode();
                        PalletCode = currentTask.RobotTargetAddressPalletCode;
                        break;
 
                    case RobotTaskTypeEnum.SplitPallet:
                        return true;
                }
            }
 
            CreateTaskDto taskDto = new CreateTaskDto
            {
                PalletCode = PalletCode,
                SourceAddress = SourceAddress ?? string.Empty,
                TargetAddress = TargetAddress ?? string.Empty,
                Roadway = roadway,
                WarehouseId = warehouseId,
                PalletType = 1,
                TaskType = taskType
            };
            var result = _httpClientHelper.Post<WebResponseContent>(nameof(ConfigKey.CreateTaskInboundAsync), taskDto.ToJson());
            if (!result.Data.Status && result.IsSuccess)
            {
                return false;
            }
 
            WMSTaskDTO taskDTO = JsonConvert.DeserializeObject<WMSTaskDTO>(result.Data.Data.ToJson() ?? string.Empty) ?? new WMSTaskDTO();
            var content = _taskService.ReceiveWMSTask(new List<WMSTaskDTO> { taskDTO });
            if (!content.Status) return false;
 
            var taskInfo = JsonConvert.DeserializeObject<Dt_Task>(content.Data.ToJson() ?? string.Empty) ?? new Dt_Task();
 
            string sourceAddress = taskDTO.SourceAddress;
 
            IDevice? device = Storage.Devices.FirstOrDefault(x => x.DeviceProDTOs.Any(d => d.DeviceChildCode == sourceAddress));
            if (device != null)
            {
                CommonConveyorLine conveyorLine = (CommonConveyorLine)device;
                conveyorLine.SetValue(ConveyorLineDBNameNew.Target, taskInfo.NextAddress, sourceAddress);
                conveyorLine.SetValue(ConveyorLineDBNameNew.TaskNo, taskInfo.TaskNum, sourceAddress);
                conveyorLine.SetValue(ConveyorLineDBNameNew.WCS_STB, 1, sourceAddress);
 
                if (_taskService.UpdateTaskStatusToNext(taskInfo).Status)
                {
                    return true;
                }
            }
            return false;
        }
 
        /// <summary>
        /// 构建库存DTO
        /// </summary>
        public static StockDTO BuildStockDTO(RobotSocketState state, int[] positions)
        {
            return new StockDTO
            {
                SourceLineNo = state.CurrentTask.RobotSourceAddressLineCode,
                SourcePalletNo = state.CurrentTask.RobotSourceAddressPalletCode,
                TargetPalletNo = state.CurrentTask.RobotTargetAddressPalletCode,
                TargetLineNo = state.CurrentTask.RobotTargetAddressLineCode,
                Details = positions
                    .Where(x => x > 0)
                    .OrderBy(x => x)
                    .Select((x, idx) => new StockDetailDTO
                    {
                        Quantity = state.RobotTaskTotalNum > 0 ? state.RobotTaskTotalNum + positions.Length : positions.Length,
                        Channel = x,
                        CellBarcode = state.CellBarcode?.Count > 0 ? state.CellBarcode[x - 1] : ""
                    })
                    .ToList()
            };
        }
 
        /// <summary>
        /// 调用拆盘API
        /// </summary>
        public HttpResponseResult<WebResponseContent> PostSplitPalletAsync(StockDTO stockDTO)
        {
            return _httpClientHelper.Post<WebResponseContent>(nameof(ConfigKey.SplitPalletAsync), stockDTO.ToJson());
        }
 
        /// <summary>
        /// 调用组盘或换盘API
        /// </summary>
        public HttpResponseResult<WebResponseContent> PostGroupPalletAsync(string configKey, StockDTO stockDTO)
        {
            return _httpClientHelper.Post<WebResponseContent>(configKey, stockDTO.ToJson());
        }
    }
}