HttpServer.cs
12.6 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
using OnlineStore.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Script.Serialization;
namespace OnlineStore.DeviceLibrary
{
public class HttpServer
{
public HttpServer()
{
}
private static string Addr_updateDeviceAlarmMsg = "/updateDeviceAlarmMsg";
public static string updateDeviceAlarmMsg(List<AlarmMsg> msgList)
{
string msg = "";
try
{
Dictionary<string, string> paramMap = new Dictionary<string, string>();
string msgListStr = JsonHelper.SerializeObject(msgList);
paramMap.Add("deviceAlarmList", msgListStr);
string server = GetAddr(Addr_updateDeviceAlarmMsg, paramMap);
DateTime startTime = DateTime.Now;
string resultStr = HttpHelper.Post(server, "", 2000);
LogUtil.debug("updateDeviceAlarmMsg " + FormUtil.GetSpanStr(DateTime.Now - startTime) + " 【" + server + "】【" + resultStr + "】");
RfidData data = JsonHelper.DeserializeJsonToObject<RfidData>(resultStr);
if (data == null)
{
return msg = " updateDeviceAlarmMsg 没有收到服务器反馈";
}
else if (data.code.Equals(0).Equals(false))
{
return msg = " updateDeviceAlarmMsg 【" + server + "】【" + resultStr + "】" + data.msg;
}
return "";
}
catch (Exception ex)
{
LogUtil.error(" updateDeviceAlarmMsg Error: " + ex.ToString());
}
return msg;
}
private static string GetAddr(string addr, Dictionary<string, string> paramsMap)
{
string server = ConfigAppSettings.GetValue(Setting_Init.httpAddr);
if (server.EndsWith("/"))
{
server = server.Substring(0, server.Length - 1);
}
string path = server + addr.Trim() + "?";
foreach (string paramName in paramsMap.Keys)
{
string par = System.Web.HttpUtility.UrlEncode(paramsMap[paramName], System.Text.Encoding.UTF8);
path += paramName + "=" + par + "&";
}
path = path.Substring(0, path.Length - 1);
return path;
}
private static string httpAddr = ConfigAppSettings.GetValue(Setting_Init.httpAddr);
public static ShelfTaskInfo ShelfFinish(string rfid, string barcode = "", string rfidLoc = "", string robotIndex = "1")
{
ShelfTaskInfo task = new ShelfTaskInfo();
task.rfid = rfid;
string api = "putShelfFinished";
DateTime startTime = DateTime.Now;
try
{
string url = httpAddr + api + "?barcode=" + barcode + "&rfid=" + rfid + "&rfidLoc=" + rfidLoc + "&robotIndex=" + robotIndex;
LogUtil.debug("http :URL:" + url);
string json = HttpHelper.Post(url, "", 10000);
if (barcode != "")
{
LogUtil.info("http :URL:" + url + " :Response:" + json + " 耗时[" + FormUtil.GetSpanStr(DateTime.Now - startTime) + "]");
}
else
{
LogUtil.debug("http :URL:" + url + " :Response:" + json);
}
if (string.IsNullOrWhiteSpace(json)) return task;
JavaScriptSerializer serializer = new JavaScriptSerializer();
Dictionary<string, object> obj = (Dictionary<string, object>)serializer.DeserializeObject(json);
if (!obj.TryGetValue("code", out object value)) return task;
if (value.ToString() != "0")
{
if (obj.TryGetValue("msg", out value))
LogUtil.error("http" + api + ": " + value.ToString());
return task;
}
if (!obj.TryGetValue("data", out value)) return task;
Dictionary<string, object> dict = (Dictionary<string, object>)value;
if (dict == null)
{
LogUtil.info("http" + api + ": data=null");
return task;
}
if (dict.TryGetValue("bigEmpty", out value))
int.TryParse(value.ToString(), out task.bigEmpty);
if (dict.TryGetValue("smallEmpty", out value))
int.TryParse(value.ToString(), out task.smallEmpty);
if (dict.TryGetValue("usedRfidList", out value))
task.usedRfidList = value.ToString();
}
catch (Exception ex)
{
LogUtil.error("http error : " + ex.ToString());
}
return task;
}
public static AllTaskInfo GetATaskInfo(string rfid = "", string barcode = "", string rfidLoc = "", string robotIndex = "")
{
AllTaskInfo task = new AllTaskInfo();
string api = "putShelfFinished";
try
{
string url = httpAddr + api + "?barcode=" + barcode + "&rfid=" + rfid + "&rfidLoc=" + rfidLoc + "&robotIndex=" + robotIndex;
string logName = "http :URL:" + url + " , ";
string json = HttpHelper.Post(url, "", 10000);
if (string.IsNullOrWhiteSpace(json)) return task;
JavaScriptSerializer serializer = new JavaScriptSerializer();
Dictionary<string, object> obj = (Dictionary<string, object>)serializer.DeserializeObject(json);
if (!obj.TryGetValue("code", out object value)) return task;
if (value.ToString() != "0")
{
if (obj.TryGetValue("msg", out value))
LogUtil.info(logName + ": " + value.ToString());
return task;
}
if (!obj.TryGetValue("data", out value)) return task;
Dictionary<string, object> dict = (Dictionary<string, object>)value;
if (dict == null)
{
LogUtil.info(logName + ": data=null");
return task;
}
if (dict.TryGetValue("bigTask", out value))
int.TryParse(value.ToString(), out task.bigTask);
if (dict.TryGetValue("smallTask", out value))
int.TryParse(value.ToString(), out task.smallTask);
if (task.bigTask > 0 || task.smallTask > 0)
{
LogUtil.debug(logName + " Response:" + json);
}
else
{
LogUtil.debug(logName + " Response:" + json);
}
}
catch (Exception ex)
{
LogUtil.error("http error : " + ex.ToString());
}
return task;
}
/// <summary>
/// 获取定位机构上的料盘信息
/// </summary>
/// <param name="robot">ABB机器人索引号</param>
/// <param name="currRFID">当前工位的RFID</param>
public static TrayInfo GetLocation(int robot, string currRFID, out string msg)
{
TrayInfo tray = new TrayInfo();
string api = "getLocation";
msg = "";
try
{
string url = httpAddr + api + "?robotIndex=" + robot + "&rfid=" + currRFID;
string logName = "http :URL:" + url + " , ";
DateTime startTime = DateTime.Now;
string json = HttpHelper.Post(url, "", 10000);
LogUtil.debug(logName + ",Response:" + json + ",耗时【" + FormUtil.GetSpanStr(DateTime.Now - startTime) + "】");
if (string.IsNullOrWhiteSpace(json))
{
return tray;
}
JavaScriptSerializer serializer = new JavaScriptSerializer();
Dictionary<string, object> obj = (Dictionary<string, object>)serializer.DeserializeObject(json);
if (!obj.TryGetValue("code", out object value)) return tray;
if (value.ToString() != "0")
{
if (obj.TryGetValue("msg", out value))
{
msg = value.ToString();
LogUtil.error(logName + ": " + value.ToString(), 500 + robot);
}
return tray;
}
if (!obj.TryGetValue("data", out value)) return tray;
Dictionary<string, object> dict = (Dictionary<string, object>)value;
if (dict == null)
{
LogUtil.info(logName + ": data=null");
return tray;
}
if (dict.TryGetValue("w", out value))
{
switch (value)
{
case "7": tray.getP = "p1"; break;
case "11": tray.getP = "p2"; break;
case "13": tray.getP = "p3"; break;
case "15": tray.getP = "p4"; break;
}
}
if (dict.TryGetValue("rfidLoc", out value))
int.TryParse(value.ToString(), out tray.shelfP);
if (dict.TryGetValue("rfid", out value))
tray.rfid = value.ToString();
if (dict.TryGetValue("realRfid", out value))
tray.realRFID = value.ToString();
if (dict.TryGetValue("barcode", out value))
tray.barcode = value.ToString();
if (dict.TryGetValue("usedRfidList", out value))
tray.usedRfidList = value.ToString();
}
catch (Exception ex)
{
LogUtil.error("http error : " + ex.ToString());
}
tray.robotNum = robot;
return tray;
}
}
public class TrayInfo
{
public int robotNum = 0;
public string getP = "";
public int shelfP = 0;
public string rfid = "";
public string realRFID = "";
public string barcode = "";
public string usedRfidList = "";
public string ToStr()
{
return "[" + robotNum + "] [" + barcode + "] [" + getP + "] [" + shelfP + "] [" + rfid + "] [" + realRFID + "] [" + usedRfidList + "] [" + updateTime.ToLongTimeString() + "]";
}
public DateTime updateTime = DateTime.Now;
}
public class ShelfTaskInfo
{
public string rfid = "";
public int bigEmpty = -1;
public int smallEmpty = -1;
//public int packageEmpty = -1;
public string usedRfidList = "";
public bool IsValid()
{
if (bigEmpty != -1 && smallEmpty != -1)
{
return true;
}
return false;
}
public string ToStr()
{
return " " + rfid + "剩余位置: 小料=" + smallEmpty + ",大料=" + bigEmpty + ",已使用料架=" + usedRfidList + " ";
}
}
public class AllTaskInfo
{
public int smallTask = -1;
public int bigTask = -1;
//public int packageTask = -1;
public string ToStr()
{
return " 剩余任务: 小料=" + smallTask + ",大料=" + bigTask + " ";
}
public bool IsValid()
{
if (smallTask != -1 && bigTask != -1)
{
return true;
}
return false;
}
}
public class AlarmMsg
{
//>>>name : 异常位置名称
public string name = "";
//>>>msgKey : 异常信息唯一标识
public string msgKey = "";
//>>>msgValue : 异常信息
public string msgValue = "";
/// <summary>
/// 异常信息
/// </summary>
/// <param name="name">异常位置名称</param>
/// <param name="key">异常信息唯一标识</param>
/// <param name="value">异常信息</param>
public AlarmMsg(string name, string key, string value)
{
this.name = name;
this.msgKey = key;
this.msgValue = value;
}
}
public class RfidData
{
//{"code":0,"msg":"ok","data":"7"}
public int code { get; set; }
public string msg { get; set; }
public Dictionary<string, string> data { get; set; }
}
}