Admin
2025-12-02 9e42f0dafa019f5ecf6b0ff425ecb966b002171e
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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
using HslCommunication;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using WIDESEA_Common;
using WIDESEA_Common.CutomerModel;
using WIDESEA_Common.TaskEnum;
using WIDESEA_Common.Tools;
using WIDESEA_Core.Utilities;
using WIDESEA_Entity.DomainModels;
using WIDESEA_Services;
using WIDESEA_Services.IRepositories;
using WIDESEA_Services.Repositories;
using WIDESEA_Services.Services;
using WIDESEA_WCS.WCSClient;
 
namespace WIDESEA_WCS.Jobs.ConveyorLine.OutboundArea
{
    public partial class OutboundAreaDispatch
    {
        /// <summary>
        /// 出库的层
        /// </summary>
        private static List<string> OutboundStationLayerNo = new List<string>() { "90100", "90200", "90300", "90400", "90500", "90600", "90700", "90800" };
        //入库托盘申请站台
        private static string ReInboundRequestStationNo = "90101";
        //出库站台交互
        //private static string OutboundStationNo = "90201";
        //提升机编号
        private static string hoisterResultNo = "90100";
        /// <summary>
        /// 出库区调度
        /// </summary>
        /// <param name="taskWCSinfoRepository"></param>
        /// <param name="client"></param>
        public static string currentModel = "Inbound";
        static int flag = 0;
        public static void OutboundAreaDispathAction(IDt_TaskWCSinfoRepository taskWCSinfoRepository, IDt_TaskRGVinfoRepository taskRGVinfoRepository, PLCClient client)
        {
            try
            {
                //出库完成
                OutboundCompleteAction(taskWCSinfoRepository, client);
                //读取提升机是否正常
                //滚筒线状态
                string hoisterResult = client.ReadValue(CLineInfoDBName.R_Line_DeviceNormal.ToString(), hoisterResultNo).ToString();
                //提升机状态
                string result = client.Read("DB506.36.0", "bool").ToString();
                if (!bool.Parse(hoisterResult) || !bool.Parse(result))
                    return;
                //入库申请
                InboundRequestAction(taskWCSinfoRepository, taskRGVinfoRepository, client);
 
                //增加补丁.增加车存储中有问题.再写
                int logicValue = int.Parse(client.ReadValue(CLineInfoDBName.R_Line_Logic.ToString(), "OutboundArea").ToString());
                if (logicValue == 4)
                {
                    string rfidResult = client.ReadValue(CLineInfoDBName.W_Line_Barcode.ToString(), "OutboundArea").ToString();
                    if (!string.IsNullOrEmpty(rfidResult) && !"0".Equals(rfidResult))
                    {
                        Dt_TaskWCSinfo wcsInfo = taskWCSinfoRepository.FindFirst(r => r.wcstask_barcode == rfidResult
                        && (r.wcstask_state == TaskState.TaskState_Empty_Out_Line_Executing.ToString()
                        || r.wcstask_state == TaskState.TaskState_Box_Out_Line_Executing.ToString()));
 
                        if (null != wcsInfo)
                        {
                            //提升机层
                            string hoisterLayer = client.ReadValue(CLineInfoDBName.R_Line_Layer.ToString(), hoisterResultNo).ToString();
                            string[] locationArray = wcsInfo.wcstask_startLocation.Split('-');
                            if (int.Parse(locationArray[0]).ToString() == hoisterLayer)
                            {
                                Dt_TaskRGVinfo rgvTask = taskRGVinfoRepository.FindFirst(r => r.rgvtask_barCode == wcsInfo.wcstask_barcode);
                                if (null == rgvTask)
                                {
                                    flag++;//避免刚完成重复写
                                    if (flag == 10)
                                    {
                                        WriteRGVState(client, false);
                                        WriteRGVState(client, true);
                                        flag = 0;
                                        return;
                                    }
                                }
                            }
                        }
                    }
                }
                else
                    flag = 0;
 
                List<Dt_TaskWCSinfo> executingTask = taskWCSinfoRepository.Find(r => (r.wcstask_state == TaskState.TaskState_RGV_Received.ToString()
                || r.wcstask_state == TaskState.TaskState_HoisterExecuting.ToString())
                 && (OutboundStationLayerNo.Contains(r.wcstask_startPoint) || OutboundStationLayerNo.Contains(r.wcstask_endPoint)));
                if (executingTask.Count < 3)
                {
                    //直接将任务添加到AGV任务
                    Dt_TaskWCSinfo outboundTask = GetOutboundTask(taskWCSinfoRepository, client);
                    if (null != outboundTask)
                    {
                        Dt_TaskRGVinfo rgvInfo = taskRGVinfoRepository.FindFirst(r => r.rgvtask_barCode == outboundTask.wcstask_barcode
                        && r.rgvtask_wcsTaskNumber == outboundTask.wcstask_taskNumber);
                        if (null == rgvInfo)
                        {
                            string rgvTaskType = RGVTaskType.RgvTaskType_Inbound.ToString();
                            //空托盘回库
                            if (outboundTask.wcstask_type.Equals(TaskType.TaskType_Empty_Pallet_Inbound.ToString()))
                                rgvTaskType = RGVTaskType.RgvTaskType_Inbound.ToString();
                            //轴承出库
                            else if (outboundTask.wcstask_type.Equals(TaskType.TaskType_Box_Pallet_Outbound.ToString())
                                || outboundTask.wcstask_type.Equals(TaskType.TaskType_Empty_Pallet_Outbound.ToString()))
                                rgvTaskType = RGVTaskType.RgvTaskType_Outbound.ToString();
 
 
                            rgvInfo = GetRGVTask(taskRGVinfoRepository, outboundTask, rgvTaskType);
                            taskRGVinfoRepository.Add(rgvInfo, true);
 
                            outboundTask.wcstask_state = TaskState.TaskState_RGV_Received.ToString();
                            taskWCSinfoRepository.Update(outboundTask, x => x.wcstask_state, true);
                        }
                    }
                }
 
                //提升机运行的任务
                Dt_TaskWCSinfo hisTask = taskWCSinfoRepository.FindFirst(r => r.wcstask_state == TaskState.TaskState_HoisterExecuting.ToString()
                && (OutboundStationLayerNo.Contains(r.wcstask_startPoint) || OutboundStationLayerNo.Contains(r.wcstask_endPoint)));
                if (null != hisTask)
                {
                    //提升机层
                    string hoisterLayer = client.ReadValue(CLineInfoDBName.R_Line_Layer.ToString(), hoisterResultNo).ToString();
                    string[] locationArray = null;
                    //空托盘回库
                    if (hisTask.wcstask_type.Equals(TaskType.TaskType_Empty_Pallet_Inbound.ToString()))
                    {
                        locationArray = hisTask.wcstask_endLocation.Split('-');
                        //RGV上报取货完成直接执行下一个
                        Dt_TaskRGVinfo rgvTask = taskRGVinfoRepository.FindFirst(r => r.rgvtask_wcsTaskNumber == hisTask.wcstask_taskNumber);
                        if (null != rgvTask && "2" == rgvTask.rgvtask_backup_2)
                        {
                            if (rgvTask.rgvtask_taskType.Contains(RGVTaskType.RgvTaskType_Inbound.ToString()))
                            {
                                Dt_TaskWCSinfo inboundTask = taskWCSinfoRepository.FindFirst(r => OutboundStationLayerNo.Contains(r.wcstask_endPoint) && r.wcstask_type ==
                                TaskType.TaskType_Empty_Pallet_Inbound.ToString() && r.wcstask_state == TaskState.TaskState_RGV_Received.ToString());
                                if (null != inboundTask)
                                    ReInboundRequestStationAction(taskWCSinfoRepository, client, inboundTask);
                                else
                                    OutboundRequestAction(taskWCSinfoRepository, client);
                                return;
                            }
                        }
                        string hoisterTaskNumber = client.ReadValue(CLineInfoDBName.R_Line_TaskNumber.ToString(), hoisterResultNo).ToString();
                        string hoisterBarcode = client.ReadValue(CLineInfoDBName.R_Line_Barcode.ToString(), hoisterResultNo).ToString();
                        //避免提升机还没取就停止了
                        if (string.IsNullOrEmpty(hoisterTaskNumber) || string.IsNullOrEmpty(hoisterBarcode))
                            return;
                    }
                    //轴承出库
                    else if (hisTask.wcstask_type.Equals(TaskType.TaskType_Box_Pallet_Outbound.ToString())
                        || hisTask.wcstask_type.Equals(TaskType.TaskType_Empty_Pallet_Outbound.ToString()))
                        locationArray = hisTask.wcstask_startLocation.Split('-');
                    //说明提升机到达了目标层
                    if (int.Parse(locationArray[0]).ToString() == hoisterLayer)
                        WriteRGVState(client, false);// 写入RGV开始执行
                }
                else
                {
                    Dt_TaskWCSinfo inboundTask = taskWCSinfoRepository.FindFirst(r => OutboundStationLayerNo.Contains(r.wcstask_endPoint) && r.wcstask_type ==
                    TaskType.TaskType_Empty_Pallet_Inbound.ToString() && r.wcstask_state == TaskState.TaskState_RGV_Received.ToString());
                    if (null != inboundTask)
                        ReInboundRequestStationAction(taskWCSinfoRepository, client, inboundTask);
                    else
                        OutboundRequestAction(taskWCSinfoRepository, client);
                }
            }
            catch (Exception ex)
            {
                WriteLog.Info("OutboundArea").Write($"{ DateTime.Now }出库区域调度失败:{ex.Message}", "OutboundArea");
            }
        }
        public static bool WriteRGVState(PLCClient client, bool value)
        {
            client.WriteValue(CLineInfoDBName.W_System_RGVState.ToString(), value);
            string rel = string.Empty;
            for (int i = 0; i < 50; i++)
            {
                rel = client.ReadValue(CLineInfoDBName.W_System_RGVState.ToString()).ToString();
                if (value == bool.Parse(rel))
                    break;
                else
                {
                    client.WriteValue(CLineInfoDBName.W_System_RGVState.ToString(), value);
                    Thread.Sleep(88);
                }
            }
            rel = client.ReadValue(CLineInfoDBName.W_System_RGVState.ToString()).ToString();
            if (value != bool.Parse(rel))
                Console.Out.WriteLine($"{DateTime.Now}写入车存储中{value}失败.");
 
            return bool.Parse(rel);
        }
        /// <summary>
        /// 查询是否有可出的出库任务
        /// </summary>
        /// <param name="taskWCSinfoRepository"></param>
        /// <param name="client"></param>
        /// <returns></returns>
        public static Dt_TaskWCSinfo GetOutboundTask(IDt_TaskWCSinfoRepository taskWCSinfoRepository, PLCClient client)
        {
            Dt_TaskWCSinfo wcsInfo = null;
 
            List<Dt_TaskWCSinfo> listTask = taskWCSinfoRepository.Find(r => r.wcstask_state == TaskState.TaskState_Assigned.ToString() &&
          (r.wcstask_type == TaskType.TaskType_Box_Pallet_Outbound.ToString() || r.wcstask_type == TaskType.TaskType_Empty_Pallet_Outbound.ToString())
          && OutboundStationLayerNo.Contains(r.wcstask_startPoint)).OrderBy(r => r.wcstask_createTime).ToList();//
 
            foreach (var item in listTask.GroupBy(r => r.wcstask_endPoint))
            {
                Dt_TaskWCSinfo wcsTask = item.OrderBy(r => r.wcstask_createTime).FirstOrDefault();
                if (null != wcsTask)
                {
                    //同一层有执行中的任务不添加
                    Dt_TaskWCSinfo executingTask = taskWCSinfoRepository.FindFirst(r => (r.wcstask_startPoint == wcsTask.wcstask_startPoint || r.wcstask_endPoint == wcsTask.wcstask_endPoint)
                    && (r.wcstask_state == TaskState.TaskState_RGV_Received.ToString() || r.wcstask_state == TaskState.TaskState_HoisterExecuting.ToString()));
                    if (null != executingTask)
                        continue;
 
                    List<Dt_TaskWCSinfo> listTarget = taskWCSinfoRepository.Find(r => r.wcstask_endPoint == wcsTask.wcstask_endPoint &&
                    r.wcstask_state != TaskState.TaskState_Assigned.ToString());
 
                    string barcode = client.ReadValue(CLineInfoDBName.R_Line_Barcode.ToString(), wcsTask.wcstask_endPoint).ToString();
                    if (string.IsNullOrEmpty(barcode) || "0".Equals(barcode))
                    {
                        if (null != listTarget && listTarget.Count > 1)
                            continue;
                        else
                        {
                            wcsInfo = wcsTask;
                            break;
                        }
                    }
                    else
                    {
                        if (null != listTarget && listTarget.Count > 0)
                            continue;
                        else
                        {
                            wcsInfo = wcsTask;
                            break;
                        }
                    }
                }
            }
 
 
 
            //foreach (var item in listTask)
            //{
            //    //同一层有执行中的任务不添加
            //    Dt_TaskWCSinfo executingTask = taskWCSinfoRepository.FindFirst(r => (r.wcstask_startPoint == item.wcstask_startPoint || r.wcstask_endPoint == item.wcstask_endPoint)
            //    && (r.wcstask_state == TaskState.TaskState_RGV_Received.ToString() || r.wcstask_state == TaskState.TaskState_HoisterExecuting.ToString()));
            //    if (null != executingTask)
            //        continue;
 
            //    //同一目标位置的不能同时出
            //    //  Dt_TaskWCSinfo endTask = taskWCSinfoRepository.FindFirst(r => r.wcstask_endPoint == item.wcstask_endPoint &&
            //    //(r.wcstask_state == TaskState.TaskState_RGV_Received.ToString() || r.wcstask_state == TaskState.TaskState_HoisterExecuting.ToString()));
            //    //  if (null != endTask)
            //    //      continue;
 
            //    List<Dt_TaskWCSinfo> listTarget = taskWCSinfoRepository.Find(r => r.wcstask_endPoint == item.wcstask_endPoint &&
            //    r.wcstask_state != TaskState.TaskState_Assigned.ToString());
 
            //    string barcode = client.ReadValue(CLineInfoDBName.R_Line_Barcode.ToString(), item.wcstask_endPoint).ToString();
            //    if (string.IsNullOrEmpty(barcode) || "0".Equals(barcode))
            //    {
            //        if (null != listTarget && listTarget.Count > 1)
            //            continue;
            //        else
            //        {
            //            wcsInfo = item;
            //            break;
            //        }
            //    }
            //    else
            //    {
            //        if (null != listTarget && listTarget.Count > 0)
            //            continue;
            //        else
            //        {
            //            wcsInfo = item;
            //            break;
            //        }
            //    }
            //}
            return wcsInfo;
        }
 
 
        public static Dt_TaskRGVinfo GetRGVTask(IDt_TaskRGVinfoRepository taskRGVinfoRepository, Dt_TaskWCSinfo wcsTask, string rgvTaskType)
        {
            Dt_TaskRGVinfo rgvInfo = new Dt_TaskRGVinfo();
            rgvInfo.rgvtask_taskId = GetTaskNumber.GetRgvTaskNumber(taskRGVinfoRepository).ToString();
            rgvInfo.rgvtask_taskType = rgvTaskType;
            rgvInfo.rgvtask_taskStatus = RGVTaskState.RgvTaskState_Wait_Send.ToString();
            rgvInfo.rgvtask_priorityCode = wcsTask.wcstask_grade.ToString();
            rgvInfo.rgvtask_startNode = wcsTask.wcstask_startLocation;
            rgvInfo.rgvtask_endNode = wcsTask.wcstask_endLocation;
            rgvInfo.rgvtask_wcsTaskNumber = wcsTask.wcstask_taskNumber;
            rgvInfo.rgvtask_barCode = wcsTask.wcstask_barcode;
            rgvInfo.rgvtask_creator = wcsTask.wcstask_creator;
            rgvInfo.rgvtask_msgTime = DateTime.Now;
            rgvInfo.rgvtask_areaCode = "OutboundArea";
            return rgvInfo;
        }
 
