Control.cs
21.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
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
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Web.Script.Serialization;
using AGVControl;
using RestSharp;
namespace BLL
{
public class Control
{
private bool loop;
//private int areaC_Index;
private Thread tAgvCall;
private Thread tAgvState;
//public List<string> Marks;
private const int REG_STATUS = 20;
//private List<string> shelfLockedNodeNames;
public delegate void AgvChangedEvent(int agvIndex);
public event AgvChangedEvent AgvChanged;
public event AgvChangedEvent AgvOnline;
public Control()
{
}
public void Start()
{
loop = true;
tAgvState = new Thread(new ThreadStart(AgvState));
tAgvCall = new Thread(new ThreadStart(AgvCall));
tAgvState.Start();
tAgvCall.Start();
}
public void Stop()
{
loop = false;
}
private void AgvState()
{
bool rtn;
while (loop)
{
Thread.Sleep(1000);
for (int i = 0; i < Common.agvInfo.Count; i++)
{
if (!CheckOnline(i)) continue;
if (!loop) break;
//获取AGV状态
rtn = Common.mir.Get_State(Common.agvInfo[i], out int stateID, out string stateText, out int battery, out string mission_text);
bool change = false;
if (rtn) change = Common.agvInfo[i].SetState(stateID, stateText, battery, mission_text);
//上报异常
if (Common.agvInfo[i].MissionText != mission_text)
{
Common.agvInfo[i].MissionText = mission_text;
if (mission_text.Equals("停靠") || mission_text.ToLower().Equals("DOCKING"))
{
Common.agvInfo[i].DockingInfo.startTime = DateTime.Now;
Common.agvInfo[i].DockingInfo.IsDocking = true;
}
}
bool isAlarm = false;
List<AlarmMsg> msglist = new List<AlarmMsg>();
if (Common.agvInfo[i].DockingInfo.IsDocking && (DateTime.Now - Common.agvInfo[i].DockingInfo.startTime).TotalMinutes.Equals(2))
{
isAlarm = true;
Common.agvInfo[i].DockingInfo.IsDocking = false;
msglist.Add(new AlarmMsg(Common.agvInfo[i].Name, "agv.Docking", mission_text));
}
if (battery <= 10)
{
isAlarm = true;
msglist.Add(new AlarmMsg(Common.agvInfo[i].Name, "agv.battery", battery.ToString()));
}
if (stateText.Equals("Error") || stateText.Equals("EmergencyStop"))
{
isAlarm = true;
msglist.Add(new AlarmMsg(Common.agvInfo[i].Name, "agv.Error.EmergencyStop", "agv状态" + stateText));
}
if (isAlarm)
BLL.AGVManager.updateDeviceAlarmMsg(msglist);
//获取地点任务状态
Thread.Sleep(50);
rtn = Common.mir.Get_Register(Common.agvInfo[i], REG_STATUS, out int regValue);
if (rtn) Common.agvInfo[i].GetPlace(regValue);
if (change)
{
Common.LogInfo(string.Format("{0} Get_State StateID={1}, StateText={2}, Battery={3}, Mission_text={4}", Common.agvInfo[i].Name, stateID, stateText, battery, mission_text));
}
//执行任务更新状态
if (stateID == 5 || change)
{
Common.LogInfo(string.Format("{0} Get_Register PLC{1}={2}", Common.agvInfo[i].Name, REG_STATUS, regValue));
AgvChanged?.Invoke(i);
}
//获取任务队列
//rtn = Common.mir.Get_Mission_Queue(Common.agvInfo[i], out List<string> mission);
//if (rtn)
//{
// string[] arr = new string[mission.Count];
// for (int j = 0; j < mission.Count; j++)
// arr[j] = Common.agvMission.FirstOrDefault(q => q.Value == mission[j]).Key;
// string missionKey = string.Join(",", arr);
// if (Common.agvInfo[i].MissionQueue != missionKey)
// {
// Common.agvInfo[i].MissionQueue = missionKey;
// AgvChanged?.Invoke(i);
// }
//}
}
}
}
private void AgvCall()
{
while (loop)
{
Thread.Sleep(1000);
for (int i = 0; i < Common.agvInfo.Count; i++)
{
if (!loop) break;
if (!Common.agvInfo[i].IsCon) continue; //AGV网络连接
if (!Common.agvInfo[i].IsUse) continue; //AGV是否可用
//Ready,Pause,Executing
if (Common.agvInfo[i].StateID != 3 && Common.agvInfo[i].StateID != 4 && Common.agvInfo[i].StateID != 5)
{
Common.LogInfo(string.Format("{0}不能调用 StateID={1}, StateText={2}", Common.agvInfo[i].Name, Common.agvInfo[i].StateID, Common.agvInfo[i].StateText));
continue;
}
switch (Common.agvInfo[i].PlaceState)
{
case PlaceState.None:
StateNone(i);
break;
case PlaceState.Move:
StateMove(i);
break;
case PlaceState.MoveFinish:
StateMoveFinish(i);
break;
case PlaceState.Enter:
StateEnter(i);
break;
case PlaceState.EnterFinish:
StateEnterFinish(i);
break;
case PlaceState.Leave:
StateLeave(i);
break;
case PlaceState.LeaveFinish:
StateLeaveFinish(i);
break;
case PlaceState.Error:
StateError(i);
break;
}
}
}
}
private bool CheckOnline(int idx)
{
bool rtn = Common.mir.CheckIP(Common.agvInfo[idx].IP);
if (rtn)
{
if (!Common.agvInfo[idx].IsCon)
{
Common.agvInfo[idx].IsCon = true;
Common.LogInfo(Common.agvInfo[idx].Name + " Online");
AgvOnline?.Invoke(idx);
AgvChanged?.Invoke(idx);
}
}
else
{
if (Common.agvInfo[idx].IsCon)
{
Common.agvInfo[idx].IsCon = false;
Common.LogInfo(Common.agvInfo[idx].Name + " Offline");
AgvOnline?.Invoke(idx);
AgvChanged?.Invoke(idx);
}
}
return rtn;
}
/// <summary>
/// agv空闲
/// </summary>
/// <param name="idx"></param>
private void StateNone(int idx)
{
bool rtn;
int index;
string RFID = "";
Agv_Info agv = Common.agvInfo[idx];
if (agv.TaskSend != "") return;
//A6出满料
rtn = FindA6Leave(out string nextNode);
if (rtn)
{
rtn = Common.mir.Add_Mission_Fleet(agv, Common.agvMission["MoveA6"]);
if (rtn)
{
agv.NextPlace = nextNode;
index = FindNode("A6");
Common.nodeInfo[index].AgvName = Common.agvInfo[idx].Name;
RFID = Common.nodeInfo[index].RFID;
index = FindNode(nextNode);
Common.nodeInfo[index].AgvName = Common.agvInfo[idx].Name;
}
Common.LogInfo(string.Format("A6有满料架[{0}]要出,目的地为{1}", RFID, nextNode));
agv.TaskSend = rtn ? "MoveA6" : "";
return;
}
if (Common.linePlace.Count == 0)
{
Common.LogInfo("Common.linePlace.Count == 0 无空料架要出");
return;
}
//ClientLevel clientLevel = Common.linePlace.Values.Max<ClientLevel>();
////出空料架
//foreach (var item in Common.linePlace)
//{
// if (!clientLevel.Equals(item.Value))
// continue;
// string name = item.Key;
// index = Common.nodeInfo.FindIndex(s => s.Name.Equals(name) && s.Action == ClientAction.NeedLeave && s.IsUse);
// if (index > -1)
// {
// rtn = Common.mir.Add_Mission_Fleet(agv, Common.agvMission["Move" + name]);
// if (rtn)
// {
// Common.nodeInfo[index].AgvName = Common.agvInfo[idx].Name;
// Common.linePlace.Remove(name);
// }
// agv.TaskSend = rtn ? "Move" + name : "";
// }
// Common.LogInfo(string.Format("[{0}-{1}] 出空料架.", name, item.Value));
// if (Common.linePlace.Count.Equals(0).Equals(false))
// Common.LogInfo("剩余需要出空料架的节点:" + string.Join(",", Common.linePlace.Keys.ToArray())
// + ";对应紧急程度:" + string.Join(",", Common.linePlace.Values.ToArray()));
// if (rtn) break;
//}
for (int i = 0; i < Common.linePlace.Count; i++)
{
string name = Common.linePlace[i];
index = Common.nodeInfo.FindIndex(s => s.Name.Equals(name) && s.IsUse);
if (index > -1)
{
rtn = Common.mir.Add_Mission_Fleet(agv, Common.agvMission["Move" + name]);
if (rtn)
{
Common.nodeInfo[index].AgvName = Common.agvInfo[idx].Name;
Common.linePlace.RemoveAt(i);
System.IO.File.WriteAllLines(Common.CONFIG_PATH + "LinePlace.txt", Common.linePlace);
}
agv.TaskSend = rtn ? "Move" + name : "";
}
Common.LogInfo(string.Format("[{0}] 出空料架.", name));
if (Common.linePlace.Count.Equals(0).Equals(false))
Common.LogInfo("剩余需要出空料架的节点:" + string.Join(",", Common.linePlace.ToArray()));
if (rtn) break;
}
}
/// <summary>
/// agv正在运动
/// </summary>
/// <param name="idx"></param>
private void StateMove(int idx)
{
}
/// <summary>
/// agv运动完成
/// </summary>
/// <param name="idx"></param>
private void StateMoveFinish(int idx)
{
bool rtn;
Agv_Info agv = Common.agvInfo[idx];
int index = FindNode(agv.Place);
if (index == -1) return;
ClientNode node = Common.nodeInfo[index];
switch (agv.Place)
{
case "A5":
if (node.Action == ClientAction.MayEnter)
{
if (agv.TaskSend == "Leave") return;
rtn = Common.mir.Add_Mission_Fleet(agv, Common.agvMission["Leave"]);
agv.TaskSend = rtn ? "Leave" : "";
if (rtn)
Common.LogInfo(string.Format("{0}到达{1},可以进入料架", agv.Name, agv.Place));
}
else
{
rtn = Common.server.ReadyEnter(agv.Place);
if (!rtn) return;
Common.LogInfo(string.Format("{0}到达{1},服务器发送ReadyEnter信号", agv.Name, agv.Place));
}
break;
case "A6":
if (node.Action == ClientAction.MayEnter)
{
if (agv.TaskSend == "Leave") return;
rtn = Common.mir.Add_Mission_Fleet(agv, Common.agvMission["Leave"]);
agv.TaskSend = rtn ? "Leave" : "";
if (rtn)
Common.LogInfo(string.Format("{0}到达{1},可以进入料架", agv.Name, agv.Place));
}
else if (node.Action == ClientAction.MayLeave)
{
if (agv.TaskSend == "Leave") return;
rtn = Common.mir.Add_Mission_Fleet(agv, Common.agvMission["Leave"]);
agv.TaskSend = rtn ? "Leave" : "";
if (rtn)
Common.LogInfo(string.Format("{0}到达{1},可以出去料架", agv.Name, agv.Place));
}
else if (node.Action == ClientAction.NeedEnter)
{
rtn = Common.server.ReadyEnter(agv.Place);
if (!rtn) return;
Common.LogInfo(string.Format("{0}到达{1},服务器发送ReadyEnter信号", agv.Name, agv.Place));
}
else if (node.Action == ClientAction.NeedLeave)
{
rtn = Common.server.ReadyLeave(agv.Place);
if (!rtn) return;
Common.LogInfo(string.Format("{0}到达{1},服务器发送ReadyLeave信号", agv.Name, agv.Place));
}
else if (node.Action == ClientAction.NeedEnterLeave)
{
//rtn = Common.server.ReadyLeave(agv.Place);
//if (!rtn) return;
}
break;
case "E1":
case "E2":
case "E3":
case "E4":
case "E5":
case "E6":
case "E7":
case "E8":
case "E9":
case "E10":
case "E11":
case "E12":
if (node.Action == ClientAction.MayEnter)
{
if (agv.TaskSend == "Leave") return;
rtn = Common.mir.Add_Mission_Fleet(agv, Common.agvMission["Leave"]);
agv.TaskSend = rtn ? "Leave" : "";
if (rtn)
Common.LogInfo(string.Format("{0}到达{1},可以进入料架", agv.Name, agv.Place));
}
else if (node.Action == ClientAction.MayLeave)
{
if (agv.TaskSend == "Leave") return;
rtn = Common.mir.Add_Mission_Fleet(agv, Common.agvMission["Leave"]);
agv.TaskSend = rtn ? "Leave" : "";
if (rtn)
Common.LogInfo(string.Format("{0}到达{1},可以出去料架", agv.Name, agv.Place));
}
else if (node.Action == ClientAction.NeedEnter)
{
rtn = Common.server.ReadyEnter(agv.Place);
if (!rtn) return;
Common.LogInfo(string.Format("{0}到达{1},服务器发送ReadyEnter信号", agv.Name, agv.Place));
}
else if (node.Action == ClientAction.NeedLeave)
{
rtn = Common.server.ReadyLeave(agv.Place);
if (!rtn) return;
Common.LogInfo(string.Format("{0}到达{1},服务器发送ReadyLeave信号", agv.Name, agv.Place));
}
break;
}
}
/// <summary>
/// agv正在进入
/// </summary>
/// <param name="idx"></param>
private void StateEnter(int idx)
{
}
/// <summary>
/// agv进入完成
/// </summary>
/// <param name="idx"></param>
private void StateEnterFinish(int idx)
{
bool rtn;
Agv_Info agv = Common.agvInfo[idx];
int index = FindNode(agv.Place);
if (index == -1) return;
ClientNode node = Common.nodeInfo[index];
switch (agv.Place)
{
case "A6":
if (node.Action == ClientAction.FinishLeave)
{
string nextPlace = agv.NextPlace;
rtn = Common.mir.Add_Mission_Fleet(agv, Common.agvMission["Move" + nextPlace]);
if (rtn)
{
agv.NextPlace = "";
}
node.AgvName = "";
agv.TaskSend = rtn ? "Move" + nextPlace : "";
if (rtn)
Common.LogInfo(string.Format("{0}完成出满料架,准备运往{1}","A6",nextPlace));
}
break;
}
}
/// <summary>
/// agv正在离开
/// </summary>
/// <param name="idx"></param>
private void StateLeave(int idx)
{
}
/// <summary>
/// agv离开完成
/// </summary>
/// <param name="idx"></param>
private void StateLeaveFinish(int idx)
{
bool rtn;
Agv_Info agv = Common.agvInfo[idx];
int index = FindNode(agv.Place);
if (index == -1) return;
ClientNode node = Common.nodeInfo[index];
switch (agv.Place)
{
case "A5":
if (node.Action == ClientAction.FinishEnter)
{
Common.LogInfo(string.Format("{0}在{1}完成进入料架", agv.Name, agv.Place));
}
break;
case "A6":
if (node.Action == ClientAction.FinishEnter)
{
Common.LogInfo(string.Format("{0}在{1}完成进入料架", agv.Name, agv.Place));
}
break;
}
}
private void StateError(int idx)
{
Agv_Info agv = Common.agvInfo[idx];
Common.LogInfo(string.Format("{0}在执行任务[{1}]出现错误:{2}",agv.Name,agv.TaskSend,agv.MissionText));
}
private int FindNode(string nodeName)
{
int idx = Common.nodeInfo.FindIndex(s => s.Name.Equals(nodeName) && s.IsUse);
return idx;
}
private bool FindA6Leave(out string nextNode)
{
nextNode = "";
int idx = Common.nodeInfo.FindIndex(s => s.Name.Equals("A6") && (s.Action == ClientAction.NeedLeave || s.Action == ClientAction.NeedEnterLeave) && s.IsUse);
if (idx == -1) return false;
bool rtn = true;//FindA6Destination(Common.nodeInfo[idx].RFID, out string dest);
string dest = "E2";
if (!rtn) return false;
idx = Common.nodeInfo.FindIndex(s => s.Name.Equals(dest)&& s.IsUse);
if (idx == -1)
{
return false;
}
else
{
nextNode = dest;
return true;
}
}
private bool FindA6Destination(string rfid, out string dest)
{
dest = "";
try
{
string url = Common.itsHttp + rfid;
var client = new RestClient(url) { Timeout = -1 };
var request = new RestRequest(Method.GET);
//request.AddHeader("Host", "10.85.17.233");
IRestResponse response = client.Execute(request);
string json = response.Content;
json = json.Replace("\r", "");
json = json.Replace("\n", "");
json = json.Replace(" ", "");
Common.LogInfo("ITS URL: " + url + " Return: " + json);
if (string.IsNullOrWhiteSpace(json)) return false;
JavaScriptSerializer serializer = new JavaScriptSerializer();
Dictionary<string, object> dic = (Dictionary<string, object>)serializer.DeserializeObject(json);
if (dic == null) return false;
bool b1 = dic.TryGetValue("id", out object id);
bool b2 = dic.TryGetValue("location", out object location);
if (b1 && b2)
{
if (id.ToString() == rfid)
{
if (Common.agvProductionLine.TryGetValue(location.ToString(), out string loc))
{
dest = loc;
return true;
}
}
}
return false;
}
catch (Exception ex)
{
Common.log.Error("FindA6Destination", ex);
return false;
}
}
}
}