NodeManager.cs
19.0 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
using AGVLib;
using Common;
using DeviceLib.DB.Config;
using DeviceLib.Model.AGV;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Threading;
namespace DeviceLib.BLL
{
public class NodeManager
{
public delegate void ClientNodeChangedEventHandler(List<ClientNode> nodes);
public static event ClientNodeChangedEventHandler NodeChanged;
static List<ClientNode> clientNodes;
public static int standby_node_type = 1;
public static int charge_node_type = 2;
public static void Load(bool manual = false)
{
clientNodes = ConfigOperation.GetNodes() ?? new List<ClientNode>();
if (manual)
LogUtil.Info("手动加载节点配置!");
else
LogUtil.Info("节点配置加载成功!");
}
public static List<ClientNode> GetClientNodes()
{
return clientNodes;
}
/// <summary>
/// 检查是否存在节点名
/// </summary>
/// <param name="names">多个节点名组成的字符串,以逗号间隔</param>
/// <param name="nodeStates"></param>
/// <param name="msg"></param>
/// <returns>true:均存在</returns>
public static bool CheckNodeNames(string names, out string msg)
{
string[] nodes = names.Split(',');
msg = "Not found node names:";
bool rtn = true;
foreach (string node in nodes)
{
var find = GetClientNodes().Find(s => s.name.Equals(node));
if (find == null)
{
msg += $"{node};";
rtn = false;
}
}
return rtn;
}
/// <summary>
/// 获取所有节点的名字
/// </summary>
/// <param name="names">多节点组成的名字,逗号分割</param>
/// <returns></returns>
public static List<NodeJobState> GetNodesState(string names)
{
string[] nodes = names.Split(',');
List<NodeJobState> nodeStates = new List<NodeJobState>();
if (names == null) return nodeStates;
foreach (string node in nodes)
{
var find = GetClientNodes().Find(s => s.name.Equals(node));
if (find != null)
{
nodeStates.Add(new NodeJobState() { Finished = false, Id = find.id, Name = find.name });
}
}
return nodeStates;
}
/// <summary>
/// 根据线体名获取线体的任务点
/// </summary>
/// <param name="names"></param>
/// <returns></returns>
public static List<NodeJobState> GetLineNodesState(string names)
{
string[] nodes = names.Split(',');
List<NodeJobState> nodeStates = new List<NodeJobState>();
if (string.IsNullOrEmpty(names)) return nodeStates;
foreach (string node in nodes)
{
var find = GetClientNodes().FindAll(s => s.name.StartsWith(node) && s.type == 4);
if (find != null)
{
foreach (var item in find)
{
nodeStates.Add(new NodeJobState() { Finished = false, Id = item.id, Name = item.name });
}
}
}
return nodeStates;
}
public static NodeJobState GetNodeState(ClientNode clientNode)
{
var find = GetClientNodes().Find(s => s.name.Equals(clientNode.name));
if (find != null)
{
return new NodeJobState() { Finished = false, Id = find.id, Name = find.name };
}
return null;
}
/// <summary>
/// 获取当前机器人可用的充电桩
/// </summary>
/// <param name="robotId"></param>
/// <returns></returns>
public static List<ClientNode> GetChargeNodes(int robotId)
{
List<ClientNode> charges = GetClientNodes().FindAll(s => s.type == charge_node_type);
List<ClientNode> availCharge = new List<ClientNode>();
if (charges != null)
{
foreach (var item in charges)
{
string[] desc = item.description?.Split(',');
if (desc != null && desc.Length > 0)
{
foreach (var item1 in desc)
{
if (item1.Equals(robotId.ToString()))
{
availCharge.Add(item);
}
}
}
}
return availCharge;
}
return null;
}
/// <summary>
/// 获取当前机器人可用的待机点
/// </summary>
/// <param name="robotId"></param>
/// <returns></returns>
public static List<ClientNode> GetStandNodes(int robotId)
{
List<ClientNode> charges = GetClientNodes().FindAll(s => s.type == standby_node_type);
List<ClientNode> availStand = new List<ClientNode>();
if (charges != null)
{
foreach (var item in charges)
{
string[] desc = item.description?.Split(',');
if (desc != null && desc.Length > 0)
{
foreach (var item1 in desc)
{
if (item1.Equals(robotId.ToString()))
{
availStand.Add(item);
}
}
}
}
return availStand;
}
return null;
}
public static ClientNode GetClientNodeByName(string name)
{
return GetClientNodes().Find(s => s.name.Equals(name));
}
/// <summary>
/// 根据线体名得到点位
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
public static ClientNode GetLineNodeByName(string name)
{
return GetClientNodes().Find(s => s.name.Equals(name) && s.type == 0);
}
public static ClientNode GetClientNode(int id, string name)
{
return GetClientNodes().Find(s => s.name.Equals(name) && s.id.Equals(id));
}
public static ClientNode GetClientNodeByIP(string ip)
{
return GetClientNodes().Find(s => s.ip.Equals(ip));
}
public static List<ClientNode> GetClientNodesByIP(string ip)
{
return GetClientNodes().FindAll(s => ip.Equals(s.ip));
}
public static ClientNode GetClientNode(int id)
{
return GetClientNodes().Find(s => s.id.Equals(id));
}
public static ClientNode FuzzySearchNode(string name)
{
return GetClientNodes().Find(s => s.name.ToLower().Contains(name.ToLower()));
}
/// <summary>
/// 根据节点名模糊搜索
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
public static List<ClientNode> FuzzySearchNodes(string name)
{
return GetClientNodes().FindAll(s => s.name.ToLower().Contains(name.ToLower()));
}
public static ClientNode GetClientNode(NodeJobState nodeState)
{
return GetClientNodeByName(nodeState.Name);
}
public static bool UpdateClientNode(ClientNode node)
{
return ConfigOperation.SetNode(node);
}
#region 服务端与客户端通讯
static ClientNode NodeToClientNode(Node node)
{
ClientNode find;
if (node.id == 0)
{
find = GetClientNodeByIP(node.remark);
if (find != null)
{
find.online = true;
find.shielded = node.shielded;
find.level = (int)node.level;
find.status = (int)node.status;
find.shelf_id = node.shelf_id;
UpdateClientNode(find);
}
//else
//{
// find = new ClientNode();
// find.online = true;
// find.shelf_id = node.shelf_id;
// find.shielded = node.shielded;
// find.level = (int)node.level;
// find.status = (int)node.status;
// AddNode(find);
// clientNodes.Add(find);
//}
}
else
{
find = GetClientNode(node.id, node.name);
if (find != null)
{
find.online = true;
find.shielded = node.shielded;
find.level = (int)node.level;
find.status = (int)node.status;
find.shelf_id = node.shelf_id;
find.description = node.remark;
UpdateClientNode(find);
}
//else
//{
// find = new ClientNode();
// find.name = node.name;
// find.id = node.id;
// find.name = node.name;
// find.online = true;
// find.shelf_id = node.shelf_id;
// find.description = node.remark;
// find.shielded = node.shielded;
// find.level = (int)node.level;
// find.status = (int)node.status;
// AddNode(find);
// clientNodes.Add(find);
//}
}
if (find == null) return null;
var allClients = GetClientNodesByIP(find.ip);
if (allClients != null)
{
foreach (var item in allClients)
{
item.online = true;
}
}
return find;
}
public static bool AddNode(ClientNode clientNode)
{
return ConfigOperation.AddNode(clientNode);
}
static Node ClientNodeToNode(ClientNode node)
{
Node find = new Node();
find.id = node.id;
find.name = node.name;
find.status = (NodeStatus)node.status;
find.level = (NodeLevel)node.level;
find.shelf_id = node.shelf_id;
if (node.id == -1)
find.remark = node.ip;
else
find.remark = node.description;
return find;
}
static Server server = new Server();
static Thread threadUpdate;
static void UpdateNodeSate()
{
while (true)
{
Thread.Sleep(2000);
if (UpdateNodeQueue != null && UpdateNodeQueue.Count > 0)
{
if (Monitor.TryEnter(locUpdate, 500))
{
UpdateNodeQueue.TryDequeue(out Node[] nodes);
try
{
foreach (var item in nodes)
{
NodeToClientNode(item);
}
NodeChanged?.Invoke(GetClientNodes());
}
catch (Exception ex)
{
LogUtil.Error($"UpdateNodeSate", ex);
}
finally
{
Monitor.Exit(locUpdate);
}
}
}
}
}
public static void InitServer()
{
threadUpdate = new Thread(UpdateNodeSate);
threadUpdate.IsBackground = true;
threadUpdate.Start();
server.NodeConnectedChanged += Server_NodeConnectedChanged;
server.NodeChanged += Server_NodeChanged;
}
private static void Server_NodeConnectedChanged(string ip, bool online)
{
var find = GetClientNodesByIP(ip);
if (find != null)
{
foreach (var item in find)
{
item.online = online;
}
NodeChanged?.Invoke(GetClientNodes());
}
}
static object locUpdate = new object();
/// <summary>
/// 待更新节点状态的队列
/// </summary>
static ConcurrentQueue<Node[]> UpdateNodeQueue = new ConcurrentQueue<Node[]>();
private static void Server_NodeChanged(Node[] clientNodes)
{
if (Monitor.TryEnter(locUpdate, 500))
{
try
{
foreach (var item in clientNodes)
{
NodeToClientNode(item);
}
NodeChanged?.Invoke(GetClientNodes());
}
catch (Exception ex)
{
LogUtil.Error($"Server_NodeChanged", ex);
}
finally
{
Monitor.Exit(locUpdate);
}
}
else
{
UpdateNodeQueue.Enqueue(clientNodes);
LogUtil.Warn($"Server_NodeChanged 未得到锁");
}
}
public static void StartServer(int port = 12000)
{
server.Start(port);
}
public static void StopServer()
{
server.Stop();
}
/// <summary>
/// 请求料架进入线体
/// </summary>
/// <param name="nodeId"></param>
public static void RequestEnter(int nodeId)
{
ClientNode clientNode = GetClientNode(nodeId);
if (clientNode != null)
{
clientNode.status = (int)NodeStatus.RequestEnter;
SendToClient(clientNode);
}
}
public static void Complete(int nodeId)
{
ClientNode clientNode = GetClientNode(nodeId);
if (clientNode != null)
{
clientNode.status = (int)NodeStatus.Complete;
SendToClient(clientNode);
}
}
public static void RequestLeave(int nodeId)
{
ClientNode clientNode = GetClientNode(nodeId);
if (clientNode != null)
{
clientNode.status = (int)NodeStatus.RequestLeave;
SendToClient(clientNode);
}
}
public static void SetToNne(int nodeId)
{
ClientNode clientNode = GetClientNode(nodeId);
if (clientNode != null)
{
clientNode.status = (int)NodeStatus.None;
SendToClient(clientNode);
}
}
/// <summary>
/// 获取节点状态
/// </summary>
/// <param name="nodeId"></param>
/// <returns></returns>
public static NodeStatus GetNodeStatus(int nodeId)
{
ClientNode clientNode = GetClientNode(nodeId);
if (clientNode != null)
{
return (NodeStatus)clientNode.status;
}
return NodeStatus.None;
}
public static void SendToClient(ClientNode clientNode)
{
sendToClient(ClientNodeToNode(clientNode));
LogUtil.Info($"SendToClient[{clientNode.status}][{clientNode.shelf_id}]: {JsonHelper.SerializeObject(clientNode)}", Setting_Str.JobLog);
}
static void sendToClient(Node node)
{
server.SendToClient(node);
}
#endregion
#region 节点确认
/// <summary>
/// 等待确认的节点
/// </summary>
static List<NodeJobState> WaitConfirmNodes = new List<NodeJobState>();
public void AddWaitNode(NodeJobState node)
{
NodeJobState find = WaitConfirmNodes.Find(s => s.Id.Equals(node.Id) && s.Name.Equals(node.Name));
if (find != null)
return;
WaitConfirmNodes.Add(node);
}
public void RemoveWaitNode(NodeJobState node)
{
NodeJobState find = WaitConfirmNodes.Find(s => s.Id.Equals(node.Id) && s.Name.Equals(node.Name));
if (find != null)
WaitConfirmNodes.Remove(node);
}
//public static bool NodeConfirm(int robotId, out string msg)
//{
// NodeJobState find;
// msg = "不存在等待确认的节点,AGV还未到达目标点";
// if (robotId == 0)
// {
// find = WaitConfirmNodes.Find(s => s.Name.Equals(name));
// }
// else
// {
// find = WaitConfirmNodes.Find(s => s.Id.Equals(id) && s.Name.Equals(name));
// }
// if (find != null)
// {
// find.Confirmed = true;
// LogUtil.Info($"节点确认成功:{JsonHelper.SerializeObject(find)}");
// return true;
// }
// else
// {
// LogUtil.Warn($"节点确认失败:{JsonHelper.SerializeObject(find)},{msg}");
// return false;
// }
//}
public static bool NodeConfirm(int id, string name, out string msg)
{
NodeJobState find;
msg = "不存在等待确认的节点,AGV还未到达目标点";
if (id == 0)
{
find = WaitConfirmNodes.Find(s => s.Name.Equals(name));
}
else
{
find = WaitConfirmNodes.Find(s => s.Id.Equals(id) && s.Name.Equals(name));
}
if (find != null)
{
find.Confirmed = true;
LogUtil.Info($"节点确认成功:{JsonHelper.SerializeObject(find)}");
return true;
}
else
{
LogUtil.Warn($"节点确认失败:{JsonHelper.SerializeObject(find)},{msg}");
return false;
}
}
#endregion
public static bool CheckNodeIsOccupied(ClientNode clientNode)
{
if (clientNode == null) return false;
return clientNode.occupiedAgv != 0;
}
public static bool ChecStoreIsOccupied(int storeId = 1)
{
ClientNode clientNode = GetClientNodes().Find(s => s.name.StartsWith($"A-{storeId}") && s.occupiedAgv != 0);
return clientNode != null;
}
public static string GetTmpPlace(ClientNode clientNode)
{
if (clientNode == null) return Setting_Str.standby;
return Setting_Str.standby;
}
}
}