NodeManager.cs
7.6 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
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;
namespace DeviceLibrary.manager
{
public class NodeManager
{
/// <summary>
/// 节点信息
/// </summary>
public static List<Node> nodeInfo;
public static void InitNodesInfos()
{
nodeInfo = new List<Node>();
XmlConfigOperation.LoadNodeInfos(nodeInfo);
WriteAllNodeInfos();
}
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());
//}
}
}
}