分支自 SuZhouGuanHong/TaiYuanTaiZhong

dengjunjie
2024-07-03 a05b17c400067475a256071c2e92a9f1b3abe343
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
using HslCommunication;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
using WIDESEA_Comm;
using WIDESEA_Comm.LogInfo;
using WIDESEA_Core.BaseProvider;
using WIDESEA_Core.EFDbContext;
using WIDESEA_Entity.DomainModels;
using WIDESEA_WCS.IRepositories;
using WIDESEA_WCS.JobsPart.Common;
using WIDESEA_WCS.Repositories;
using WIDESEA_WCS.WCSClient;
using WIDESEA_WMS.IRepositories;
using WIDESEA_WMS.Repositories;
using static System.Collections.Specialized.BitVector32;
 
namespace WIDESEA_WCS
{
    public class Gantry2
    {
        #region 视觉检测追溯车轮信息
        /// <summary>
        /// 视觉检测追溯车轮信息
        /// </summary>
        /// <param name="client"></param>
        public void QuerySJSN(PLCClient client)
        {
            VOLContext Context = new VOLContext();
            Idt_info_traceRepository traceRepository = new dt_info_traceRepository(Context);
            List<string> names = new List<string>() { "视觉检测1", "视觉检测2", "视觉检测3", "视觉检测4" };
            foreach (string name in names)
            {
                try
                {
                    var Read_Load = client.ReadByOrder<bool>("Read_Load", name);//视觉检测开始信号
                    if (Read_Load)
                    {
                        var PartID_Load = client.ReadByOrder<string>("PartID_Load", name);//车轮SN号
                        var State_Load = client.ReadByOrder<Int16>("State_Load ", name);//车轮状态
                        var LoadTime = client.ReadByOrder<string>("LoadTime ", name);//视觉检测开始时间
                        var time = DateTime.ParseExact(LoadTime, "yyyyMMddHHmmss", CultureInfo.CurrentCulture);
                        var info_trace = traceRepository.Find(x => x.trace_SN == PartID_Load).FirstOrDefault();
                        if (info_trace != null)
                        {
                            info_trace.trace_tcvm_number = name;
                            info_trace.trace_tcvm_start_time = time;
                            traceRepository.Update(info_trace, x => new { x.trace_tcvm_number, x.trace_tcvm_start_time }, true);
                        }
                        client.WriteByOrder("ReadFinished_Load", true, name);
                    }
                    else
                        client.WriteByOrder("ReadFinished_Load", false, name);
                    var Read_Unload = client.ReadByOrder<bool>("Read_Unload", name);//视觉检测完成信号
                    if (Read_Unload)
                    {
                        var PartID_Unload = client.ReadByOrder<string>("PartID_Unload", name);//车轮SN号
                        var State_Unload = client.ReadByOrder<Int16>("State_Unload  ", name);//车轮状态
                        var UnloadTime = client.ReadByOrder<string>("UnloadTime ", name);//视觉检测开始时间
                        var info_trace = traceRepository.Find(x => x.trace_SN == PartID_Unload).FirstOrDefault();
                        var time = DateTime.ParseExact(UnloadTime, "yyyyMMddHHmmss", CultureInfo.CurrentCulture);
                        if (info_trace != null)
                        {
                            info_trace.trace_tcvm_finish_time = time;
                            info_trace.trace_tcvm_statu = State_Unload;
                            traceRepository.Update(info_trace, x => new { x.trace_tcvm_finish_time, x.trace_tcvm_statu }, true);
                        }
                        client.WriteByOrder("ReadFinished_Unload", true, name);
                    }
                    else
                        client.WriteByOrder("ReadFinished_Unload", false, name);
                }
                catch (Exception ex)
                {
                    WriteLog.Write_Log("报错日志", name, "错误信息!", $"错误信息:{ex.Message}");
                }
            }
        }
        #endregion
 
