wanshenmean
2026-03-17 737dec3c384f394fd6f9849b4480b697d1ba35d5
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
208
209
210
211
212
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.Workflow.Abstractions;
 
namespace WIDESEAWCS_Tasks
{
    /// <summary>
    /// 机器人任务处理器:负责任务获取、下发、入库任务回传及库存 DTO 构建。
    /// </summary>
    public class RobotTaskProcessor
    {
        // 通过网关访问 Socket,避免业务层直接依赖 TcpSocketServer。
        private readonly ISocketClientGateway _socketClientGateway;
        private readonly RobotStateManager _stateManager;
        private readonly IRobotTaskService _robotTaskService;
        private readonly ITaskService _taskService;
        private readonly HttpClientHelper _httpClientHelper;
 
        public RobotTaskProcessor(
            ISocketClientGateway socketClientGateway,
            RobotStateManager stateManager,
            IRobotTaskService robotTaskService,
            ITaskService taskService,
            HttpClientHelper httpClientHelper)
        {
            _socketClientGateway = socketClientGateway;
            _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>
        /// 下发取货指令(Pickbattery)到机器人客户端。
        /// </summary>
        public async Task SendSocketRobotPickAsync(Dt_RobotTask task, RobotSocketState state)
        {
            string taskString = $"Pickbattery,{task.RobotSourceAddress}";
            bool result = await _socketClientGateway.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());
        }
    }
}