wangxinhui
2025-10-14 0705cb6170a9ba77ba48bbb6dcebb9cf3d73cbea
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
using OfficeOpenXml.FormulaParsing.Excel.Functions.RefAndLookup;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WIDESEA_DTO.Agv;
using WIDESEAWCS_Common.APIEnum;
using WIDESEAWCS_Common.TaskEnum;
using WIDESEAWCS_Core;
using WIDESEAWCS_Core.Helper;
using WIDESEAWCS_DTO.Agv;
using WIDESEAWCS_Model.Models;
using WIDESEAWCS_QuartzJob;
 
namespace WIDESEAWCS_Tasks
{
    public partial class AGV_CPJob
    {
        public void SendAGVTask()
        {
            try
            {
                var AllTasks = _taskService.Db.Queryable<Dt_Task>().Where(x => (nameof(AGV_CPJob).Contains(x.DeviceCode))).ToList();
                var stationMangers = _stationMangerRepository.QueryData();
                #region 入库任务推送
                {
                    var newTasksIn = AllTasks.Where(x => (x.TaskState == TaskStatusEnum.AGV_Execute.ObjToInt()) && x.TaskType>=TaskTypeEnum.Inbound.ObjToInt() && nameof(AGV_CPJob).Contains(x.DeviceCode) && !string.IsNullOrEmpty(x.DeviceCode) && x.TaskType!=999).ToList().OrderByDescending(x => x.Grade).ThenBy(x => x.CreateDate).ToList();
                    if (newTasksIn.Count>0)
                    {
                        SendAgvTasks(newTasksIn);
                    }
                }
                #endregion
                DateTime _taskStartTime = DateTime.Today;
                #region 出库推送任务1线
                {
                    //按时间查找
                    var timeTasksOut1 = AllTasks.Where(x => (x.TaskState == TaskStatusEnum.New.ObjToInt()) && x.TaskType < TaskTypeEnum.Inbound.ObjToInt() && x.NextAddress == "5236" && x.CreateDate<= _taskStartTime).OrderByDescending(x => x.Grade).ThenBy(x => x.CreateDate).Take(5).ToList();
 
                    //新创建任务
                    var newTasksOut1 = AllTasks.Where(x => (x.TaskState == TaskStatusEnum.New.ObjToInt()) && x.TaskType < TaskTypeEnum.Inbound.ObjToInt() && x.NextAddress=="5236").OrderByDescending(x => x.Grade).ThenBy(x => x.CreateDate).Take(5).ToList();
 
                    var pushTasks1= timeTasksOut1.Count<=0 ? newTasksOut1 : timeTasksOut1;
                    //当前线体任务
                    var downTasksOut1 = AllTasks.Where(x => (x.TaskState > TaskStatusEnum.New.ObjToInt()) && x.TaskType < TaskTypeEnum.Inbound.ObjToInt() && x.NextAddress == "5236").ToList();
 
                    Dt_StationManger? stationManger = stationMangers.FirstOrDefault(x => x.AGVStationCode == "5236");
                    IDevice? device = Storage.Devices.FirstOrDefault(x => x.DeviceCode == stationManger.StationDeviceCode);
                    if (device != null)
                    {
                        OtherDevice otherDevice = (OtherDevice)device;
                        short stationCurrentStatus = device.Communicator.Read<short>("DB29.112");
                        if (pushTasks1.Count > 0 && downTasksOut1.Count <= 0 && stationCurrentStatus == 1)
                        {
                            SendAgvTasks(pushTasks1);
                            WriteInfo("5236", $"1线5236无任务,状态{stationCurrentStatus}可下发,下发任务:{pushTasks1.Select(x => x.TaskNum)}");
                        }
                    }
                    
                }
                #endregion
                #region 出库推送任务2线
                {
                    //按时间查找
                    var timeTasksOut2 = AllTasks.Where(x => (x.TaskState == TaskStatusEnum.New.ObjToInt()) && x.TaskType < TaskTypeEnum.Inbound.ObjToInt() && x.NextAddress == "5243" && x.CreateDate <= _taskStartTime).OrderByDescending(x => x.Grade).ThenBy(x => x.CreateDate).Take(5).ToList();
 
                    //新创建任务
                    var newTasksOut2 = AllTasks.Where(x => (x.TaskState == TaskStatusEnum.New.ObjToInt()) && x.TaskType < TaskTypeEnum.Inbound.ObjToInt() && x.NextAddress == "5243").OrderByDescending(x => x.Grade).ThenBy(x => x.CreateDate).Take(5).ToList();
 
                    var pushTasks2 = timeTasksOut2.Count <= 0 ? newTasksOut2 : timeTasksOut2;
 
                    //当前线体任务
                    var downTasksOut2 = AllTasks.Where(x => (x.TaskState > TaskStatusEnum.New.ObjToInt()) && x.TaskType < TaskTypeEnum.Inbound.ObjToInt() && x.NextAddress == "5243").ToList();
 
                    Dt_StationManger? stationManger = stationMangers.FirstOrDefault(x => x.AGVStationCode == "5243");
                    IDevice? device = Storage.Devices.FirstOrDefault(x => x.DeviceCode == stationManger.StationDeviceCode);
                    if (device != null)
                    {
                        OtherDevice otherDevice = (OtherDevice)device;
                        short stationCurrentStatus = device.Communicator.Read<short>("DB29.126");
                        if (pushTasks2.Count > 0 && downTasksOut2.Count <= 0 && stationCurrentStatus == 1)
                        {
                            SendAgvTasks(pushTasks2);
                            WriteInfo("5243", $"2线5243无任务,状态{stationCurrentStatus}可下发,下发任务:{pushTasks2.Select(x => x.TaskNum)}");
                        }
                    }
 
                }
                #endregion
                #region 出库推送任务3线
                {
                    //按时间查找
                    var timeTasksOut3 = AllTasks.Where(x => (x.TaskState == TaskStatusEnum.New.ObjToInt()) && x.TaskType < TaskTypeEnum.Inbound.ObjToInt() && x.NextAddress == "5250" && x.CreateDate <= _taskStartTime).OrderByDescending(x => x.Grade).ThenBy(x => x.CreateDate).Take(5).ToList();
 
                    //新创建任务
                    var newTasksOut3 = AllTasks.Where(x => (x.TaskState == TaskStatusEnum.New.ObjToInt()) && x.TaskType < TaskTypeEnum.Inbound.ObjToInt() && x.NextAddress == "5250").OrderByDescending(x => x.Grade).ThenBy(x => x.CreateDate).Take(5).ToList();
 
                    var pushTasks3 = timeTasksOut3.Count <= 0 ? newTasksOut3 : timeTasksOut3;
 
                    //当前线体任务
                    var downTasksOut3 = AllTasks.Where(x => (x.TaskState > TaskStatusEnum.New.ObjToInt()) && x.TaskType < TaskTypeEnum.Inbound.ObjToInt() && x.NextAddress == "5250").ToList();
 
                    Dt_StationManger? stationManger = stationMangers.FirstOrDefault(x => x.AGVStationCode == "5250");
                    IDevice? device = Storage.Devices.FirstOrDefault(x => x.DeviceCode == stationManger.StationDeviceCode);
                    if (device != null)
                    {
                        OtherDevice otherDevice = (OtherDevice)device;
                        short stationCurrentStatus = device.Communicator.Read<short>("DB29.140");
                        if (pushTasks3.Count > 0 && downTasksOut3.Count <= 0 && stationCurrentStatus == 1)
                        {
                            SendAgvTasks(pushTasks3);
                            WriteInfo("5250", $"3线5250无任务,状态{stationCurrentStatus}可下发,下发任务:{pushTasks3.Select(x => x.TaskNum)}");
                        }
                    }
 
                }
                #endregion
                #region 空框搬运
                {
                    var newTasksLocations = _taskService.Db.Queryable<Dt_Task>().Where(x => x.TaskState == TaskStatusEnum.New.ObjToInt() && nameof(AGV_CPJob).Contains(x.DeviceCode) && !string.IsNullOrEmpty(x.DeviceCode) && x.Remark == "测试空框").ToList().OrderBy(x => x.Grade).ThenBy(x => x.CreateDate).ToList();
                    if (newTasksLocations.Count>0)
                    {
                        foreach (var task in newTasksLocations)
                        {
                            try
                            {
                                string CurrentAddress = GetAGVAddress(task.CurrentAddress);
                                string NextAddress = GetAGVAddress(task.NextAddress);
                                AgvTaskDTO taskDTO = new AgvTaskDTO()
                                {
                                    TaskCode = task.AgvTaskNum,
                                    ReqCode = DateTime.Now.ToString("yyMMdd") + task.AgvTaskNum,
                                    TaskTyp = "CP",
                                    ctnrCode = task.PalletCode,
                                    PositionCodePath = new List<CodePath>()
                                    {
                                        new CodePath()
                                        {
                                            type="05",
                                            positionCode=CurrentAddress
                                        },
                                        new CodePath()
                                        {
                                            type="05",
                                            positionCode=NextAddress
                                        }
                                    },
                                };
                                //发送AGV任务
                                WebResponseContent content = _taskService.AgvSendTask(taskDTO, APIEnum.Agv_CPSendTask);
                                if (!content.Status)
                                    throw new Exception(content.Message);
                                task.TaskState = TaskStatusEnum.AGV_Takeing.ObjToInt();
                                task.CurrentAddress = CurrentAddress;
                                task.NextAddress = NextAddress;
                                _taskService.UpdateTask(task, TaskStatusEnum.AGV_Executing);
                            }
                            catch (Exception ex)
                            {
                                task.TaskState = TaskStatusEnum.Exception.ObjToInt();
                                task.ExceptionMessage = ex.Message;
                            }
                        }
                    }
                }
                #endregion
            }
            catch (Exception ex)
            {
                WriteError(nameof(AGV_CPJob), ex.Message, ex);
            }
        }
        /// <summary>
        /// AGV取放货回调
        /// </summary>
        public void ContinueAGVTask()
        {
            try
            {
                //获取是否有安全申请中的任务
                var continueTasks = _taskService.Db.Queryable<Dt_Task>().Where(x => (x.TaskState == TaskStatusEnum.AGV_WaitToExecute.ObjToInt()) && nameof(AGV_CPJob).Contains(x.DeviceCode) && !string.IsNullOrEmpty(x.DeviceCode)).ToList().OrderBy(x => x.Grade).ThenBy(x => x.CreateDate).ToList();
                foreach (var continueTask in continueTasks)
                {
                    //取货回调
                    if (continueTask.TaskType.GetTaskTypeGroup() == TaskTypeGroup.InboundGroup)
                    {
                        Dt_StationManger stationManger = _stationMangerRepository.QueryFirst(x => x.AGVStationCode == continueTask.CurrentAddress || x.StationCode == continueTask.CurrentAddress);
                        if (stationManger == null)
                        {
                            continue;
                        }
                        IDevice? device = Storage.Devices.FirstOrDefault(x => x.DeviceCode == stationManger.StationDeviceCode);
                        if (device == null)
                        {
                            continue;
                        }
                        OtherDevice otherDevice = (OtherDevice)device;
                        short canTake = otherDevice.GetValue<GroundStationDBName, short>(GroundStationDBName.R_IsCanTake, stationManger.StationCode);
                        if (canTake != 1)
                        {
                            continue;
                        }
                    }
                    else//放货回调
                    {
                        Dt_StationManger stationManger = _stationMangerRepository.QueryFirst(x => x.AGVStationCode == continueTask.NextAddress);
                        if (stationManger == null)
                        {
                            continue;
                        }
                        IDevice? device = Storage.Devices.FirstOrDefault(x => x.DeviceCode == stationManger.StationDeviceCode);
                        if (device == null)
                        {
                            continue;
                        }
                        OtherDevice otherDevice = (OtherDevice)device;
                        short canPut = otherDevice.GetValue<GroundStationDBName, short>(GroundStationDBName.R_IsCanPut, stationManger.StationCode);
                        if (canPut != 1)
                        {
                            continue;
                        }
                    }
                    //获取调入参数
                    AGVBoxApplyPassDTO boxApplyPassDTO = new AGVBoxApplyPassDTO()
                    {
                        ReqCode = Guid.NewGuid().ToString().Replace("-", ""),
                        ReqTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
                        TaskCode = continueTask.AgvTaskNum
                    };
                    if (continueTask.TaskType.GetTaskTypeGroup() == TaskTypeGroup.InboundGroup)
                    {
                        boxApplyPassDTO.Type = "1";
                    }
                    else
                    {
                        boxApplyPassDTO.Type = "2";
                    }
                    //请求料箱回调接口
                    WebResponseContent content = _taskService.AgvBoxApplyPass(boxApplyPassDTO);
                    if (content.Status && continueTask.TaskType.GetTaskTypeGroup() == TaskTypeGroup.InboundGroup && boxApplyPassDTO.TaskCode == continueTask.AgvTaskNum)
                    {
                        _taskService.UpdateTask(continueTask, TaskStatusEnum.AGV_Executing);
                    }
                    else if (content.Status && continueTask.TaskType.GetTaskTypeGroup() == TaskTypeGroup.OutbondGroup && boxApplyPassDTO.TaskCode== continueTask.AgvTaskNum)
                    {
                        _taskService.UpdateTask(continueTask, TaskStatusEnum.AGV_Puting);
                    }
                    else
                    {
                        continueTask.ExceptionMessage = content.Message;
                        _taskService.UpdateTask(continueTask, TaskStatusEnum.Exception);
                    }
                }
                    
            }
            catch (Exception ex)
            {
                WriteError(nameof(AGV_CPJob), ex.Message, ex);
            }
        }
        /// <summary>
        /// 下发AGV任务
        /// </summary>
        /// <param name="tasks"></param>
        public void SendAgvTasks(List<Dt_Task> tasks)
        {
            foreach (var task in tasks)
            {
                try
                {
                    if (task.TaskType.GetTaskTypeGroup() == TaskTypeGroup.OutbondGroup)
                        task.CurrentAddress = GetAGVAddress(task.CurrentAddress);
                    else
                        task.NextAddress = GetAGVAddress(task.NextAddress);
                    AgvTaskDTO taskDTO = new AgvTaskDTO()
                    {
                        TaskCode = task.AgvTaskNum,
                        ReqCode = DateTime.Now.ToString("yyMMdd") + task.AgvTaskNum,
                        TaskTyp = task.TaskType < TaskTypeEnum.Inbound.ObjToInt() ? "CPC" : "CPR",
                        ctnrCode = task.PalletCode,
                        PositionCodePath = new List<CodePath>()
                                    {
                                        new CodePath()
                                        {
                                            type = "05",
                                            positionCode = task.CurrentAddress
                                        },
                                        new CodePath()
                                        {
                                            type = "05",
                                            positionCode = task.NextAddress
                                        }
                                    }
                    };
                    if (taskDTO.TaskTyp == "CPC")
                    {
                        taskDTO.Priority = task.Grade.ToString();
                    }
                    if (task.GroupId.IsNotEmptyOrNull())
                    {
                        taskDTO.GroupId = task.GroupId;
                    }
                    if (task.TaskType == TaskTypeEnum.OutMesRworkProduct.ObjToInt())
                    {
                        taskDTO.Priority = "127";
                    }
                    //发送AGV任务
                    WebResponseContent content = _taskService.AgvSendTask(taskDTO, APIEnum.Agv_CPSendTask);
                    if (!content.Status)
                        throw new Exception(content.Message);
                    if (taskDTO.TaskTyp == "CPC")
                    {
                        task.Dispatchertime = DateTime.Now;
                        _taskService.UpdateTask(task, TaskStatusEnum.AGV_Executing);
                    }
                    else
                    {
                        task.Dispatchertime = DateTime.Now;
                        _taskService.UpdateTask(task, TaskStatusEnum.AGV_Takeing);
                    }
                }
                catch (Exception ex)
                {
                    task.TaskState = TaskStatusEnum.Exception.ObjToInt();
                    task.ExceptionMessage = ex.Message;
                    _taskService.UpdateTask(task, TaskStatusEnum.Exception);
                }
            }
        }
        /// <summary>
        /// 获取AGV地址
        /// </summary>
        /// <param name="Address"></param>
        /// <returns></returns>
        public static string GetAGVAddress(string Address)
        {
            string[] targetCodes = Address.Split("-");
            if (targetCodes.Length == 5)
            {
                var Row = Convert.ToInt16(targetCodes[1]);
                var Column = Convert.ToInt16(targetCodes[2]);
                var Layer = Convert.ToInt16(targetCodes[3]);
                var a = Row switch
                {
                    1 => "A",
                    2 => "B",
                    3 => "C",
                    4 => "D",
                    5 => "E",
                    _ => throw new Exception($"未定义的排,地址:【{Address}】"),
                };
                var b = Layer > 9 ? "" + Layer : "0" + Layer;
                var c = Column > 9 ? "" + Column : "0" + Column;
                if (Column == 10) c = "010";
                Address = a + b + c;
            }
            else
            {
                throw new Exception($"地址有误,地址:【{Address}】");
            }
            return Address;
        }
    }
}