        #region 模式2查询车轮订单和车轮参数
        /// <summary>
        /// 模式2查询车轮订单和车轮参数
        /// </summary>
        /// <param name="client"></param>
        public void QueryWheelOrderData(PLCClient client)
        {
            VOLContext Context = new VOLContext();
            Idt_geometry_dataRepository dataRepository = new dt_geometry_dataRepository(Context);
            IVV_Mes_WorkinfoRepository workinfoRepository = new VV_Mes_WorkinfoRepository(Context);
            List<string> names = new List<string>() { "1单元申请查询订单和车轮参数", "2单元申请查询订单和车轮参数", "检测上料申请查询订单和车轮参数" };
            foreach (var name in names)
            {
                try
                {
                    var oi_on = client.ReadByOrder<Byte>("oi_on", name); //申请
 
                    if (oi_on == 1)
                    {
                        var wp_id = client.ReadByOrder<string>("o_wp_id", name);
 
                        #region SN号不能为空
                        if (string.IsNullOrEmpty(wp_id))
                        {
                            client.WriteByOrder("i_status", (byte)0, name);
                            WriteLog.Write_Log(name, wp_id, "错误信息!", $"读取桁架信息:\n车轮SN:{wp_id}\n\n写入桁架信息:\ni_status:2\noi_on:0\n\n车轮SN号为空");
                            continue;
                        }
                        #endregion
 
                        #region 工单不能为空
                        var Work = workinfoRepository.Find(x => x.SN == wp_id && (name.Contains("检测上料") ? x.processCode == "28" : x.processCode == "17")).OrderByDescending(x => x.CreateTime).FirstOrDefault();
                        if (Work == null)
                        {
                            client.WriteByOrder("i_status", (byte)2, name);
                            WriteLog.Write_Log(name, wp_id, "错误信息!", $"读取桁架信息:\n车轮SN:{wp_id}\n\n写入桁架信息:\ni_status:2\noi_on:0\n\n未查询到工单详情");
                            continue;
                        }
                        #endregion
 
                        #region 车轮参数不能为空
                        var geometry = Pipeline.QueryMateriel(Work.drawingNo);
                        if (geometry == null)
                        {
                            client.WriteByOrder("i_status", (byte)3, name);
                            WriteLog.Write_Log(name, wp_id, "错误信息!", $"读取桁架信息:\n车轮SN:{wp_id}\n\n写入桁架信息:\ni_status:3\noi_on:0\n\n图号:{Work.drawingNo}未查询到车轮信息参数");
                            continue;
                        }
                        #endregion
 
                        #region 写入参数
                        client.WriteByOrder("i_stackNoRange", geometry.stackNoRange, name);//打包数量
                        client.WriteByOrder("i_drawing_id", Work.drawingNo, name);//图号
                        client.WriteByOrder("i_job_id", Work.jobID, name);//工单编号
                        client.WriteByOrder("i_heat_id", Work.heatID, name);//炉代号
                        client.WriteByOrder("i_batch_id", Work.heatBatchID, name);//热处理批次
 
                        client.WriteByOrder("i_parameter_a", (float)geometry.a, name);
                        client.WriteByOrder("i_parameter_b", (float)geometry.b, name);
                        client.WriteByOrder("i_parameter_c", (float)geometry.c, name);
                        client.WriteByOrder("i_parameter_d", (float)geometry.d, name);
                        client.WriteByOrder("i_parameter_e", (float)geometry.e, name);
                        client.WriteByOrder("i_parameter_f", (float)geometry.f, name);
                        client.WriteByOrder("i_parameter_g", (float)geometry.g, name);
                        client.WriteByOrder("i_parameter_h", (float)geometry.h, name);
 
                        client.WriteByOrder("i_status", (byte)1, name);
                        client.WriteByOrder("i_wp_id", wp_id, name);
                        #endregion
                    }
                }
                catch (Exception ex)
                {
                    WriteLog.Write_Log("报错日志", name, "错误信息!", $"错误信息:{ex.Message}");
                }
            }
        }
        #endregion
 
