WinMain.xaml.cs
37.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
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
using HHECS.Bll;
using HHECS.Common;
using HHECS.Controls;
using HHECS.Model;
using HHECS.OPC;
using HHECS.View.Win;
using S7.Net;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Diagnostics;
using System.Linq;
using System.Runtime.Remoting;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
namespace HHECS.View
{
/// <summary>
/// Main.xaml 的交互逻辑
/// </summary>
public partial class WinMain : BaseWindow
{
#region 属性
/// <summary>
/// OPC地址
/// </summary>
public string PLCIP { get; set; }
public OPCHelp PLC { get; set; }
/// <summary>
/// 时钟同步锁
/// </summary>
private object locker = new object();
/// <summary>
/// 子窗体
/// </summary>
public Dictionary<string, Window> ChildrenWin { get; set; } = new Dictionary<string, Window>();
/// <summary>
/// 主控时钟
/// </summary>
private System.Timers.Timer timer = new System.Timers.Timer();
/// <summary>
/// 结束时钟标志
/// </summary>
private bool stop = false;
/// <summary>
/// 用于创建设备PLC
/// </summary>
public List<Plc> PLCs { get; set; } = new List<Plc>();
/// <summary>
/// 设备
/// </summary>
public List<Equipment> Equipments { get; set; }
/// <summary>
/// 设备属性
/// </summary>
public List<EquipmentProp> EquipmentProps { get; set; }
/// <summary>
/// 设备类型
/// </summary>
public List<EquipmentType> EquipmentTypes { get; set; }
/// <summary>
/// 设备属性模板
/// </summary>
public List<EquipmentTypePropTemplate> EquipmentTypePropTemplates { get; set; }
/// <summary>
/// 堆垛机处理类
/// </summary>
public List<StockerExcute> StockerExcutes { get; set; } = new List<StockerExcute>();
/// <summary>
/// 站台处理类
/// </summary>
public List<StationExcute> StationExcutes { get; set; } = new List<StationExcute>();
/// <summary>
/// 日志监控
/// </summary>
public LogInfo LogInfo { get; set; }
/// <summary>
/// 堆垛机监控
/// </summary>
public StockerMonitor StockerMonitor { get; set; }
/// <summary>
/// 站台模型监控类
/// </summary>
public List<StationModel> StationModels { get; set; } = new List<StationModel>();
int tempCount;
#endregion
public WinMain()
{
InitializeComponent();
}
private void Init()
{
//时钟初始化
InitTimer();
//日志初始化
Logger.LogWrite += Logger_LogWrite;
this.WindowState = WindowState.Maximized;
this.Lab1.Content = $"欢迎:{AppSession.User.UserName} {DateTime.Now.ToLocalTime()}";
//日志
LogInfo = new LogInfo(1350, 600);
//堆垛机监控
StockerMonitor = new StockerMonitor(30, 2, false, 1700, 150);
this.StockerView.Content = StockerMonitor;
StockerInfo stockerInfo = new StockerInfo(500, 600)
{
ControlName = "stocker1"
};
stockerInfo.DoubleInEvent += StockerInfo_DoubleInEvent;
stockerInfo.EmptyOutEvent += StockerInfo_EmptyOutEvent;
stockerInfo.OverrideTask += StockerInfo_OverrideTask;
stockerInfo.ForkErrorEvent += StockerInfo_ForkErrorEvent;
stockerInfo.DeleteTask += StockerInfo_DeleteTask;
this.EquipmentPanel.Children.Add(stockerInfo);
//SPMain.Children.Add(stockerInfo);
this.EquipmentPanel.Children.Add(LogInfo);
//this.SPMain.Children.Add(LogInfo);
//LED
AppSession.LEDExcute.BeginSendInfo();
#region 基础数据
var result1 = AppSession.Bll.GetCommonModelByCondition<Equipment>("where disable = 0");
var result2 = AppSession.Bll.GetCommonModelByCondition<EquipmentProp>("");
var result3 = AppSession.Bll.GetCommonModelByCondition<EquipmentType>("");
var result4 = AppSession.Bll.GetCommonModelByCondition<EquipmentTypePropTemplate>("");
if (!result1.Success || !result2.Success || !result3.Success || !result4.Success)
{
MessageBox.Show("初始化设备信息异常");
Btn_BeginExcute.IsEnabled = false;
Btn_EndExcute.IsEnabled = false;
return;
}
Equipments = result1.Data;
EquipmentProps = result2.Data;
EquipmentTypes = result3.Data;
EquipmentTypePropTemplates = result4.Data;
//组合逻辑外键
Equipments.ForEach(t =>
{
t.EquipmentType = EquipmentTypes.FirstOrDefault(i => i.Id == t.EquipmentTypeId);
t.EquipmentProps.AddRange(EquipmentProps.Where(i => i.EquipmentId == t.Id).ToList());
});
EquipmentProps.ForEach(t =>
{
t.Equipment = Equipments.FirstOrDefault(i => i.Id == t.EquipmentId);
//组合地址
t.Address = $"S7:[{t.Equipment.ConnectName}]{t.Address}";
t.EquipmentTypePropTemplate = EquipmentTypePropTemplates.FirstOrDefault(i => i.Id == t.EquipmentTypePropTemplateId);
});
//判断逻辑外键是否组合完毕
if (Equipments.Count(t => t.EquipmentType == null || t.EquipmentProps.Count == 0) > 0)
{
MessageBox.Show("初始化设备信息失败,请检查基础数据");
Btn_BeginExcute.IsEnabled = false;
Btn_EndExcute.IsEnabled = false;
return;
}
//if (EquipmentProps.Count(t => t.Equipment == null || t.EquipmentTypePropTemplate == null) > 0)
//{
// MessageBox.Show("初始化设备属性失败,请检查基础数据");
// Btn_BeginExcute.IsEnabled = false;
// Btn_EndExcute.IsEnabled = false;
// return;
//}
//组合站监控类
Equipments.ForEach(t =>
{
if (t.EquipmentType.Code.Contains("Station"))
{
StationModels.Add(new StationModel()
{
Name = t.Name
});
}
});
this.DGStation.ItemsSource = StationModels;
//组合处理类
EquipmentTypes.ForEach(t =>
{
if (t.Code == "stocker")
{
StockerExcutes.Add(new SingeForkStockerExcute() { EquipmentType = t });
}
if (t.Code == "StationForPortOut")
{
StationExcutes.Add(new StationOutExcute() { EquipmentType = t });
}
if (t.Code == "StationForPortInOrOut")
{
StationExcutes.Add(new StationInOrOutExcute() { EquipmentType = t });
}
if (t.Code == "StationForPortIn")
{
StationExcutes.Add(new StationInExcute() { EquipmentType = t });
}
if (t.Code == "StationForStockerIn")
{
StationExcutes.Add(new StationForStockerInExcute() { EquipmentType = t });
}
if (t.Code == "StationForStockerOut")
{
StationExcutes.Add(new StationForStockerOutExcute() { EquipmentType = t });
}
if (t.Code == "StationForStockerInOrOut")
{
StationExcutes.Add(new StationForStockerInOrOutExcute() { EquipmentType = t });
}
});
#endregion
#region OPC
try
{
PLCIP = ConfigurationManager.AppSettings["OPCServerIP"];
PLC = new OPCHelp(PLCIP);
}
catch (Exception ex)
{
MessageBox.Show("未能获取到OPC配置,请检查。", "注意", MessageBoxButton.OK, MessageBoxImage.Error);
}
//组合Dataitem和Plc
//try
//{
// Equipments.ForEach(t =>
// {
// if (PLCs.Count(i => i.IP == t.IP) == 0)
// {
// PLCs.Add(new Plc(CpuType.S71500, t.IP, 0, 1));
// }
// });
// Equipments.ForEach(a => a.EquipmentProps.Where(t => t.EquipmentTypePropTemplate.PropType == "PLC").ToList().ForEach(t =>
// {
// t.DataItem = S7Helper.FromAddress(t.Address);
// }));
// //打开PLC
// PLCs.ForEach(t => t.Open());
//}
//catch (PlcException plcex)
//{
// string error = $"打开通讯连接出现异常:{plcex.Message};请检查网络配置,然后重启本程序!";
// MessageBox.Show(error);
// Logger.Log(error, LogLevel.Error);
// return;
//}
//catch (Exception ex)
//{
// MessageBox.Show("组合数据地址时发生错误,请检查数据地址是否配置正确。");
// Btn_BeginExcute.IsEnabled = false;
// Btn_EndExcute.IsEnabled = false;
// return;
//}
#endregion
#region 与WMS心跳
Task.Run(async () =>
{
while (true)
{
await Task.Delay(30000);
var temp = AppSession.Bll.HeartBeat();
if (!temp.Success)
{
stop = true;
Logger.Log($"与WMS失去连接,请检查网络情况,处理停止:{temp.Msg}", LogLevel.Warning);
}
}
});
#endregion
}
#region 堆垛机空出、重入、取货错误和重新下发任务
/// <summary>
/// 取货错误
/// </summary>
/// <param name="stocker"></param>
/// <returns></returns>
private BllResult StockerInfo_ForkErrorEvent(Equipment stocker)
{
//if (PLCs == null)
//{
// return BllResultFactory.Error(null, "底层通讯未连接");
//}
//var plc = PLCs.FirstOrDefault(t => t.IP == stocker.IP);
//if (plc == null || plc.IsConnected == false || plc.IsAvailable == false)
//{
// return BllResultFactory.Error(null, "底层通讯未连接");
//}
if (PLC == null || PLC.GetConnStatus() == false)
{
//AddLogToUI("地址读取失败,请检查通讯连接", 2);
return BllResultFactory.Error(null, "地址读取失败,请检查通讯连接");
}
var taskProp = stocker.EquipmentProps.FirstOrDefault(t => t.EquipmentTypePropTemplateCode == "Fork1TaskNo");
if (taskProp == null)
{
return BllResultFactory.Error(null, $"未找到堆垛机{stocker.Name}的任务属性");
}
var taskResult = AppSession.Bll.GetCommonModelByCondition<TaskEntity>($"where id = {taskProp.Value}");
if (taskResult.Success)
{
var task = taskResult.Data[0];
BllResult result = AppSession.Bll.TaskForkErrorHandle(task.Id);
if (result.Success)
{
//删除当前堆垛机执行的任务
var prop = stocker.EquipmentProps.FirstOrDefault(t => t.EquipmentTypePropTemplateCode == "WCSFork1TaskFlag");
prop.Value = ForkTaskFlag.删除任务.GetIndexString();
//result = S7Helper.PlcSplitWrite(plc, new List<EquipmentProp>() { prop }, 20);
result = PLC.WriteAddress(new List<EquipmentProp>() { prop });
if (result.Success)
{
return BllResultFactory.Sucess(null, "成功");
}
else
{
Logger.Log($"通知WMS取货错误处理成功,但是下发{stocker}删除任务{task.Id}失败!", LogLevel.Error);
return BllResultFactory.Error($"通知WMS取货错误处理成功,但是下发{stocker}删除任务{task.Id}失败!");
}
}
else
{
return BllResultFactory.Error($"堆垛机{stocker.Name}的任务{task.Id}处理取货错误失败:{result.Msg}");
}
}
else
{
return BllResultFactory.Error(null, $"未找到{stocker.Name}对应的任务Id:{taskProp.Value}");
}
}
/// <summary>
/// 重新下发
/// </summary>
/// <param name="stocker"></param>
/// <returns></returns>
private BllResult StockerInfo_OverrideTask(Equipment stocker)
{
//if (PLCs == null)
//{
// return BllResultFactory.Error(null, "底层通讯未连接");
//}
//var plc = PLCs.FirstOrDefault(t => t.IP == stocker.IP);
//if (plc == null || plc.IsConnected == false || plc.IsAvailable == false)
//{
// return BllResultFactory.Error(null, "底层通讯未连接");
//}
if (PLC == null || PLC.GetConnStatus() == false)
{
//AddLogToUI("地址读取失败,请检查通讯连接", 2);
return BllResultFactory.Error(null, "地址读取失败,请检查通讯连接");
}
var taskProp = stocker.EquipmentProps.FirstOrDefault(t => t.EquipmentTypePropTemplateCode == "Fork1TaskNo");
if (taskProp == null)
{
return BllResultFactory.Error(null, $"未找到堆垛机{stocker.Name}的任务属性");
}
var taskResult = AppSession.Bll.GetCommonModelByCondition<TaskEntity>($"where id = {taskProp.Value}");
if (taskResult.Success)
{
var task = taskResult.Data[0];
if (task.LastStatus == TaskEntityStatus.下发堆垛机库内取货任务.GetIndexInt() || task.LastStatus == TaskEntityStatus.下发堆垛机库内放货任务.GetIndexInt() || task.LastStatus == TaskEntityStatus.下发堆垛机库外取货任务.GetIndexInt() || task.LastStatus == TaskEntityStatus.下发堆垛机库外放货任务.GetIndexInt())
{
var prop = stocker.EquipmentProps.FirstOrDefault(t => t.EquipmentTypePropTemplateCode == "WCSFork1TaskFlag");
prop.Value = ForkTaskFlag.删除任务.GetIndexString();
//var result = S7Helper.PlcSplitWrite(plc, new List<EquipmentProp>() { prop }, 20);
var result = PLC.WriteAddress(new List<EquipmentProp>() { prop });
if (result.Success)
{
task.SendAgain = 1;
result = AppSession.Bll.UpdateCommonModel<TaskEntity>(task);
if (result.Success)
{
return BllResultFactory.Sucess(null, "成功");
}
else
{
Logger.Log($"通知{stocker}删除任务{task.Id}成功,但是更新任务重新下发的表示失败!", LogLevel.Error);
return BllResultFactory.Error($"通知{stocker}删除任务{task.Id}成功,但是更新任务重新下发的表示失败!");
}
}
else
{
Logger.Log($"通知{stocker}删除任务{task.Id}失败", LogLevel.Error);
return BllResultFactory.Error($"通知{stocker}删除任务{task.Id}失败");
}
}
else
{
return BllResultFactory.Error(null, $"{stocker.Name}对应的任务Id:{taskProp.Value}任务状态不能重新下发");
}
}
else
{
return BllResultFactory.Error(null, $"未找到{stocker.Name}对应的任务Id:{taskProp.Value}");
}
}
/// <summary>
/// 空出处理
/// </summary>
/// <param name="stocker"></param>
/// <returns></returns>
private BllResult StockerInfo_EmptyOutEvent(Equipment stocker)
{
//if (PLCs == null)
//{
// return BllResultFactory.Error(null, "底层通讯未连接");
//}
//var plc = PLCs.FirstOrDefault(t => t.IP == stocker.IP);
//if (plc == null || plc.IsConnected == false || plc.IsAvailable == false)
//{
// return BllResultFactory.Error(null, "底层通讯未连接");
//}
if (PLC == null || PLC.GetConnStatus() == false)
{
//AddLogToUI("地址读取失败,请检查通讯连接", 2);
return BllResultFactory.Error(null, "地址读取失败,请检查通讯连接");
}
var taskProp = stocker.EquipmentProps.FirstOrDefault(t => t.EquipmentTypePropTemplateCode == "Fork1TaskNo");
var emptyOutProp = stocker.EquipmentProps.FirstOrDefault(t => t.EquipmentTypePropTemplateCode == "Fork1EmptyOut");
if (taskProp == null | emptyOutProp == null)
{
return BllResultFactory.Error(null, $"未找到堆垛机{stocker.Name}的任务和空出属性");
}
if (emptyOutProp.Value != "True")
{
return BllResultFactory.Error(null, $"堆垛机{stocker.Name}未报空出错误");
}
var taskResult = AppSession.Bll.GetCommonModelByCondition<TaskEntity>($"where id = {taskProp.Value}");
if (taskResult.Success)
{
var task = taskResult.Data[0];
if (task.LastStatus < TaskEntityStatus.任务完成.GetIndexInt())
{
var result = AppSession.Bll.EmptyOutHandle(task);
if (result.Success)
{
var prop = stocker.EquipmentProps.FirstOrDefault(t => t.EquipmentTypePropTemplateCode == "WCSFork1TaskFlag");
prop.Value = ForkTaskFlag.删除任务.GetIndexString();
//result = S7Helper.PlcSplitWrite(plc, new List<EquipmentProp>() { prop }, 20);
result = PLC.WriteAddress(new List<EquipmentProp>() { prop });
if (result.Success)
{
return BllResultFactory.Sucess($"空出处理成功");
}
else
{
Logger.Log($"通知{stocker}删除任务{task.Id}失败", LogLevel.Error);
return BllResultFactory.Error($"空出处理,调用WMS成功但通知{stocker}删除任务{task.Id}失败");
}
}
else
{
return BllResultFactory.Error($"空出处理调用WMS失败:{result.Msg}");
}
}
else
{
return BllResultFactory.Error(null, $"{stocker.Name}对应的任务Id:{taskProp.Value}已完成");
}
}
else
{
return BllResultFactory.Error(null, $"未找到{stocker.Name}对应的任务Id:{taskProp.Value}");
}
}
/// <summary>
/// 重入
/// </summary>
/// <param name="location"></param>
/// <param name="stocker"></param>
/// <returns></returns>
private BllResult StockerInfo_DoubleInEvent(Location location, Equipment stocker)
{
//if (PLCs == null)
//{
// return BllResultFactory.Error(null, "底层通讯未连接");
//}
//var plc = PLCs.FirstOrDefault(t => t.IP == stocker.IP);
//if (plc == null || plc.IsConnected == false || plc.IsAvailable == false)
//{
// return BllResultFactory.Error(null, "底层通讯未连接");
//}
if (PLC == null || PLC.GetConnStatus() == false)
{
//AddLogToUI("地址读取失败,请检查通讯连接", 2);
return BllResultFactory.Error(null, "地址读取失败,请检查通讯连接");
}
var addresses = stocker.EquipmentProps;
//获取任务和是否重入
var taskProp = addresses.FirstOrDefault(t => t.EquipmentTypePropTemplateCode == "Fork1TaskNo");
var doubleInProp = addresses.FirstOrDefault(t => t.EquipmentTypePropTemplateCode == "Fork1DoubleIn");
if (taskProp == null || doubleInProp == null)
{
return BllResultFactory.Error(null, $"未找到堆垛机{stocker.Name}的任务和重入属性");
}
if (doubleInProp.Value != "True")
{
return BllResultFactory.Error(null, $"堆垛机{stocker.Name}未报重入错误");
}
var taskResult = AppSession.Bll.GetCommonModelByCondition<TaskEntity>($"where id = {taskProp.Value}");
if (taskResult.Success)
{
var task = taskResult.Data[0];
if (task.LastStatus < TaskEntityStatus.任务完成.GetIndexInt())
{
//按秋生要求,锁上发生重入的库位;
var res = AppSession.Bll.UpdateLocationStatus(task.DestinationLocation, AppSession.WarehouseId, "lock");
if (!res.Success)
{
return BllResultFactory.Error(null, $"堆垛机{stocker.Name}锁定重入库位失败;原因:{res.Msg}");
}
BllResult temp = AppSession.Bll.TaskDoubleInHandle("1", location.Code, task.Id);
if (temp.Success)
{
var excute = StockerExcutes.FirstOrDefault(t => t.EquipmentType.Id == stocker.EquipmentTypeId);
BllResult sendResult = excute.SendTaskToStocker(stocker, PLC, ForkAction.货叉1号, ForkTaskFlag.重新分配入库地址, location.RowIndex.ToString(), location.Line.ToString(), location.Layer.ToString(), "0", task.Id.ToString());
if (sendResult.Success)
{
return BllResultFactory.Sucess(null, "成功");
}
else
{
//回滚
AppSession.Bll.TaskDoubleInHandle("0", "", task.Id);
return BllResultFactory.Error(null, $"处理{stocker.Name}重入失败:{sendResult.Msg}");
}
}
else
{
return BllResultFactory.Error(null, $"处理{stocker.Name}重入失败:{temp.Msg}");
}
}
else
{
return BllResultFactory.Error(null, $"{stocker.Name}对应的任务Id:{taskProp.Value}已完成");
}
}
else
{
return BllResultFactory.Error(null, $"未找到{stocker.Name}对应的任务Id:{taskProp.Value}");
}
}
/// <summary>
/// 任务删除
/// </summary>
/// <param name="stocker"></param>
/// <returns></returns>
private BllResult StockerInfo_DeleteTask(Equipment stocker)
{
if (PLC == null || PLC.GetConnStatus() == false)
{
//AddLogToUI("地址读取失败,请检查通讯连接", 2);
return BllResultFactory.Error(null, "地址读取失败,请检查通讯连接");
}
var taskProp = stocker.EquipmentProps.FirstOrDefault(t => t.EquipmentTypePropTemplateCode == "Fork1TaskNo");
if (taskProp == null)
{
return BllResultFactory.Error(null, $"未找到堆垛机{stocker.Name}的任务属性");
}
//var taskResult = AppSession.Bll.GetCommonModelByCondition<TaskEntity>($"where id = {taskProp.Value}");
//if (taskResult.Success)
//{
//var task = taskResult.Data[0];
var prop = stocker.EquipmentProps.FirstOrDefault(t => t.EquipmentTypePropTemplateCode == "WCSFork1TaskFlag");
prop.Value = ForkTaskFlag.删除任务.GetIndexString();
//var result = S7Helper.PlcSplitWrite(plc, new List<EquipmentProp>() { prop }, 20);
var result = PLC.WriteAddress(new List<EquipmentProp>() { prop });
if (result.Success)
{
return BllResultFactory.Sucess(null, $"删除任务{taskProp.Value}成功");
}
else
{
Logger.Log($"通知{stocker}删除任务{taskProp.Value}失败", LogLevel.Error);
return BllResultFactory.Error($"通知{stocker}删除任务{taskProp.Value}失败");
}
//}
//else
//{
// return BllResultFactory.Error(null, $"未找到{stocker.Name}对应的任务Id:{taskProp.Value}");
//}
}
#endregion
#region 组件
/// <summary>
/// 日志记录
/// </summary>
/// <param name="sender"></param>
/// <param name="args"></param>
private void Logger_LogWrite(object sender, LogEventArgs args)
{
Dispatcher.Invoke(() =>
{
switch (args.LogLevel)
{
case LogLevel.Info:
LogExecute.WriteInfoLog(args.Content);
LogInfo.AddLogs(args.Content, 0);
break;
case LogLevel.Error:
LogExecute.WriteInfoLog(args.Content);
LogInfo.AddLogs(args.Content, 3);
break;
case LogLevel.Warning:
LogExecute.WriteInfoLog(args.Content);
LogInfo.AddLogs(args.Content, 2);
break;
case LogLevel.Success:
LogExecute.WriteInfoLog(args.Content);
LogInfo.AddLogs(args.Content, 1);
break;
case LogLevel.Exception:
LogExecute.WriteExceptionLog(args.Content, args.Exception);
LogInfo.AddLogs(args.Content, 3);
break;
default:
break;
}
});
}
#endregion
#region 时钟逻辑
private void Timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
lock (locker)
{
try
{
Stopwatch stopwatch = new Stopwatch();
stopwatch.Restart();
#region 是否结束处理
if (stop)
{
if (PLC.GetConnStatus())
{
PLC.CloseConn();
}
Dispatcher.Invoke(() =>
{
Btn_BeginExcute.IsEnabled = true;
Btn_EndExcute.IsEnabled = false;
timer.Enabled = false;
});
return;
}
//if (PLCs.Count > 0 && PLCs.Count(t => t.IsAvailable == false || t.IsConnected == false) > 0)
//{
// try
// {
// PLCs.ForEach(t => t.Close());
// PLCs.ForEach(t => t.Open());
// }
// catch (Exception ex)
// {
// }
// if (PLCs.Count > 0 && PLCs.Count(t => t.IsAvailable == false || t.IsConnected == false) > 0)
// {
// PLCs = new List<Plc>();
// Equipments.ForEach(t =>
// {
// if (PLCs.Count(i => i.IP == t.IP) == 0)
// {
// PLCs.Add(new Plc(CpuType.S71500, t.IP, 0, 1));
// }
// });
// PLCs.ForEach(t => t.Open());
// if (PLCs.Count > 0 && PLCs.Count(t => t.IsAvailable == false || t.IsConnected == false) > 0)
// {
// Logger.Log($"连接不可用", LogLevel.Error);
// stop = true;
// return;
// }
// }
//}
//PLCs = new List<Plc>();
//Equipments.ForEach(t =>
//{
// if (PLCs.Count(i => i.IP == t.IP) == 0)
// {
// PLCs.Add(new Plc(CpuType.S71500, t.IP, 0, 1));
// }
//});
//PLCs.ForEach(t => t.Open());
//if (PLCs.Count > 0 && PLCs.Count(t => t.IsAvailable == false || t.IsConnected == false) > 0)
//{
// Logger.Log($"连接不可用", LogLevel.Error);
// stop = true;
// return;
//}
#endregion
#region 检查是否连接正常
if (!PLC.GetConnStatus())
{
Logger.Log($"失去通讯连接,请关闭连接后重新打开", LogLevel.Error);
stop = true;
return;
}
#endregion
//读取所有地址值
//PLCs.ForEach(t => S7Helper.PlcSplitRead(t, Equipments.Where(i => t.IP == t.IP).SelectMany(i => i.EquipmentProps).Where(a => a.EquipmentTypePropTemplate.PropType == "PLC").ToList(), 10));
var result = PLC.ReadAddress(Equipments.SelectMany(t => t.EquipmentProps).Where(a => a.EquipmentTypePropTemplate.PropType == "PLC").ToList());
if (!result.Success)
{
Logger.Log($"读取地址错误:{result.Msg},处理停止,请检查网络配置", LogLevel.Error);
stop = true;
return;
}
Dispatcher.Invoke(() =>
{
foreach (var item in this.EquipmentPanel.Children)
{
if (item is StockerInfo temp)
{
var a = Equipments.Find(t => t.Code == temp.ControlName);
temp.SetProps(a);
StockerMonitor.SetStocker(0, int.TryParse(a.EquipmentProps.Find(t => t.EquipmentTypePropTemplateCode == "CurrentColumn").Value, out int column) == true ? column : 0);
}
//if (item is StationInfo station)
//{
// var a = DeviceEntities.Find(t => t.Code == station.Name);
// station.SetProp(DeviceAddressEntities.FindAll(t => t.DeviceId == a.Id));
//}
}
StationModels.ForEach(t => t.SetProp(Equipments.FirstOrDefault(a => a.Name == t.Name)?.EquipmentProps));
});
#region 堆垛机处理
StockerExcutes.ForEach(t =>
{
//一般根据堆垛机现在的位置来决定优先执行入库还是出库
t.Excute(Equipments.Where(a => a.EquipmentType.Id == t.EquipmentType.Id).ToList(), PLC);
});
#endregion
#region 站台处理
StationExcutes.ForEach(t =>
{
List<Equipment> equipments = Equipments.Where(a => a.EquipmentType.Id == t.EquipmentType.Id).ToList();
t.Excute(equipments, PLC);
});
#endregion
//给与50毫秒缓冲时间
Thread.Sleep(50);
//PLCs?.ForEach(t => t.Close());
//Logger.Log( stopwatch.Elapsed.ToString(),LogLevel.Info);
}
catch (Exception ex)
{
//发生异常,结束处理
//stop = true;
Logger.Log($"程序处理出现异常:{ex.Message};", LogLevel.Exception, ex);
}
}
}
#endregion
#region 窗体事件
/// <summary>
/// 重连WMS
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Btn_ReConnectWMS_Click(object sender, RoutedEventArgs e)
{
AppSession.Client = AppSession.InitHttpClient();
var temp = AppSession.Bll.GetUserWithRoles(AppSession.User.UserCode, AppSession.User.Password);
if (temp.Success)
{
MessageBox.Show("重连成功");
}
else
{
MessageBox.Show("重连失败");
}
}
private void InitTimer()
{
timer.Interval = AppSession.Interval; //1秒触发一次
timer.Elapsed += Timer_Elapsed; ;
timer.AutoReset = true;//每到指定时间Elapsed事件是到时间就触发
timer.Enabled = false; //指示 Timer 是否应引发 Elapsed 事件。
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
AppSession.Bll.Combine(AppSession.MenuOperations.Where(t => t.ParentId == null).ToList(), AppSession.Bll.GetAllMenuOperation().Data.Where(t => t.MenuType == "catalog" || t.MenuType == "menu").ToList());
MenuMain.ItemsSource = AppSession.MenuOperations.Where(t => t.ParentId == null).OrderBy(t => t.OrderNum).ToList();
//初始化
Init();
}
private void Window_Closed(object sender, EventArgs e)
{
stop = true;
timer.Enabled = false;
Thread.Sleep(1000);
Application.Current.Shutdown();
}
private void MenuMain_Checked(object sender, RoutedEventArgs e)
{
try
{
MenuOperation menu = (MenuOperation)((MenuItem)e.OriginalSource).Header;
var win = ChildrenWin.FirstOrDefault(t => t.Key == menu.Url).Value;
if (String.IsNullOrWhiteSpace(menu?.Url) || menu.Url == "#")
{
return;
}
if (win == null)
{
win = (Window)Activator.CreateInstance(null, menu.Url).Unwrap();
ChildrenWin.Add(menu.Url, win);
}
win.Show();
if (win.WindowState == WindowState.Minimized)
win.WindowState = WindowState.Normal;
win.Activate();
}
catch (Exception ex)
{
LogExecute.WriteExceptionLog("打开菜单", ex);
MessageBox.Show("打开菜单出现异常!");
}
}
private void Btn_BeginExcute_Click(object sender, RoutedEventArgs e)
{
if (PLC == null)
{
MessageBox.Show("PLC连接初始化失败! 请检查PLC配置然后重启本程序", "注意", MessageBoxButton.OK, MessageBoxImage.Error);
return;
}
if (PLC.OpenConn() == false)
{
MessageBox.Show("打开PLC连接失败");
return;
}
PLC.CreateGroup("group1");
PLC.AddOPCItems(Equipments.SelectMany(t => t.EquipmentProps).ToList());
stop = false;
timer.Enabled = true;
Btn_BeginExcute.IsEnabled = false;
Btn_EndExcute.IsEnabled = true;
}
private void Btn_EndExcute_Click(object sender, RoutedEventArgs e)
{
stop = true;
Btn_BeginExcute.IsEnabled = true;
Btn_EndExcute.IsEnabled = false;
}
#endregion
private void Btn_LED_Click(object sender, RoutedEventArgs e)
{
AppSession.LEDExcute.LeftInfoQueue.Enqueue($"左侧计数{tempCount}\\nhjhkhkj\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\ndfsdfdfs\\n\\n\\ndfsdf");
AppSession.LEDExcute.RightInfoQueue.Enqueue($"右侧计数{tempCount}\\nhjhkhkj");
tempCount++;
}
}
}