分支自 SuZhouGuanHong/TaiYuanTaiZhong

dengjunjie
2024-06-20 00ad010a45f2f58807eb0f4f70e2dcf66ef35085
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WIDESEA_Comm.LogInfo;
using WIDESEA_Comm;
using WIDESEA_Core.EFDbContext;
using WIDESEA_WCS.IRepositories;
using WIDESEA_WCS.Repositories;
using WIDESEA_WCS.WCSClient;
using WIDESEA_WMS.IRepositories;
using WIDESEA_WMS.Repositories;
using HslCommunication;
using WIDESEA_Entity.DomainModels;
using WIDESEA_Comm.TaskNo;
using WIDESEA_WCS.JobsPart.Common;
using System.Xml.Linq;
using WIDESEA_Common;
 
namespace WIDESEA_WCS
{
    public class Pipeline
    {
 
        /// <summary>
        /// 上料区
        /// </summary>
        /// <param name="client"></param>
        public void Loadinglevel(PLCClient client, string number = "上料区")
        {
            try
            {
                VOLContext Context = new VOLContext();
                Idt_plcinfoheadRepository repository = new dt_plcinfoheadRepository(Context);
                Idt_plcinfodetailRepository plcRepository = new dt_plcinfodetailRepository(Context);
                Idt_stationinfoRepository stationinfoRepository = new dt_stationinfoRepository(Context);
                var plc = repository.FindFirst(x => x.plcinfo_name == client.PLCName);
                var Gantry_client = PLCClient.Clients.FirstOrDefault(t => t.PLCName == "桁架");
                if (Gantry_client == null) throw new Exception("桁架调度服务未开启!");
                if (!Gantry_client.IsConnected) throw new Exception("与桁架连接超时!");
                var Gantryplc = repository.FindFirst(x => x.plcinfo_name == Gantry_client.PLCName);
 
 
                #region 上料完成
 
                var AreaNr = Gantry_client.ReadByOrder<Int16>("W_AreaNr", number);//读取货位号信息
                if (AreaNr != 0)
                {
                    var station = stationinfoRepository.Find(x => x.area == area_code(number) && x.stationCode.Substring(x.stationCode.Length - 1, 1) == AreaNr.ToString()).FirstOrDefault();
                    if (station != null)
                    {
                        var bindSNS = station.bindSN.Split(",");
                        var Gantrydetails = plcRepository.Find(x => x.plcdetail_iotype == Gantryplc.plcinfo_iotyep && x.plcdetail_number == number).ToList();
 
                        #region 信息查询确认
                        var QueryDate = (bool)DBExtension.Read(Gantrydetails.Where(x => x.plcdetail_name == "R_QueryDate").First(), Gantry_client);//信息查询
                        if (QueryDate)
                        {
                            var Date_Vaild = (bool)DBExtension.Read(Gantrydetails.Where(x => x.plcdetail_name == "R_Date_Vaild").First(), Gantry_client);//信息确认
                            if (!Date_Vaild)//信息有误,记录日志
                            {
                                Gantry_client.WriteByOrder("W_RequestUnload", false, number);//信号为false桁架停止进入
                                station.location_state = LocationStateEnum.Abnormal.ToString();
                                station.remark = $"未查询到SN号:{bindSNS[bindSNS.Length - 1]}的订单";
                                stationinfoRepository.Update(station, true);
                                WriteDBLog.Error("上料区信息查询", $"写入桁架信息:\nW_RequestUnload:false\n\n{station.stationCode}未查询到SN号:{bindSNS[bindSNS.Length - 1]}的订单", "PCS");
                                return;
                            }
                        }
                        #endregion
 
                        #region 货位状态更新复位
                        var finished = (bool)DBExtension.Read(Gantrydetails.Where(x => x.plcdetail_name == "R_Unlod_finished").First(), Gantry_client);//夹取完成
                        var updatefinished = (bool)DBExtension.Read(Gantrydetails.Where(x => x.plcdetail_name == "W_Storage_update").First(), Gantry_client);//货位状态更新
                        if (!finished && updatefinished)
                        {
                            Gantry_client.WriteByOrder("W_Storage_update", false, number);//货位状态更新
                            WriteLog.Write_Log(number + "上料完成", number + "货位状态更新信号复位", "成功!",
                                        $"读取桁架信息:\n上料完成信号:{finished}\n\n写入桁架信息:\n货位状态更新信号:{false}");
                        }
                        #endregion
 
                        if (finished && !updatefinished)
                        {
 
                            #region 判断区域号是否一致
                            //var area = Gantry_client.ReadByOrder<Int16>("place_prog", number);//放料完成的区域号
 
                            //WriteLog.Write_Log(number + "取料完成出错", bindSNS[bindSNS.Length - 1], "判断区域号是否一致", $"PCS区域号:{AreaNr};\n桁架区域号:{area}\n错误信息:PCS区域号与桁架区域号不一致!");
                            //if (AreaNr != area)
                            //{
                            //    client.WriteByOrder("W_AlarmSignal", (Int16)2, number);//报警
                            //    //WriteDBLog.Error(name + "报警", $"PCS区域号:{AreaNr};桁架区域号:{area}错误信息:PCS区域号与桁架区域号不一致!", "PCS");
                            //    WriteLog.Write_Log(number + "取料完成出错", bindSNS[bindSNS.Length - 1], "判断区域号是否一致", $"PCS区域号:{AreaNr};\n桁架区域号:{area}\n错误信息:PCS区域号与桁架区域号不一致!");
                            //    return;
                            //}
                            #endregion
 
                            #region 判断车轮数量是否一致
                            //var quantity = Gantry_client.ReadByOrder<Int16>("pos_prog", number);//桁架记录的车轮数量
 
                            //WriteLog.Write_Log(number + "取料完成出错", bindSNS[bindSNS.Length - 1], "判断车轮数量是否一致", $"区域号:{AreaNr};\n桁架车轮数:{quantity}\nPCS车轮数:{station.quantity}\n错误信息:PCS区域号与桁架区域号不一致!");
                            //if (quantity != station.quantity)
                            //{
                            //    client.WriteByOrder("W_AlarmSignal", (Int16)2, number);//报警
                            //    WriteLog.Write_Log(number + "取料完成出错", bindSNS[bindSNS.Length - 1], "判断车轮数量是否一致", $"区域号:{AreaNr};\n桁架车轮数:{quantity}\nPCS车轮数:{station.quantity}\n错误信息:PCS区域号与桁架区域号不一致!");
                            //    return;
                            //}
                            #endregion
 
                            Updatemes(bindSNS[bindSNS.Length - 1]);
                            station.quantity = station.quantity - 1;
                            station.bindSN = OperStr(bindSNS);
                            if (station.quantity <= 0)
                            {
                                station.stationType = string.Empty;
                                station.Number = string.Empty;
                                station.heatNumber = string.Empty;
                                station.tray_status = "EmptyTray";
                            }
                            var count = stationinfoRepository.Update(station, true);
                            if (count < 1)
                            {
                                WriteDBLog.Error("取料完成", $"上料位信息更新失败!上料位编号:{station.stationCode}", "PCS");
                                return;
                            }
 
                            Gantry_client.WriteByOrder("W_Storage_update", true, number);//货位状态更新
                            Gantry_client.WriteByOrder("W_RequestUnload", false, number);//信号为false桁架停止进入
 
                            #region 清空信息
                            Gantry_client.WriteByOrder("W_AreaNr", (Int16)0, number);//区域货位号
                            Gantry_client.WriteByOrder("W_IndexNr", (Int16)0, number);//托盘上的第几个车轮
                            Gantry_client.WriteByOrder("W_Storage_Type", (Int16)0, number); //托盘类型1-横放;2-竖放
                            Gantry_client.WriteByOrder("W_Wheel_Type", (Int16)0, number);//车轮类型
                            #endregion
 
                            #region 日志记录
                            WriteDBLog.Success("取料完成", $"写入桁架信息:\n货位状态更新:{true}\nW_RequestUnload:{false}", "PCS");
                            #endregion
                        }
                    }
                }
 
                #endregion
 
                #region 申请上料
 
                var Station = stationinfoRepository.Find(x => x.area == area_code(number) && x.enable && x.location_state == LocationStateEnum.Stroge.ToString() && x.quantity > 0).OrderBy(x => x.lastUpdateTime).FirstOrDefault();
                if (Station != null)
                {
                    if (!Gantry_client.ReadByOrder<bool>("W_RequestUnload", number))
                    {
                        var SNS = Station.bindSN.Split(",");
                        List<string> list = new List<string>();
                        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);
                            Gantry_client.WriteByOrder("W_RequestUnload", false, number);//信号为false桁架停止进入
                            #region 日志记录
                            //WriteDBLog.Error("上料区", $"写入桁架信息:\nW_RequestUnload:false\n\n{Station.stationCode}车轮数量与SN号数量不一致", "PCS");
                            #endregion
                            return;
                        }
 
                        var details = plcRepository.Find(x => x.plcdetail_iotype == plc.plcinfo_iotyep && x.plcdetail_number == Station.stationCode).ToList();
                        var PalletSignal = (Int16)DBExtension.Read(details.Where(x => x.plcdetail_name == "R_PalletSignal").First(), client);//读取托盘信号:1:有,2无
                        var MaterialSignal = (Int16)DBExtension.Read(details.Where(x => x.plcdetail_name == "R_MaterialSignal").First(), client);//读取货物信号:1:有,2无
 
                        if (PalletSignal == 1 && MaterialSignal == 1)
                        {
                            var geometry = QueryMateriel(Station.stationType);
                            if (geometry == null)
                            {
                                Station.remark = "未找到车轮信息";
                                stationinfoRepository.Update(Station, x => new { x.remark }, true);
                                Gantry_client.WriteByOrder("W_RequestUnload", false, number);//信号为false桁架停止进入
                                return;
                            }
                            Gantry_client.WriteByOrder("W_Wheel_Type", (Int16)Convert.ToInt16(geometry.TypeId), number);//车轮类型
                            var area = Convert.ToInt16(Station.stationCode.Substring(Station.stationCode.Length - 1, 1));
                            Gantry_client.WriteByOrder("W_AreaNr", (Int16)area, number);//区域货位号
                            Gantry_client.WriteByOrder("W_IndexNr", (Int16)SNS.Length, number);//托盘上的第几个车轮
                            Gantry_client.WriteByOrder("W_Storage_Type", (Int16)1, number); //托盘类型1-横放;2-竖放
                            Gantry_client.WriteByOrder("W_Wheel_id", SNS[SNS.Length - 1], number);//车轮SN号
                            Gantry_client.WriteByOrder("W_RequestUnload", true, number);
                            #region 日志记录
                            WriteDBLog.Success("上料区申请", $"读取托盘光电信号:{PalletSignal}\n读取第一个车轮光电信号:{MaterialSignal}\n\n" +
                                $"写入桁架信息:\n区域货位号:{area}\n托盘上的第几个车轮:{SNS.Length}\n托盘类型:{1}\n车轮类型:{geometry.TypeId}" +
                                $"\n车轮SN号:{SNS[SNS.Length - 1]}\nW_RequestUnload:true", "PCS");
                            #endregion
                        }
                        else
                        {
                            Gantry_client.WriteByOrder("W_RequestUnload", false, number);
                        }
 
                        #region 更新一个检测上料任务
                        //Idt_agvtaskRepository agvtaskService = new dt_agvtaskRepository(Context);
                        //if (!agvtaskService.Find(x => x.agv_toaddress == Station.stationCode).Any())
                        //{
                        //    var task = agvtaskService.Find(x => x.agv_taskstate == "Queue" && (x.agv_tasktype == "TaskType_OutsourceOutbound" || x.agv_tasktype == "TaskType_Outbound") && x.agv_toaddress == "").OrderByDescending(x => x.agv_grade).ThenBy(x => x.agv_createtime).FirstOrDefault();
                        //    if (task != null)
                        //    {
                        //        task.agv_taskstate = "Create";
                        //        task.agv_toaddress = Station.stationCode;
                        //        agvtaskService.Update(task, true);
                        //        WriteDBLog.Success("更新一个检测上料任务", $"任务编号:{task.agv_tasknum}", "PCS");
                        //    }
                        //}
                        #endregion
                    }
                }
                else
                {
                    Gantry_client.WriteByOrder("W_RequestUnload", false, number);
                }
 
