wanshenmean
2025-04-08 413b51f88d342dc90d92ebfa2e500b8f660db09c
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
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
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;
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()));
                    int resetTaskFbInteractiver = Convert.ToInt32(plcClient.ReadValue(TaskDBName.resetTaskFbInteractiveR.ToString()));
                    //0初始状态 1 RCS更新了一条任务状态
                    if (resetTaskFbInteractiver == 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;
                            }
                        }
                    }
                    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.Info("AGV更新任务状态").Write("任务号" + taskId + "任务状态" + taskState, "AGV更新任务状态");
                        dt_agvtask agvTask = agvtaskRepository.Find(r => r.agv_tasknum == taskId).FirstOrDefault();
 
                        if (1 == taskState)//任务起点执行中
                        {
                            if (null == agvTask)
                            {
                                plcClient.WriteValue(TaskDBName.taskFbInteractiveW.ToString(), 1);
                                WriteLog.Info("AGV更新任务状态").Write("WCS写入AGV任务号,当前任务号不存在任务列表里," + taskId + DateTime.Now + "任务起点执行中状态确认1", "AGV更新任务状态");
                            }
                            if (agvTask.agv_taskstate == AGVTaskStateEnum.Executing.ToString())
                            {
                                plcClient.WriteValue(TaskDBName.taskFbInteractiveW.ToString(), 1);
                                WriteLog.Info("AGV更新任务状态").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.Info("AGV更新任务状态").Write("WCS写入AGV任务号" + taskId + "任务起点执行中状态确认1", "AGV更新任务状态");
                        }
                        else if (2 == taskState)//任务起点已完成
                        {
                            if (null == agvTask)
                            {
                                plcClient.WriteValue(TaskDBName.taskFbInteractiveW.ToString(), 1);
                                WriteLog.Info("AGV更新任务状态").Write("WCS写入AGV任务号,当前任务号不存在任务列表里," + taskId + DateTime.Now + "任务起点完成状态确认1", "AGV更新任务状态");
                                return;
                            }
                            if (agvTask.agv_taskstate == AGVTaskStateEnum.Complete.ToString())
                            {
                                plcClient.WriteValue(TaskDBName.taskFbInteractiveW.ToString(), 1);
                                WriteLog.Info("AGV更新任务状态").Write("WCS再次写入AGV任务号" + taskId + DateTime.Now + "任务起点完成状态确认1", "AGV更新任务状态");
                            }
                            else if (agvTask.agv_taskstate != AGVTaskStateEnum.Executing.ToString())
                            {
                                plcClient.WriteValue(TaskDBName.taskFbInteractiveW.ToString(), 1);
                                WriteLog.Info("AGV更新任务状态").Write("WCS写入AGV任务号,当前任务号的任务状态不是起点执行中," + taskId + DateTime.Now + "任务起点完成状态确认1", "AGV更新任务状态");
                            }
                            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.Info("AGV更新任务状态").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 = 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 = 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"))
                                    {
                                        if (stock.MaterialStatus=="OK")
                                        {
                                            plcClient.WriteValue(ConveyorLineInfoDBName.MaterOK.ToString(), agvTask.agv_fromaddress, 0);
                                        }
                                        else
                                        {
                                            plcClient.WriteValue(ConveyorLineInfoDBName.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.Info("AGV更新任务状态").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.Info("AGV更新任务状态").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)
                                    {
                                        MESback WMSbackresult = new MESback();
                                        WriteLog.Info("AGV更新任务状态").Write("分拨台调用OutStockMaterMove" + taskId , "AGV更新任务状态");
                                        WMSbackresult = MESAPIInvoke.OutStockMaterMove(agvTask.agv_fromaddress, agvTask.agv_materielid, 4, Convert.ToInt32(iswork.agv_qty));
                                        if (WMSbackresult.Code > 0) { new Exception(WMSbackresult.Message); }
                                    }
                                }
                                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.Info("AGV更新任务状态").Write("WCS写入AGV任务号" + taskId + DateTime.Now + "任务起点完成状态确认1", "AGV更新任务状态");
                            }
                        }
                        else if (3 == taskState)//任务终点执行中
                        {
                            if (null == agvTask)
                            {
                                plcClient.WriteValue(TaskDBName.taskFbInteractiveW.ToString(), 1);
                                WriteLog.Info("AGV更新任务状态").Write("WCS写入AGV任务号,当前任务号不存在任务列表里," + taskId + DateTime.Now + "任务终点执行中状态确认1", "AGV更新任务状态");
                            }
                            if (agvTask.agv_taskstate == AGVTaskStateEnum.Executing1.ToString())
                            {
                                plcClient.WriteValue(TaskDBName.taskFbInteractiveW.ToString(), 1);
                                WriteLog.Info("AGV更新任务状态").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.Info("AGV更新任务状态").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.Info("AGV更新任务状态").Write("WCS写入AGV任务号" + taskId + DateTime.Now + "任务终点执行中状态确认1", "AGV更新任务状态");
                            }
                        }
                        else if (4 == taskState)//任务终点完成
                        {
                            if (null == agvTask)
                            {
                                plcClient.WriteValue(TaskDBName.taskFbInteractiveW.ToString(), 1);
                                WriteLog.Info("AGV更新任务状态").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("JR")) { }
                            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) 
                                {
                                    agvTask.ErrMsg = WMSbackresult.Message;
                                    agvTask.agv_realesstime = DateTime.Now;
                                    agvtaskRepository.Update(agvTask, true);
                                    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.Info("烘烤").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) 
                                {
                                    agvTask.ErrMsg = WMSbackresult.Message;
                                    agvTask.agv_realesstime = DateTime.Now;
                                    agvtaskRepository.Update(agvTask, true);
                                    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) 
                                    {
                                        agvTask.ErrMsg = WMSbackresult.Message;
                                        agvTask.agv_realesstime = DateTime.Now;
                                        agvtaskRepository.Update(agvTask, true);
                                        new Exception(WMSbackresult.Message); return;
                                    }
                                }
                                else
                                {
                                    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);
                                            WriteLog.Info("烘烤").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) 
                                    {
                                        agvTask.ErrMsg = WMSbackresult.Message;
                                        agvTask.agv_realesstime = DateTime.Now;
                                        agvtaskRepository.Update(agvTask, true);
                                        throw new Exception(WMSbackresult.Message); 
                                    }
                                    WebResponseContent content = new WebResponseContent();
                                    content =group_StockRepository.DbContextBeginTransaction(() =>
                                    {
                                        var hxstock1 = group_StockRepository.FindFirst(f => f.BarCode == materbarcode[0]);
                                        group_StockRepository.Delete(hxstock1, true);
                                        var hxstock2 = group_StockRepository.FindFirst(f => f.BarCode == materbarcode[1]);
                                        group_StockRepository.Delete(hxstock2, true);
                                        //如果终点地址是去烘烤箱的删除库存
                                        //location.location_state = LocationStateEnum.LocationState_Empty.ToString();
                                        //    locationRepository.Update(location, true);
                                        return WebResponseContent.Instance.OK();
                                    });
                                    if (content.Status == false) { throw new Exception("删除库存不成功"); }
                                }
                            }
                            else
                            {
                                stock = group_StockRepository.Find(f => f.BarCode == agvTask.agv_materbarcode).OrderByDescending(f => f.created_time).FirstOrDefault();
                                if ((agvTask.agv_toaddress.Contains("JJK") || agvTask.agv_toaddress.Contains("0101"))&&(stock==null||stock.MaterialStatus=="nou"))
                                {
                                    WriteLog.Info("入库库存数据错误").Write(stock.MaterialStatus+"\t"+stock.BarCode+"\t" + taskId, "入库库存数据错误");
                                    stock.MaterialStatus = agvTask.agv_materbarcode.Split('*')[6];
                                    group_StockRepository.Update(stock,true);
                                    throw new Exception("库存数据不对");
                                }
                                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(ConveyorLineInfoDBName.MaterOK.ToString(), agvTask.agv_toaddress, 1);
                                            }
                                            else
                                            {
                                                plcClient.WriteValue(ConveyorLineInfoDBName.MaterNG.ToString(), agvTask.agv_toaddress, 1);
                                            }
                                        }
                                        if (agvTask.agv_toaddress.Contains("TBHCJ")) { group_StockRepository.Delete(stock, true); }
                                    }
                                    //如果终点地址是库内,需要给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) 
                                        {
                                            agvTask.ErrMsg = WMSbackresult.Message;
                                            agvTask.agv_realesstime = DateTime.Now;
                                            agvtaskRepository.Update(agvTask, true);
                                            new Exception(WMSbackresult.Message); return; 
                                        }
                                    }
                                }
                                else if (agvTask.agv_toaddress.Contains("SB"))
                                {
                                    plcClient.WriteValue(ConveyorLineInfoDBName.MaterNG.ToString(), agvTask.agv_fromaddress, 0);
                                    plcClient.WriteValue(ConveyorLineInfoDBName.MaterOK.ToString(), agvTask.agv_fromaddress, 0);
                                    if (agvTask.agv_toaddress.Contains("TBSB"))
                                    {
                                        char[] batchnos = new char[100];
                                        string materialtype = agvTask.agv_materbarcode.Split('*')[0];
                                        for (int i = 0; i < materialtype.Length; i++)
                                        {
                                            batchnos[i] = materialtype[i];
                                        }
                                            PLCClient plc = WCSService.Clients.Find(v => v.PLCDescroption == agvTask.agv_toaddress);
                                            string ismateriala = plc.WriteValue(ConveyorLineInfoDBName.W_TBSB_BatchNo.ToString(), agvTask.agv_toaddress, batchnos).ToString();
                                        WriteLog.Info("W_TBSB_BatchNo").Write("W_TBSB_BatchNo" + agvTask.agv_tasknum +"\t"+agvTask.agv_toaddress+"\t"+materialtype + DateTime.Now, "W_TBSB_BatchNo");
                                    }
                                    else if(agvTask.agv_toaddress.Contains("GYSB"))
                                    {
                                        PLCClient plc = WCSService.Clients.Find(v => v.PLCDescroption == agvTask.agv_toaddress);
                                        plc.WriteValue(ConveyorLineInfoDBName.W_GYSB_BatchNo.ToString(), agvTask.agv_toaddress, "                    ").ToString();
                                        string materialtype = agvTask.agv_materbarcode.Split('*')[0];
                                        string ismateriala = plc.WriteValue(ConveyorLineInfoDBName.W_GYSB_BatchNo.ToString(), agvTask.agv_toaddress, materialtype).ToString();
                                        WriteLog.Info("W_GYSB_BatchNo").Write("W_GYSB_BatchNo" + agvTask.agv_tasknum + "\t" + agvTask.agv_toaddress + "\t" + materialtype + DateTime.Now, "W_GYSB_BatchNo");
                                    }
                                    else if (agvTask.agv_toaddress.Contains("FTSB"))
                                    {
                                        PLCClient plc = WCSService.Clients.Find(v => v.PLCDescroption == agvTask.agv_toaddress);
                                        plc.WriteValue(ConveyorLineInfoDBName.W_FQSB_BatchNo.ToString(), agvTask.agv_toaddress, "                    ").ToString();
                                        string materialtype = agvTask.agv_materbarcode.Split('*')[0];
                                        string ismateriala = plc.WriteValue(ConveyorLineInfoDBName.W_FQSB_BatchNo.ToString(), agvTask.agv_toaddress, materialtype).ToString();
                                        WriteLog.Info("W_FQSB_BatchNo").Write("W_FQSB_BatchNo" + agvTask.agv_tasknum + "\t" + agvTask.agv_toaddress + "\t" + materialtype + DateTime.Now, "W_FQSB_BatchNo");
                                    }
                                    //如果终点地址是去设备
                                    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);
                                        WriteLog.Info("正极烘箱保存条码").Write(barcode.numtype+"\t" + DateTime.Now, "正极烘箱保存条码");
                                    }
                                    else
                                    {
                                        //添加物料条码,三楼输送线出口取前两个条码
                                        var barcode = tasknumberRep.FindFirst(v => v.taskno == 2);
                                        barcode.numtype = barcode.numtype + agvTask.agv_materbarcode + ";";
                                        tasknumberRep.Update(barcode, true);
                                        WriteLog.Info("负极烘箱保存条码").Write(barcode.numtype + "\t" + DateTime.Now, "负极烘箱保存条码");
                                    }
                                }
                                else if (agvTask.agv_toaddress.Contains("JJK"))
                                {
                                    location.location_state = LocationStateEnum.LocationState_Stored.ToString();
                                    locationRepository.Update(location, true);
                                    if (stock.MaterialStatus == "OK")
                                    {
                                        plcClient.WriteValue(ConveyorLineInfoDBName.MaterOK.ToString(), agvTask.agv_toaddress, 0);
                                    }
                                    else
                                    {
                                        plcClient.WriteValue(ConveyorLineInfoDBName.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) 
                                        {
                                            agvTask.ErrMsg = WMSbackresult.Message;
                                            agvTask.agv_realesstime = DateTime.Now;
                                            agvtaskRepository.Update(agvTask, true);
                                            return; 
                                        }
                                    }
                                    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) 
                                        {
                                            agvTask.ErrMsg = WMSbackresult.Message;
                                            agvTask.agv_realesstime = DateTime.Now;
                                            agvtaskRepository.Update(agvTask, true);
                                            return; 
                                        }
                                    }
                                }
                            }
 
                            // 上传AGV运行数据 by xiaoyang
                            SendMESTask.SendMesTask(agvTask, 1);
 
                            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.Info("AGV更新任务状态").Write("WCS写入AGV任务号" + taskId + DateTime.Now + "任务终点完成状态确认1", "AGV更新任务状态");
                        }
                        else if (5 == taskState)
                        {
                            if (null == agvTask)
                            {
                                plcClient.WriteValue(TaskDBName.taskFbInteractiveW.ToString(), 1);
                                WriteLog.Info("AGV更新任务状态").Write("WCS写入AGV任务号,当前任务号不存在任务列表里," + taskId + DateTime.Now + "任务取消状态确认1", "AGV更新任务状态");
                            }
                            if (agvTask.agv_taskstate == AGVTaskStateEnum.Executing.ToString())
                            {
 
                                dt_agvtask agv = new dt_agvtask()
                                {
                                    agv_id = new Guid(),
                                    agv_barcode = agvTask.agv_barcode,
                                    agv_code = agvTask.agv_code,
                                    agv_createtime = DateTime.Now,
                                    agv_fromaddress = agvTask.agv_fromaddress,
                                    agv_grade = agvTask.agv_grade,
                                    agv_materbarcode = agvTask.agv_materbarcode,
                                    agv_materielid = agvTask.agv_materielid,
                                    agv_qty = agvTask.agv_qty,
                                    agv_remark = agvTask.agv_remark,
                                    agv_taskstate = "Create",
                                    agv_tasktype = agvTask.agv_tasktype,
                                    agv_toaddress = agvTask.agv_toaddress,
                                    agv_userid = agvTask.agv_userid,
                                    agv_worktype = agvTask.agv_worktype
                                };
                                if (agv.agv_fromaddress.Contains("JJK")&&agv.agv_toaddress.Contains("TBHCJ"))
                                {
                                    agv.agv_tasknum = "KH-" + tasknumber.GetTaskNumber(tasknumberRep)+agvTask.agv_tasknum.Substring(agvTask.agv_tasknum.Length-2,2);
                                }
                                else
                                {
                                    agv.agv_tasknum = "KH-" + tasknumber.GetTaskNumber(tasknumberRep);
                                }
                                agvtaskRepository.Add(agv, true);
                            }
                            else if (agvTask.agv_taskstate == AGVTaskStateEnum.Complete.ToString() || agvTask.agv_taskstate == AGVTaskStateEnum.Executing1.ToString() || agvTask.agv_taskstate == AGVTaskStateEnum.Complete1.ToString())
                            {
                                //如果是设备就不用更改,是缓存架货位状态为已存储,(库存表)location_id=(货位表)的货位
                                base_ware_location fromlocation = locationRepository.FindFirst(f => f.upper_code == agvTask.agv_fromaddress || f.down_code == agvTask.agv_fromaddress);
                                if (fromlocation!=null)
                                {
                                    fromlocation.location_state = LocationStateEnum.LocationState_Empty.ToString();
                                    locationRepository.Update(fromlocation,true);
                                }
                                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_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) 
                                    {
                                        agvTask.ErrMsg = WMSbackresult.Message.ToString();
                                        agvtaskRepository.Update(agvTask, true);
                                        new Exception(WMSbackresult.Message);
                                        return;
                                    }
                                }
                                else if (agvTask.agv_toaddress.Contains("JR"))
                                {
                                    if (agvTask.agv_taskstate == AGVTaskStateEnum.Complete.ToString()) 
                                    {
                                        var iswork = agvtaskRepository.FindFirst(f => f.agv_remark == "true" && f.agv_tasknum == taskId);
                                        if (iswork != null)
                                        {
                                            MESback WMSbackresult = new MESback();
                                            WMSbackresult = MESAPIInvoke.OutStockMaterMove(agvTask.agv_fromaddress, agvTask.agv_materielid, 4, Convert.ToInt32(iswork.agv_qty), agvTask.agv_tasknum);
                                            if (WMSbackresult.Code > 0) { new Exception(WMSbackresult.Message); return; }
                                        }
                                    }
 
                                }
                                else if (agvTask.agv_toaddress.Contains("LJHCX")) { }
                                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
                                    {
                                        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);
                                            WriteLog.Info("烘烤").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) { throw new Exception(WMSbackresult.Message); }
                                        WebResponseContent content = new WebResponseContent();
                                        content = group_StockRepository.DbContextBeginTransaction(() =>
                                        {
                                            var hxstock1 = group_StockRepository.FindFirst(f => f.BarCode == materbarcode[0]);
                                            group_StockRepository.Delete(hxstock1, true);
                                            var hxstock2 = group_StockRepository.FindFirst(f => f.BarCode == materbarcode[1]);
                                            group_StockRepository.Delete(hxstock2, true);
                                            //如果终点地址是去烘烤箱的删除库存
                                            //location.location_state = LocationStateEnum.LocationState_Empty.ToString();
                                            //    locationRepository.Update(location, true);
                                            return WebResponseContent.Instance.OK();
                                        });
                                        if (content.Status == false) { throw new Exception("删除库存不成功"); }
                                    }
                                }
                                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(ConveyorLineInfoDBName.MaterOK.ToString(), agvTask.agv_toaddress, 1);
                                                }
                                                else
                                                {
                                                    plcClient.WriteValue(ConveyorLineInfoDBName.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(ConveyorLineInfoDBName.MaterNG.ToString(), agvTask.agv_fromaddress, 0);
                                        plcClient.WriteValue(ConveyorLineInfoDBName.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(ConveyorLineInfoDBName.MaterOK.ToString(), agvTask.agv_toaddress, 0);
                                        }
                                        else
                                        {
                                            plcClient.WriteValue(ConveyorLineInfoDBName.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) { return; }
                                        }
                                        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) { return; }
                                        }
                                    }
                                }
                            }
                            agvtask_HtyRepository.AddTaskHistory(agvTask, OperateType.Cancel.ToString());
                            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.Info("AGV更新任务状态").Write("WCS写入AGV任务号" + taskId + DateTime.Now + "任务异常状态确认1", "AGV更新任务状态");
                            agvtaskRepository.Delete(agvTask, true);
                        }
                    }
                    //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.Info("AGV更新任务状态").Write("WCS接收AGV任务反馈异常" + ex.Message.ToString() + DateTime.Now, "AGV更新任务状态");
                }
                finally
                {
                    Interlocked.Exchange(ref _readUpdateAGVTaskSignalso, 0);
                }
            }
 
        }
    }
 
}