WinMain.xaml.cs
11.7 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
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.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>
/// 时钟同步锁
/// </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<IStockerExcute> StockerExcutes { get; set; } = new List<IStockerExcute>();
/// <summary>
/// 站台处理类
/// </summary>
public List<IStationExcute> StationExcutes { get; set; } = new List<IStationExcute>();
#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()}";
//堆垛机监控
this.StockerView.Content = new StockerMonitor(16, 4, false, 900, 150);
#region 基础数据
var result1 = AppSession.Bll.GetCommonModelByCondition<Equipment>("");
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.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;
}
//组合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));
}
});
EquipmentProps.Where(t => t.EquipmentTypePropTemplate.DataType == "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;
}
//组合处理类
EquipmentTypes.ForEach(t =>
{
if (t.Code == "stocker")
{
StockerExcutes.Add(new XiangdianStockerExcute() { EquipmentType = t });
}
if (t.Code == "StationOut")
{
StationExcutes.Add(new XiangdianOutStationExcute() { EquipmentType = t });
}
if (t.Code == "StationInOrOut")
{
StationExcutes.Add(new XiangdianInOrOutStationExcute() { EquipmentType = t });
}
});
#endregion
}
#region 组件
/// <summary>
/// todo:日志记录
/// </summary>
/// <param name="sender"></param>
/// <param name="args"></param>
private void Logger_LogWrite(object sender, LogEventArgs args)
{
throw new NotImplementedException();
}
#endregion
#region 时钟逻辑
private void Timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
lock (locker)
{
try
{
#region 是否结束处理
if (stop)
{
Dispatcher.Invoke(() =>
{
Btn_BeginExcute.IsEnabled = true;
Btn_EndExcute.IsEnabled = false;
timer.Enabled = false;
});
return;
}
if (PLCs.Count(t => t.IsAvailable == false || t.IsConnected == false) > 0)
{
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.DataType == "PLC").ToList(), 20));
#region 堆垛机处理
StockerExcutes.ForEach(t =>
{
//一般根据堆垛机现在的位置来决定优先执行入库还是出库
t.Excute(Equipments.Where(a => a.EquipmentType.Id == t.EquipmentType.Id).ToList(), PLCs);
});
#endregion
#region 站台处理
StationExcutes.ForEach(t =>
{
t.Excute(Equipments.Where(a => a.EquipmentType.Id == t.EquipmentType.Id).ToList(), PLCs);
});
#endregion
}
catch (Exception ex)
{
//发生异常,结束处理
stop = true;
Logger.Log($"程序处理出现异常:{ex.Message};程序处理已停止;", LogLevel.Error);
}
}
}
#endregion
#region 窗体事件
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)
{
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
}
}