                #endregion
 
 
                #region MyRegion
 
                /////查找上料区的货位
                //var Stations = stationinfoRepository.Find(x => x.area == area_code(number)).OrderBy(x => x.lastUpdateTime);//根据时间先后排序
                //                                                                                                           //需测试一下
                //var Station = Stations?.Where(x => x.enable && x.location_state == LocationStateEnum.Stroge.ToString() && x.quantity > 0).OrderBy(x => x.quantity).FirstOrDefault();
 
                //if (Station != null)
                //{
                //    var SNS = Station.bindSN.Split(",");
                //    List<string> list = new List<string>();
                //    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, true);
                //        Gantry_client.WriteByOrder("W_RequestUnload", false, number);//信号为false桁架停止进入
                //        #region 日志记录
                //        WriteDBLog.Error("上料区", $"写入桁架信息:\nW_RequestUnload:false\n\n{Station.stationCode}车轮数量与SN号数量不一致", "PCS");
                //        #endregion
                //        return;
                //    }
                //    var details = plcRepository.Find(x => x.plcdetail_iotype == plc.plcinfo_iotyep && x.plcdetail_number == Station.stationCode).ToList();
                //    var PalletSignal = (Int16)DBExtension.Read(details.Where(x => x.plcdetail_name == "R_PalletSignal").First(), client);//读取托盘信号:1:有,2无
                //    var MaterialSignal = (Int16)DBExtension.Read(details.Where(x => x.plcdetail_name == "R_MaterialSignal").First(), client);//读取货物信号:1:有,2无
 