        #region 模式2取放料完成
        /// <summary>
        /// 模式2取放料完成
        /// </summary>
        /// <param name="client"></param>
        public void Finished(PLCClient client)
        {
            VOLContext Context = new VOLContext();
            Idt_stationinfoRepository stationinfoRepository = new dt_stationinfoRepository(Context);
            IVV_Mes_WorkinfoRepository workinfoRepository = new VV_Mes_WorkinfoRepository(Context);
            Idt_mes_detailRepository mes_DetailRepository = new dt_mes_detailRepository(Context);
            List<string> names = new List<string>() { "1单元取料放料", "2单元取料放料" };
            foreach (string name in names)
            {
                try
                {
                    var areaCode = Pipeline.area_code(name.Contains("1单元") ? "1单元下料区" : "2单元下料区");
 
 
                    var R_Finished = client.ReadByOrder<Int16>("R_Finished", name); //1:取料完成;2:放料完成
                    var W_Update_Finished = client.ReadByOrder<Int16>("W_Update_Finished", name);//1:取料完成;2:放料完成
 
                    #region 复位完成信号
                    if (R_Finished != 1 && W_Update_Finished == 1 || R_Finished != 2 && W_Update_Finished == 2)
                    {
                        client.WriteByOrder("W_Update_Finished", (Int16)0, name);
                    }
                    #endregion
 
                    #region 取料完成
                    if (R_Finished == 1 && W_Update_Finished != 1)
                    {
                        var R_Wheel_id = client.ReadByOrder<string>("R_Wheel_id", name);//车轮SN号
                        var R_In_Clamp_Area = client.ReadByOrder<Int16>("R_In_Clamp_Area", name);//下料位号
                        var R_IndexNr = client.ReadByOrder<Int16>("R_IndexNr", name); //托盘上的第几个车轮
                        var station = stationinfoRepository.Find(x => x.area == areaCode && x.stationCode.Substring(x.stationCode.Length - 1, 1) == R_In_Clamp_Area.ToString()).FirstOrDefault();
                        if (station != null)
                        {
                            if (!station.bindSN.Contains(R_Wheel_id))
                            {
                                client.WriteByOrder("W_IsError", (Int16)5, name);//报警
                                client.WriteByOrder("W_Wheel_id", R_Wheel_id, name);//车轮SN号
                                throw new Exception($"取料完成失败!下料位号:{station.stationCode}\nW_IsError:{5}\n车轮SN号:{R_Wheel_id}\n取第几个车轮:{R_IndexNr}\n下料位车轮数:{station.quantity}\n错误信息:当前下料位无此车轮!");
                            }
                            var bindSNS = station.bindSN.Split(",");
                            Updatemes(R_Wheel_id, name);
                            station.bindSN = OperStr(bindSNS, R_Wheel_id);
                            var billets = station.billetID.Split(",");
                            station.billetID = OperStr(billets);
                            station.quantity = station.quantity - 1;
                            if (station.quantity <= 0)
                            {
                                station.stationType = string.Empty;
                                station.Number = string.Empty;
                                station.heatNumber = string.Empty;
                                station.tray_status = TrayStateEnum.EmptyTray.ToString();
                            }
                            var count = stationinfoRepository.Update(station, true);
                            if (count < 1) throw new Exception($"取料完成信息更新失败!下料位编号:{station.stationCode}");
 
                            client.WriteByOrder("W_Update_Finished", (Int16)1, name);//1:取料更新完成;2:放料更新完成
                            client.WriteByOrder("W_In_Clamp_Area", R_In_Clamp_Area, name);//下料位号
                            client.WriteByOrder("W_IndexNr", R_IndexNr, name);//托盘上的第几个车轮
                            client.WriteByOrder("W_Wheel_id", R_Wheel_id, name);//车轮SN号
                        }
                        WriteLog.Write_Log("模式2" + name + "取料完成", station.stationCode, $"车轮SN号:{R_Wheel_id}", $"下料位号:{R_In_Clamp_Area}");
                    }
                    #endregion
 
                    #region 放料完成
                    else if (R_Finished == 2 && W_Update_Finished != 2)
                    {
                        var R_Wheel_id = client.ReadByOrder<string>("R_Wheel_id", name);//车轮SN号
                        var R_In_Clamp_Area = client.ReadByOrder<Int16>("R_In_Clamp_Area", name);//下料位号
                        var R_IndexNr = client.ReadByOrder<Int16>("R_IndexNr", name); //托盘上的第几个车轮
                        var station = stationinfoRepository.Find(x => x.area == areaCode && x.stationCode.Substring(x.stationCode.Length - 1, 1) == R_In_Clamp_Area.ToString()).FirstOrDefault();
                        if (station != null)
                        {
                            if (!string.IsNullOrEmpty(station.bindSN))
                            {
                                if (station.bindSN.Contains(R_Wheel_id))
                                {
                                    client.WriteByOrder("W_IsError", (Int16)5, name);//报警
                                    client.WriteByOrder("W_Wheel_id", R_Wheel_id, name);//车轮SN号
                                    throw new Exception($"放料完成失败!下料位号:{station.stationCode}\nW_IsError:{5}\n车轮SN号:{R_Wheel_id}\n放第几个车轮:{R_IndexNr}\n下料位车轮数:{station.quantity}\n错误信息:已存在车轮SN号!");
                                }
                            }
                            var Mes_Work = workinfoRepository.Find(x => x.SN == R_Wheel_id && x.processCode == "17").OrderByDescending(x => x.CreateTime).FirstOrDefault();
                            #region 货位状态更新
                            station.bindSN = string.IsNullOrEmpty(station.bindSN) ? R_Wheel_id : station.bindSN + "," + R_Wheel_id;
                            station.billetID = string.IsNullOrEmpty(station.billetID) ? Mes_Work.billetID.ToString() : station.billetID + "," + Mes_Work.billetID;
                            station.quantity++;
                            if (station.quantity == 1)
                            {
                                station.stationType = Mes_Work.drawingNo;
                                station.Number = Mes_Work.workOrder;
                                station.heatNumber = Mes_Work.heatID;
                                station.tray_status = TrayStateEnum.StrogeTray.ToString();
                            }
                            var count = stationinfoRepository.Update(station, true);
                            if (count < 1)
                                throw new Exception($"放料完成信息更新失败!下料位编号:{station.stationCode}");
                            #endregion
 
                            #region 修改工单状态
                            var Detail = mes_DetailRepository.FindFirst(x => x.jobID == Mes_Work.jobID && x.SN == Mes_Work.SN);
                            Detail.Status = "下线";
                            Detail.FinishTime = DateTime.Now;
                            count = mes_DetailRepository.Update(Detail, x => new { x.Status, x.FinishTime }, true);
                            if (count < 1)
                                throw new Exception($"工单子表信息更新失败!车轮SN号:{Detail.SN}");
                            #endregion
 
                            client.WriteByOrder("W_Update_Finished", (Int16)2, name);//1:取料更新完成;2:放料更新完成
                            client.WriteByOrder("W_In_Clamp_Area", R_In_Clamp_Area, name);//下料位号
                            client.WriteByOrder("W_IndexNr", R_IndexNr, name);//托盘上的第几个车轮
                            client.WriteByOrder("W_Wheel_id", R_Wheel_id, name);//车轮SN号
                        }
                        WriteLog.Write_Log("模式2" + name + "放料完成", station.stationCode, $"车轮SN号:{R_Wheel_id}", $"下料位号:{R_In_Clamp_Area}");
                    }
                    #endregion
 
                    var R_Request = client.ReadByOrder<Int16>("R_Request", name);//1:取料;2:放料
                    #region 取料
                    if (R_Request == 1)
                    {
                        var R_Wheel_id = client.ReadByOrder<string>("R_Wheel_id", name);//车轮SN号
                        var R_In_Clamp_Area = client.ReadByOrder<Int16>("R_In_Clamp_Area", name);//下料位号
                        var R_IndexNr = client.ReadByOrder<Int16>("R_IndexNr", name); //托盘上的第几个车轮
                        var station = stationinfoRepository.Find(x => x.area == areaCode && x.stationCode.Substring(x.stationCode.Length - 1, 1) == R_In_Clamp_Area.ToString()).FirstOrDefault();
                        if (station != null)
                        {
                            var Work = workinfoRepository.Find(x => x.SN == R_Wheel_id && x.processCode == "28").OrderByDescending(x => x.CreateTime).FirstOrDefault();
                            if (Work == null)
                            {
                                client.WriteByOrder("W_IsError", (Int16)5, name);//报警
                                client.WriteByOrder("W_Wheel_id", R_Wheel_id, name);//车轮SN号
                                throw new Exception($"申请取料失败!下料位号:{station.stationCode}\nW_IsError:{5}\n车轮SN号:{R_Wheel_id}\n取第几个车轮:{R_IndexNr}\n下料位车轮数:{station.quantity}\n错误信息:未查询到检测工单信息!");
                            }
                            if (!station.bindSN.Contains(R_Wheel_id))
                            {
                                client.WriteByOrder("W_IsError", (Int16)5, name);//报警
                                client.WriteByOrder("W_Wheel_id", R_Wheel_id, name);//车轮SN号
                                throw new Exception($"申请取料失败!下料位号:{station.stationCode}\nW_IsError:{5}\n车轮SN号:{R_Wheel_id}\n取第几个车轮:{R_IndexNr}\n下料位车轮数:{station.quantity}\n错误信息:当前下料位无此车轮!");
                            }
                            if (station.location_state != LocationStateEnum.Stroge.ToString())
                            {
                                client.WriteByOrder("W_IsError", (Int16)4, name);//取料不允许
                                client.WriteByOrder("W_Wheel_id", R_Wheel_id, name);//车轮SN号
                                throw new Exception($"申请取料失败!下料位号:{station.stationCode}\nW_IsError:{4}\n车轮SN号:{R_Wheel_id}\n取第几个车轮:{R_IndexNr}\n下料位车轮数:{station.quantity}\n错误信息:下料位货位状态不为有货!");
                            }
                            if (station.quantity != R_IndexNr)
                            {
                                client.WriteByOrder("W_IsError", (Int16)5, name);//报警
                                client.WriteByOrder("W_Wheel_id", R_Wheel_id, name);//车轮SN号
                                throw new Exception($"申请取料失败!下料位号:{station.stationCode}\nW_IsError:{5}\n车轮SN号:{R_Wheel_id}\n取第几个车轮:{R_IndexNr}\n下料位车轮数:{station.quantity}\n错误信息:机械手抓取车轮与下料位数量不匹配!");
                            }
                            client.WriteByOrder("W_In_Clamp_Area", (Int16)R_In_Clamp_Area, name);
                            client.WriteByOrder("W_IndexNr", (Int16)R_IndexNr, name);
                            client.WriteByOrder("W_IsError", (Int16)3, name);//取料允许
                            client.WriteByOrder("W_Wheel_id", R_Wheel_id, name);//车轮SN号
                        }
                        else
                        {
                            client.WriteByOrder("W_IsError", (Int16)5, name);//报警
                            client.WriteByOrder("W_Wheel_id", R_Wheel_id, name);//车轮SN号
                            throw new Exception($"申请取料失败!下料位号:{R_In_Clamp_Area};W_IsError:{5},未匹配到下料位号!");
                        }
                        WriteLog.Write_Log("模式2" + name + "申请取料", station.stationCode, $"W_IsError:{3},车轮SN号:{R_Wheel_id}", $"下料位号:{R_In_Clamp_Area}\n取第几个车轮:{R_IndexNr};\n下料位车轮数:{station.quantity}");
                    }
                    #endregion
 
                    #region 放料
                    else if (R_Request == 2)
                    {
                        var R_Wheel_id = client.ReadByOrder<string>("R_Wheel_id", name);//车轮SN号
                        var R_In_Clamp_Area = client.ReadByOrder<Int16>("R_In_Clamp_Area", name);//下料位号
                        var R_IndexNr = client.ReadByOrder<Int16>("R_IndexNr", name); //托盘上的第几个车轮
                        var R_PartStatus = client.ReadByOrder<Int16>("R_PartStatus", name);//车轮状态
                        var station = stationinfoRepository.Find(x => x.area == areaCode && x.stationCode.Substring(x.stationCode.Length - 1, 1) == R_In_Clamp_Area.ToString()).FirstOrDefault();
                        if (station != null)
                        {
                            if (!string.IsNullOrEmpty(station.bindSN))
                            {
                                if (station.bindSN.Contains(R_Wheel_id))
                                {
                                    client.WriteByOrder("W_IsError", (Int16)5, name);//报警
                                    client.WriteByOrder("W_Wheel_id", R_Wheel_id, name);//车轮SN号
                                    throw new Exception($"申请放料失败!下料位号:{station.stationCode}\nW_IsError:{5}\n车轮SN号:{R_Wheel_id}\n放第几个车轮:{R_IndexNr}\n下料位车轮数:{station.quantity}\n错误信息:已存在车轮SN号!");
                                }
                            }
                            if (station.location_state != LocationStateEnum.Stroge.ToString())
                            {
                                client.WriteByOrder("W_IsError", (Int16)2, name);//放料不允许
                                client.WriteByOrder("W_Wheel_id", R_Wheel_id, name);//车轮SN号
                                throw new Exception($"申请放料失败!下料位号:{station.stationCode}\nW_IsError:{2}\n车轮SN号:{R_Wheel_id}\n放第几个车轮:{R_IndexNr}\n下料位车轮数:{station.quantity}\n错误信息:下料位货位状态不为有货!");
                            }
                            if (station.quantity + 1 != R_IndexNr)
                            {
                                client.WriteByOrder("W_IsError", (Int16)5, name);//报警
                                client.WriteByOrder("W_Wheel_id", R_Wheel_id, name);//车轮SN号
                                throw new Exception($"申请放料失败!下料位号:{station.stationCode}\nW_IsError:{5}\n车轮SN号:{R_Wheel_id}\n放第几个车轮:{R_IndexNr}\n下料位车轮数:{station.quantity}\n错误信息:机械手放料车轮与下料位数量不匹配!");
                            }
                            client.WriteByOrder("W_In_Clamp_Area", (Int16)R_In_Clamp_Area, name);
                            client.WriteByOrder("W_IndexNr", (Int16)R_IndexNr, name);
                            client.WriteByOrder("W_IsError", (Int16)1, name);//放料允许
                            client.WriteByOrder("W_Wheel_id", R_Wheel_id, name);//车轮SN号
                        }
                        else
                        {
                            client.WriteByOrder("W_IsError", (Int16)5, name);//报警
                            client.WriteByOrder("W_Wheel_id", R_Wheel_id, name);//车轮SN号
                            throw new Exception($"申请放料失败!下料位号:{R_In_Clamp_Area};未匹配到下料位号!");
                        }
                        WriteLog.Write_Log("模式2" + name + "申请放料", station.stationCode, $"W_IsError:{1},车轮SN号:{R_Wheel_id}", $"下料位号:{R_In_Clamp_Area}\n放第几个车轮:{R_IndexNr}\n下料位车轮数:{station.quantity}\n车轮状态:{R_PartStatus}");
                    }
                    #endregion
 
 
                }
                catch (Exception ex)
                {
                    WriteLog.Write_Log("报错日志", name, "错误信息!", $"错误信息:{ex.Message}");
                }
            }
        }
        #endregion
 
