MiR.cs
17.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
using RestSharp;
using System;
using System.Collections.Generic;
using System.Linq;
using AssemblyLine;
using System.Web.Script.Serialization;
namespace BLL
{
public class MiR
{
private bool loop;
private Asa.File.Log log;
//private List<string> _mission;
//private System.Threading.Thread tTask;
private System.Threading.Thread tRecon;
private System.Threading.Thread[] tReadState;
private Dictionary<string, string> place = new Dictionary<string, string>();
public delegate void StatusEvent(int idx, string sta);
public event StatusEvent StatusChanged;
//public delegate void WaitEvent(string id);
//public event WaitEvent Wait;
private const string REG_STATUS = "20";
private const string REG_SHELF = "21";
private const string REG_RFID1 = "24";
private const string REG_RFID2 = "25";
public MiR()
{
string path = AppDomain.CurrentDomain.BaseDirectory + "Config\\AgvPlace.csv";
if (System.IO.File.Exists(path))
{
string[] line = System.IO.File.ReadAllLines(path);
for (int i = 0; i < line.Length; i++)
{
string[] tt = line[i].Split(',');
if (tt.Length == 2)
place.Add(tt[0], tt[1]);
}
}
}
public string[] MissionName()
{
return place.Keys.ToList().ToArray();
}
public void Start()
{
loop = true;
//_mission = new List<string>();
log = new Asa.File.Log(Common.LOG_PATH, "MiR");
tReadState = new System.Threading.Thread[Common.Agv.Length];
tRecon = new System.Threading.Thread(new System.Threading.ThreadStart(Reconnect));
tRecon.Start();
//tTask = new System.Threading.Thread(new System.Threading.ThreadStart(TaskAllot));
//tTask.Start();
}
public void Exit()
{
loop = false;
//if (tReadState != null)
//{
// for (int i = 0; i < tReadState.Length; i++)
// {
// if (tReadState[i] != null) tReadState[i].Abort();
// }
//}
}
public bool RunMission(int idx)
{
try
{
string url = "http://" + Common.Agv[idx].IP + "/api/v2.0.0/status?whitelist=state_id,state_text";
var client = new RestClient(url);
log.OutInfo(url);
client.Timeout = -1;
var request = new RestRequest(Method.PUT);
request.AddHeader("Host", Common.Agv[idx].IP);
request.AddHeader("Content-Type", "application/json");
request.AddHeader("Authorization", Common.Agv[idx].Key);
request.AddParameter("application/json", "{\"state_id\":3}", ParameterType.RequestBody);
IRestResponse response = client.Execute(request);
string s = response.Content;
log.OutInfo("Request " + s);
return true;
}
catch (Exception ex)
{
log.OutError(ex);
return false;
}
}
public bool PauseMission(int idx)
{
try
{
string url = "http://" + Common.Agv[idx].IP + "/api/v2.0.0/status?whitelist=state_id,state_text";
var client = new RestClient(url);
log.OutInfo(url);
client.Timeout = -1;
var request = new RestRequest(Method.PUT);
request.AddHeader("Host", Common.Agv[idx].IP);
request.AddHeader("Content-Type", "application/json");
request.AddHeader("Authorization", Common.Agv[idx].Key);
request.AddParameter("application/json", "{\"state_id\":4}", ParameterType.RequestBody);
IRestResponse response = client.Execute(request);
string s = response.Content;
log.OutInfo("Request " + s);
return true;
}
catch (Exception ex)
{
log.OutError(ex);
return false;
}
}
public bool LeaveFinish(int idx)
{
return Write(idx, REG_STATUS, 4);
}
public bool StatusWait(int idx)
{
Common.Agv[idx].Status = MiR_Status.Wait;
return Write(idx, REG_STATUS, 5);
}
public bool StatusArriveWait(int idx)
{
Common.Agv[idx].Status = MiR_Status.ArriveWait;
return Write(idx, REG_STATUS, 6);
}
public void WriteRFID(int idx, string s)
{
if (s.Length == 0)
{
Write(idx, REG_RFID1, 0);
Write(idx, REG_RFID2, 0);
}
else
{
Write(idx, REG_RFID1, s[0]);
Write(idx, REG_RFID2, Convert.ToInt32(s.Substring(1, s.Length - 1)));
}
}
public string ReadRFID(int idx)
{
string s = "";
s += Convert.ToChar(Read(idx, REG_RFID1));
s += Convert.ToString(Read(idx, REG_RFID2));
return s;
}
public void ExistShelf(int idx)
{
try
{
if (place.TryGetValue(Common.Agv[idx].Name + "_IO", out string value))
{
string url = "http://" + Common.Agv[idx].IP + "/api/v2.0.0/io_modules/" + value + "/status";
var client = new RestClient(url);
log.OutInfo(url);
client.Timeout = -1;
var request = new RestRequest(Method.GET);
request.AddHeader("Content-Type", "application/json");
request.AddHeader("Authorization", Common.Agv[idx].Key);
IRestResponse response = client.Execute(request);
string s = response.Content;
log.OutInfo("ExistShelf: Request " + s.Replace("\r\n", ""));
JavaScriptSerializer serializer = new JavaScriptSerializer();
Dictionary<string, object> json1 = (Dictionary<string, object>)serializer.DeserializeObject(s);
object[] code = (object[])json1["input_state"];
Common.Agv[idx].ExistShelf = Convert.ToBoolean(code[3]);
}
else
{
log.OutInfo(Common.Agv[idx].Name + "_IO None");
Common.Agv[idx].ExistShelf = false;
}
}
catch (Exception ex)
{
log.OutError(ex);
}
}
public bool Mission(int idx, string place)
{
if (this.place.TryGetValue(place, out string value))
{
string url = "http://" + Common.Agv[idx].IP + "/api/v2.0.0/mission_queue";
var client = new RestClient(url);
log.OutInfo(url);
client.Timeout = -1;
var request = new RestRequest(Method.POST);
request.AddHeader("Host", Common.Agv[idx].IP);
request.AddHeader("Content-Type", "application/json");
request.AddHeader("Authorization", Common.Agv[idx].Key);
request.AddParameter("application/json", "{\"mission_id\":\"" + value + "\",\"parameters\":[]}", ParameterType.RequestBody);
IRestResponse response = client.Execute(request);
string s = response.Content;
s = s.Replace("\r\n", "");
log.OutInfo("Request " + s);
return true;
}
else
return false;
}
private int Read(int idx, string reg)
{
try
{
string url = "http://" + Common.Agv[idx].IP + "/api/v2.0.0/registers/" + reg + "?whitelist=value";
var client = new RestClient(url);
log.OutInfo(url);
client.Timeout = -1;
var request = new RestRequest(Method.GET);
request.AddHeader("Host", Common.Agv[idx].IP);
request.AddHeader("Content-Type", "application/json");
request.AddHeader("Authorization", Common.Agv[idx].Key);
request.AddParameter("application/json", "", ParameterType.RequestBody);
IRestResponse response = client.Execute(request);
string s = response.Content;
JavaScriptSerializer serializer = new JavaScriptSerializer();
Dictionary<string, object> json1 = (Dictionary<string, object>)serializer.DeserializeObject(s);
string ss = json1["value"].ToString();
log.OutInfo("Request:" + s.Replace("\r\n", "") + " value=" + ss);
float ff = Convert.ToSingle(ss);
return Convert.ToInt32(ff);
}
catch (Exception ex)
{
log.OutError(ex);
return -1;
}
}
private bool Write(int idx, string reg, int value)
{
try
{
string url = "http://" + Common.Agv[idx].IP + "/api/v2.0.0/registers/" + reg;
var client = new RestClient(url);
log.OutInfo(url);
client.Timeout = -1;
var request = new RestRequest(Method.POST);
request.AddHeader("Host", Common.Agv[idx].IP);
request.AddHeader("Content-Type", "application/json");
request.AddHeader("Authorization", Common.Agv[idx].Key);
request.AddParameter("application/json", "{\"value\":" + value + "}", ParameterType.RequestBody);
IRestResponse response = client.Execute(request);
string s = response.Content;
s = s.Replace("\r\n", "");
log.OutInfo("Request " + s);
int index = s.IndexOf("id");
if (index == -1) return false;
index = s.IndexOf(reg, index);
if (index == -1) return false;
index = s.IndexOf("value", index);
if (index == -1) return false;
index = s.IndexOf(value.ToString(), index);
if (index == -1) return false;
return true;
}
catch (Exception ex)
{
log.OutError(ex);
return false;
}
}
private void Reconnect()
{
bool rtn;
while (loop)
{
for (int i = 0; i < Common.Agv.Length; i++)
{
try
{
rtn = CheckIP(Common.Agv[i].IP);
if (rtn)
{
if (!Common.Agv[i].IsCon)
{
Common.Agv[i].IsCon = true;
//Common.Agv[i].IsCancel = false;
Common.Agv[i].IsUse = false;
StatusChanged?.Invoke(i, "Connect");
tReadState[i] = new System.Threading.Thread(new System.Threading.ParameterizedThreadStart(ReadState));
tReadState[i].Start(i);
}
}
else
{
if (Common.Agv[i].IsCon)
{
Common.Agv[i].IsCon = false;
Common.Agv[i].IsUse = false;
StatusChanged?.Invoke(i, "None");
}
}
}
catch (Exception ex)
{
log.OutError(ex);
}
}
System.Threading.Thread.Sleep(10000);
}
}
//private void TaskAllot()
//{
// bool rtn;
// while (loop)
// {
// System.Threading.Thread.Sleep(100);
// if (_mission.Count == 0) continue;
// for (int i = 0; i < Common.Agv.Length; i++)
// {
// if (!Common.Agv[i].IsCon) continue; //AGV连接
// switch (Common.Agv[i].Status) //AGV状态
// {
// case MiR_Status.Wait:
// Wait?.Invoke(Common.Agv[i].Place);
// break;
// case MiR_Status.Move:
// break;
// case MiR_Status.Error:
// break;
// case MiR_Status.Standby:
// Common.Agv[i].Finish = false;
// Common.Agv[i].Place = _mission[0];
// _mission.RemoveAt(0);
// rtn = Mission(i, "Move" + Common.Agv[i].Place);
// rtn = Ready(i);
// break;
// case MiR_Status.Roll:
// break;
// }
// }
// }
//}
private void ReadState(object obj)
{
int idx = (int)obj;
while (Common.Agv[idx].IsCon)
{
try
{
string url = "http://" + Common.Agv[idx].IP + "/api/v2.0.0/status?whitelist=state_id,state_text";
var client = new RestClient(url);
log.OutInfo(url);
client.Timeout = -1;
var request = new RestRequest(Method.GET);
request.AddHeader("Host", Common.Agv[idx].IP);
request.AddHeader("Content-Type", "application/json");
request.AddHeader("Authorization", Common.Agv[idx].Key);
IRestResponse response = client.Execute(request);
string JsonData = response.Content;
log.OutInfo("Request " + JsonData);
JavaScriptSerializer serializer = new JavaScriptSerializer();
Dictionary<string, object> json1 = (Dictionary<string, object>)serializer.DeserializeObject(JsonData);
string state_id = json1["state_id"].ToString();
string state_text = json1["state_text"].ToString();
int id = Convert.ToInt32(state_id);
if (id != Common.Agv[idx].StateID)
{
Common.Agv[idx].StateID = id;
Common.Agv[idx].StateText = state_text;
StatusChanged?.Invoke(idx, state_text);
}
if (id == 3 || id == 4 || id == 5 || id == 7) //Ready,Pause,Executing,Completed
Common.Agv[idx].IsUse = true;
else
Common.Agv[idx].IsUse = false;
Common.Agv[idx].Status = (MiR_Status)Read(idx, REG_STATUS); //PLC20的寄存器状态
log.OutInfo(Common.Agv[idx].Name + " Status=" + Common.Agv[idx].Status);
}
catch (Exception ex)
{
log.OutError(ex);
}
System.Threading.Thread.Sleep(3000);
}
}
/// <summary>
/// 检查IP地址
/// </summary>
/// <param name="ip"></param>
/// <returns></returns>
private bool CheckIP(string ip)
{
//IP合法
string pattern = @"^((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?)$";
bool rtn = System.Text.RegularExpressions.Regex.IsMatch(ip, pattern);
if (!rtn)
{
log.OutInfo("非法的IP地址" + ip);
return false;
}
//Ping服务端
try
{
System.Net.NetworkInformation.Ping ping = new System.Net.NetworkInformation.Ping();
System.Net.NetworkInformation.PingReply result = ping.Send(ip, 1000);
ping.Dispose();
if (result.Status != System.Net.NetworkInformation.IPStatus.Success)
{
log.OutInfo("Ping " + ip + " 请求没有响应");
return false;
}
return true;
}
catch(Exception ex)
{
log.OutError(ex);
return false;
}
}
}
public enum MiR_Status
{
/// <summary>
/// 没有动作
/// </summary>
None = 0,
/// <summary>
/// 小车到达
/// </summary>
Arrive = 1,
/// <summary>
/// 小车已准备好
/// </summary>
Ready = 2,
/// <summary>
/// 完成进入
/// </summary>
FinishEnter = 3,
/// <summary>
/// 完成离开
/// </summary>
FinishLeave = 4,
/// <summary>
/// 等待
/// </summary>
Wait = 5,
/// <summary>
/// 小车到达后等待
/// </summary>
ArriveWait = 6,
/// <summary>
/// 报错
/// </summary>
Error = 10,
/// <summary>
/// 执行任务中
/// </summary>
StartMission = 11
}
public enum MiR_Dev_Status
{
None,
Starting,
ShuttingDown,
Ready,
Pause,
Executing,
Aborted,
Completed,
Docked,
Docking,
EmergencyStop,
ManualControl,
Error
}
}