                //    var AreaNr = Gantry_client.ReadByOrder<Int16>("W_AreaNr", number);//读取货位号信息
                //    if (PalletSignal == 1 && MaterialSignal == 1)
                //    {
                //        if (AreaNr == 0)
                //        {
                //            Gantry_client.WriteByOrder("W_Wheel_Type", (Int16)Convert.ToInt16(QueryMateriel(Station.stationType) == null ? QueryMateriel1(Station.stationType).TypeId : QueryMateriel(Station.stationType).TypeId), number);//车轮类型
                //            var area = Convert.ToInt16(Station.stationCode.Substring(Station.stationCode.Length - 1, 1));
                //            Gantry_client.WriteByOrder("W_AreaNr", (Int16)area, number);//区域货位号
                //            Gantry_client.WriteByOrder("W_IndexNr", (Int16)SNS.Length, number);//托盘上的第几个车轮
                //            Gantry_client.WriteByOrder("W_Storage_Type", (Int16)1, number); //托盘类型1-横放;2-竖放
                //            Gantry_client.WriteByOrder("W_Wheel_id", SNS[SNS.Length - 1], number);//车轮SN号
                //            Gantry_client.WriteByOrder("W_RequestUnload", true, number);
                //            #region 日志记录
                //            WriteDBLog.Success("上料区申请", $"读取托盘光电信号:{PalletSignal}\n读取第一个车轮光电信号:{MaterialSignal}\n\n" +
                //                $"写入桁架信息:\n区域货位号:{area}\n托盘上的第几个车轮:{SNS.Length}\n托盘类型:{1}\n车轮类型:{(QueryMateriel(Station.stationType).TypeId == null ? QueryMateriel1(Station.stationType).TypeId : QueryMateriel(Station.stationType).TypeId)}" +
                //                $"\n车轮SN号:{SNS[SNS.Length - 1]}\nW_RequestUnload:true", "PCS");
                //            #endregion
                //        }
                //    }
                //    else
                //    {
                //        Gantry_client.WriteByOrder("W_RequestUnload", false, number);
                //    }
 
