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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection.Metadata;
using System.Text;
using System.Threading.Tasks;
using WIDESEA_DTO.Agv;
using WIDESEAWCS_Common.TaskEnum;
using WIDESEAWCS_Core;
using WIDESEAWCS_Core.Helper;
using WIDESEAWCS_Model.Models;
using static Dm.net.buffer.ByteArrayBuffer;
using WIDESEAWCS_QuartzJob;
using WIDESEAWCS_DTO.Agv;
 
namespace WIDESEAWCS_Tasks
{
    public partial class AGV_CSJJob
    {
        /// <summary>
        /// 下发AGV任务
        /// </summary>
        public void SendAGVTask()
        {
            try
            {
                var newTasks = _taskService.Db.Queryable<Dt_Task>().Where(x => x.TaskState == TaskStatusEnum.AGV_Execute.ObjToInt() && nameof(AGV_CSJJob).Contains(x.DeviceCode)).ToList().OrderBy(x => x.Grade).ThenBy(x => x.CreateDate).ToList();
                foreach (var agvTask in newTasks)
                {
                    try
                    {
                        AgvTaskDTO taskDTO = new AgvTaskDTO()
                        {
                            ReqCode = Guid.NewGuid().ToString().Replace("-", ""),
                            TaskTyp = AgvTaskType(agvTask.TaskType, agvTask.DeviceCode),
                            PositionCodePath = new List<CodePath>()
                        {
                            new CodePath()
                            {
                                type="00",
                                positionCode=agvTask.CurrentAddress
                            },
                            new CodePath()
                            {
                                type="00",
                                positionCode=agvTask.NextAddress
                            }
                        },
                            TaskCode = agvTask.AgvTaskNum,
                            PodTyp = agvTask.PalletType < 3 ? "ZC" : "DX",
                        };
                        WebResponseContent content = _taskService.AgvSendTask(taskDTO);
                        if (content.Status)
                        {
                            agvTask.TaskState = TaskStatusEnum.AGV_Executing.ObjToInt();
                            //agvTask.Remark = content.Data.ObjToString();
                            _taskService.UpdateTask(agvTask, TaskStatusEnum.AGV_Executing);
                        }
                        else
                        {
                            agvTask.TaskState = TaskStatusEnum.Exception.ObjToInt();
                            //agvTask.Remark = content.Data.ObjToString();
                            agvTask.ExceptionMessage = content.Message;
                        }
                    }
                    catch (Exception ex)
                    {
                        agvTask.TaskState = TaskStatusEnum.Exception.ObjToInt();
                        //agvTask.Remark = content.Data.ObjToString();
                        agvTask.ExceptionMessage = ex.Message;
                        WriteError(nameof(AGV_CSJJob), ex.Message, ex);
                    }
                }
                _taskService.UpdateData(newTasks);
                //出库绑定查询
                Dt_Task outBound = _taskService.Db.Queryable<Dt_Task>().Where(x => x.TaskState == TaskStatusEnum.Exception.ObjToInt() && nameof(AGV_CSJJob).Contains(x.DeviceCode) && x.TaskType==TaskTypeEnum.Outbound.ObjToInt()).ToList().OrderBy(x => x.Grade).ThenBy(x=>x.CreateDate).First();
                if (outBound != null)
                {
                    AgvPodBerthAndMatDTO andMatDTO = new AgvPodBerthAndMatDTO()
                    {
                        ReqCode = Guid.NewGuid().ToString().Replace("-", ""),
                        PositionCode=outBound.TargetAddress
                    };
                    WebResponseContent content = _taskService.AgvPodBerthAndMat(andMatDTO);
                    if (content.Status)
                    {
                        outBound.TaskState = TaskStatusEnum.AGV_Execute.ObjToInt();
                        outBound.ExceptionMessage = "";
                        //agvTask.Remark = content.Data.ObjToString();
                        _taskService.UpdateTask(outBound, TaskStatusEnum.AGV_Execute);
                    }
                }
            }
            catch (Exception ex)
            {
                WriteError(nameof(AGV_CSJJob), ex.Message, ex);
            }
        }
        /// <summary>
        /// 下发AGV继续执行任务
        /// </summary>
        public void SendAGVWaitToTask()
        {
            try
            {
                var WaitToTasks = _taskService.Db.Queryable<Dt_Task>().Where(x => x.TaskState == TaskStatusEnum.AGV_WaitToExecute.ObjToInt() && nameof(AGV_CSJJob).Contains(x.DeviceCode)).ToList().OrderBy(x => x.Grade).ThenBy(x => x.CreateDate).ToList();
                foreach (var WaitToTask in WaitToTasks)
                {
                    if (WaitToTask.TaskType.GetTaskTypeGroup() == TaskTypeGroup.InboundGroup)
                    {
                        if (WaitToTasks.FirstOrDefault(x=>x.TaskState==TaskStatusEnum.AGV_Puting.ObjToInt() || x.TaskState == TaskStatusEnum.Finish.ObjToInt()) !=null)
                        {
                            continue;
                        }
                        Dt_StationManger stationManger = _stationMangerRepository.QueryFirst(x => x.AGVStationCode == WaitToTask.NextAddress);
                        if (stationManger == null)
                        {
                            continue;
                        }
                        IDevice? device = Storage.Devices.FirstOrDefault(x => x.DeviceCode == stationManger.StationDeviceCode);
                        if (device == null)
                        {
                            continue;
                        }
                        OtherDevice otherDevice = (OtherDevice)device;
                        bool canPut = otherDevice.GetValue<GroundStationDBName, bool>(GroundStationDBName.R_IsCanPut, stationManger.StationCode);
                        bool requestPut = otherDevice.GetValue<GroundStationDBName, bool>(GroundStationDBName.W_PutRequest, stationManger.StationCode);
                        if (!requestPut)
                        {
                            otherDevice.SetValue(GroundStationDBName.W_PutRequest, true, stationManger.StationCode);
                            continue;
                        }
                        else if (!canPut)
                        {
                            continue;
                        }
                    }
                    else
                    {
                        Dt_StationManger stationManger = _stationMangerRepository.QueryFirst(x => x.AGVStationCode == WaitToTask.CurrentAddress);
                        if (stationManger == null)
                        {
                            continue;
                        }
                        IDevice? device = Storage.Devices.FirstOrDefault(x => x.DeviceCode == stationManger.StationDeviceCode);
                        if (device == null)
                        {
                            continue;
                        }
                        OtherDevice otherDevice = (OtherDevice)device;
                        bool canTake = otherDevice.GetValue<GroundStationDBName, bool>(GroundStationDBName.R_IsCanTake, stationManger.StationCode);
                        bool requestTake = otherDevice.GetValue<GroundStationDBName, bool>(GroundStationDBName.W_TakeRequest, stationManger.StationCode);
                        if (!requestTake)
                        {
                            otherDevice.SetValue(GroundStationDBName.W_TakeRequest, true, stationManger.StationCode);
                            continue;
                        }
                        else if (!canTake)
                        {
                            continue;
                        }
                    }
 
                    AgvSecureReplyDTO replyDTO = new AgvSecureReplyDTO()
                    {
                        ReqCode = Guid.NewGuid().ToString().Replace("-", ""), //WaitToTask.TaskNum.ToString(),
                        taskCode = WaitToTask.AgvTaskNum,
                    };
                    WebResponseContent content = _taskService.AgvSecureReply(replyDTO);
                    if (content.Status && WaitToTask.TaskType.GetTaskTypeGroup() == TaskTypeGroup.InboundGroup)
                    {
                        WaitToTask.TaskState = TaskStatusEnum.AGV_Puting.ObjToInt();
                        _taskService.UpdateTask(WaitToTask, TaskStatusEnum.AGV_Puting);
                        break;
                    }
                    else if (content.Status && WaitToTask.TaskType.GetTaskTypeGroup() == TaskTypeGroup.OutbondGroup)
                    {
                        WaitToTask.TaskState = TaskStatusEnum.AGV_Executing.ObjToInt();
                        _taskService.UpdateTask(WaitToTask, TaskStatusEnum.AGV_Executing);
                    }
                    else
                    {
                        WaitToTask.TaskState = TaskStatusEnum.Exception.ObjToInt();
                        WaitToTask.ExceptionMessage = content.Message;
                        _taskService.UpdateTask(WaitToTask, TaskStatusEnum.Exception);
                    }
                }
                _taskService.UpdateData(WaitToTasks);
            }
            catch (Exception ex)
            {
                Console.Out.WriteLine(nameof(AGV_CSJJob) + ":" + ex.Message);
            }
        }
 
        public string AgvTaskType(int TaskType, string DeviceCode)
        {
            switch (DeviceCode)
            {
                case "AGV_CSJ":
                    {
                        return TaskType == TaskTypeEnum.ProductionReturn.ObjToInt() ? "23" : "24";
                    }
                case "AGV_ZH":
                    {
                        if (TaskType == TaskTypeEnum.InboundXB.ObjToInt())
                            return "20";
                        else if (TaskType == TaskTypeEnum.InboundJT.ObjToInt())
                            return "21";
                        else return "22";
                    }
                default:
                    throw new Exception($"设备编号错误");
            }
        }
    }
}