wangxinhui
2024-11-06 8f392cc88b0768b74efca3b68785cf5aa1c38e70
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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
using System;
using System.Linq;
using System.Threading;
using WIDESEA_Common;
using WIDESEA_Common.Tools;
using WIDESEA_Core.EFDbContext;
using WIDESEA_Core.Utilities;
using WIDESEA_Entity.DomainModels;
using WIDESEA_Services.IRepositories;
using WIDESEA_Services.Repositories;
using WIDESEA_Services.Services;
using WIDESEA_WCS.WCSClient;
 
namespace WIDESEA_WCS.SchedulerExecute.AGV
{
    public partial class AGVSchedulerExecute
    {
        private static int _readUpdateAGVTaskSignalso = 0;
        public static void UpdateState(PLCClient plcClient)
        {
            if (Interlocked.Exchange(ref _readUpdateAGVTaskSignalso, 1) == 0)
            {
                try
                {
                    if (plcClient == null)
                    {
                        return;
                    }
                    VOLContext context = new VOLContext();
                    Idt_task_numberRepository tasknumberRep = new dt_task_numberRepository(context);
                    Idt_agvtaskRepository agvtaskRepository = new dt_agvtaskRepository(context);
                    Idt_agvtask_htyRepository agvtask_HtyRepository = new dt_agvtask_htyRepository(context);
                    Ibase_ware_locationRepository locationRepository = new base_ware_locationRepository(context);
                    Ibill_group_stockRepository group_StockRepository = new bill_group_stockRepository(context);
                    dt_task_numberService tasknumber = new dt_task_numberService(tasknumberRep);
                    int taskFbInteractive = Convert.ToInt32(plcClient.ReadValue(TaskDBName.taskFbInteractiveR.ToString()));
                    int taskFbInteractiveW = Convert.ToInt32(plcClient.ReadValue(TaskDBName.taskFbInteractiveW.ToString()));
                    //0初始状态 1 RCS更新了一条任务状态
                    if (1 == taskFbInteractive && taskFbInteractiveW == 0)
                    {
                        string taskId = plcClient.ReadValue(TaskDBName.taskIDFb.ToString()).ToString();//任务ID
                        if (taskId.Contains("\b")) { taskId = taskId.Remove(0, 1); }
                        int taskState = int.Parse(plcClient.ReadValue(TaskDBName.taskStatusFb.ToString()).ToString());//1起点执行中,2起点已完成,3终点执行中,4终点已完成
                        if (0 == taskState)//无含义
                            return;
                        //WriteLog.GetLog().Write("任务号" + taskId + "任务状态" + taskState, "AGV更新任务状态");
                        dt_agvtask agvTask = agvtaskRepository.Find(r => r.agv_tasknum == taskId).FirstOrDefault();
 
                        if (1 == taskState)//任务起点执行中
                        {
                            if (null == agvTask)
                            {
                                throw new Exception("起点执行失败未找到任务,ID:" + taskId);
                            }
                            if (agvTask.agv_taskstate == AGVTaskStateEnum.Executing.ToString())
                            {
                                plcClient.WriteValue(TaskDBName.taskFbInteractiveW.ToString(), 1);
                                WriteLog.GetLog().Write("WCS再次写入AGV任务号" + taskId + DateTime.Now + "任务起点执行中状态确认1", "AGV更新任务状态");
                            }
                            agvTask.agv_taskstate = AGVTaskStateEnum.Executing.ToString();
                            agvTask.agv_finishedtime = DateTime.Now;
                            agvtaskRepository.Update(agvTask, true);
                            plcClient.WriteValue(TaskDBName.taskFbInteractiveW.ToString(), 1);
                            for (int i = 0; i < 5; i++)
                            {
                                Thread.Sleep(300);
                                var agvnumber = Convert.ToInt32(plcClient.ReadValue(TaskDBName.taskFbInteractiveW.ToString()));
                                if (agvnumber != 1)
                                {
                                    plcClient.WriteValue(TaskDBName.taskFbInteractiveW.ToString(), 1);
                                }
                                else
                                {
                                    break;
                                }
                            }
                            WriteLog.GetLog().Write("WCS写入AGV任务号" + taskId + "任务起点执行中状态确认1", "AGV更新任务状态");
                        }
                        else if (2 == taskState)//任务起点已完成
                        {
                            if (null == agvTask)
                            {
                                plcClient.WriteValue(TaskDBName.taskFbInteractiveW.ToString(), 1);
                                WriteLog.GetLog().Write("WCS写入AGV任务号,当前任务号不存在任务列表里," + taskId + DateTime.Now + "任务起点完成状态确认1", "AGV更新任务状态");
                                return;
                            }
                            if (agvTask.agv_taskstate == AGVTaskStateEnum.Complete.ToString())
                            {
                                plcClient.WriteValue(TaskDBName.taskFbInteractiveW.ToString(), 1);
                                WriteLog.GetLog().Write("WCS再次写入AGV任务号" + taskId + DateTime.Now + "任务起点完成状态确认1", "AGV更新任务状态");
                            }
                            else if (agvTask.agv_taskstate != AGVTaskStateEnum.Executing.ToString())
                            {
                                throw new Exception("任务状态更改为2起点已完成失败,任务ID:" + agvTask.agv_tasknum + DateTime.Now + ",任务状态不是起点执行中!");
                            }
                            if (agvTask.agv_tasknum.Contains(":"))
                            {
                                if (agvTask.agv_fromaddress.Contains("ZZLJ"))
                                {
                                    var loc = locationRepository.FindFirst(f => f.upper_code == agvTask.agv_fromaddress || f.down_code == agvTask.agv_toaddress);
                                    loc.location_state = LocationStateEnum.LocationState_Stored.ToString();
                                    locationRepository.Update(loc, true);
                                }
                                //agvtask_HtyRepository.AddTaskHistory(agvTask, OperateType.Finished.ToString());
                                agvtaskRepository.Delete(agvTask, true);
                                plcClient.WriteValue(TaskDBName.taskFbInteractiveW.ToString(), 1);
                                for (int i = 0; i < 5; i++)
                                {
                                    Thread.Sleep(300);
                                    var agvnumber = Convert.ToInt32(plcClient.ReadValue(TaskDBName.taskFbInteractiveW.ToString()));
                                    if (agvnumber != 1)
                                    {
                                        plcClient.WriteValue(TaskDBName.taskFbInteractiveW.ToString(), 1);
                                    }
                                    else
                                    {
                                        break;
                                    }
                                }
                                WriteLog.GetLog().Write("WCS写入AGV任务号" + taskId + DateTime.Now + "任务起点完成状态确认1", "AGV更新任务状态");
                            }
                            else
                            {
                                agvTask.agv_taskstate = AGVTaskStateEnum.Complete.ToString();
 
                                //货位状态改成空,location_id为空
                                bill_group_stock stock = new bill_group_stock();
                                if (agvTask.agv_fromaddress.Contains("0201") && agvTask.agv_toaddress.Contains("ZZLJ")) { }
                                else if (agvTask.agv_fromaddress.Contains("WLX") && agvTask.agv_toaddress.Contains("0101")) { }
                                else if (agvTask.agv_toaddress.Contains("GMHX")) { }
                                else if (agvTask.agv_toaddress.Contains("HXWLX"))
                                {
                                    if (!agvTask.agv_fromaddress.Contains("KPHLX"))
                                    {
                                        var materbarcode = ""; /*agvTask.agv_materbarcode.Split(";");*/
                                        for (int i = 0; i < materbarcode.Count(); i++)
                                        {
                                            stock = null;/*group_StockRepository.Find(f => f.BarCode == materbarcode[i]).OrderByDescending(f => f.created_time).FirstOrDefault();*/
                                            if (stock != null)
                                            {
                                                stock.location_id = null;
                                                group_StockRepository.Update(stock, true);
                                            }
                                            if (materbarcode[0].Equals(materbarcode[1])) { break; }
                                        }
                                    }
                                }
                                else
                                {
                                    stock = null;// group_StockRepository.Find(f => f.BarCode == agvTask.agv_materbarcode).OrderByDescending(f => f.created_time).FirstOrDefault();
                                    if (stock != null)
                                    {
                                        stock.location_id = null;
                                        group_StockRepository.Update(stock, true);
                                    }
                                }
                                base_ware_location location = locationRepository.Find(f => f.upper_code == agvTask.agv_fromaddress || f.down_code == agvTask.agv_fromaddress).FirstOrDefault();
                                if (location != null)
                                {
                                    location.location_state = LocationStateEnum.LocationState_Empty.ToString();
                                    locationRepository.Update(location, true);
                                    if (agvTask.agv_fromaddress.Contains("HCJ") || agvTask.agv_fromaddress.Contains("CBJ") || agvTask.agv_fromaddress.Contains("JJK") || agvTask.agv_fromaddress.Contains("ZZLJ"))
                                    {
                                        //plcClient.WriteValue(ConveyorLineGetLogDBName.MaterOK.ToString(), agvTask.agv_fromaddress, 0);
                                        //plcClient.WriteValue(ConveyorLineGetLogDBName.MaterNG.ToString(), agvTask.agv_fromaddress, 0);
                                    }
                                }
                                //如果是从库内出库,需要给WMS反馈出库物料确认搬走
                                if (agvTask.agv_fromaddress.Contains("JJK"))
                                {
                                    MESback WMSbackresult = new MESback();
                                    if (agvTask.agv_fromaddress.Contains("Z"))
                                    {
                                        //WMSbackresult = MESAPIInvoke.OutStockMaterMove(agvTask.agv_fromaddress, agvTask.agv_materielid, 1, 0);
                                    }
                                    else if (agvTask.agv_fromaddress.Contains("F"))
                                    {
                                        //WMSbackresult = MESAPIInvoke.OutStockMaterMove(agvTask.agv_fromaddress, agvTask.agv_materielid, 2, 0);
                                    }
                                    if (WMSbackresult.Code > 0) { new Exception(WMSbackresult.Message); return; }
                                    WriteLog.GetLog().Write("WCS反馈WMS任务号" + taskId + "任务起点完", "AGV更新任务状态");
                                }
                                else if (agvTask.agv_fromaddress.Contains("0101") || agvTask.agv_fromaddress.Contains("0201"))
                                {
                                    MESback WMSbackresult = new MESback();
                                    if (agvTask.agv_toaddress.Contains("ZZLJ"))
                                    {
                                        //WMSbackresult = MESAPIInvoke.OutStockMaterMove(agvTask.agv_fromaddress, "空托盘", 3, 0);
                                    }
                                    else
                                    {
                                        //WMSbackresult = MESAPIInvoke.OutStockMaterMove(agvTask.agv_fromaddress, agvTask.agv_materielid, 3, 0);
                                    }
                                    if (WMSbackresult.Code > 0) { new Exception(WMSbackresult.Message); return; }
                                    WriteLog.GetLog().Write("WCS反馈WMS任务号" + taskId + "任务起点完", "AGV更新任务状态");
                                }
                                else if (agvTask.agv_fromaddress.Contains("FBT"))
                                {
                                    var iswork = agvtaskRepository.FindFirst(f => f.agv_remark == "true" && f.agv_tasknum == taskId);
                                    if (iswork != null)
                                    {
                                        // var sum = "";//agvtaskRepository.Find(f => f.agv_materbarcode.Contains(iswork.agv_materbarcode.Substring(0, 2)) && f.agv_toaddress.Contains(iswork.agv_toaddress.Substring(0, 2))).Count();
                                        MESback WMSbackresult = new MESback();
                                        if (agvTask.agv_fromaddress.Contains("Z"))
                                        {
                                            //WMSbackresult = MESAPIInvoke.OutStockMaterMove(agvTask.agv_fromaddress, agvTask.agv_materielid, 4, sum);
                                        }
                                        else if (agvTask.agv_fromaddress.Contains("F"))
                                        {
                                            //WMSbackresult = MESAPIInvoke.OutStockMaterMove(agvTask.agv_fromaddress, agvTask.agv_materielid, 4, sum);
                                        }
                                        if (WMSbackresult.Code > 0) { new Exception(WMSbackresult.Message); return; }
                                    }
                                }
                                agvtaskRepository.Update(agvTask, true);
                                plcClient.WriteValue(TaskDBName.taskFbInteractiveW.ToString(), 1);
                                for (int i = 0; i < 5; i++)
                                {
                                    Thread.Sleep(300);
                                    var agvnumber = Convert.ToInt32(plcClient.ReadValue(TaskDBName.taskFbInteractiveW.ToString()));
                                    if (agvnumber != 1)
                                    {
                                        plcClient.WriteValue(TaskDBName.taskFbInteractiveW.ToString(), 1);
                                    }
                                    else
                                    {
                                        break;
                                    }
                                }
                                WriteLog.GetLog().Write("WCS写入AGV任务号" + taskId + DateTime.Now + "任务起点完成状态确认1", "AGV更新任务状态");
                            }
                        }
                        else if (3 == taskState)//任务终点执行中
                        {
                            if (null == agvTask)
                            {
                                throw new Exception("终点执行失败未找到任务,ID:" + taskId);
                            }
                            if (agvTask.agv_taskstate == AGVTaskStateEnum.Executing1.ToString())
                            {
                                plcClient.WriteValue(TaskDBName.taskFbInteractiveW.ToString(), 1);
                                WriteLog.GetLog().Write("WCS再次写入AGV任务号" + taskId + DateTime.Now + "任务终点执行中状态确认1", "AGV更新任务状态");
                            }
                            if (agvTask.agv_tasknum.Contains(":"))
                            {
                                string[] taskidsplit = agvTask.agv_tasknum.Split(":");
                                var alltask = agvtaskRepository.Find(v => v.agv_tasknum.Contains(taskidsplit[0])).ToList();
                                foreach (var item in alltask)
                                {
                                    //agvtask_HtyRepository.AddTaskHistory(item, OperateType.Abnormal.ToString());
                                    agvtaskRepository.Delete(item, true);
                                    if (item.agv_fromaddress.Contains("ZZLJ"))
                                    {
                                        var loc = locationRepository.FindFirst(f => f.upper_code == agvTask.agv_fromaddress || f.down_code == agvTask.agv_toaddress);
                                        loc.location_state = LocationStateEnum.LocationState_Stored.ToString();
                                        locationRepository.Update(loc, true);
                                    }
                                }
                                plcClient.WriteValue(TaskDBName.taskFbInteractiveW.ToString(), 1);
                                for (int i = 0; i < 5; i++)
                                {
                                    Thread.Sleep(300);
                                    var agvnumber = Convert.ToInt32(plcClient.ReadValue(TaskDBName.taskFbInteractiveW.ToString()));
                                    if (agvnumber != 1)
                                    {
                                        plcClient.WriteValue(TaskDBName.taskFbInteractiveW.ToString(), 1);
                                    }
                                    else
                                    {
                                        break;
                                    }
                                }
                                WriteLog.GetLog().Write("WCS写入AGV任务号" + taskId + DateTime.Now + "分段任务异常确认1", "AGV更新任务状态");
                            }
                            else
                            {
                                agvTask.agv_taskstate = AGVTaskStateEnum.Executing1.ToString();
                                //agvTask.agv_realesstime = DateTime.Now;
                                agvtaskRepository.Update(agvTask, true);
                                plcClient.WriteValue(TaskDBName.taskFbInteractiveW.ToString(), 1);
                                for (int i = 0; i < 5; i++)
                                {
                                    Thread.Sleep(300);
                                    var agvnumber = Convert.ToInt32(plcClient.ReadValue(TaskDBName.taskFbInteractiveW.ToString()));
                                    if (agvnumber != 1)
                                    {
                                        plcClient.WriteValue(TaskDBName.taskFbInteractiveW.ToString(), 1);
                                    }
                                    else
                                    {
                                        break;
                                    }
                                }
                                WriteLog.GetLog().Write("WCS写入AGV任务号" + taskId + DateTime.Now + "任务终点执行中状态确认1", "AGV更新任务状态");
                            }
                        }
                        else if (4 == taskState)//任务终点完成
                        {
                            if (null == agvTask)
                            {
                                plcClient.WriteValue(TaskDBName.taskFbInteractiveW.ToString(), 1);
                                WriteLog.GetLog().Write("WCS写入AGV任务号,当前任务号不存在任务列表里," + taskId + DateTime.Now + "任务终点完成状态确认1", "AGV更新任务状态");
                                return;
                            }
                            else if (agvTask.agv_taskstate != AGVTaskStateEnum.Executing1.ToString())
                            {
                                //plcClient.WriteValue(TaskDBName.taskFbInteractiveW.ToString(), 1);
                                throw new Exception("任务状态更改为4终点已完成失败,任务ID:" + agvTask.agv_tasknum + DateTime.Now + ",任务状态不是终点执行中!");
                            }
                            //如果是设备就不用更改,是缓存架货位状态为已存储,(库存表)location_id=(货位表)的货位
                            base_ware_location location = locationRepository.FindFirst(f => f.upper_code == agvTask.agv_toaddress || f.down_code == agvTask.agv_toaddress);
                            bill_group_stock stock = new bill_group_stock();
                            if (agvTask.agv_fromaddress.Contains("0201") && agvTask.agv_toaddress.Contains("ZZLJ"))
                            {
                                //location.location_state = LocationStateEnum.LocationState_Wait.ToString();
                                locationRepository.Update(location, true);
                            }
                            else if (agvTask.agv_toaddress.Contains("LJHCX")) { }
                            else if (agvTask.agv_fromaddress.Contains("WLX") && agvTask.agv_toaddress.Contains("0101"))
                            {
                                location.location_state = LocationStateEnum.LocationState_Stored.ToString();
                                locationRepository.Update(location, true);
                                //MESback WMSbackresult = MESAPIInvoke.InStockMaterBind(agvTask.agv_toaddress, "空托盘", agvTask.agv_tasknum, "OK", 3);
                                //if (WMSbackresult.Code > 0) { new Exception(WMSbackresult.Message); return; }
                            }
                            else if (agvTask.agv_toaddress.Contains("GMHX"))
                            {
                                //var materbarcode = agvTask.agv_materbarcode.Split(",");
                                //List<BakingClass> materials = new List<BakingClass>();
                                //for (int i = 0; i < materbarcode.Count(); i++)
                                //{
                                //    BakingClass bakingClass = new BakingClass();
                                //    bakingClass.BarCode = materbarcode[i];
                                //    bakingClass.MaterialType = agvTask.agv_materielid;
                                //    materials.Add(bakingClass);
                                //    WriteLog.GetLog("烘烤").Write("materials" + materials[i].BarCode + "\t" + materials[i].MaterialType + "\t" + bakingClass.BarCode + "\t" + bakingClass.MaterialType + DateTime.Now, "烘烤物料条码");
                                //}
                                //MESback WMSbackresult = MESAPIInvoke.BakingFeedingBinding(agvTask.agv_toaddress, materials);
                                //if (WMSbackresult.Code > 0) { new Exception(WMSbackresult.Message); return; }
                            }
                            else if (agvTask.agv_toaddress.Contains("HXWLX"))
                            { //如果是进烘箱的任务,需要告诉WMS两个进烘箱物料的条码和位置,需要问胡工怎么给他值
                                if (agvTask.agv_fromaddress.Contains("KPHLX"))
                                {
                                    //var materials = WebApiHelper.ParseFromJson<List<BakingClass>>(agvTask.agv_materbarcode);
                                    //MESback WMSbackresult = MESAPIInvoke.BakingFeedingBinding(agvTask.agv_toaddress, materials);
                                    //if (WMSbackresult.Code > 0) { new Exception(WMSbackresult.Message); return; }
                                }
                                else
                                {
                                    WebResponseContent content = new WebResponseContent();
                                    group_StockRepository.DbContextBeginTransaction(() =>
                                    {
                                        //var materbarcode = agvTask.agv_materbarcode.Split(";");
                                        //List<BakingClass> materials = new List<BakingClass>();
                                        //for (int i = 0; i < materbarcode.Count(); i++)
                                        //{
                                        //    BakingClass bakingClass = new BakingClass();
                                        //    stock = group_StockRepository.Find(f => f.BarCode == materbarcode[i]).OrderByDescending(f => f.created_time).FirstOrDefault();
                                        //    bakingClass.BarCode = materbarcode[i];
                                        //    bakingClass.MaterialType = stock.MaterialType;
                                        //    materials.Add(bakingClass);
                                        //    group_StockRepository.Delete(stock, true);
                                        //    WriteLog.GetLog("烘烤").Write("materials" + materials[i].BarCode + "\t" + materials[i].MaterialType + "\t" + bakingClass.BarCode + "\t" + bakingClass.MaterialType + DateTime.Now, "烘烤物料条码");
                                        //}
                                        //MESback WMSbackresult = MESAPIInvoke.BakingFeedingBinding(agvTask.agv_toaddress, materials);
                                        //if (WMSbackresult.Code > 0) { new Exception(WMSbackresult.Message); return WebResponseContent.Instance.Error(WMSbackresult.Message); }
                                        //如果终点地址是去烘烤箱的删除库存
                                        location.location_state = LocationStateEnum.LocationState_Empty.ToString();
                                        locationRepository.Update(location, true);
                                        return WebResponseContent.Instance.OK();
                                    });
                                }
                            }
                            else
                            {
                                //stock = group_StockRepository.Find(f => f.BarCode == agvTask.agv_materbarcode).OrderByDescending(f => f.created_time).FirstOrDefault();
                                if (!agvTask.agv_toaddress.Contains("SB") && !agvTask.agv_fromaddress.Contains("WLX") && !agvTask.agv_toaddress.Contains("JJK") && !agvTask.agv_toaddress.Contains("WLX"))
                                {
                                    if (stock == null)
                                    {
                                        //bill_group_stock newstock = new bill_group_stock
                                        //{
                                        //    BarCode = agvTask.agv_materbarcode,
                                        //    MaterialType = agvTask.agv_materielid,
                                        //    MaterialStatus = "nou",
                                        //    first_tb = 0,
                                        //    location_id = location.id,
                                        //    TB_Status = "",
                                        //    FQ_Status = "",
                                        //    GY_Status = "",
                                        //    QX_Status = "",
                                        //    created_time = DateTime.Now,
                                        //    created_user = "WCS",
                                        //    updated_time = DateTime.Now,
                                        //    updated_user = "WCS"
                                        //};
                                        //group_StockRepository.Add(newstock, true);
                                    }
                                    else
                                    {
                                        if (agvTask.agv_fromaddress.Contains("JJK"))
                                        {
                                            stock.TB_Status = "";
                                            stock.FQ_Status = "";
                                            stock.GY_Status = "";
                                            stock.QX_Status = "";
                                        }
                                        stock.location_id = location.id;
                                        stock.created_time = DateTime.Now;
                                        stock.updated_time = DateTime.Now;
                                        group_StockRepository.Update(stock, true);
                                    }
                                    location.location_state = LocationStateEnum.LocationState_Stored.ToString();
                                    locationRepository.Update(location, true);
                                    if (agvTask.agv_toaddress.Contains("HCJ") || agvTask.agv_toaddress.Contains("CBJ") || agvTask.agv_toaddress.Contains("ZZLJ"))
                                    {
                                        if (!agvTask.agv_fromaddress.Contains("SB"))
                                        {
                                            if (stock.MaterialStatus == "OK")
                                            {
                                                //plcClient.WriteValue(ConveyorLineGetLogDBName.MaterOK.ToString(), agvTask.agv_toaddress, 1);
                                            }
                                            else
                                            {
                                                //plcClient.WriteValue(ConveyorLineGetLogDBName.MaterNG.ToString(), agvTask.agv_toaddress, 1);
                                            }
                                        }
                                    }
                                    //如果终点地址是库内,需要给WMS反馈入库物料绑定
                                    if (agvTask.agv_toaddress.Length < 12)
                                    {
                                        //MESback WMSbackresult = MESAPIInvoke.InStockMaterBind(agvTask.agv_toaddress, stock.MaterialType, stock.BarCode, stock.MaterialStatus, 3);
                                        //if (WMSbackresult.Code > 0) { new Exception(WMSbackresult.Message); return; }
                                    }
                                }
                                else if (agvTask.agv_toaddress.Contains("SB"))
                                {
                                    //plcClient.WriteValue(ConveyorLineGetLogDBName.MaterNG.ToString(), agvTask.agv_fromaddress, 0);
                                    //plcClient.WriteValue(ConveyorLineGetLogDBName.MaterOK.ToString(), agvTask.agv_fromaddress, 0);
                                    //如果终点地址是去设备
                                    group_StockRepository.Delete(stock, true);
                                }
                                else if (agvTask.agv_toaddress.Contains("WLX001"))
                                {
                                    if (agvTask.agv_code.Contains("正"))
                                    {
                                        //添加物料条码,三楼输送线出口取前两个条码
                                        var barcode = tasknumberRep.FindFirst(v => v.taskno == 1);
                                        //barcode.numtype = barcode.numtype + agvTask.agv_materbarcode + ";";
                                        tasknumberRep.Update(barcode, true);
                                    }
                                    else
                                    {
                                        //添加物料条码,三楼输送线出口取前两个条码
                                        var barcode = tasknumberRep.FindFirst(v => v.taskno == 2);
                                        //barcode.numtype = barcode.numtype + agvTask.agv_materbarcode + ";";
                                        tasknumberRep.Update(barcode, true);
                                    }
                                }
                                else if (agvTask.agv_toaddress.Contains("JJK"))
                                {
                                    location.location_state = LocationStateEnum.LocationState_Stored.ToString();
                                    locationRepository.Update(location, true);
                                    if (stock.MaterialStatus == "OK")
                                    {
                                        //plcClient.WriteValue(ConveyorLineGetLogDBName.MaterOK.ToString(), agvTask.agv_toaddress, 0);
                                    }
                                    else
                                    {
                                        //plcClient.WriteValue(ConveyorLineGetLogDBName.MaterNG.ToString(), agvTask.agv_toaddress, 0);
                                    }
                                    if (agvTask.agv_toaddress.Contains("Z"))
                                    {
                                        //MESback WMSbackresult = MESAPIInvoke.InStockMaterBind(agvTask.agv_toaddress, stock.MaterialType, stock.BarCode, stock.MaterialStatus, 1);
                                        //if (WMSbackresult.Code > 0) { new Exception(WMSbackresult.Message); }
                                    }
                                    else if (agvTask.agv_toaddress.Contains("F"))
                                    {
                                        //MESback WMSbackresult = MESAPIInvoke.InStockMaterBind(agvTask.agv_toaddress, stock.MaterialType, stock.BarCode, stock.MaterialStatus, 2);
                                        //if (WMSbackresult.Code > 0) { new Exception(WMSbackresult.Message); }
                                    }
                                }
                            }
                            agvtaskRepository.Delete(agvTask, true);
                            plcClient.WriteValue(TaskDBName.taskFbInteractiveW.ToString(), 1);
                            for (int i = 0; i < 5; i++)
                            {
                                Thread.Sleep(300);
                                var agvnumber = Convert.ToInt32(plcClient.ReadValue(TaskDBName.taskFbInteractiveW.ToString()));
                                if (agvnumber != 1)
                                {
                                    plcClient.WriteValue(TaskDBName.taskFbInteractiveW.ToString(), 1);
                                }
                                else
                                {
                                    break;
                                }
                            }
                            WriteLog.GetLog().Write("WCS写入AGV任务号" + taskId + DateTime.Now + "任务终点完成状态确认1", "AGV更新任务状态");
                        }
                    }
                    //if (taskFbInteractive == 1 && taskFbInteractiveW == 1)//反馈没收到重新反馈
                    //    plcClient.WriteValue(TaskDBName.taskFbInteractiveW.ToString(), 0);
                    if (taskFbInteractive == 0 && taskFbInteractiveW == 1)
                    {
                        plcClient.WriteValue(TaskDBName.taskFbInteractiveW.ToString(), 0);
                        for (int i = 0; i < 5; i++)
                        {
                            Thread.Sleep(300);
                            var agvnumber = Convert.ToInt32(plcClient.ReadValue(TaskDBName.taskFbInteractiveW.ToString()));
                            if (agvnumber != 0)
                            {
                                plcClient.WriteValue(TaskDBName.taskFbInteractiveW.ToString(), 0);
                            }
                            else
                            {
                                break;
                            }
                        }
                    }
 
                }
                catch (Exception ex)
                {
                    WriteLog.GetLog().Write("WCS接收AGV任务反馈异常" + ex.Message.ToString() + DateTime.Now, "AGV更新任务状态");
                }
                finally
                {
                    Interlocked.Exchange(ref _readUpdateAGVTaskSignalso, 0);
                }
            }
 
        }
    }
 
}