                //    var Gantrydetails = plcRepository.Find(x => x.plcdetail_iotype == Gantryplc.plcinfo_iotyep && x.plcdetail_number == number).ToList();
                //    var Gantry_Out_of_Area = (bool)DBExtension.Read(Gantrydetails.Where(x => x.plcdetail_name == "R_Gantry_Out_of_Area").First(), Gantry_client);//桁架是否在区域内
 
                //    var QueryDate = (bool)DBExtension.Read(Gantrydetails.Where(x => x.plcdetail_name == "R_QueryDate").First(), Gantry_client);//信息查询
                //    if (QueryDate)
                //    {
                //        var Date_Vaild = (bool)DBExtension.Read(Gantrydetails.Where(x => x.plcdetail_name == "R_Date_Vaild").First(), Gantry_client);//信息确认
                //        if (!Date_Vaild)//信息有误,记录日志
                //        {
                //            Gantry_client.WriteByOrder("W_RequestUnload", false, number);//信号为false桁架停止进入
                //            Station.location_state = LocationStateEnum.Abnormal.ToString();
                //            Station.remark = $"未查询到SN号:{SNS[SNS.Length - 1]}的订单";
                //            stationinfoRepository.Update(Station, true);
                //            WriteDBLog.Error("上料区信息查询", $"写入桁架信息:\nW_RequestUnload:false\n\n{Station.stationCode}未查询到SN号:{SNS[SNS.Length - 1]}的订单", "PCS");
                //            return;
                //        }
                //    }
                //    var finished = (bool)DBExtension.Read(Gantrydetails.Where(x => x.plcdetail_name == "R_Unlod_finished").First(), Gantry_client);//夹取完成
                //    var updatefinished = (bool)DBExtension.Read(Gantrydetails.Where(x => x.plcdetail_name == "W_Storage_update").First(), Gantry_client);//货位状态更新
                //    if (!finished && updatefinished)
                //        Gantry_client.WriteByOrder("W_Storage_update", false, number);//货位状态更新
                //    if (finished && !updatefinished)
                //    {
                //        Gantry_client.WriteByOrder("W_RequestUnload", false, number);//信号为false桁架停止进入
                //        Updatemes(SNS[SNS.Length - 1]);
                //        Station.quantity = Station.quantity - 1;
                //        Station.bindSN = OperStr(SNS);
                //        if (Station.quantity <= 0)
                //        {
                //            Station.stationType = string.Empty;
                //            Station.Number = string.Empty;
                //            Station.heatNumber = string.Empty;
                //            Station.tray_status = "EmptyTray";
                //        }
                //        var count = stationinfoRepository.Update(Station, true);
                //        if (count < 1)
                //        {
                //            WriteDBLog.Error("取料完成", $"上料位信息更新失败!上料位编号:{Station.stationCode}", "PCS");
                //            return;
                //        }
 
