wangxinhui
2026-03-26 ad05ef2341fe19a4220f7ada1987636f9ec4a1a9
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
using Microsoft.IdentityModel.Tokens;
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;
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;
using WIDESEAWCS_TaskInfoService;
 
namespace WIDESEAWCS_Tasks
{
    public partial class AGVJob
    {
        public void SendAGVTask()
        {
            try
            {
                var newTasksOut = _taskService.Db.Queryable<Dt_Task>().Where(x => (x.TaskState == TaskStatusEnum.AGV_Execute.ObjToInt()) && x.TaskType==TaskTypeEnum.Outbound.ObjToInt() && x.DeviceCode == "AGV").ToList().OrderBy(x => x.Grade).ThenBy(x => x.TaskNum).ToList();
                var newTasksIn = _taskService.Db.Queryable<Dt_Task>().Where(x => (x.TaskState == TaskStatusEnum.AGV_Execute.ObjToInt()) && x.TaskType == TaskTypeEnum.Inbound.ObjToInt() && x.DeviceCode == "AGV").ToList().OrderBy(x => x.Grade).ThenBy(x => x.TaskNum).ToList();
                var taskDownOut = _taskService.Db.Queryable<Dt_Task>().Where(x => (x.TaskState > TaskStatusEnum.AGV_Execute.ObjToInt()) && x.TaskType == TaskTypeEnum.Outbound.ObjToInt() && x.DeviceCode == "AGV").OrderBy(x => x.TaskNum).ToList();
                #region 出库任务下发
                if (newTasksOut.Count>0)
                {
                    WriteLog.Write_Log("AGV出库任务下发", "出库任务下发接口", "添加任务", $"任务:{newTasksOut.ToJson()}");
                    foreach (var GroupTask in newTasksOut.GroupBy(x=>x.NextAddress))
                    {
                        int taskCount = 6;
                        if (GroupTask.ObjToInt() > 201)
                        {
                            taskCount = 4;
                        }
                        var tasks = GroupTask.Take(taskCount).ToList();
                        TimeSpan span = DateTime.Now - tasks.FirstOrDefault().CreateDate;
                        int taskDownCount = taskDownOut.Where(x => x.NextAddress == GroupTask.Key).Count();
                        if (taskDownCount < (GroupTask.ObjToInt() > 201 ? 4 : 6) && (tasks.Count >= 4 || (int)span.TotalSeconds >= 20))
                        {
                            try
                            {
                                AgvTaskSendDTO agvTaskSend = new AgvTaskSendDTO()
                                {
                                    MissionData = new List<MissionDataItem>()
                                };
                                string taskGroupId = Guid.NewGuid().ToString().Replace("-", "");
                                
                                foreach (var task in tasks)
                                {
                                    //获取目标点货位
                                    Dt_LocationInfo locationInfoStart = _locationInfoRepository.QueryFirst(x => x.LocationCode == task.CurrentAddress);
                                    //获取拣选出库站台
                                    Dt_StationManger stationMangerEnd = _stationMangerRepository.QueryFirst(x => x.PickStationCode == task.NextAddress);
 
                                    if (locationInfoStart == null || stationMangerEnd == null) throw new Exception($"未找到任务号${task.TaskNum}起始点{task.CurrentAddress}或目标点{task.NextAddress}位置信息");
                                    agvTaskSend.RequestId = taskGroupId;
                                    agvTaskSend.MissionCode = taskGroupId;
                                    agvTaskSend.ViewBoardType = "W01";
                                    //料箱子搬运任务
                                    MissionDataItem missionDataItem = new MissionDataItem()
                                    {
                                        Sequence = task.TaskNum,
                                        BinCode = task.PalletCode,
                                        StartPosition = locationInfoStart.AgvPoint,
                                        StartSlotCode = locationInfoStart.LocationCode,
                                        EndPosition = stationMangerEnd.StationCode,
                                        EndSlotCode = stationMangerEnd.CraneStationCode,
                                        TakeActionConfirm = false,
                                        TakeActionInform = false,
                                        PutActionConfirm = true,
                                        PutActionInform = true,
                                    };
                                    agvTaskSend.MissionData.Add(missionDataItem);
                                }
                                if (tasks.OrderByDescending(x => x.Grade).FirstOrDefault()?.Grade == 0)
                                {
                                    agvTaskSend.Priority = 99;
                                }
                                else
                                {
                                    agvTaskSend.Priority = 99 - tasks.OrderByDescending(x => x.Grade).FirstOrDefault().Grade;
                                }
                                tasks.ForEach(x =>
                                {
                                    x.GroupId = taskGroupId;
                                });
                                //发送AGV任务
                                WebResponseContent content = _taskService.AgvSendTask(agvTaskSend, APIEnum.AgvSendTask);
                                if (!content.Status)
                                    throw new Exception(content.Message);
                                tasks.ForEach(x =>
                                {
                                    x.Dispatchertime = DateTime.Now;
                                    x.TaskState = TaskStatusEnum.AGV_Executing.ObjToInt();
                                });
                                _taskService.UpdateData(tasks);
                                Thread.Sleep(500);
                            }
                            catch (Exception ex)
                            {
                                tasks.ForEach(x =>
                                {
                                    x.TaskState = TaskStatusEnum.Exception.ObjToInt();
                                    x.ExceptionMessage = ex.Message;
                                });
                                _taskService.UpdateData(tasks);
                            }
                        }
                    }
                }
                #endregion
                #region 入库任务下发
                if (newTasksIn.Count > 0)
                {
                    WriteLog.Write_Log("AGV入库任务下发", "出库任务下发接口", "添加任务", $"任务:{newTasksIn.ToJson()}");
                    foreach (var task in newTasksIn)
                    {
                        try
                        {
                            AgvTaskSendDTO agvTaskSend = new AgvTaskSendDTO()
                            {
                                MissionData = new List<MissionDataItem>()
                            };
                            //获取目标点货位
                            Dt_LocationInfo locationInfoEnd = _locationInfoRepository.QueryFirst(x => x.LocationCode == task.NextAddress);
                            //获取线体入库站台
                            Dt_StationManger stationMangerStart = _stationMangerRepository.QueryFirst(x => x.StationCode == task.CurrentAddress);
                            if (locationInfoEnd == null || stationMangerStart == null) throw new Exception($"未找到任务号${task.TaskNum}起始点{task.CurrentAddress}或目标点{task.NextAddress}位置信息");
                            agvTaskSend.RequestId = Guid.NewGuid().ToString().Replace("-", "");
                            agvTaskSend.MissionCode = task.TaskNum.ToString();
                            agvTaskSend.ViewBoardType = "W02";
                            if (task.Grade == 0)
                            {
                                agvTaskSend.Priority = 99;
                            }
                            else
                            {
                                agvTaskSend.Priority = 99 - task.Grade;
                            }
                            //料箱子搬运任务
                            MissionDataItem missionDataItem = new MissionDataItem()
                            {
                                Sequence = task.TaskNum,
                                BinCode = task.PalletCode,
                                StartPosition = stationMangerStart.StationCode,
                                StartSlotCode = stationMangerStart.CraneStationCode,
                                EndPosition = locationInfoEnd.AgvPoint,
                                EndSlotCode = locationInfoEnd.LocationCode,
                                TakeActionConfirm = true,
                                TakeActionInform = true,
                                PutActionConfirm = false,
                                PutActionInform = false,
                            };
                            agvTaskSend.MissionData.Add(missionDataItem);
                            //发送AGV任务
                            WebResponseContent content = _taskService.AgvSendTask(agvTaskSend, APIEnum.AgvSendTask);
                            if (!content.Status)
                                throw new Exception(content.Message);
                            task.Dispatchertime = DateTime.Now; task.TaskState = TaskStatusEnum.AGV_Executing.ObjToInt();
                            _taskService.UpdateData(task);
                            Thread.Sleep(500);
                        }
                        catch (Exception ex)
                        {
                            task.TaskState = TaskStatusEnum.Exception.ObjToInt();
                            task.ExceptionMessage = ex.Message;
                            _taskService.UpdateData(task);
                        }
                    }    
                }
                #endregion
            }
            catch (Exception ex)
            {
                WriteError(nameof(AGVJob), ex.Message, ex);
            }
        }
    }
}