MiR_API.cs
21.1 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
568
569
570
571
using System;
using System.Collections.Generic;
using System.Web.Script.Serialization;
using AGVControl;
using RestSharp;
namespace BLL
{
public class MiR_API
{
private const int httpTimeout = 5000;
public MiR_API()
{
}
/// <summary>
/// 获取IO模块guid列表
/// </summary>
/// <param name="info"></param>
/// <param name="guid"></param>
/// <returns></returns>
//public bool Get_IO_Modules(AGVControl.Agv_Info info, out string[] guid)
//{
// try
// {
// guid = null;
// string url = "http://" + info.IP + "/api/v2.0.0/status?whitelist=state_id,state_text";
// string json = HttpGet(url, info.IP, info.Authorization);
// //log.OutInfo("URL: " + url+"\n"+"Return: " + json);
// if (string.IsNullOrWhiteSpace(json)) return false;
// JavaScriptSerializer serializer = new JavaScriptSerializer();
// object[] obj = (object[])serializer.DeserializeObject(json);
// if (obj == null) return false;
// guid = new string[obj.Length];
// for (int i = 0; i < obj.Length; i++)
// {
// Dictionary<string, object> dic = (Dictionary<string, object>)obj[i];
// guid[i] = dic["guid"].ToString();
// }
// return true;
// }
// catch (Exception ex)
// {
// guid = null;
// Common.log.Error("Get_IO_Modules", ex);
// return false;
// }
//}
/// <summary>
/// 获取IO模块的状态
/// </summary>
/// <param name="info"></param>
/// <param name="input"></param>
/// <param name="output"></param>
/// <returns></returns>
//public bool Get_IO_Status(Agv_Info info, out bool[] input, out bool[] output)
//{
// try
// {
// input = null;
// output = null;
// string url = "http://" + info.IP + "/api/v2.0.0/io_modules/" + info.IOID + "/status";
// string json = HttpGet(url, info.IP, info.Authorization);
// //log.OutInfo("URL: " + url + "\n" + "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;
// object[] objInput = (object[])dic["input_state"];
// input = new bool[objInput.Length];
// for (int i = 0; i < input.Length; i++)
// input[i] = Convert.ToBoolean(objInput[i]);
// object[] objOutput = (object[])dic["output_state"];
// output = new bool[objOutput.Length];
// for (int i = 0; i < output.Length; i++)
// output[i] = Convert.ToBoolean(objOutput[i]);
// return true;
// }
// catch (Exception ex)
// {
// input = null;
// output = null;
// //Common.log.Error("", ex);
// return false;
// }
//}
/// <summary>
/// 获取PLC寄存器的内容
/// </summary>
/// <param name="info"></param>
/// <param name="reg"></param>
/// <param name="regValue"></param>
/// <returns></returns>
public bool Get_Register(Agv_Info info, int reg, out int regValue)
{
try
{
regValue = -1;
string url = "http://" + info.IP + "/api/v2.0.0/registers/" + reg + "?whitelist=value";
string json = HttpGet(url, info.IP, info.Authorization);
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;
string s = dic["value"].ToString();
float f = Convert.ToSingle(s);
regValue = Convert.ToInt32(f);
return true;
}
catch (Exception ex)
{
regValue = -1;
Common.log.Error("Get_Register", ex);
return false;
}
}
/// <summary>
/// 设置PLC寄存器的内容
/// </summary>
/// <param name="info"></param>
/// <param name="reg"></param>
/// <param name="regValue"></param>
/// <returns></returns>
public bool Set_Register(Agv_Info info, int reg, int regValue)
{
try
{
string url = "http://" + info.IP + "/api/v2.0.0/registers/" + reg;
string body = "{\"value\": " + regValue + "}";
string json = HttpPost(url, info.IP, info.Authorization, body);
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;
string s = dic["value"].ToString();
float f = Convert.ToSingle(s);
if (regValue == Convert.ToInt32(f))
return true;
else
return false;
}
catch (Exception ex)
{
Common.log.Error("Set_Register", ex);
return false;
}
}
/// <summary>
/// 添加任务到任务队列
/// </summary>
/// <param name="info"></param>
/// <param name="mission_id"></param>
/// <returns></returns>
public bool Add_Mission(Agv_Info info, string mission_id)
{
try
{
//防止上一个任务已执行但返回失败时,删除任务
Del_Mission(info);
string url = "http://" + info.IP + "/api/v2.0.0/mission_queue";
string body = "{\"mission_id\":\"" + mission_id + "\"}";
string json = HttpPost(url, info.IP, info.Authorization, body);
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;
string s = dic["mission_id"].ToString();
if (s == mission_id)
return true;
else
return false;
}
catch (Exception ex)
{
Common.log.Error("Add_Mission", ex);
return false;
}
}
/// <summary>
/// 添加任务到任务队列
/// </summary>
/// <param name="info"></param>
/// <param name="mission_id"></param>
/// <returns></returns>
public bool Add_Mission_Fleet(Agv_Info info, string mission_id)
{
try
{
//防止上一个任务已执行但返回失败时,删除任务
Del_Mission(info);
string ip = Common.appConfig.AppSettings.Settings["FLEET"].Value;
string url = "http://" + ip + "/api/v2.0.0/mission_scheduler";
string body = "{\"mission_id\":\"" + mission_id + "\",\"robot_id\":" + info.ID + "}";
string json = HttpPost(url, info.IP, info.Authorization, body);
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;
string s = dic["mission_id"].ToString();
if (s == mission_id)
return true;
else
return false;
}
catch (Exception ex)
{
Common.log.Error("Add_Mission_Fleet", ex);
return false;
}
}
/// <summary>
/// 获取所有任务列表
/// </summary>
/// <param name="info"></param>
/// <param name="mission">任务(名称,guid)</param>
/// <returns></returns>
public bool Get_Mission(Agv_Info info, out Dictionary<string, string> mission)
{
mission = null;
try
{
string url = "http://" + info.IP + "/api/v2.0.0/missions/search?whitelist=guid,name";
string body = "{\"filters\" :[]}";
string json = HttpPost(url, info.IP, info.Authorization, body);
if (string.IsNullOrWhiteSpace(json)) return false;
JavaScriptSerializer serializer = new JavaScriptSerializer();
object[] obj = (object[])serializer.DeserializeObject(json);
if (obj == null) return false;
mission = new Dictionary<string, string>();
for (int i = 0; i < obj.Length; i++)
{
Dictionary<string, object> dic = (Dictionary<string, object>)obj[i];
mission.Add(dic["name"].ToString(), dic["guid"].ToString());
}
return true;
}
catch (Exception ex)
{
Common.log.Error("Get_Mission", ex);
return false;
}
}
/// <summary>
/// 获取当前任务队列guid
/// </summary>
/// <param name="info"></param>
/// <param name="mission"></param>
/// <returns></returns>
public bool Get_Mission_Queue(Agv_Info info, out List<string> mission)
{
mission = null;
try
{
string url = "http://" + info.IP + "/api/v2.0.0/mission_queue/search";
string body = "{\"filters\" : [{\"fieldname\": \"state\", \"operator\": \"IN\", \"value\": [\"Pending\", \"Executing\"]}]}";
string json = HttpPost(url, info.IP, info.Authorization, body);
if (string.IsNullOrWhiteSpace(json)) return false;
JavaScriptSerializer serializer = new JavaScriptSerializer();
object[] obj = (object[])serializer.DeserializeObject(json);
if (obj == null) return false;
mission = new List<string>();
Dictionary<string, object> dic;
for (int i = 0; i < obj.Length; i++)
{
dic = (Dictionary<string, object>)obj[i];
mission.Add(dic["id"].ToString());
}
for (int i = 0; i < mission.Count; i++)
{
url = "http://" + info.IP + "/api/v2.0.0/mission_queue/" + mission[i];
json = HttpGet(url, info.IP, info.Authorization);
serializer = new JavaScriptSerializer();
dic = (Dictionary<string, object>)serializer.DeserializeObject(json);
mission[i] = dic["mission_id"].ToString();
}
return true;
}
catch (Exception ex)
{
Common.log.Error("Get_Mission_Queue", ex);
return false;
}
}
/// <summary>
/// 删除当前所有任务
/// </summary>
/// <param name="info"></param>
public void Del_Mission(Agv_Info info)
{
try
{
string url = "http://" + info.IP + "/api/v2.0.0/mission_queue";
HttpDel(url, info.IP, info.Authorization);
}
catch (Exception ex)
{
Common.log.Error("Del_Mission", ex);
}
}
/// <summary>
/// 获取当前小车状态
/// </summary>
/// <param name="info"></param>
/// <param name="stateID"></param>
/// <param name="stateText"></param>
/// <param name="battery"></param>
/// <param name="mission_text"></param>
/// <returns></returns>
public bool Get_State(Agv_Info info, out int stateID, out string stateText, out int battery, out string mission_text)
{
stateID = -1;
stateText = "";
battery = 0;
mission_text = "";
try
{
string url = "http://" + info.IP + "/api/v2.0.0/status?whitelist=state_id,state_text,battery_percentage,mission_text";
string json = HttpGet(url, info.IP, info.Authorization);
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;
stateID = Convert.ToInt32(dic["state_id"].ToString());
stateText = dic["state_text"].ToString();
mission_text = dic["mission_text"].ToString();
string s = dic["battery_percentage"].ToString();
float f = Convert.ToSingle(s);
battery = Convert.ToInt32(f);
return true;
}
catch (Exception ex)
{
Common.log.Error("Get_State", ex);
return false;
}
}
/// <summary>
/// 小车状态改为Ready
/// </summary>
/// <param name="info"></param>
/// <returns></returns>
public bool State_Ready(Agv_Info info)
{
try
{
string url = "http://" + info.IP + "/api/v2.0.0/status?whitelist=state_id,state_text";
string body = "{\"state_id\": 3}";
string json = HttpPut(url, info.IP, info.Authorization, body);
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;
string s = dic["state_id"].ToString();
if (s == "3")
return true;
else
return false;
}
catch (Exception ex)
{
Common.log.Error("State_Ready", ex);
return false;
}
}
/// <summary>
/// 小车状态改为Pause
/// </summary>
/// <param name="info"></param>
/// <returns></returns>
public bool State_Pause(Agv_Info info)
{
try
{
string url = "http://" + info.IP + "/api/v2.0.0/status?whitelist=state_id,state_text";
string body = "{\"state_id\": 4}";
string json = HttpPut(url, info.IP, info.Authorization, body);
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;
string s = dic["state_id"].ToString();
if (s == "4")
return true;
else
return false;
}
catch (Exception ex)
{
Common.log.Error("State_Pause", ex);
return false;
}
}
/// <summary>
/// 清除错误
/// </summary>
/// <param name="info"></param>
/// <returns></returns>
public bool Clear_Error(Agv_Info info)
{
try
{
string url = "http://" + info.IP + "/api/v2.0.0/status";
string body = "{\"clear_error\": true}";
string json = HttpPut(url, info.IP, info.Authorization, body);
if (string.IsNullOrWhiteSpace(json)) return false;
return true;
}
catch (Exception ex)
{
Common.log.Error("Clear_Error", ex);
return false;
}
}
/// <summary>
/// 检查IP地址
/// </summary>
/// <param name="ip"></param>
/// <returns></returns>
public 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)
{
Common.log.Info("非法的IP地址" + ip);
return false;
}
//Ping服务端
try
{
System.Net.NetworkInformation.Ping ping = new System.Net.NetworkInformation.Ping();
System.Net.NetworkInformation.PingReply result = ping.Send(ip, 2000);
ping.Dispose();
if (result.Status != System.Net.NetworkInformation.IPStatus.Success)
{
Common.log.Info("Ping " + ip + " 请求没有响应");
return false;
}
return true;
}
catch (Exception ex)
{
Common.log.Error("CheckIP", ex);
return false;
}
}
private string HttpGet(string url, string ip, string authorization)
{
RestClient client = new RestClient(url) { Timeout = httpTimeout };
RestRequest request = new RestRequest(Method.GET);
request.AddHeader("Host", ip);
request.AddHeader("Content-Type", "application/json");
request.AddHeader("Accept-Language", "zh_CN");
request.AddHeader("Authorization", authorization);
IRestResponse response = client.Execute(request);
string s = response.Content;
s = s.Replace("\n", "");
s = s.Replace(" ", "");
Common.log.Debug("HttpGet URL: " + url + "\r\nReturn: " + s);
return s;
}
private string HttpPost(string url, string ip, string authorization, string body)
{
var client = new RestClient(url) { Timeout = httpTimeout };
var request = new RestRequest(Method.POST);
request.AddHeader("Host", ip);
request.AddHeader("Content-Type", "application/json");
request.AddHeader("Accept-Language", "zh_CN");
request.AddHeader("Authorization", authorization);
request.AddParameter("application/json", body, ParameterType.RequestBody);
IRestResponse response = client.Execute(request);
string s = response.Content;
s = s.Replace("\n", "");
s = s.Replace(" ", "");
Common.log.Debug(string.Format("HttpPost URL: {0}; Body: {1}\r\nReturn: {2}", url, body, s));
return s;
}
private string HttpPut(string url, string ip, string authorization, string body)
{
RestClient client = new RestClient(url) { Timeout = httpTimeout };
RestRequest request = new RestRequest(Method.PUT);
request.AddHeader("Host", ip);
request.AddHeader("Content-Type", "application/json");
request.AddHeader("Accept-Language", "zh_CN");
request.AddHeader("Authorization", authorization);
request.AddParameter("application/json", body, ParameterType.RequestBody);
IRestResponse response = client.Execute(request);
string s = response.Content;
s = s.Replace("\n", "");
s = s.Replace(" ", "");
Common.log.Debug(string.Format("HttpPut URL: {0}; Body: {1}\r\nReturn: {2}", url, body, s));
return s;
}
private void HttpDel(string url, string ip, string authorization)
{
RestClient client = new RestClient(url) { Timeout = httpTimeout };
RestRequest request = new RestRequest(Method.DELETE);
request.AddHeader("Host", ip);
request.AddHeader("Content-Type", "application/json");
request.AddHeader("Authorization", authorization);
request.AddHeader("Accept-Language", "zh_CN");
request.AddParameter("application/json", "", ParameterType.RequestBody);
IRestResponse response = client.Execute(request);
string s = response.Content;
Common.log.Debug("HttpDel URL: " + url);
}
}
}