                //        #region 清空信息
                //        Gantry_client.WriteByOrder("W_AreaNr", (Int16)0, number);//区域货位号
                //        Gantry_client.WriteByOrder("W_IndexNr", (Int16)0, number);//托盘上的第几个车轮
                //        Gantry_client.WriteByOrder("W_Storage_Type", (Int16)0, number); //托盘类型1-横放;2-竖放
                //        Gantry_client.WriteByOrder("W_Wheel_Type", (Int16)0, number);//车轮类型
                //        #endregion
 
                //        Gantry_client.WriteByOrder("W_Storage_update", true, number);//货位状态更新
                //        #region 日志记录
                //        WriteDBLog.Success("取料完成", $"写入桁架信息:\n货位状态更新:{true}\nW_RequestUnload:{false}", "PCS");
                //        #endregion
 
                //        #region 报警
                //        PalletSignal = (Int16)DBExtension.Read(details.Where(x => x.plcdetail_name == "R_PalletSignal").First(), client);//读取托盘信号:1:有,2无
                //        MaterialSignal = (Int16)DBExtension.Read(details.Where(x => x.plcdetail_name == "R_MaterialSignal").First(), client);//读取货物信号:1:有,2无
                //        if (Station.quantity > 0 && (PalletSignal != 1 || MaterialSignal != 1))
                //        {
                //            Station.location_state = LocationStateEnum.Abnormal.ToString();
                //            Station.remark = "车轮数量大于0,光电检测无托盘或无车轮";
                //            stationinfoRepository.Update(Station, true);
                //            client.WriteByOrder("W_AlarmSignal", (Int16)2, number);//报警
                //            WriteDBLog.Error(number + "报警", $"货位编号:{Station.stationCode};错误信息:{Station.remark}", "PCS");
                //        }
                //        else if (Station.quantity < 1 && MaterialSignal == 1)
                //        {
                //            Station.location_state = LocationStateEnum.Abnormal.ToString();
                //            Station.remark = "车轮数量小于0,光电检测有车轮";
                //            stationinfoRepository.Update(Station, true);
                //            client.WriteByOrder("W_AlarmSignal", (Int16)2, number);//报警
                //            WriteDBLog.Error(number + "报警", $"货位编号:{Station.stationCode};错误信息:{Station.remark}", "PCS");
                //        }
                //        #endregion
 
 
                //    }
                //}
                //else
                //    Gantry_client.WriteByOrder("W_RequestUnload", false, number);//信号为false桁架停止进入 
                #endregion
            }
            catch (Exception ex)
            {
                //WriteDBLog.Error("上料区", $"错误信息:{ex.Message}", "PCS");
                WriteLog.Write_Log("报错日志", number, "错误信息!", $"错误信息:{ex.Message}");
            }
        }
        /// <summary>
        /// 自动取空托
        /// </summary>
        /// <param name="client"></param>
        public void AutoEmptyTray(PLCClient client)
        {
            try
            {
                VOLContext Context = new VOLContext();
                Idt_plcinfoheadRepository repository = new dt_plcinfoheadRepository(Context);
                Idt_plcinfodetailRepository plcRepository = new dt_plcinfodetailRepository(Context);
                Idt_agvtaskRepository agvtaskService = new dt_agvtaskRepository(Context);
                Idt_stationinfoRepository stationinfoRepository = new dt_stationinfoRepository(Context);
                var plc = repository.FindFirst(x => x.plcinfo_name == client.PLCName);
                List<string> stationCodes = new List<string>() { "S01001001", "S01001002", "W01001001", "W01001002", "W01001003" };
                dt_stationinfo EmptyStation = null;
                string remark = "";
                foreach (var stationCode in stationCodes)
                {
                    try
                    {
                        remark = "查询站台";
                        var station = stationinfoRepository.Find(x => x.stationCode == stationCode && x.enable).FirstOrDefault();
                        if (station == null) continue;
                        remark = "查询起点是否存在任务";
                        if (stationCode.Contains("W") && agvtaskService.Find(x => x.agv_fromaddress == stationCode || x.agv_toaddress == stationCode).Any()) continue;
                        else
                        {
                            if (agvtaskService.Find(x => x.agv_fromaddress == stationCode).Any()) continue;
                        }
                        remark = "读取光电信号";
                        var details = plcRepository.Find(x => x.plcdetail_iotype == plc.plcinfo_iotyep && x.plcdetail_number == stationCode).ToList();
                        var PalletSignal = (Int16)DBExtension.Read(details.Where(x => x.plcdetail_name == "R_PalletSignal").First(), client);//读取托盘信号:1:有,2无
                        var MaterialSignal = (Int16)DBExtension.Read(details.Where(x => x.plcdetail_name == "R_MaterialSignal").First(), client);//读取货物信号:1:有,2无
                        if (PalletSignal == 1 && MaterialSignal == 2 && (station.stationCode.Contains("S0100100") ? station.tray_status == "EmptyTray" : true))
                        {
                            #region 查询是否存在补空托队列任务
                            //var area = station.tray_type == "SmallTray" ? "11" : "10";
                            //remark = "查询是否存在补空托队列任务";
                            //var task = agvtaskService.Find(x => (x.agv_toaddress.Contains("W") || x.agv_toaddress.Contains("X")) && x.agv_taskstate == "Queue" && x.agv_tasktype == "TaskType_EmptyPallet" && x.agv_Traytype == station.tray_type).OrderByDescending(x => x.agv_grade).ThenBy(x => x.agv_createtime).FirstOrDefault();
                            //if (task != null)
                            //{
                            //    task.agv_fromaddress = station.stationCode;
                            //    task.StarQuantity = 0;
                            //    task.agv_taskstate = "Create";
                            //    if (station.stationCode.Contains("S0100100"))
                            //        task.agv_grade = 3;
                            //    agvtaskService.Update(task, true);
                            //    station.location_state = LocationStateEnum.Busy.ToString();
                            //    stationinfoRepository.Update(station, true);
                            //    WriteDBLog.Success("更新补空托队列任务", $"任务编号:{task.agv_tasknum},托盘信号:{PalletSignal},货物信号:{MaterialSignal}", "PCS");
                            //    continue;
                            //} 
                            #endregion
 
                            #region 创建取空托队列任务
                            dt_agvtask agvtask = new dt_agvtask()
                            {
                                agv_fromaddress = station.stationCode,
                                agv_id = Guid.NewGuid(),
                                agv_tasknum = IdenxManager.GetTaskNo("KH-", "WMS"),
                                agv_grade = station.stationCode.Contains("S0100100") ? 3 : 1,
                                agv_createtime = DateTime.Now,
                                agv_taskstate = AGVTaskStateEnum.Queue.ToString(),
                                agv_qty = 1,
                                StarQuantity = 0,
                                EndQuantity = 0,
                                agv_tasktype = AGVTaskTypeEnum.TaskType_EmptyPallet.ToString(),
                                agv_toaddress = "",
                                agv_userid = "系统",
                                agv_TrayStatus = "EmptyTray",
                                agv_Traytype = station.tray_type,
                            };
                            agvtaskService.Add(agvtask, true);
                            station.location_state = LocationStateEnum.Busy.ToString();
                            stationinfoRepository.Update(station, true);
                            WriteDBLog.Success("创建自动取空托队列任务", $"任务编号:{agvtask.agv_tasknum},托盘信号:{PalletSignal},货物信号:{MaterialSignal}", "PCS");
                            #endregion
                        }
                    }
                    catch (Exception ex)
                    {
                        WriteLog.Write_Log("报错日志", "创建自动取空托队列任务", "错误信息!", $"{stationCode}:{remark}错误信息:{ex.Message}");
                    }
                }
            }
            catch (Exception ex)
            {
                WriteLog.Write_Log("报错日志", "创建自动取空托队列任务", "错误信息!", $"错误信息:{ex.Message}");
            }
        }
 
        /// <summary>
        /// 检测光电状态与系统货位状态是否一致
        /// </summary>
        /// <param name="client"></param>
        public void StationState(PLCClient client)
        {
 
            List<string> names = new List<string>() { "1单元下料区", "2单元下料区", "3单元下料区" };/*, "上料区"*/
            foreach (var name in names)
            {
                VOLContext Context = new VOLContext();
                Idt_stationinfoRepository stationinfoRepository = new dt_stationinfoRepository(Context);
                var Stations = stationinfoRepository.Find(x => x.area == area_code(name) && x.enable && !x.location_state.Contains("Busy") && x.location_state != LocationStateEnum.Abnormal.ToString());
                foreach (var Station in Stations)
                {
                    try
                    {
                        var PalletSignal = client.ReadByOrder<Int16>("R_PalletSignal", Station.stationCode);//读取托盘信号:1:有,2无
                        var MaterialSignal = client.ReadByOrder<Int16>("R_MaterialSignal", Station.stationCode);//读取货物信号:1:有,2无
                        if (Station.quantity > 0 && (PalletSignal != 1 || MaterialSignal != 1))//系统货位有车轮,光电检测无车轮
                        {
                            Station.location_state = LocationStateEnum.Abnormal.ToString();
                            Station.remark = $"车轮数量大于0,托盘光电信号为:{PalletSignal},货物光电信号为:{MaterialSignal}";
                            stationinfoRepository.Update(Station, true);
                            client.WriteByOrder("W_AlarmSignal", (Int16)2, name);//报警
                            WriteDBLog.Error(name + "报警", $"货位编号:{Station.stationCode};错误信息:{Station.remark}", "PCS");
                            continue;
                        }
                        if (Station.location_state == LocationStateEnum.Stroge.ToString() && PalletSignal != 1)
                        {
                            Station.location_state = LocationStateEnum.Abnormal.ToString();
                            Station.remark = $"货位状态为有货,托盘光电信号为:{PalletSignal},货物光电信号为:{MaterialSignal}";
                            stationinfoRepository.Update(Station, true);
                            client.WriteByOrder("W_AlarmSignal", (Int16)2, name);//报警
                            WriteDBLog.Error(name + "报警", $"货位编号:{Station.stationCode};错误信息:{Station.remark}", "PCS");
                            continue;
                        }
                        if (Station.location_state == LocationStateEnum.Empty.ToString() && (PalletSignal == 1 || MaterialSignal == 1))//系统货位为空,光电检测有料
                        {
                            Station.location_state = LocationStateEnum.Abnormal.ToString();
                            Station.remark = $"货位状态为空货位,托盘光电信号为:{PalletSignal},货物光电信号为:{MaterialSignal}";
                            stationinfoRepository.Update(Station, true);
                            client.WriteByOrder("W_AlarmSignal", (Int16)2, name);//报警
                            WriteDBLog.Error(name + "报警", $"货位编号:{Station.stationCode};错误信息:{Station.remark}", "PCS");
                            continue;
                        }
                        //if (Station.quantity < 1 && MaterialSignal == 1)//系统货位无车轮,光电检测有车轮
                        //{
                        //    Station.location_state = LocationStateEnum.Abnormal.ToString();
                        //    Station.remark = "货位无车轮,光电检测有车轮";
                        //    stationinfoRepository.Update(Station, true);
                        //    client.WriteByOrder("W_AlarmSignal", (Int16)2, name);//报警
                        //    continue;
                        //}
                    }
                    catch (Exception ex)
                    {
                        WriteLog.Write_Log("报错日志", name + "检测光电报错", "错误信息!", $"{Station.stationCode}:错误信息:{ex.Message}");
                    }
                }
            }
        }
        public static string area_code(string area_name)
        {
            VOLContext Context = new VOLContext();
            Idt_areainfoRepository areainfoRepository = new dt_areainfoRepository(Context);
            var areainfo = areainfoRepository.Find(x => x.area_name == area_name).FirstOrDefault();
            return areainfo.area_code.ToString();
        }
 
        public string OperStr(string[] strArrty)
        {
            string[] newstr = strArrty.RemoveLast(1);
            string Newsn = string.Join(",", newstr);
 
            return Newsn;
        }
        public static dt_geometry_data QueryMateriel(string type)
        {
            VOLContext Context = new VOLContext();
            Idt_geometry_dataRepository dataRepository = new dt_geometry_dataRepository(Context);
            var materielinfo = dataRepository.Find(x => x.Description == type).OrderBy(x => x.TypeId).FirstOrDefault();
            return materielinfo;
        }
        public static dt_geometry_data_detectionline QueryMateriel1(string type)
        {
            VOLContext Context = new VOLContext();
            Idt_geometry_data_detectionlineRepository detectionlineRepository = new dt_geometry_data_detectionlineRepository(Context);
            var materielinfo1 = detectionlineRepository.Find(x => x.Description == type).OrderBy(x => x.TypeId).FirstOrDefault();
            return materielinfo1;
        }
        public static string QueryMateriel(int typeId)
        {
            VOLContext Context = new VOLContext();
            Idt_geometry_dataRepository dataRepository = new dt_geometry_dataRepository(Context);
            var materielinfo = dataRepository.FindFirst(x => x.TypeId == typeId);
            return materielinfo.Description;
        }
        /// <summary>
        /// 更改工单状态
        /// </summary>
        /// <param name="SN"></param>
        public static void Updatemes(string SN)
        {
            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_Detail = freeDB.Select<dt_mes_detail>().Where(x => x.SN == SN).First();
            var Mes_Work = workinfoRepository.Find(x => x.SN == SN && x.processCode == "28").FirstOrDefault();
            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;
                //freeDB.DataBase.Update<dt_mes_detail>().SetSource(mes_Detail).UpdateColumns(x => new { x.Status, x.FinishTime }).ExecuteAffrows();
                mes_DetailRepository.Update(mes_Detail, true);
                //var mes_Head = freeDB.Select<dt_mes_head>().Where(x => x.jobID == mes_Detail.jobID).First();
                var mes_Head = mes_HeadRepository.Find(x => x.jobID == mes_Detail.jobID).FirstOrDefault();
                if (mes_Head != null)
                {
                    mes_Head.finishNum = mes_Head.finishNum + 1;
                    if (mes_Head.finishNum == mes_Head.quantity) mes_Head.FinishTime = DateTime.Now;
                    mes_HeadRepository.Update(mes_Head, true);
                }
            }
        }
    }
}