WinMain.xaml.cs
42.2 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
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
using HHECS.Bll;
using HHECS.Controls;
using HHECS.EquipmentExcute;
using HHECS.Model.BllModel;
using HHECS.Model.Common;
using HHECS.Model.Entities;
using HHECS.Model.Enums;
using HHECS.OPC;
using HHECS.Scan;
using HHECS.View.Win;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
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>
/// 设备
/// </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 StationMonitor stationMonitor { get; set; }
/// <summary>
/// 站台模型监控类
/// </summary>
public List<StationModel> StationModels { get; set; } = new List<StationModel>();
/// <summary>
/// PLC状态
/// </summary>
private static List<PlcStatus> siemens = new List<PlcStatus>();
#endregion
public WinMain()
{
InitializeComponent();
}
private void Init()
{
//时钟初始化
InitTimer();
//LED初始化
AppSession.LEDExcute.BeginSendInfo();
#region 初始化窗体界面
//日志初始化
Logger.LogWrite += Logger_LogWrite;
this.WindowState = WindowState.Maximized;
this.Lab1.Content = $"欢迎:{AppSession.User.UserName} {DateTime.Now.ToLocalTime()}";
//立体库站台运行图
stationMonitor = new StationMonitor();
this.MonitorPanel1.Children.Add(stationMonitor);
//立体库堆垛机运行图
stockerMonitor = new StockerMonitor(14, 2, false, 1200, 240);
stockerMonitor.ControlName = "stocker1";
this.MonitorPanel.Children.Add(stockerMonitor);
//堆垛机监控
StockerInfo stockerInfo = new StockerInfo(800, 400)
{
ControlName = "堆垛机"
};
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);
//日志控件
LogInfo = new LogInfo(1600, 404);
this.EquipmentPanel.Children.Add(LogInfo);
#endregion
#region 初始化OPC配置
try
{
this.PLCIP = ConfigurationManager.AppSettings["OPCServerIP"];
this.PLC = new OPCHelp(PLCIP);
}
catch (Exception ex)
{
MessageBox.Show("未能获取到OPC配置,请检查。", "注意", MessageBoxButton.OK, MessageBoxImage.Error);
}
#endregion
#region 与WMS心跳
Task.Run(async () =>
{
while (true)
{
await Task.Delay(30000);
var temp = AppSession.Bll.HeartBeat();
if (!temp.Success)
{
this.Dispatcher.Invoke(() =>
{
this.Btn_EndExcute_Click(null, null);
this.Btn_ReConnectWMS.IsEnabled = true;
});
Logger.Log($"与WMS失去连接,请检查网络情况,处理停止:{temp.Msg}", LogLevel.Warning);
}
else
{
// this.Dispatcher.Invoke(() => this.Btn_ReConnectWMS.IsEnabled = false);
}
}
});
#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, AppSession.Client, AppSession.Urls);
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, AppSession.Client, AppSession.Urls);
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())
{
BllResult temp = AppSession.Bll.TaskDoubleInHandle("1", location.Code, task.Id, AppSession.WarehouseId);
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, AppSession.WarehouseId);
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:
case LogLevel.Success:
LogExecute.WriteLog(LogLevel.Info.ToString(), args.Content);
LogInfo.AddLogs(args.Content, args.LogLevel);
break;
case LogLevel.Error:
case LogLevel.Warning:
LogExecute.WriteLog(args.LogLevel.ToString(), args.Content);
LogInfo.AddLogs(args.Content, args.LogLevel);
break;
case LogLevel.Exception:
LogExecute.WriteExceptionLog(args.Content, args.Exception);
LogInfo.AddLogs(args.Content, args.LogLevel);
break;
default:
break;
}
});
}
#endregion
#region 时钟逻辑
private void Timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
lock (locker)
{
try
{
//给与50毫秒缓冲时间
Thread.Sleep(200);
//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;
}
#endregion
#region 检查是否连接正常
if (!PLC.GetConnStatus())
{
this.Btn_EndExcute_Click(null, null);
Logger.Log($"失去通讯连接,请重新打开", LogLevel.Error);
return;
}
#endregion
//禁用列表超过2分钟的,从禁用列表排除
siemens.RemoveAll(x => x.Time < DateTime.Now.AddSeconds(-10));
//测试可以ping通的设备,能ping通去读取OPC,不能ping通的加入到禁用列表
//List<EquipmentProp> props = new List<EquipmentProp>();
IEnumerable<String> ips = Equipments.Select(t => t.IP).Distinct();
foreach (var ip in ips)
{
if (!siemens.Exists(x => x.Station == ip))
{
if (Util.PingTest(ip))
{
foreach (var item in Equipments.Where(x => x.IP == ip))
{
item.Disable = false;
}
}
else
{
foreach (var item in Equipments.Where(x => x.IP == ip))
{
item.Disable = true;
}
siemens.Add(new PlcStatus() { Station = ip, Msg = "PING不通", Time = DateTime.Now });
}
}
}
//读取所有地址值
//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());
List<EquipmentProp> props = Equipments.Where(x => x.Disable == false).SelectMany(x => x.EquipmentProps).Where(x => x.EquipmentTypePropTemplate.PropType == "PLC").ToList();
var result = PLC.ReadAddress(props);
if (!result.Success)
{
Dispatcher.Invoke(() => this.Btn_EndExcute_Click(null, null));
Logger.Log($"读取地址错误:{result.Msg},处理停止,请检查网络配置", LogLevel.Error);
return;
}
#region 更新控件显示
Dispatcher.Invoke(() =>
{
foreach (var item in this.MonitorPanel.Children)
{
if (item is StockerMonitor temp)
{
var stocker = Equipments.FirstOrDefault(x => x.Code == temp.ControlName && x.Disable == false);
if (stocker != null)
{
String currentColumn = stocker.EquipmentProps.Find(t => t.EquipmentTypePropTemplateCode == "CurrentColumn").Value;
temp.SetStocker(0, int.TryParse(currentColumn, out int column) == true ? column : 0);
}
}
}
foreach (var item in this.EquipmentPanel.Children)
{
if (item is StockerInfo temp)
{
var stocker = Equipments.FirstOrDefault(x => x.Code == temp.ControlName && x.Disable == false);
if (stocker != null)
{
temp.SetProps(stocker);
}
}
}
foreach (var item in Equipments.Where(x => x.EquipmentType.Code == "conveyor"))
{
var cv = stationMonitor.GetStationMonitor(item.Code);
//var cv = this.Template.FindName(item.Code, stationMonitor);
//}//var cv = stationMonitor.FindName(item.Code);
//var cv = this.FindName(item.Code);
if (cv is StationV stationV)
{
stationV.SetProp(item.EquipmentProps);
}
if (cv is StationH stationH)
{
stationH.SetProp(item.EquipmentProps);
}
}
StationModels.ForEach(t => t.SetProp(Equipments.FirstOrDefault(x => x.Name == t.Name && x.Disable == false)?.EquipmentProps));
});
#endregion
#region 堆垛机处理
StockerExcutes.ForEach(x =>
{
//一般根据堆垛机现在的位置来决定优先执行入库还是出库
x.Excute(Equipments.Where(y => y.EquipmentType.Id == x.EquipmentType.Id && y.Disable == false).ToList(), PLC);
});
#endregion
#region 站台处理
//华恒站台处理
StationExcutes.ForEach(t => t.Excute(Equipments.Where(x => x.Disable == false).ToList(), PLC));
////AGV呼叫处理
//AgvCalls.ForEach(t =>
//{
// t.Excute(Equipments.Where(a => a.EquipmentType.Id == t.EquipmentType.Id).ToList(), PLC);
//});
#endregion
}
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)
{
var temp = ConnectWMS();
if (temp.Success)
{
MessageBox.Show("连接WMS成功");
// this.Btn_ReConnectWMS.IsEnabled = false;
}
else
{
MessageBox.Show("连接WMS失败");
// this.Btn_ReConnectWMS.IsEnabled = true;
}
}
/// <summary>
/// 时钟初始化
/// </summary>
private void InitTimer()
{
timer.Interval = AppSession.Interval; //1秒触发一次
timer.Elapsed += Timer_Elapsed; ;
timer.AutoReset = true;//每到指定时间Elapsed事件是到时间就触发
timer.Enabled = false; //指示 Timer 是否应引发 Elapsed 事件。
}
/// <summary>
/// 载入
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
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") && AppSession.MenuOperations.Count(a => a.Id == t.Id) > 0).ToList());
MenuMain.ItemsSource = AppSession.MenuOperations.Where(t => t.ParentId == null).OrderBy(t => t.OrderNum).ToList();
//初始化
Init();
}
/// <summary>
/// 关闭窗口
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Window_Closed(object sender, EventArgs e)
{
stop = true;
timer.Enabled = false;
Thread.Sleep(1000);
Application.Current.Shutdown();
}
/// <summary>
/// 激活点击的菜单对应的窗口
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
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("打开菜单出现异常!");
}
}
/// <summary>
/// 开始处理
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
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;
}
this.InitEquipments();
this.PLC.CreateGroup("group1");
this.PLC.AddOPCItems(Equipments.SelectMany(t => t.EquipmentProps).ToList());
this.stop = false;
this.timer.Enabled = true;
this.Btn_BeginExcute.IsEnabled = false;
this.Btn_EndExcute.IsEnabled = true;
}
/// <summary>
/// 停止处理
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Btn_EndExcute_Click(object sender, RoutedEventArgs e)
{
this.stop = true;
this.Btn_BeginExcute.IsEnabled = true;
this.Btn_EndExcute.IsEnabled = false;
}
/// <summary>
/// LED测试
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
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++;
}
/// <summary>
/// 拣选台回库
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Btn_GoBack_Click(object sender, RoutedEventArgs e)
{
if (PLC == null || PLC.GetConnStatus() == false)
{
//AddLogToUI("地址读取失败,请检查通讯连接", 2);
MessageBox.Show("地址读取失败,请检查通讯连接");
return;
}
var station1 = Equipments.FirstOrDefault(x => x.Code == "station1");
var props = station1.EquipmentProps.FirstOrDefault(x => x.EquipmentTypePropTemplateCode == "Barcode");
var queryResult = AppSession.Bll.GetTaskUncompleteByPalletCode(props.Value);
if (queryResult.Success)
{
var task = queryResult.Data;
if (task.LastStatus != TaskEntityStatus.到达拣选站台.GetIndexInt())
{
MessageBox.Show($"托盘{props.Value}对应的任务{task.Id}状态为{task.LastStatus},状态非【到达拣选站台】,请检查");
}
else
{
var UpdateResult = AppSession.Bll.SetTaskStatus(task.Id, TaskEntityStatus.拣选台回库.GetIndexInt());
if (UpdateResult.Success)
{
Logger.Log($"任务{task.Id}回库成功", LogLevel.Success);
}
else
{
Logger.Log($"任务{task.Id}回库失败,消息:{UpdateResult.Msg}", LogLevel.Error);
}
}
}
else
{
MessageBox.Show($"未获取到{props.Value}对应的任务");
}
}
#endregion
#region 初始化设备信息数据
private BllResult ConnectWMS()
{
List<KeyValuePair<string, string>> list = new List<KeyValuePair<string, string>>
{
new KeyValuePair<string, string>("username", AppSession.WmsUser),
new KeyValuePair<string, string>("password", AppSession.WmsPassword),
new KeyValuePair<string, string>("warehouse", AppSession.WarehouseId.ToString() + "," + AppSession.WarehouseCode),
new KeyValuePair<string, string>("rememberMe", "false")
};
//这里用同步的方式,因为我们是在定时器线程中调用的,不影响UI线程
AppSession.Client = AppSession.InitHttpClient();
return AppSession.Bll.FormPost(list, WMSUrls.Login, AppSession.Client, AppSession.Urls);
}
private void InitEquipments()
{
try
{
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.Where(t => Equipments.Exists(a => a.Id == t.EquipmentId)).ToList();
EquipmentTypes = result3.Data.Where(t => Equipments.Exists(a => a.EquipmentTypeId == t.Id)).ToList();
EquipmentTypePropTemplates = result4.Data.Where(t => EquipmentTypes.Exists(a => a.Id == t.EquipmentTypeId)).ToList();
//组合逻辑外键
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;
}
//组合站监控类
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 == "StationForStockerOut")
{
StationExcutes.Add(new StationForStockerOutExcute() { EquipmentType = t });
}
if (t.Code == "StationForStockerIn")
{
StationExcutes.Add(new StationForStockerInExcute() { EquipmentType = t });
}
if (t.Code == "StationForStockerInOrOut")
{
StationExcutes.Add(new StationForStockerInOrOutExcute() { EquipmentType = t });
}
if (t.Code == "StationOut")
{
StationExcutes.Add(new StationOutExcute() { EquipmentType = t });
}
if (t.Code == "StationIn")
{
StationExcutes.Add(new StationInExcute() { EquipmentType = t });
}
if (t.Code == "StationInOrOut")
{
StationExcutes.Add(new StationInOrOutExcute() { EquipmentType = t });
}
if (t.Code == "ScanPoint")
{
StationExcutes.Add(new ScanPoint() { EquipmentType = t });
}
});
}
catch (Exception ex)
{
MessageBox.Show("初始化设备信息出错:" + ex.Message, "注意", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
#endregion
}
}