Node.cs
10.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
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Threading;
using System.Windows.Forms;
using DeviceLibrary.manager;
using TcpKPIO;
namespace DeviceLibrary
{
public class Node
{
/// <summary>
/// 节点ID
/// </summary>
public int Id { get; set; }
/// <summary>
/// 节点名称
/// </summary>
public string Name { get; set; }
public NodeType Type { get; set; }
/// <summary>
/// 所属车间
/// </summary>
public string Workshop { get; set; }
/// <summary>
/// 节点IP
/// </summary>
public string IP { get; set; } = "";
/// <summary>
/// 在线状态
/// </summary>
public bool Online { get; set; }
public string IOState
{
get
{
if (IP.Equals(""))
return "";
else
return IOManager.GetDI(IP);
}
set { }
}
/// <summary>
/// 是否可调用
/// </summary>
public bool IsUse
{
set
{
if (!isuse.Equals(value))
{
isuse = value;
XmlConfigOperation.SetNodeIsUse(this);
}
}
get { return isuse; }
}
private bool isuse;
/// <summary>
/// 节点状态
/// </summary>
public NodeStatus nodeStatus { get; set; }
protected string rfid = "00";
public ExtendEquip ExtendEquip { get; set; }
/// <summary>
/// RFID
/// </summary>
public string RFID
{
set
{
if (value.Length < 2)
rfid = value.PadLeft(2, '0');
else
rfid = value;
}
get
{
return rfid;
}
}
public ClientLevel ClientLevel { get; set; } = ClientLevel.Low;
/// <summary>
/// 别名
/// </summary>
public string AliceName { get; set; }
/// <summary>
/// 警告信息
/// </summary>
public string WarnMsg { get; set; } = "";
/// <summary>
/// 状态比较
/// </summary>
/// <param name="obj"></param>
/// <returns></returns>
public bool StateEquals(NodeStatus obj)
{
return nodeStatus.Equals(obj);
}
/// <summary>
/// 获取当前状态
/// </summary>
/// <returns></returns>
public NodeStatus GetState()
{
return nodeStatus;
}
/// <summary>
/// 更新节点状态
/// </summary>
/// <param name="status"></param>
public void UpdateNodeStatus(NodeStatus status)
{
nodeStatus = status;
}
/// <summary>
/// 客户端节点
/// </summary>
/// <param name="name"></param>
/// <param name="rfid"></param>
/// <param name="action"></param>
/// <param name="level"></param>
public Node(string name, string rfid = "", NodeStatus status = NodeStatus.None)
{
Name = name;
RFID = rfid;
nodeStatus = status;
}
public Node() { }
Thread tPingClient; //ping
public void Init()
{
ExtendEquip = new ExtendEquip(IP);
tPingClient = new Thread(new ThreadStart(KeepLiveClient));
tPingClient.IsBackground = true;
tPingClient.Start();
}
string nodeIostate = "";
/// <summary>
/// 节点改变事件
/// </summary>
/// <param name="nodeIndex"></param>
public delegate void NodeChangedEvent(Node node);
/// <summary>
/// 节点改变
/// </summary>
public event NodeChangedEvent NodeChanged;
/// <summary>
/// 节点在线
/// </summary>
public event NodeChangedEvent NodeOnline;
private void KeepLiveClient()
{
while (true)
{
try
{
if (!isuse)
continue;
Thread.Sleep(300);
if (!string.IsNullOrEmpty(IP))
{
bool check = HttpManager.CheckIP(Name, IP);
if (Online != check)
{
Online = check;
NodeOnline?.Invoke(this);
}
}
else
{
if (Online == false)
{
Online = true;
NodeOnline?.Invoke(this);
}
}
if (!nodeIostate.Equals(IOState))
{
nodeIostate = IOState;
NodeChanged?.Invoke(this);
}
}
catch (Exception e)
{
Common.LogUtil.error("KeepLiveClient:" + Name, e);
}
}
}
/// <summary>
/// 节点状态的文本形式
/// </summary>
/// <returns></returns>
public string StatetText()
{
string s = string.Format("[Name={0}, NodeStatus={1}, RFID={2},ClientLevel ={3}]", Name, nodeStatus.ToString(), RFID, ClientLevel.ToString());
return s;
}
public override string ToString()
{
return string.Format("[Name={0},AliceName={1},Workshop={2}]", Name, AliceName, Workshop);
}
/// <summary>
/// 脱机
/// </summary>
public void Offline()
{
RFID = "00";
nodeStatus = NodeStatus.None;
Online = false;
NodeOnline?.Invoke(this);
}
public Node ToCopy()
{
PropertyInfo[] infos = this.GetType().GetProperties();
Node node = new Node();
PropertyInfo[] infos2 = node.GetType().GetProperties();
for (int i = 0; i < infos2.Length; i++)
{
infos2[i].SetValue(node, Convert.ChangeType(infos[i].GetValue(this), infos[i].PropertyType));
}
return node;
}
public override bool Equals(object obj)
{
Node node = obj as Node;
if (node == null)
return false;
if (!node.StateEquals(GetState()) || node.RFID != RFID || node.ClientLevel != ClientLevel)
return true;
return false;
}
public void UpdateNode(Node node)
{
UpdateNodeStatus(node.GetState());
ClientLevel = node.ClientLevel;
RFID = node.RFID;
Common.LogUtil.info("节点更新 " + StatetText());
NodeChanged?.Invoke(this);
}
}
public class ExtendEquip
{
public string IP { get; set; }
/// <summary>
/// 确认按钮
/// </summary>
public bool ButtonConfirmSig
{
get
{
//if (CanConfirm)
{
if (IP.Equals(""))
return false;
else
{
return GetIO();
}
}
//else
//{
// return false;
//}
}
}
private bool GetIO()
{
bool rtn = IOManager.GetDI(IP, 0);
if (rtn)
{
Common.LogUtil.info($"线体按钮确认[{IP}]");
System.Threading.Tasks.Task.Factory.StartNew(delegate
{
IOManager.WriteDO(IP, 0, 1);
System.Threading.Thread.Sleep(5000);
IOManager.WriteDO(IP, 0, 0);
});
}
return rtn;
}
/// <summary>
/// Api确认
/// </summary>
public bool ApiConfirmSig
{
get
{
return apiconfirmSig;
}
set
{
{
apiconfirmSig = value;
}
}
}
private bool apiconfirmSig = false;
public ExtendEquip(string ip)
{
IP = ip;
}
/// <summary>
/// 允许确认
/// </summary>
public void AllowConfirm()
{
Reset();
}
public void Reset()
{
apiconfirmSig = false;
IOManager.WriteDO(IP, 0, 0);
Common.LogUtil.info($"线体确认状态重置[{IP}]:apiconfirmSig={apiconfirmSig}");
}
public string ToState()
{
return $"【{IP}】【apiconfirmSig={apiconfirmSig}】【ButtonConfirmSig={ButtonConfirmSig}】";
}
}
/// <summary>
/// 节点状态
/// </summary>
public enum NodeStatus : byte
{
/// <summary>
/// 没有动作
/// </summary>
None = 0,
/// <summary>
/// 需要7寸D料车
/// </summary>
NeedD = 1,
/// <summary>
/// 需要大尺寸C料车
/// </summary>
NeedC = 2,
/// <summary>
/// 需要进入料车
/// </summary>
NeedEnter = 3,
/// <summary>
/// 需要出去料车
/// </summary>
NeedLeave = 4,
/// <summary>
/// 需要进入离开料车
/// </summary>
NeedEnterLeave = 5,
/// <summary>
/// 准备进入料车,服务器发送
/// </summary>
ReadyEnter = 6,
/// <summary>
/// 可以进入料车
/// </summary>
MayEnter = 7,
/// <summary>
/// 完成进入料车
/// </summary>
FinishEnter = 8,
/// <summary>
/// 准备离开料车,服务器发送
/// </summary>
ReadyLeave = 9,
/// <summary>
/// 可以出去料车
/// </summary>
MayLeave = 10,
/// <summary>
/// 完成出去料车
/// </summary>
FinishLeave = 11,
}
/// <summary>
/// 客户端的优先级
/// </summary>
public enum ClientLevel : byte
{
/// <summary>
/// 低
/// </summary>
Low = 0,
/// <summary>
/// 中等
/// </summary>
Middle = 1,
/// <summary>
/// 高
/// </summary>
High = 2
}
public enum NodeType
{
AutoCharge,
Standby,
Node,
Lift
}
}