Control.cs
15.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
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);
Common.log.Info(string.Format("{0} Get_State StateID={1}, StateText={2}, Battery={3}, Mission_text={4}", Common.agvInfo[i].Name, stateID, stateText, battery, mission_text));
if (rtn) Common.agvInfo[i].SetState(stateID, stateText, battery, mission_text);
//获取地点任务状态
Thread.Sleep(50);
rtn = Common.mir.Get_Register(Common.agvInfo[i], REG_STATUS, out int regValue);
Common.log.Info(string.Format("{0} Get_Register PLC{1}={2}", Common.agvInfo[i].Name, REG_STATUS, regValue));
if (rtn) Common.agvInfo[i].GetPlace(regValue);
//执行任务更新状态
if (stateID == 5)
{
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.log.Info(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.log.Info(Common.agvInfo[idx].Name + " Online");
AgvOnline?.Invoke(idx);
AgvChanged?.Invoke(idx);
}
}
else
{
if (Common.agvInfo[idx].IsCon)
{
Common.agvInfo[idx].IsCon = false;
Common.log.Info(Common.agvInfo[idx].Name + " Offline");
AgvOnline?.Invoke(idx);
AgvChanged?.Invoke(idx);
}
}
return rtn;
}
private void StateNone(int idx)
{
bool rtn;
int index;
Agv_Info agv = Common.agvInfo[idx];
if (agv.TaskSend != "") return;
//出满料
rtn = FindA6Leave(out string nextNode);
if (rtn)
{
rtn = Common.mir.Add_Mission(agv, Common.agvMission["MoveA6"]);
if (rtn)
{
agv.NextPlace = nextNode;
index = FindNode("A6");
Common.nodeInfo[index].AgvName = Common.agvInfo[idx].Name;
index = FindNode(nextNode);
Common.nodeInfo[index].AgvName = Common.agvInfo[idx].Name;
}
agv.TaskSend = rtn ? "MoveA6" : "";
return;
}
//出空料架
for (int i = 0; i < Common.linePlace.Count; i++)
{
string name = Common.linePlace[i];
index = Common.nodeInfo.FindIndex(s => s.Name.Equals(name) && s.Action == ClientAction.NeedLeave && s.IsUse);
if (index > -1)
{
rtn = Common.mir.Add_Mission(agv, Common.agvMission["Move" + name]);
if (rtn)
{
Common.nodeInfo[index].AgvName = Common.agvInfo[idx].Name;
Common.linePlace.RemoveAt(i);
}
agv.TaskSend = rtn ? "Move" + name : "";
}
if (rtn) break;
}
}
private void StateMove(int idx)
{
}
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(agv, Common.agvMission["Leave"]);
agv.TaskSend = rtn ? "Leave" : "";
}
else
{
rtn = Common.server.ReadyEnter(agv.Place);
if (!rtn) return;
}
break;
case "A6":
if (node.Action == ClientAction.MayEnter)
{
if (agv.TaskSend == "Leave") return;
rtn = Common.mir.Add_Mission(agv, Common.agvMission["Leave"]);
agv.TaskSend = rtn ? "Leave" : "";
}
else if (node.Action == ClientAction.MayLeave)
{
if (agv.TaskSend == "Leave") return;
rtn = Common.mir.Add_Mission(agv, Common.agvMission["Leave"]);
agv.TaskSend = rtn ? "Leave" : "";
}
else if (node.Action == ClientAction.NeedEnter)
{
rtn = Common.server.ReadyEnter(agv.Place);
if (!rtn) return;
}
else if (node.Action == ClientAction.NeedLeave)
{
rtn = Common.server.ReadyLeave(agv.Place);
if (!rtn) return;
}
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(agv, Common.agvMission["Leave"]);
agv.TaskSend = rtn ? "Leave" : "";
}
else if (node.Action == ClientAction.MayLeave)
{
if (agv.TaskSend == "Leave") return;
rtn = Common.mir.Add_Mission(agv, Common.agvMission["Leave"]);
agv.TaskSend = rtn ? "Leave" : "";
}
else if (node.Action == ClientAction.NeedEnter)
{
rtn = Common.server.ReadyEnter(agv.Place);
if (!rtn) return;
}
else if (node.Action == ClientAction.NeedLeave)
{
rtn = Common.server.ReadyLeave(agv.Place);
if (!rtn) return;
}
break;
}
}
private void StateEnter(int idx)
{
}
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)
{
}
break;
}
}
private void StateLeave(int idx)
{
}
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)
{
}
break;
case "A6":
if (node.Action == ClientAction.FinishEnter)
{
}
break;
}
}
private void StateError(int idx)
{
}
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 = FindA6Destination(Common.nodeInfo[idx].RFID, out string dest);
if (!rtn) return false;
idx = Common.nodeInfo.FindIndex(s => s.Name.Equals(dest) && s.Action == ClientAction.NeedEnter && 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.log.Info("URL: " + url + "\r\nReturn: " + 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;
}
}
}
}