NodeManager.cs
13.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
using Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DeviceLibrary.manager;
using System.Windows.Forms;
using System.Drawing;
using static DeviceLibrary.Node;
using System.Runtime.CompilerServices;
using static System.Net.Mime.MediaTypeNames;
using System.Runtime.ConstrainedExecution;
using log4net.Util;
namespace DeviceLibrary.manager
{
public class NodeManager
{
/// <summary>
/// 节点信息
/// </summary>
public static List<Node> nodeInfo;
public static void InitNodesInfos()
{
InitLinesInAirShower();
nodeInfo = new List<Node>();
XmlConfigOperation.LoadNodeInfos(nodeInfo);
WriteAllNodeInfos();
}
public static void ReLoadNodes()
{
try
{
List<Node> nodes = new List<Node>();
XmlConfigOperation.LoadNodeInfos(nodes);
nodeInfo = nodes;
}
catch (Exception ex)
{
MessageBox.Show("重载节点失败:" + ex.StackTrace, "提示");
}
}
public static void InitView(DataGridView DgvNode)
{
BindingSource bindingSource = new BindingSource();
foreach (Node node in NodeManager.nodeInfo)
{
//if (!node.Type.Equals(NodeType.Node))
// continue;
bindingSource.Add(node);
}
DgvNode.AutoGenerateColumns = false;
DgvNode.AutoSize = true;
DgvNode.DataSource = bindingSource;
DgvNode.RowPostPaint += delegate
{
DgvNode.RowsDefaultCellStyle.BackColor = Color.White;
DgvNode.AlternatingRowsDefaultCellStyle.BackColor = Color.LightBlue;
};
DgvNode.Columns.Add(new DataGridViewCheckBoxColumn() { HeaderText = "启用", DataPropertyName = "IsUse" });
//DgvNode.Columns.Add(new DataGridViewTextBoxColumn() { HeaderText = "编号", DataPropertyName = "Id" });
DgvNode.Columns.Add(new DataGridViewTextBoxColumn() { HeaderText = "名称", DataPropertyName = "Name" });
DgvNode.Columns.Add(new DataGridViewTextBoxColumn() { HeaderText = "别名", DataPropertyName = "AliceName" });
DgvNode.Columns.Add(new DataGridViewTextBoxColumn() { HeaderText = "车间", DataPropertyName = "Workshop" });
DgvNode.Columns.Add(new DataGridViewTextBoxColumn() { HeaderText = "IP", DataPropertyName = "IP" });
DgvNode.Columns.Add(new DataGridViewTextBoxColumn() { HeaderText = "动作", DataPropertyName = "nodeStatus" });
DgvNode.Columns.Add(new DataGridViewTextBoxColumn() { HeaderText = "等级", DataPropertyName = "ClientLevel" });
DgvNode.Columns.Add(new DataGridViewTextBoxColumn() { HeaderText = "RFID", DataPropertyName = "RFID" });
DgvNode.Columns.Add(new DataGridViewTextBoxColumn() { HeaderText = "IO", DataPropertyName = "IOState" });
DgvNode.Columns.Add(new DataGridViewTextBoxColumn() { HeaderText = "状态", DataPropertyName = "Online" });
DgvNode.RowsDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
//int n;
//DgvNode.Columns.Add(new DataGridViewTextBoxColumn() { HeaderText = "名称"});
//DgvNode.Columns.Add(new DataGridViewTextBoxColumn() { HeaderText = "IP" });
//DgvNode.Columns.Add(new DataGridViewTextBoxColumn() { HeaderText = "动作" });
//DgvNode.Columns.Add(new DataGridViewTextBoxColumn() { HeaderText = "等级" });
//DgvNode.Columns.Add(new DataGridViewTextBoxColumn() { HeaderText = "RFID" });
//DgvNode.Columns.Add(new DataGridViewTextBoxColumn() { HeaderText = "连接" });
//DgvNode.Columns.Add(new DataGridViewTextBoxColumn() { HeaderText = "IO" });
//DgvNode.Columns.Add(new DataGridViewButtonColumn() { HeaderText = "启用" });
//DgvNode.RowsDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
//for (int i = 0; i < NodeManager.nodeInfo.Count; i++)
//{
// //if (!NodeManager.nodeInfo[i].Type.Equals(NodeType.Node))
// // continue;
// n = DgvNode.Rows.Add(NodeManager.nodeInfo[i].ToRow());
// DgvNode.Rows[n].HeaderCell.Value = (n + 1).ToString();
// if (i % 2 == 0)
// DgvNode.Rows[n].DefaultCellStyle.BackColor = Color.LightBlue;
// if (!NodeManager.nodeInfo[i].Online)
// DgvNode.Rows[n].DefaultCellStyle.ForeColor = Color.Red;
//}
}
public static void UpdateDataSource(DataGridView dataGridView, int curRow = 0, int curColum = 0)
{
BindingSource bindingSource = new BindingSource();
foreach (Node node in NodeManager.nodeInfo)
{
//if (!node.Type.Equals(NodeType.Node))
// continue;
bindingSource.Add(node);
}
dataGridView.DataSource = bindingSource;
if (dataGridView.Rows.Count >= curRow
&& dataGridView.Columns.Count >= curColum)
{
dataGridView.CurrentCell = dataGridView.Rows[curRow].Cells[curColum]; ;
}
dataGridView.Refresh();
}
public static Node GetNodeById(int id)
{
return nodeInfo.Find(s => s.Id.Equals(id));
}
public static Node GetNodeByName(string name)
{
return nodeInfo.Find(s => s.Name.Equals(name));
}
public static Node GetNodeByType(NodeType nodeType = NodeType.AutoCharge)
{
return nodeInfo.Find(s => s.Type.Equals(nodeType));
}
public static Node GetNode(string name, NodeType nodeType = NodeType.Node)
{
return nodeInfo.Find(s => s.Name.Equals(name) && s.Type.Equals(nodeType));
}
public static bool CheckNode(string nodename)
{
Node node = nodeInfo.Find(s => s.Name.Equals(nodename));
if (node == null)
return false;
else
return true;
}
public static bool CheckNodes(string nodenames, out string names)
{
string[] nodes = nodenames.Split(',');
names = "";
foreach (string item in nodes)
{
Node node = nodeInfo.Find(s => s.Name.Equals(item));
if (node == null)
{
names += item + ",";
}
}
if (names.Equals(""))
return true;
return false;
}
/// <summary>
/// 是否存在节点
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
public static bool HasNode(string name)
{
int idx = nodeInfo.FindIndex(s => s.Name.Equals(name));
return idx == -1 ? false : true;
}
public static void UpdateNode(Node node)
{
Node node1 = NodeManager.nodeInfo.Find(s => s.Name == node.Name);
if (node1 == null)
{
Common.LogUtil.error("UpdateNode: " + node.Name + " 不存在");
return;
}
if (!node1.Equals(node))
{
node1.UpdateNode(node);
}
}
public static void RegisEvent(NodeChangedEvent changedEvent, NodeChangedEvent onlineEvent)
{
foreach (Node node in nodeInfo)
{
node.NodeChanged += changedEvent;
node.NodeOnline += onlineEvent;
}
}
public static void UnRegisEvent(NodeChangedEvent changedEvent, NodeChangedEvent onlineEvent)
{
foreach (Node node in nodeInfo)
{
node.NodeChanged -= changedEvent;
node.NodeOnline -= onlineEvent;
}
}
public static void WriteAllNodeInfos()
{
//foreach (Node item in nodeInfo)
//{
// LogUtil.info(item.ToString());
//}
}
#region 跨门 跨风淋门
/// <summary>
/// 风淋门内的线体
/// key:车间名
/// val:线体名
/// </summary>
static Dictionary<string, List<string>> linesInAirShower = new Dictionary<string, List<string>>();
static void InitLinesInAirShower()
{
//添加3D的
linesInAirShower.Add("3D", new List<string>() { "S3Tail" });
//添加4C的
linesInAirShower.Add("4C", new List<string>() { "C14Tail", "C15Tail" });
}
/// <summary>
/// 检查是否需要跨门
/// </summary>
/// <param name="cur"></param>
/// <param name="next"></param>
/// <returns></returns>
public static bool NeedCrossDoor(AgvInfo agv, Node cur, Node next, out bool isIn, out bool isAirDoor)
{
isIn = true;
isAirDoor = false;
if (agv.Scope.Workshop.Equals("1F")) return false;
if (agv.Scope.Workshop.Equals("4C")) return false;
if (agv.Scope.Workshop.Equals("3C")) return false;
try
{
if (cur == null) cur = agv.Place;
if (next == null) next = agv.Place;
if (cur.Workshop.Equals("4C") || next.Workshop.Equals("4C"))
{
if (next.Workshop.Equals("4C"))
{
return true;
}
else if (cur.Workshop.Equals("4C"))
{
isIn = false;
return true;
}
}
else//在相同车间
{
if(cur.Type.Equals(NodeType.Lift) || cur.Type.Equals(NodeType.Standby) || cur.Type.Equals(NodeType.AutoCharge))
{
if(linesInAirShower.ContainsKey(next.Workshop)&& linesInAirShower[next.Workshop].Contains(next.Name))
{
//电梯到风淋门内
isAirDoor = true;
isIn = true;
return true;
}
}
else if(next.Type.Equals(NodeType.Lift) || next.Type.Equals(NodeType.Standby) || next.Type.Equals(NodeType.AutoCharge))
{
if (linesInAirShower.ContainsKey(cur.Workshop) && linesInAirShower[cur.Workshop].Contains(cur.Name))
{
//风淋门内到电梯
isAirDoor = true;
isIn = false;
return true;
}
}
else if(cur.Type.Equals(NodeType.Node) && next.Type.Equals(NodeType.Node))
{
if (linesInAirShower.ContainsKey(cur.Workshop) && linesInAirShower.ContainsKey(next.Workshop))
{
if (linesInAirShower[cur.Workshop].Contains(cur.Name))//当前点在风淋门内
{
if (linesInAirShower[next.Workshop].Contains(next.Name))//目标点在风淋门内
{
return false;
}
else//目标点在风淋门外
{
isAirDoor = true;
isIn = false;
return true;
}
}
else//当前点在风淋门外
{
if (linesInAirShower[next.Workshop].Contains(next.Name))//目标点在风淋门内
{
isAirDoor = true;
return true;
}
else//目标点在风淋门外
{
return false;
}
}
}
else
{
return false;
}
}
}
}
catch (Exception ex)
{
LogUtil.error("NeedCrossDoor", ex);
}
return false;
}
/// <summary>
/// 是否需要跨两个门
/// </summary>
/// <param name="agv"></param>
/// <param name="node"></param>
/// <returns></returns>
public static bool NeedCrossTwoDoor(AgvInfo agv, Node src, Node dst)
{
if (agv.Scope.Workshop.Equals("1F")) return false;
if (agv.Scope.Workshop.Equals("4C")) return false;
if (agv.Scope.Workshop.Equals("3C")) return false;
if (agv.Scope.Workshop.Equals("3D")) return false;
try
{
if (src == null) src = agv.Place;
if (dst == null) dst = agv.Place;
if (!src.Workshop.Equals(dst.Workshop) && linesInAirShower.ContainsKey(dst.Workshop)
&& linesInAirShower[dst.Workshop].Contains(dst.Name))
{
return true;
}
}
catch (Exception ex)
{
LogUtil.error("NeedCrossTwoDoor", ex);
}
return false;
}
#endregion
}
}