PrintExcuteCX.cs
50.9 KB
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
using HHECS.Application.Enums;
using HHECS.BLL.EquipmentExcute.Marking;
using HHECS.BllModel;
using HHECS.Communication;
using HHECS.Communication.PLC;
using HHECS.Dal;
using HHECS.Dal.Repository;
using HHECS.DAL.Repository;
using HHECS.Executor.EquipmentHandler;
using HHECS.Infrastructure.CommonHelper;
using HHECS.Infrastructure.Enums;
using HHECS.Infrastructure.Notice;
using HHECS.Model.Entities;
using HHECS.Model.Enums;
using HHECS.Model.Enums.Machine;
using HHHECS.Model.Enums;
using NPOI.SS.Formula.Functions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
namespace HHECS.BLL.EquipmentExcute.Machine
{
/// <summary>
/// 焊接
/// </summary>
public class PrintExcuteCX : StationExecute
{
public PrintExcuteCX(EquipmentType equipmentType) : base(equipmentType)
{
}
/// <summary>
/// 执行上料完成
/// 注意:allEquipments引用所有设备,此为共享应用
/// </summary>
/// <param name="station"></param>
/// <param name="allEquipments"></param>
/// <param name="plc"></param>
/// <returns></returns>
public override BllResult ExcuteArrive(Equipment station, List<Equipment> allEquipments, PLCCore plc, User user)
{
try
{
//var arriveBarcode = station[StationProps.ArriveBarcode.ToString()];
var number = station[StationProps.ArriveRealAddress.ToString()];
var baseMaterialRepository = new BaseMaterialRepository();
var cutPlanHeadRepository = new CutPlanHeadRepository();
var cutPlandDilt = new CutPlanDetailRepository();
//上游没法保证排序,按照管子号排序
var cutPlanHeads = cutPlanHeadRepository.Where(t => t.State == (int)CutPlanHeadState.初始 && t.LineCode == station.LineCode).OrderByDescending(t=>t.LotNo).ToList();
if (cutPlanHeads.Count == 0)
{
var msg = cutPlanHeads.Count > 1 ? $"{cutPlanHeads.Count}条未使用的" : $"没有找到已经到达的管材";
return BllResultFactory.Error($"处理打码工位【{station.WorkStationCode}】设备【{station.Name}】位置到达失败,切割方案存在{msg}");
}
cutPlanHeads[0].State = (int)CutPlanHeadState.下料完成;
cutPlanHeads[0].DischargeEndTime = DateTime.Now;
//var result = ExcuteWalkPlan(station, allEquipments, plc, user);
var material = baseMaterialRepository.Where(t => t.MaterialCode == cutPlanHeads[0].MaterialCode).First();
if (material == null)
{
return BllResultFactory.Error($"处理打码工位【{station.WorkStationCode}】的设备【{station.Name}】上料请求失败,没有找到物料");
}
using (var uw = DALHelper.GetFreeSql().CreateUnitOfWork())
{
try
{
cutPlanHeadRepository.UnitOfWork = uw;
cutPlanHeadRepository.Update(cutPlanHeads[0]);
var s = cutPlandDilt.Where(t => t.HeadKeys == cutPlanHeads[0].Keys).ToList();
// count = s.Count + count;
BllResult sendResult = SendAckToPlc(station, plc, StationMessageFlag.WCSPLCACK, StationLoadStatus.ArriveReply, number.Value, cutPlanHeads[0].Id.ToString(), s.Count().ToString(), Convert.ToInt32(cutPlanHeads[0].Pipelength) - Convert.ToInt32(cutPlanHeads[0].OddmentsLength), material.TypesPlcCode, cutPlanHeads[0].Pipelength, material.Diameter, material.Thickness);
if (sendResult.Success)
{
uw.Commit();
return BllResultFactory.Success($"处理打码工位【{station.WorkStationCode}】设备【{station.Name}】位置到达成功,库存批次码【{cutPlanHeads[0].Id.ToString()}】信息写入设备成功");
}
else
{
uw.Rollback();
return BllResultFactory.Error($"处理打码工位【{station.WorkStationCode}】设备【{station.Name}】位置到达失败,库存批次码【{cutPlanHeads[0].Id.ToString()}】信息写入设备失败,原因:{sendResult.Msg}");
}
}
catch (Exception ex)
{
uw.Rollback();
return BllResultFactory.Exception(ex, $"处理打码工位【{station.WorkStationCode}】的设备【{station.Name}】位置到达的时候,处理库存id【{cutPlanHeads[0].Id.ToString()}】信息发生异常,原因:{ex.Message}");
}
}
}
catch (Exception ex)
{
return BllResultFactory.Exception(ex, $"处理打码工位【{station.WorkStationCode}】设备【{station.Name}】位置到达失败时候,异常原因:{ex.Message}");
}
}
/// </summary>
/// 切割请求
/// </summary>
/// <param name="station"></param>
/// <param name="allEquipments"></param>
/// <param name="plc"></param>
/// <param name="user"></param>
/// <returns></returns>
public override BllResult ExcuteControl(Equipment station, List<Equipment> allEquipments, PLCCore plc, User user)
{
var controlType = station[StationProps.ControlType.ToString()];
if (controlType != null)
{
if (controlType.Value == PLCControlType.激光除锈.GetIndexString())
{
return ExcuteDerusting(station, allEquipments, plc, user);
}
else if (controlType.Value == PLCControlType.余料除锈.GetIndexString())
{
return ExcuteStopDerusting(station, allEquipments, plc, user);
}
else
{
return BllResultFactory.Error($"处理打码工位【{station.WorkStationCode}】的设备【{station.Name}】执行PLC发给WCS的控制指令失败,任务类型【{controlType.Value}】没有做对应的处理!");
}
}
else
{
return BllResultFactory.Error($"处理打码工位【{station.WorkStationCode}】的设备【{station.Name}】执行PLC发给WCS的控制指令失败,设备属性中不存在任务类型【ControlType】");
}
}
public BllResult ExcutePintCut(Equipment station, List<Equipment> allEquipments, PLCCore plc, User user)
{
try
{
using (var uw = DALHelper.GetFreeSql().CreateUnitOfWork())
{
//cutPlanDetail.UnitOfWork = uw;
if (SendDataToMarking.markingFlag == MarkingFlagTocx.初始)
{
return BllResultFactory.Success();
}
else if (SendDataToMarking.markingFlag == MarkingFlagTocx.空闲)
{
return MarkingNormalExcute.PrintCutBarcode(true);
}
else if (SendDataToMarking.markingFlag == MarkingFlagTocx.设备收到)
{
BllResult sendResult = SendAckToPlc(station, plc, StationMessageFlag.WCSPLCACK, StationLoadStatus.ControlReply, "0", "0", "0", 10, 0, 0, 0, 0, 0);
if (sendResult.Success)
{
SendDataToMarking.markingFlag = MarkingFlagTocx.空闲;
SendDataToMarking.waitACK = false;
return BllResultFactory.Success($"处理打码工位【{station.WorkStationCode}】设备【{station.Name}】切割线请求成功,回复切割线完成信息写入PLC成功!");
}
else
{
return BllResultFactory.Error($"处理打码工位【{station.WorkStationCode}】设备【{station.Name}】切割线请求失败,回复切割线完成信息写入PLC失败失败,原因:{sendResult.Msg}");
}
}
else
{
return BllResultFactory.Error($"处理打码工位【{station.WorkStationCode}】设备【{station.Name}】切割线请求失败,回复切割线打标指令有误:失败");
}
}
}
catch (Exception ex)
{
return BllResultFactory.Exception(ex, $"处理打码工位【{station.WorkStationCode}】设备【{station.Name}】打印请求,出现异常,原因:{ex.Message}");
}
}
/// <summary>
/// 计算行走长度 --本项目弃用
/// </summary>
/// <param name="station"></param>
/// <param name="allEquipments"></param>
/// <param name="stepTraceList"></param>
/// <param name="plc"></param>
/// <returns></returns>
public BllResult ExcuteWalkPlan(Equipment station, List<Equipment> allEquipments, PLCCore plc, User user)
{
try
{
//第一次切割所需的坡口长度(每个管段都需要头尾2端消耗长度)
var sysDictDataRepository = DALHelper.GetFreeSql().GetRepository<SysDictData>();
var sysDictData = sysDictDataRepository.Where(t => t.DictType == "CutConfig" && t.DictLabel == "CutFlanConfig").First();
if (sysDictData == null)
{
return BllResultFactory.Error($"因为中控系统字典中未设置切割损耗长度!请在中控系统字典中设置!");
}
var a = Convert.ToDecimal(sysDictData.DictValue);
//打码和管子一端的距离
sysDictDataRepository = DALHelper.GetFreeSql().GetRepository<SysDictData>();
sysDictData = sysDictDataRepository.Where(t => t.DictType == "CutConfig" && t.DictLabel == "PintLength").First();
if (sysDictData == null)
{
return BllResultFactory.Error($"因为中控系统字典中未设置初始打码长度!请在中控系统字典中设置!");
}
var b = Convert.ToDecimal(sysDictData.DictValue);
//切割机和传感器的距离
sysDictDataRepository = DALHelper.GetFreeSql().GetRepository<SysDictData>();
sysDictData = sysDictDataRepository.Where(t => t.DictType == "CutConfig" && t.DictLabel == "EquipmentLength").First();
if (sysDictData == null)
{
return BllResultFactory.Error($"因为中控系统字典中未设置传感器和切割机的距离!请在中控系统字典中设置!");
}
var c = Convert.ToDecimal(sysDictData.DictValue);
//打码机和传感器的距离
sysDictDataRepository = DALHelper.GetFreeSql().GetRepository<SysDictData>();
sysDictData = sysDictDataRepository.Where(t => t.DictType == "CutConfig" && t.DictLabel == "PrintLength").First();
if (sysDictData == null)
{
return BllResultFactory.Error($"因为中控系统字典中未设置打码机和传感器的距离!请在中控系统字典中设置!");
}
var d = Convert.ToDecimal(sysDictData.DictValue);
//只能打一个码的长度
sysDictDataRepository = DALHelper.GetFreeSql().GetRepository<SysDictData>();
sysDictData = sysDictDataRepository.Where(t => t.DictType == "CutConfig" && t.DictLabel == "OnlyPrintLength").First();
if (sysDictData == null)
{
return BllResultFactory.Error($"因为中控系统字典中未设置只能打一个码的长度!请在中控系统字典中设置!");
}
var e = Convert.ToDecimal(sysDictData.DictValue);
//首段坡口损耗
sysDictDataRepository = DALHelper.GetFreeSql().GetRepository<SysDictData>();
sysDictData = sysDictDataRepository.Where(t => t.DictType == "CutConfig" && t.DictLabel == "BevelLoss").First();
if (sysDictData == null)
{
return BllResultFactory.Error($"因为中控系统字典中未设置首段坡口损耗!请在中控系统字典中设置!");
}
var f = Convert.ToDecimal(sysDictData.DictValue);
//每次累加完管段和磨损量的长度,每次打码的初始量
decimal L = 0;
//管段长度,初始为0
var l = 0;
var cutPlanHeadRepository = new CutPlanHeadRepository();
var cutPlanDetailRepository = new CutPlanDetailRepository();
//CutOrPrintPlan cutOrPrintPlan=new CutOrPrintPlan();
List<CutOrPrintPlan> cutOrPrintPlanList = new List<CutOrPrintPlan>();
var cutPlanHead = cutPlanHeadRepository.Where(t => t.State == (int)CutPlanHeadState.下料开始).First();
var cutPlanDetails = cutPlanDetailRepository.Where(t => t.HeadKeys == cutPlanHead.Keys).OrderByDescending(t => t.CutLevel).ToList();
//每根长管第一次都要切割
CutOrPrintPlan cutOrPrintPlan = new CutOrPrintPlan();
cutOrPrintPlan.HeadKeys = cutPlanHead.Keys;
cutOrPrintPlan.WalkTotal = L + c + f;
cutOrPrintPlan.State = 0;
cutOrPrintPlan.Control = (int)PLCControlType.首端坡口;
cutOrPrintPlan.BarCode = cutPlanDetails[0].BarCode;
cutOrPrintPlan.CreateBy = user.UserCode;
cutOrPrintPlan.CreateTime = DateTime.Now;
cutOrPrintPlanList.Add(cutOrPrintPlan);
//初始长度加上首段坡口的损耗
L += f;
for (int i = 0; i < cutPlanDetails.Count; i++)
{
l = (int)cutPlanDetails[i].Cuttinglength;
//每个管子需要2个打码和一个切割
CutOrPrintPlan cutOrPrintPlan1 = new CutOrPrintPlan();
cutOrPrintPlan1.HeadKeys = cutPlanHead.Keys;
cutOrPrintPlan1.WalkTotal = L + b + d;
cutOrPrintPlan1.State = 0;
cutOrPrintPlan1.Control = (int)PLCControlType.打印指令;
cutOrPrintPlan1.BarCode = cutPlanDetails[i].BarCode;
cutOrPrintPlan1.CreateBy = user.UserCode;
cutOrPrintPlan1.CreateTime = DateTime.Now;
cutOrPrintPlanList.Add(cutOrPrintPlan1);
if (cutPlanDetails[i].Cuttinglength >= e)
{
CutOrPrintPlan cutOrPrintPlan2 = new CutOrPrintPlan();
cutOrPrintPlan2.HeadKeys = cutPlanHead.Keys;
//1.3.标准件管段码(管段2端分别打码)1.4.成品管线管段码(管段1端打码)
cutOrPrintPlan2.WalkTotal = cutPlanDetails[i].Isendproduct == 0 ? L + l - b + d : L + b + b + d;
//cutOrPrintPlan2.WalkTotal = L + l - b + d;
cutOrPrintPlan2.State = 0;
cutOrPrintPlan2.Control = (int)PLCControlType.打印指令;
cutOrPrintPlan2.BarCode = cutPlanDetails[i].BarCode;
cutOrPrintPlan2.CreateBy = user.UserCode;
cutOrPrintPlan2.CreateTime = DateTime.Now;
cutOrPrintPlanList.Add(cutOrPrintPlan2);
}
//切割之前算一次损耗,把损耗长度预留在内
L += l + a;
CutOrPrintPlan cutOrPrintPlan3 = new CutOrPrintPlan();
cutOrPrintPlan3.HeadKeys = cutPlanHead.Keys;
cutOrPrintPlan3.WalkTotal = L + c;
cutOrPrintPlan3.State = 0;
cutOrPrintPlan3.Control = (int)PLCControlType.切割指令;
cutOrPrintPlan3.BarCode = cutPlanDetails[i].BarCode;
cutOrPrintPlan3.CreateBy = user.UserCode;
cutOrPrintPlan3.CreateTime = DateTime.Now;
cutOrPrintPlanList.Add(cutOrPrintPlan3);
//切割之后算一次损耗,把损耗长度预留在内
L += a;
}
#region 余料和废料处理
//BllService bllService = new BllService();
//var dict = bllService.GetDictWithDetails(x => x.Code == "MaxOddmentLength", y => y.Code == station.LineCode);
//if (!dict.Success || dict.Data == null || dict.Data.DictDetails.Count == 0)
//{
// return BllResultFactory.Error($"处理工位【{station.WorkStationCode}】设备【{station.Name}】地址请求失败,因为WCS字典中未设置线体可以自动运输的最小管段长度!请在WCS字典中设置!");
//}
//var mixPipeLength = dict.Data.DictDetails[0].Value;
//if (cutPlanHead.Pipelength - L > Convert.ToDecimal(mixPipeLength))
//{
// CutOrPrintPlan cutOrPrintPlan4 = new CutOrPrintPlan();
// cutOrPrintPlan4.HeadKeys = cutPlanHead.Keys;
// cutOrPrintPlan4.WalkTotal = L + b;
// cutOrPrintPlan4.State = 0;
// cutOrPrintPlan4.Control = (int)PLCControlType.打印指令;
// cutOrPrintPlan4.BarCode = cutPlanHead.OddmentsCode;
// cutOrPrintPlan4.CreateBy = user.UserCode;
// cutOrPrintPlan4.CreateTime = DateTime.Now;
// cutOrPrintPlanList.Add(cutOrPrintPlan4);
// CutOrPrintPlan cutOrPrintPlan5 = new CutOrPrintPlan();
// cutOrPrintPlan5.HeadKeys = cutPlanHead.Keys;
// cutOrPrintPlan5.WalkTotal = L + b + b;
// cutOrPrintPlan5.State = 0;
// cutOrPrintPlan5.Control = (int)PLCControlType.打印指令;
// cutOrPrintPlan5.BarCode = cutPlanHead.OddmentsCode;
// cutOrPrintPlan5.CreateBy = user.UserCode;
// cutOrPrintPlan5.CreateTime = DateTime.Now;
// cutOrPrintPlanList.Add(cutOrPrintPlan5);
//}
#endregion
#region 没有余料和废料的处理
//只有没有余料编码和余料长度的
if (cutPlanHead.OddmentsLength == 0 && string.IsNullOrEmpty(cutPlanHead.OddmentsCode))
{
//找到最后一个切割指令
var lastCutPlan = cutOrPrintPlanList.LastOrDefault(t => t.Control == (int)PLCControlType.切割指令);
lastCutPlan.Control = (int)PLCControlType.最后一段;
BusWorkOrderDetailRepository busWorkOrderDetailRepository = new BusWorkOrderDetailRepository();
var workOrderDetail = busWorkOrderDetailRepository.Where(t => t.BarCode == lastCutPlan.BarCode && t.WorkCenterCode == "CutBevel").ToList();
workOrderDetail[0].State = (int)EnumOrderBodyStatus.生产中;
busWorkOrderDetailRepository.Update(workOrderDetail);
//最后一段打码只能连续挨着打,不能一头一尾,应为管子长度为虚拟,以防打码到管子以外的地方
var lastPrintPlan = cutOrPrintPlanList.LastOrDefault(t => t.Control == (int)PLCControlType.打印指令);
var lastTwoPrintPlan = cutOrPrintPlanList.FirstOrDefault(t => t.Control == (int)PLCControlType.打印指令 && t.BarCode == lastPrintPlan.BarCode);
lastPrintPlan.WalkTotal = lastTwoPrintPlan.WalkTotal + b;
//cutOrPrintPlanList.Remove(lastCutPlan);
}
#endregion
#region 根据长度排序和计算单次行走长度
//冒泡排序
for (int j = 0; j < cutOrPrintPlanList.Count - 1; j++)
{
for (int i = 0; i < cutOrPrintPlanList.Count - 1; i++)
{
CutOrPrintPlan temp;
if (cutOrPrintPlanList[i].WalkTotal > cutOrPrintPlanList[i + 1].WalkTotal)
{
temp = cutOrPrintPlanList[i];
cutOrPrintPlanList[i] = cutOrPrintPlanList[i + 1];
cutOrPrintPlanList[i + 1] = temp;
}
}
}
//计算每个操作需要行走的距离
for (int i = 0; i < cutOrPrintPlanList.Count; i++)
{
if (i == 0)
{
cutOrPrintPlanList[i].Walk = cutOrPrintPlanList[i].WalkTotal;
}
else
{
cutOrPrintPlanList[i].Walk = cutOrPrintPlanList[i].WalkTotal - cutOrPrintPlanList[i - 1].WalkTotal;
}
}
#endregion
CutOrPrintPlanRepository cutOrPrintPlanRepository = new CutOrPrintPlanRepository();
cutOrPrintPlanRepository.Insert(cutOrPrintPlanList);
return BllResultFactory.Success();
}
catch (Exception ex)
{
return BllResultFactory.Exception(ex, $"处理工位【{station.WorkStationCode}】设备【{station.Name}】计算切割打码行走任务,发生异常:{ex.Message}");
}
}
///// <summary>
///// 处理打标请求
///// </summary>
///// <param name="station"></param>
///// <param name="allEquipments"></param>
///// <param name="stepTraceList"></param>
///// <param name="plc"></param>
///// <returns></returns>
//public BllResult ExcutePint(Equipment station, List<Equipment> allEquipments, PLCCore plc, User user, bool isSurplus = false)
//{
/// <summary>
/// 处理打标请求
/// </summary>
/// <param name="station"></param>
/// <param name="allEquipments"></param>
/// <param name="stepTraceList"></param>
/// <param name="plc"></param>
/// <returns></returns>
public BllResult ExcutePint(Equipment station, List<Equipment> allEquipments, PLCCore plc, User user)
{
try
{
if (SendDataToMarking.markingFlag == MarkingFlagTocx.初始 || SendDataToMarking.markingFlag == MarkingFlagTocx.打标中)
{
return BllResultFactory.Success();
}
string barcode = "";
var printCode = "";
var printTCode = "";
var length = 0;
CutPlanHead cutPlanHead = null;
CutPlanDetail cutPlanDetail = null;
var cutPlanHeadRepository = new CutPlanHeadRepository();
var cutPlanDetailRepository = new CutPlanDetailRepository();
var cutOrPrintPlanRepository = new CutOrPrintPlanRepository();
var number = station[StationProps.ArriveRealAddress.ToString()];
var controlOtherBarcode = station[StationProps.ControlOtherBarcode.ToString()];
var controlBarcode = station[StationProps.ControlBarcode.ToString()];
//如果有套料明细ID就是管段打码请求,否则就是余料、废料打码请求
if (!string.IsNullOrWhiteSpace(controlBarcode.Value))
{
//查询套料方案明细
cutPlanDetail = cutPlanDetailRepository.Where(x => x.BarCode == controlBarcode.Value && x.CutState == CutPlanDetailState.激光除锈.GetIndexInt()).OrderBy(t => t.CutLevel).First();
if (cutPlanDetail == null)
{
return BllResultFactory.Error($"处理打码工位【{station.WorkStationCode}】的设备【{station.Name}】打印请求失败,根据keys【{cutPlanDetail.BarCode}】未找到套料方案!");
}
}
else
{
return BllResultFactory.Error($"处理打码工位【{station.WorkStationCode}】的设备【{station.Name}】打印请求失败,根据一件一码【{controlBarcode.Value}】未找到套料方案!");
}
//查询物料信息
var materialRepository = new BaseMaterialRepository();
var cutplanhed = cutPlanHeadRepository.Where(t => t.Keys == cutPlanDetail.HeadKeys).First();
if (cutplanhed == null)
{
return BllResultFactory.Error($"处理打码工位【{station.WorkStationCode}】的设备【{station.Name}】打印请求失败,根据管材【{cutPlanDetail.BarCode}】的切割计划的物料编码【{cutPlanHead.MaterialCode}】找不到切割信息!请检查基础数据!");
}
var material = materialRepository.Where(x => x.MaterialCode == cutplanhed.MaterialCode).First();
if (material == null)
{
return BllResultFactory.Error($"处理打码工位【{station.WorkStationCode}】的设备【{station.Name}】打印请求失败,根据管材【{cutPlanDetail.BarCode}】的套料计划的物料编码【{cutPlanHead.MaterialCode}】找不到物料信息!请检查基础数据!");
}
if (material.Diameter == 0 || material.Thickness == 0)
{
return BllResultFactory.Error($"处理打码工位【{station.WorkStationCode}】设备【{station.Name}】打印请求失败,物料【{material.MaterialCode}】【{material.MaterialName}】的管径、壁厚参数不得为0,请检查基础数据!");
}
if (SendDataToMarking.markingFlag == MarkingFlagTocx.空闲)
{
#region 普通管段打码,如果管段状态是初始,那么就要修改套料状态再发送打码指令,否则就是多次打码,那就直接发送打码指令
// 管段的套料明细ID
//barcode = cutPlanDetail.BarCode.ToString();
if (cutPlanDetail.CutState != CutPlanDetailState.初始.GetIndexInt())
{
printCode = cutPlanDetail.BarCode;
printTCode = cutPlanDetail.Extend1;
length = printCode.Length;
int length1 = printTCode.Length;
int baseLength = length1 / 3; // 每部分的基础长度
int remainder = length1 % 3; // 剩余字符数
// 计算每部分的实际长度
int part1Length = baseLength + (remainder >= 1 ? 1 : 0);
int part2Length = baseLength + (remainder >= 2 ? 1 : 0);
int part3Length = baseLength; // 最后一部分取剩余字符
// 截取子字符串
string part1 = printTCode.Substring(0, part1Length);
string part2 = printTCode.Substring(part1Length, part2Length);
string part3 = printTCode.Substring(part1Length + part2Length);
var printResult = MarkingNormalExcute.PrintBarcodeToDB("0003", printCode, part1, part2, part3);
if (printResult.Success)
{
return BllResultFactory.Success($"处理打码工位【{station.WorkStationCode}】设备【{station.Name}】打印请求成功,给打标机发送条码【{cutPlanDetail.BarCode}】成功!");
}
else
{
return BllResultFactory.Error($"处理打码工位【{station.WorkStationCode}】设备【{station.Name}】打印请求失败,给打标机发送条码【{cutPlanDetail.BarCode}】失败,原因:{printResult.Msg}");
}
}
else
{
try
{
printCode = cutPlanDetail.BarCode;
printTCode = cutPlanDetail.Extend1;
length = printCode.Length;
int length1 = printTCode.Length;
int baseLength = length1 / 3; // 每部分的基础长度
int remainder = length1 % 3; // 剩余字符数
// 计算每部分的实际长度
int part1Length = baseLength + (remainder >= 1 ? 1 : 0);
int part2Length = baseLength + (remainder >= 2 ? 1 : 0);
int part3Length = baseLength; // 最后一部分取剩余字符
// 截取子字符串
string part1 = printTCode.Substring(0, part1Length);
string part2 = printTCode.Substring(part1Length, part2Length);
string part3 = printTCode.Substring(part1Length + part2Length);
// 发送打标机进行打标
var printResult = MarkingNormalExcute.PrintBarcodeToDB("0004", printCode, part1, part2, part3);
if (printResult.Success)
{
// 发送回复确认打标成功
return BllResultFactory.Success($"处理打码工位【{station.WorkStationCode}】设备【{station.Name}】打印请求成功,给打标机发送条码【{cutPlanDetail.BarCode}】成功!管段号以及优先级【{cutPlanDetail.CutLevel}】");
}
else
{
return BllResultFactory.Error($"处理打码工位【{station.WorkStationCode}】设备【{station.Name}】打印请求失败,给打标机发送条码【{cutPlanDetail.BarCode}】失败,原因:{printResult.Msg}");
}
}
catch (Exception ex)
{
return BllResultFactory.Exception(ex, $"处理打码工位【{station.WorkStationCode}】设备【{station.Name}】打印请求出现异常,异常原因:" + ex.Message);
}
}
#endregion
}
else if (SendDataToMarking.markingFlag == MarkingFlagTocx.设备收到)
{
using (var uw = DALHelper.GetFreeSql().CreateUnitOfWork())
{
if (cutPlanDetail.CutState == CutPlanDetailState.激光除锈.GetIndexInt())
{
cutPlanDetailRepository.UnitOfWork = uw;
// 更新状态
cutPlanDetail.CutState = CutPlanDetailState.激光打标.GetIndexInt();
cutPlanDetail.UpdateTime = DateTime.Now;
//更新套料计划明细
cutPlanDetailRepository.Update(cutPlanDetail);
}
//decimal cutLength = cutPlanDetail == null ? cutPlanHead.OddmentsLength : cutPlanDetail.Cuttinglength;
BllResult sendResult = SendAckToPlc(station, plc, StationMessageFlag.WCSPLCACK, StationLoadStatus.ControlReply, number.Value, cutPlanDetail.BarCode.ToString(), "0", PLCControlType.打印指令.GetIndexInt(), material.TypesPlcCode, cutPlanDetail.Cuttinglength, material.Diameter, material.Thickness);
if (sendResult.Success)
{
uw.Commit();
SendDataToMarking.markingFlag = MarkingFlagTocx.空闲;
SendDataToMarking.waitACK = false;
return BllResultFactory.Success($"处理打码工位【{station.WorkStationCode}】设备【{station.Name}】打印请求成功,回复打标完成信息写入PLC成功!");
}
else
{
uw?.Rollback();
return BllResultFactory.Error($"处理打码工位【{station.WorkStationCode}】设备【{station.Name}】打印请求成功,回复打标完成信息写入PLC失败失败,原因:{sendResult.Msg}");
}
}
}
else
{
return BllResultFactory.Error($"处理打码工位【{station.WorkStationCode}】设备【{station.Name}】打印请求出现异常,未找到初始数据");
}
//return BllResultFactory.Error();
}
catch (Exception ex)
{
return BllResultFactory.Exception(ex, $"处理打码工位【{station.WorkStationCode}】设备【{station.Name}】打印请求,出现异常,原因:{ex.Message}");
}
}
public BllResult ExcutePintYL(Equipment station, List<Equipment> allEquipments, PLCCore plc, User user)
{
try
{
if (SendDataToMarking.markingFlag == MarkingFlagTocx.初始 || SendDataToMarking.markingFlag == MarkingFlagTocx.打标中)
{
return BllResultFactory.Success();
}
string barcode = "";
var printCode = "";
var printTCode = "";
var length = 0;
CutPlanHead cutPlanHead = null;
CutPlanDetail cutPlanDetail = null;
var cutPlanHeadRepository = new CutPlanHeadRepository();
var cutPlanDetailRepository = new CutPlanDetailRepository();
var cutOrPrintPlanRepository = new CutOrPrintPlanRepository();
var number = station[StationProps.ArriveRealAddress.ToString()];
var controlOtherBarcode = station[StationProps.ControlOtherBarcode.ToString()];
var controlBarcode = station[StationProps.ControlBarcode.ToString()];
if (!string.IsNullOrWhiteSpace(controlBarcode.Value))
{
var convertResult = int.TryParse(controlBarcode.Value, out int cuttingDetailId);
if (!convertResult)
{
return BllResultFactory.Error($"处理打码工位【{station.WorkStationCode}】的设备【{station.Name}】打印余料请求失败,工单明细ID【{controlBarcode.Value}】转化为整数失败!");
}
if (cuttingDetailId < 1)
{
return BllResultFactory.Error($"处理打码工位【{station.WorkStationCode}】的设备【{station.Name}】打印余料请求失败,工单明细ID【{controlBarcode.Value}】不能小于1!");
}
//查询套料方案明细
cutPlanHead = cutPlanHeadRepository.Where(x => x.Id == cuttingDetailId).First();
if (cutPlanHead == null)
{
return BllResultFactory.Error($"处理打码工位【{station.WorkStationCode}】的设备【{station.Name}】打印余料请求失败,根据工单明细ID【{controlBarcode.Value}】找不到套料明细!");
}
}
else
{
return BllResultFactory.Error($"处理打码工位【{station.WorkStationCode}】的设备【{station.Name}】打印余料请求失败,根据一件一码【{controlBarcode.Value}】未找到套料方案!");
}
//查询物料信息
var materialRepository = new BaseMaterialRepository();
var material = materialRepository.Where(x => x.MaterialCode == cutPlanHead.MaterialCode).First();
if (material == null)
{
return BllResultFactory.Error($"处理打码工位【{station.WorkStationCode}】的设备【{station.Name}】打印余料请求失败,根据管材【{cutPlanHead.LotNo}】的套料计划的物料编码【{cutPlanHead.MaterialCode}】找不到物料信息!请检查基础数据!");
}
if (material.Diameter == 0 || material.Thickness == 0)
{
return BllResultFactory.Error($"处理打码工位【{station.WorkStationCode}】设备【{station.Name}】打印余料请求失败,物料【{material.MaterialCode}】【{material.MaterialName}】的管径、壁厚参数不得为0,请检查基础数据!");
}
if (SendDataToMarking.markingFlag == MarkingFlagTocx.空闲)
{
#region 余料发送打标机、废料直接响应PLC
printCode = cutPlanHead.OddmentsCode;
printTCode = cutPlanHead.OddmentsCode;
length = printCode.Length;
int length1 = printCode.Length;
int baseLength = length1 / 3; // 每部分的基础长度
int remainder = length1 % 3; // 剩余字符数
// 计算每部分的实际长度
int part1Length = baseLength + (remainder >= 1 ? 1 : 0);
int part2Length = baseLength + (remainder >= 2 ? 1 : 0);
int part3Length = baseLength; // 最后一部分取剩余字符
// 截取子字符串
string part1 = printCode.Substring(0, part1Length);
string part2 = printCode.Substring(part1Length, part2Length);
string part3 = printCode.Substring(part1Length + part2Length);
if (cutPlanHead.OddmentsLength != 0)
{
// 发送打标
var printResult = MarkingNormalExcute.PrintBarcodeToDB("0003", cutPlanHead.OddmentsCode, part1, part2, part3);
if (printResult.Success)
{
return BllResultFactory.Success($"处理打码工位【{station.WorkStationCode}】设备【{station.Name}】打印余料请求成功,给打标机发送余料条码【{cutPlanHead.OddmentsCode}】成功!");
}
else
{
return BllResultFactory.Error($"处理打码工位【{station.WorkStationCode}】设备【{station.Name}】打印余料请求失败,给打标机发送余料条码【{cutPlanHead.OddmentsCode}】失败,原因:{printResult.Msg}!");
}
}
#endregion
}
else if (SendDataToMarking.markingFlag == MarkingFlagTocx.设备收到)
{
decimal cutLength = cutPlanDetail == null ? cutPlanHead.OddmentsLength : cutPlanDetail.Cuttinglength;
BllResult sendResult = SendAckToPlc(station, plc, StationMessageFlag.WCSPLCACK, StationLoadStatus.ControlReply, "0", "0", "0", PLCControlType.余料打印指令.GetIndexInt(), 0, 0, 0, 0, 0);
if (sendResult.Success)
{
SendDataToMarking.markingFlag = MarkingFlagTocx.空闲;
SendDataToMarking.waitACK = false;
return BllResultFactory.Success($"处理打码工位【{station.WorkStationCode}】设备【{station.Name}】打印余料请求成功,回复打标完成信息写入PLC成功!");
}
else
{
return BllResultFactory.Error($"处理打码工位【{station.WorkStationCode}】设备【{station.Name}】打印余料请求成功,回复打标完成信息写入PLC失败失败,原因:{sendResult.Msg}");
}
}
return BllResultFactory.Error();
}
catch (Exception ex)
{
return BllResultFactory.Exception(ex, $"处理打码工位【{station.WorkStationCode}】设备【{station.Name}】打印请求,出现异常,原因:{ex.Message}");
}
}
//}
/// <summary>
/// 处理除锈请求
/// </summary>
/// <param name="station"></param>
/// <param name="allEquipments"></param>
/// <param name="stepTraceList"></param>
/// <param name="plc"></param>
/// <returns></returns>
public BllResult ExcuteDerusting(Equipment station, List<Equipment> allEquipments, PLCCore plc, User user)
{
try
{
var cutplanH = new CutPlanHeadRepository();
var cutPlanDetail = new CutPlanDetailRepository();
var controlOtherBarcode = station[StationProps.ControlBarcode.ToString()];
var a = cutplanH.Where(t => t.State == (int)CutPlanHeadState.下料完成 && station.LineCode == t.LineCode && t.Id.ToString() == controlOtherBarcode.Value).ToList();
var b = cutPlanDetail.Where(t => t.HeadKeys == a[0].Keys && t.CutState == (int)CutPlanDetailState.初始).OrderBy(t=>t.CutLevel).ToList(); // 正序拿到第一个可以除锈的管子
string barcode = b[0].BarCode;
using (var uw = DALHelper.GetFreeSql().CreateUnitOfWork())
{
cutPlanDetail.UnitOfWork = uw;
cutplanH.UnitOfWork = uw;
if (SendDataTo.markingFlag == MarkingFlag.初始)
{
return BllResultFactory.Success();
}
else if (SendDataTo.markingFlag == MarkingFlag.空闲)
{
if ((int)b[0].Cuttinglength <= 200)
{
BllResult sendResult = SendAckToPlc(station, plc, StationMessageFlag.WCSPLCACK, StationLoadStatus.ControlReply, "0", b[0].Id.ToString(), "0", PLCControlType.激光除锈.GetIndexInt(), 0, (int)b[0].Cuttinglength, 0, 0, 0);
if (sendResult.Success)
{
b[0].CutState = (int)CutPlanDetailState.激光除锈;
b[0].measureStartTime = DateTime.Now;
cutPlanDetail.Update(b[0]);
uw.Commit();
return BllResultFactory.Success($"处理短管除锈工位【{station.WorkStationCode}】设备【{station.Name}】除锈请求成功,回复除锈完成信息写入PLC成功!");
}
else
{
uw.Rollback();
return BllResultFactory.Error($"处理短管除锈工位【{station.WorkStationCode}】设备【{station.Name}】除锈请求成功,回复除锈完成信息写入PLC失败失败,原因:{sendResult.Msg}");
}
}
else
{
return MarkingNormalExcuteCX.PrintDerustingToStop(barcode);
}
}
else if (SendDataTo.markingFlag == MarkingFlag.打标完成)
{
BllResult sendResult = SendAckToPlc(station, plc, StationMessageFlag.WCSPLCACK, StationLoadStatus.ControlReply, "0", b[0].Id.ToString(), "0", PLCControlType.激光除锈.GetIndexInt(), 0, (int)b[0].Cuttinglength, 0, 0, 0);
if (sendResult.Success)
{
SendDataTo.markingFlag = MarkingFlag.空闲;
SendDataTo.waitACK = false;
b[0].CutState = (int)CutPlanDetailState.激光除锈;
b[0].measureStartTime = DateTime.Now;
cutPlanDetail.Update(b[0]);
uw.Commit();
return BllResultFactory.Success($"处理除锈工位【{station.WorkStationCode}】设备【{station.Name}】除锈请求成功,回复除锈完成信息写入PLC成功!");
}
else
{
uw.Rollback();
return BllResultFactory.Error($"处理除锈工位【{station.WorkStationCode}】设备【{station.Name}】除锈请求成功,回复除锈完成信息写入PLC失败失败,原因:{sendResult.Msg}");
}
}
else if (SendDataTo.markingFlag == MarkingFlag.打标中)
{
NoticeBus.Notice($"处理设备[{station.Name}],激光除锈失败失败!设备状态为【{SendDataTo.markingFlag}】!", Level.Info);
return BllResultFactory.Success();
}
else
{
return BllResultFactory.Error($"处理除锈工位【{station.WorkStationCode}】设备【{station.Name}】除锈请求失败,等待除锈设备除锈完成,打标机状态:{SendDataTo.markingFlag}");
}
}
}
catch (Exception ex)
{
return BllResultFactory.Exception(ex, $"处理除锈工位【{station.WorkStationCode}】设备【{station.Name}】除锈请求,出现异常,原因:{ex.Message}");
}
}
public BllResult ExcuteStopDerusting(Equipment station, List<Equipment> allEquipments, PLCCore plc, User user)
{
try
{
if (SendDataTo.markingFlag == MarkingFlag.初始)
{
return BllResultFactory.Success();
}
else if (SendDataTo.markingFlag == MarkingFlag.空闲)
{
var cutplanH = new CutPlanHeadRepository();
var controlOtherBarcode = station[StationProps.ControlBarcode.ToString()];
var a = cutplanH.Where(t => t.State == (int)CutPlanHeadState.下料完成 && station.LineCode == t.LineCode && t.Id.ToString() == controlOtherBarcode.Value).ToList();
var barCode = "123";
if (a.Count > 0)
{
barCode = a[0].OddmentsCode;
}
return MarkingNormalExcuteCX.PrintDerustingToStop(barCode);
}
else if (SendDataTo.markingFlag == MarkingFlag.打标完成)
{
BllResult sendResult = SendAckToPlc(station, plc, StationMessageFlag.WCSPLCACK, StationLoadStatus.ControlReply, "0", "0", "0", PLCControlType.余料除锈.GetIndexInt(), 0, 0, 0, 0, 0);
if (sendResult.Success)
{
SendDataTo.markingFlag = MarkingFlag.空闲;
SendDataTo.waitACK = false;
return BllResultFactory.Success($"处理余料除锈工位【{station.WorkStationCode}】设备【{station.Name}】余料除锈请求成功,回复除锈完成信息写入PLC成功!");
}
else
{
return BllResultFactory.Error($"处理余料除锈工位【{station.WorkStationCode}】设备【{station.Name}】余料除锈请求成功,回复除锈完成信息写入PLC失败失败,原因:{sendResult.Msg}");
}
}
else if (SendDataTo.markingFlag == MarkingFlag.打标中)
{
NoticeBus.Notice($"处理设备[{station.Name}],激光余料除锈失败失败!设备状态为【{SendDataTo.markingFlag}】!", Level.Info);
return BllResultFactory.Success();
}
else
{
return BllResultFactory.Error($"处理余料除锈工位【{station.WorkStationCode}】设备【{station.Name}】余料除锈请求失败,等待除锈设备除锈完成,打标机状态:{SendDataTo.markingFlag}");
}
}
catch (Exception ex)
{
return BllResultFactory.Exception(ex, $"处理余料除锈工位【{station.WorkStationCode}】设备【{station.Name}】余料除锈请求,出现异常,原因:{ex.Message}");
}
}
public string Chek(Equipment equipment, List<Equipment> allEquipments, CutPlanTaskRepository cutPlanTaskRepository, Equipment nextStation)
{
var msg = "";
var leftTask = cutPlanTaskRepository.Where(t => t.Status < 100 && t.Status > 0 && t.IsAuto && t.NowStation == nextStation.SelfAddress).ToList();
if (leftTask.Any())
{
return msg = $"处理工位【{equipment.WorkStationCode}】设备【{equipment.Name}】地址请求失败,左坡口存在任务[{leftTask[0].Id}]";
}
var OperationModel = nextStation[StationProps.OperationModel.ToString()];
var TotalError = nextStation[StationProps.TotalError.ToString()];
var AllowEntry = nextStation[StationProps.AllowEntry.ToString()];
if (OperationModel.Value != OperationModelFlag.Online.GetIndexString() || TotalError.Value != TotalErrorFlag.Normal.GetIndexString() || AllowEntry.Value != StationAllowEntry.Allow.GetIndexString())
{
var tempMsg = "";
if (OperationModel.Value != OperationModelFlag.Online.GetIndexString())
{
tempMsg += $"联机、";
}
if (TotalError.Value != TotalErrorFlag.Normal.GetIndexString())
{
tempMsg += $"无故障、";
}
if (AllowEntry.Value != StationAllowEntry.Allow.GetIndexString())
{
tempMsg += $"允许放货";
}
return msg = $"处理工位【{equipment.WorkStationCode}】设备【{equipment.Name}】地址请求失败,-{nextStation.Code}工位不满足[{tempMsg}-{nextStation.Name}设备信号异常";
}
return msg = string.Empty;
}
}
}