        #region 模式2NG车轮下料
        /// <summary>
        /// 模式2NG车轮下料(合并到模式2取放料完成)
        /// </summary>
        /// <param name="client"></param>
        public void NGLayofflevel(PLCClient client)
        {
            VOLContext Context = new VOLContext();
            Idt_stationinfoRepository stationinfoRepository = new dt_stationinfoRepository(Context);
            Idt_mes_detailRepository mes_DetailRepository = new dt_mes_detailRepository(Context);
            Idt_plcinfodetailRepository plcRepository = new dt_plcinfodetailRepository(Context);
            Idt_plcinfoheadRepository repository = new dt_plcinfoheadRepository(Context);
            IVV_Mes_WorkinfoRepository workinfoRepository = new VV_Mes_WorkinfoRepository(Context);
            var Pipeline_client = PLCClient.Clients.FirstOrDefault(t => t.PLCName == "链条机");
            if (Pipeline_client == null) throw new Exception("链条机调度服务未开启!");
            if (!Pipeline_client.IsConnected) throw new Exception("与链条机连接超时!");
            var Pipelineplc = repository.FindFirst(x => x.plcinfo_name == Pipeline_client.PLCName);
            List<string> names = new List<string>() { "X01001003", "X02001003" };
            foreach (var name in names)
            {
                try
                {
                    var Area = name == "X01001003" ? "5" : "6";
                    #region 下料完成
                    var finished = client.ReadByOrder<Boolean>("o_load_finished ", name);//放料完成
                    var updatefinished = client.ReadByOrder<Boolean>("i_storage_update ", name);//货位状态更新
 
                    #region 货位状态更新复位
                    if (!finished && updatefinished)
                    {
                        client.WriteByOrder("i_storage_update", false, name);//货位状态更新
                        #region 日志记录
                        WriteLog.Write_Log("模式2" + name + "放料完成", name + "货位状态更新信号复位", "成功!",
                            $"读取桁架信息:\n放料完成信号:{finished}\n\n写入桁架信息:\n货位状态更新信号:{false}");
                        #endregion
                    }
                    #endregion
 
                    if (finished && !updatefinished)
                    {
                        client.WriteByOrder("i_enabl_load", false, name);//是否允许
 
                        #region 读取桁架信息
                        var WheelType = client.ReadByOrder<Int16>("o_wheel_type ", name); //车轮类型
                        var Wheel_SN = client.ReadByOrder<string>("o_wheel_id", name);//车轮SN号
                        if (string.IsNullOrEmpty(Wheel_SN))
                        {
                            WriteLog.Write_Log("模式2" + name + "放料完成出错", "车轮SN号为空", "读取桁架信息:", $"车轮SN号:{Wheel_SN}为空");
                            continue;
                        }
 
                        var Mes_Work = workinfoRepository.Find(x => x.SN == Wheel_SN && x.processCode == "17").OrderByDescending(x => x.CreateTime).FirstOrDefault();
                        if (Mes_Work == null)
                        {
                            WriteLog.Write_Log("模式2" + name + "放料完成出错", Wheel_SN, "读取桁架信息:", $"车轮SN号:{Wheel_SN}未查询到工单信息");
                            continue;
                        }
                        #endregion
 
                        var AreaNr = client.ReadByOrder<Int16>("i_areaNr", name);
                        if (AreaNr == 0)
                        {
                            client.WriteByOrder("i_storage_update", true, name);//货位状态更新
                            continue;
                        }
 
                        #region 判断区域号是否一致
                        var area = client.ReadByOrder<Int16>("o_place_prog", name);//放料完成的区域号
 
                        if (AreaNr != area)
                        {
                            Pipeline_client.WriteByOrder("W_AlarmSignal", (Int16)2, name);//报警
                            WriteLog.Write_Log("模式2" + name + "放料完成出错", Wheel_SN, "判断区域号是否一致", $"PCS区域号:{AreaNr};\n桁架区域号:{area}\n错误信息:PCS区域号与桁架区域号不一致!");
                            continue;
                        }
                        #endregion
 
                        var station = stationinfoRepository.Find(x => x.stationCode == name && x.stationCode.Substring(x.stationCode.Length - 1, 1) == AreaNr.ToString() && x.enable).FirstOrDefault();
                        if (station != null)
                        {
                            #region 判断车轮数量是否一致
                            var quantity = client.ReadByOrder<Int16>("o_pos_prog", name);//桁架记录的车轮数量
 
                            if (quantity != station.quantity + 1)
                            {
                                Pipeline_client.WriteByOrder("W_AlarmSignal", (Int16)2, name);//报警
                                WriteLog.Write_Log("模式2" + name + "放料完成出错", Wheel_SN, "判断车轮数量是否一致", $"区域号:{AreaNr};\n桁架车轮数:{quantity}\nPCS车轮数:{station.quantity + 1}\n错误信息:PCS区域号与桁架区域号不一致!");
                                continue;
                            }
                            #endregion
 
                            if (!string.IsNullOrEmpty(station.bindSN))
                            {
                                if (station.bindSN.Contains(Wheel_SN))
                                    throw new Exception($"{station.stationCode}已存在车轮SN号:{Wheel_SN}");
                            }
 
                            #region 清空给桁架的下料信息
                            client.WriteByOrder("i_indexNr", (Int16)0, name);//托盘上的第几个车轮
                            client.WriteByOrder("i_storage_type", (Int16)0, name);//托盘类型1-横放;2-竖放(暂时只有横放托盘)
                            client.WriteByOrder("i_areaNr", (Int16)0, name);//区域货位号
                            client.WriteByOrder("i_storage_update", true, name);//货位状态更新
                            WriteLog.Write_Log("模式2" + name + "放料完成", Wheel_SN, "成功!",
                                $"读取桁架信息:\n放料完成信号:{finished}\n车轮SN:{Wheel_SN}\n轮型ID:{WheelType}\n下料位号:{AreaNr}\n\n" +
                                $"处理写入桁架信息:\n区域货位号:{0}\n托盘上的第几个车轮:{0}\n托盘类型:{0}\n\n" +
                                $"写入桁架信息:\n货位状态更新信号:{true}\n是否允许下料:{false}");
                            #endregion
 
                            #region 货位状态更新
                            station.bindSN = string.IsNullOrEmpty(station.bindSN) ? Wheel_SN : station.bindSN + "," + Wheel_SN;
                            station.billetID = string.IsNullOrEmpty(station.billetID) ? Mes_Work.billetID.ToString() : station.billetID + "," + Mes_Work.billetID;
                            station.quantity++;
                            if (station.quantity == 1)
                            {
                                station.stationType = Mes_Work.drawingNo;
                                station.Number = Mes_Work.workOrder;
                                station.heatNumber = Mes_Work.heatID;
                                station.tray_status = "StrogeTray";
                            }
                            station.remark = "";
                            var count = stationinfoRepository.Update(station, true);
                            if (count < 1)
                                throw new Exception($"下料位信息更新失败!下料位编号:{station.stationCode}");
                            #endregion
 
                            #region 修改工单状态
                            var Detail = mes_DetailRepository.FindFirst(x => x.jobID == Mes_Work.jobID && x.SN == Mes_Work.SN);
                            Detail.Status = "下线";
                            Detail.FinishTime = DateTime.Now;
                            count = mes_DetailRepository.Update(Detail, x => new { x.Status, x.FinishTime }, true);
                            if (count < 1)
                                throw new Exception($"工单子表信息更新失败!车轮SN号:{Detail.SN}");
                            #endregion
                        }
                    }
                    #endregion
 
                    #region 下料位数量传给桁架
                    var Stations = stationinfoRepository.Find(x => x.area == Area).ToList();
                    foreach (var station in Stations)
                    {
                        client.WriteByOrder(station.stationCode, (Int16)station.quantity, name);
                    }
                    #endregion
                    var Station = Stations.Where(x => x.stationCode == name).FirstOrDefault();
                    //if (Station.enable)
                    //    client.WriteByOrder(Station.stationCode, Station.location_state != LocationStateEnum.Stroge.ToString(), name);
 
                    #region 申请下料
                    var Request_Load = client.ReadByOrder<Boolean>("o_request_load ", name);//下料申请
                    if (Request_Load)
                    {
                        #region 读取桁架信息
                        var Wheel_Type = client.ReadByOrder<Int16>("o_wheel_type", name);//车轮类型
                        var Wheel_id = client.ReadByOrder<string>("o_wheel_id", name);//车轮SN号
                        if (string.IsNullOrEmpty(Wheel_id))
                        {
                            WriteLog.Write_Log("模式2" + name + "申请放料异常", "车轮SN号为空", "", $"读取桁架信息:\n车轮SN号:{Wheel_id}为空");
                            continue;
                        }
                        var Work = workinfoRepository.Find(x => x.SN == Wheel_id && x.processCode == "17").OrderByDescending(x => x.CreateTime).FirstOrDefault();
                        if (Work == null)
                        {
                            client.WriteByOrder("i_enabl_load", false, name);//信号为false桁架停止进入
                            WriteLog.Write_Log("模式2" + name + "申请放料异常", Wheel_id, "查询机加工工单", $"读取桁架信息:\n车轮SN号:{Wheel_id}未查询到工单信息\n写入桁架信息:\n是否允许下料:{false}");
                            continue;
                        }
                        #endregion
 
                        var wheel = Pipeline.QueryMateriel(Work.drawingNo);
                        if (wheel == null)
                        {
                            client.WriteByOrder("i_enabl_load", false, name);//信号为false桁架停止进入
                            WriteLog.Write_Log("模式2" + name + "申请放料异常", Wheel_id, $"查询{Work.drawingNo}车轮参数", $"读取桁架信息:\n车轮SN号:{Wheel_id}未查询到工单信息\n写入桁架信息:\n是否允许下料:{false}");
                            continue;
                        }
 
                        #region 换图号,下料口其他图号托盘全部入库
                        if (Station.enable && Station.location_state == LocationStateEnum.Stroge.ToString() && Station.quantity > 0 && Station.stationType != Work.drawingNo)
                        {
                            agvtask(stationinfoRepository, Station, Work, "TaskType_OutsourceInbound");
                            continue;
                        }
 
                        #endregion
 
                        #region 托盘类型不一致需更换托盘
                        if (Station.enable && Station.tray_type != "LargeTray" && wheel.e > 1100)
                        {
                            client.WriteByOrder("i_enabl_load", false, name);//信号为false桁架停止进入
                            Station.location_state = LocationStateEnum.Trayswitching.ToString();
                            stationinfoRepository.Update(Station, x => new { x.location_state }, true);
                            continue;
                        }
                        #endregion
 
                        var PartStatus = client.ReadByOrder<Int16>("o_partstatus ", name);//1:ok;2:nok
 
                        var load_zone = client.ReadByOrder<Boolean>("o_load_zone ", name);// 进入下料区
                        if (load_zone) continue;
                        WriteLog.Write_Log("模式2" + name + "申请放料", Wheel_id, "申请放料", $"读取桁架信息:\n车轮SN:{Wheel_id}\n轮型ID:{Wheel_Type}\n车轮状态:{PartStatus}\n是否进入下料区:{load_zone}");
 
                        if ((Int16)PartStatus == 2)
                        {
                            if (!(Station.enable && Station.location_state == LocationStateEnum.Stroge.ToString() && Station.quantity < wheel.stackNoRange && Station.stationType == Work.drawingNo))
                            {
                                if (Station.enable && Station.location_state == LocationStateEnum.Stroge.ToString() && Station.quantity > 0)
                                {
                                    agvtask(stationinfoRepository, Station, Work, "TaskType_OutsourceInbound");
                                    continue;
                                }
                                if (!(Station.enable && Station.location_state == LocationStateEnum.Stroge.ToString() && Station.quantity == 0)) continue;
                            }
                            var Pipelinedetails = plcRepository.Find(x => x.plcdetail_iotype == Pipelineplc.plcinfo_iotyep && x.plcdetail_number == Station.stationCode).ToList();
                            var PalletSignal = (Int16)DBExtension.Read(Pipelinedetails.Where(x => x.plcdetail_name == "R_PalletSignal").First(), Pipeline_client);//读取托盘信号:1:有,2无
                            var MaterialSignal = (Int16)DBExtension.Read(Pipelinedetails.Where(x => x.plcdetail_name == "R_MaterialSignal").First(), Pipeline_client);//读取货物信号:1:有,2无
                            if (PalletSignal == 1)
                            {
                                List<string> list = new List<string>();
                                if (Station.bindSN != null)
                                {
                                    var SNS = Station.bindSN.Split(",");
                                    foreach (var SN in SNS)
                                    {
                                        if (!string.IsNullOrEmpty(SN))
                                            list.Add(SN);
                                    }
                                }
 
 
                                //缓存架上车轮数量与SN号数量不一致
                                if (list.Count != Station.quantity)
                                {
                                    Station.location_state = LocationStateEnum.Abnormal.ToString();
                                    Station.remark = "车轮数量与SN号数量不一致";
                                    stationinfoRepository.Update(Station, x => new { x.location_state, x.remark }, true);
                                    continue;
                                }
                                var area = Convert.ToInt16(Station.stationCode.Substring(Station.stationCode.Length - 1, 1));
                                client.WriteByOrder("i_areaNr", (Int16)area, name);//区域货位号
                                client.WriteByOrder("i_indexNr", (Int16)(Station.quantity + 1), name);//托盘上的第几个车轮
                                client.WriteByOrder("i_storage_type", (Int16)1, name);//托盘类型1-横放;2-竖放(暂时只有横放托盘)
                                client.WriteByOrder("i_enabl_load", true, name);//是否允许
                                Station.remark = "桁架下料";
                                stationinfoRepository.Update(Station, x => new { x.remark }, true);
                                #region 日志记录
                                WriteLog.Write_Log("模式2" + name + "申请放料", Wheel_id, "允许放料",
                                    $"读取桁架信息:\n车轮SN:{Wheel_id}\n轮型ID:{Wheel_Type}\n车轮状态:合格" +
                                    $"\n读取托盘光电信号:{PalletSignal}" +
                                    $"\n\n写入桁架信息:\n下料位号:{area}\n第几个车轮:{(Station.quantity + 1)}\n托盘类型:1\n是否允许下料:true");
                                #endregion
                                continue;
                            }
                        }
                    }
                    #endregion
                }
                catch (Exception ex)
                {
                    WriteLog.Write_Log("报错日志", "模式2NG车轮下料", name, $"错误信息:{ex.Message}");
                }
            }
        }
        #endregion
        public static string OperStr(string[] strArrty, string SN)
        {
            string[] newstr = strArrty.Where(x => x != SN).ToArray();
            string Newsn = string.Join(",", newstr);
 
            return Newsn;
        }
 