        public static void InboundRequestAction(IDt_TaskWCSinfoRepository taskWCSinfoRepository, IDt_TaskRGVinfoRepository taskRGVinfoRepository, PLCClient client)
        {
            try
            {
                string barcode = client.ReadValue(CLineInfoDBName.R_Line_Barcode.ToString(), ReInboundRequestStationNo).ToString();
                bool load = bool.Parse(client.ReadValue(CLineInfoDBName.R_Line_LoadSleep.ToString(), ReInboundRequestStationNo).ToString());
                if (!string.IsNullOrEmpty(barcode) && !"0".Equals(barcode) && load)
                {
                    Dt_TaskWCSinfo wcsInfo = taskWCSinfoRepository.FindFirst(r => r.wcstask_barcode == barcode && r.wcstask_type == TaskType.TaskType_Empty_Pallet_Inbound.ToString());
                    if (null == wcsInfo)
                    {
                        //有出库任务,不能下发
                        Dt_TaskRGVinfo executingRgvTask = taskRGVinfoRepository.FindFirst(r => r.rgvtask_areaCode == "OutboundArea"
                        && OutboundStationLayerNo.Contains(r.rgvtask_endNode));
                        if (null != executingRgvTask)
                            return;
 
                        //当前存在未完成的任务   入库
                        wcsInfo = taskWCSinfoRepository.FindFirst(r => OutboundStationLayerNo.Contains(r.wcstask_endPoint));
                        if (null != wcsInfo)
                        {
                            Dt_TaskRGVinfo rgvTask = taskRGVinfoRepository.FindFirst(r => r.rgvtask_wcsTaskNumber == wcsInfo.wcstask_taskNumber);
                            if (null != rgvTask)
                            {
                                //取货完成
                                if ("2" != rgvTask.rgvtask_backup_2)
                                    return;
                            }
                            else
                                return;
                        }
 
                        //申请入库任务
                        WebResponseContent content = WMSApi.PostInboundRequstToWMS(barcode);
                        if (content.Status)
                        {
                            WmsTaskInfo wmsTask = JsonConvert.DeserializeObject<WmsTaskInfo>(content.Data.ToString());
                            wcsInfo = CommonFunction.AddWCSEmptyInboundTask(wmsTask, taskWCSinfoRepository);
 
                            string rgvTaskType = string.Empty;
                            if (wcsInfo.wcstask_type.Equals(TaskType.TaskType_Empty_Pallet_Inbound.ToString()))
                                rgvTaskType = RGVTaskType.RgvTaskType_Inbound.ToString();
                            //轴承出库
                            else if (wcsInfo.wcstask_type.Equals(TaskType.TaskType_Box_Pallet_Outbound.ToString())
                                || wcsInfo.wcstask_type.Equals(TaskType.TaskType_Empty_Pallet_Outbound.ToString()))
                                rgvTaskType = RGVTaskType.RgvTaskType_Outbound.ToString();
 
                            taskWCSinfoRepository.DbContextBeginTransaction(() =>
                            {
                                Dt_TaskRGVinfo rgvInfo = GetRGVTask(taskRGVinfoRepository, wcsInfo, rgvTaskType);
                                taskRGVinfoRepository.Add(rgvInfo, true);
 
                                wcsInfo.wcstask_state = TaskState.TaskState_RGV_Received.ToString();
                                taskWCSinfoRepository.Add(wcsInfo, true);
                                return content.OK();
                            });
                        }
                        else
                            throw new Exception("入库申请失败:" + content.Message);
                    }
                }
            }
            catch (Exception ex)
            {
                Console.Out.WriteLine(DateTime.Now + ex.Message);
            }
        }
    }
}