        public string OperStr(string[] strArrty)
        {
            string[] newstr = strArrty.RemoveLast(1);
            string Newsn = string.Join(",", newstr);
 
            return Newsn;
        }
 
        /// <summary>
        /// 更改工单状态
        /// </summary>
        /// <param name="SN"></param>
        public static void Updatemes(string SN, string name)
        {
            VOLContext Context = new VOLContext();
            Idt_mes_detailRepository mes_DetailRepository = new dt_mes_detailRepository(Context);
            Idt_mes_headRepository mes_HeadRepository = new dt_mes_headRepository(Context);
            IVV_Mes_WorkinfoRepository workinfoRepository = new VV_Mes_WorkinfoRepository(Context);
            var Mes_Work = workinfoRepository.Find(x => x.SN == SN && (name.Contains("2单元取料放料") ? x.processCode == "28" : x.processCode == "17")).FirstOrDefault();
            if (Mes_Work != null)
            {
                var mes_Detail = mes_DetailRepository.Find(x => x.SN == SN && x.jobID == Mes_Work.jobID).FirstOrDefault();
                if (mes_Detail != null)
                {
                    mes_Detail.Status = "上线";
                    mes_Detail.FinishTime = DateTime.Now;
                    mes_DetailRepository.Update(mes_Detail, x => new { x.Status, x.FinishTime }, true);
                    var mes_Head = mes_HeadRepository.Find(x => x.jobID == mes_Detail.jobID).FirstOrDefault();
                    if (mes_Head != null)
                    {
                        mes_Head.finishNum++;
                        if (mes_Head.finishNum == mes_Head.quantity) mes_Head.FinishTime = DateTime.Now;
                        mes_HeadRepository.Update(mes_Head, x => new { x.finishNum, x.FinishTime }, true);
                    }
                }
            }
        }
 
        #region 下料位添加队列任务
        public void agvtask(Idt_stationinfoRepository stationinfoRepository, dt_stationinfo stationinfo, VV_Mes_Workinfo Work, string tasktype)
        {
            VOLContext Context = new VOLContext();
            Idt_agvtaskRepository agvtaskRepository = new dt_agvtaskRepository(Context);
            IVV_Mes_WorkinfoRepository workinfoRepository = new VV_Mes_WorkinfoRepository(Context);
            Work = workinfoRepository.Find(x => x.workOrder == stationinfo.Number && x.processCode == "17").FirstOrDefault();
            var task = agvtaskRepository.Find(x => x.agv_fromaddress == stationinfo.stationCode).FirstOrDefault();
            if (task != null) return;
            if (!string.IsNullOrEmpty(Work.area))
                tasktype = "TaskType_OutsourceInbound";
            AGVTask.AddQueueTask(stationinfoRepository, agvtaskRepository, stationinfo, tasktype, Work);
        }
        #endregion
    }
}