ServerCommunication.cs
20.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
using HalconDotNet;
using Newtonsoft.Json;
using OnlineStore;
using OnlineStore.Common;
using OnlineStore.Common.util;
using OnlineStore.LoadCSVLibrary;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Reflection;
using System.Security.Policy;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.TrackBar;
namespace DeviceLibrary
{
public class ServerCommunication
{
volatile StoreStatus _storeStatus = StoreStatus.Debugging;
public StoreStatus storeStatus
{
get => _storeStatus;
set
{
if (_storeStatus != value)
LogUtil.info($"set storeStatus to {value}");
_storeStatus = value;
}
}
static string server = ConfigHelper.Config.Get("http_server", "http://192.168.1.123:8800/");
static string CID = ConfigHelper.Config.Get("CID");
int StoreID = 1;
string StoreName = "";
string WarnMsg = "";
private System.Timers.Timer serverConnectTimer = new System.Timers.Timer();
object serverclock = new object();
public ServerCommunication()
{
serverConnectTimer.Interval = 1000;
serverConnectTimer.AutoReset = true;
serverConnectTimer.Enabled = true;
serverConnectTimer.Elapsed += ServerConnectTimer_Elapsed;
GC.KeepAlive(serverConnectTimer);
LogUtil.info($"server:{server},cid:{CID}");
}
private void ServerConnectTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
if (!server.StartsWith("http"))
return;
SendLineStatus();
}
public void StartConnectServer()
{
serverConnectTimer.Enabled = true;
}
public void StopConnectServer()
{
serverConnectTimer.Enabled = false;
}
/// <summary>
/// 获取整个料仓的状态
/// </summary>
Operation getLineBoxStatus()
{
//构建发送给服务器的对象
Operation lineOperation = new Operation();
lineOperation.msg = "";
lineOperation.alarmList = new List<AlarmInfo>();
lineOperation.cid = CID;
lineOperation.seq = ConfigAppSettings.nextSeq();
lineOperation.status = 1;
lineOperation.data = new Dictionary<string, string>();
//lineOperation.data = lineOperation.data.Concat(GetAgvBtnStatus()).ToDictionary(x => x.Key, v => v.Value);
lineOperation.status = (int)storeStatus;
BoxStatus boxStatus = new BoxStatus();
boxStatus.boxId = StoreID;
//状态
boxStatus.status = (int)storeStatus;
string sendmsg = "";
if (commandResultMsg.Count() > 0)
{
if (commandResultMsg.TryDequeue(out string msg))
{
lineOperation.msg = msg;
lineOperation.msg = lineOperation.msg.Trim().Trim(',');
boxStatus.msg = lineOperation.msg;
}
}
else if (!string.IsNullOrEmpty(WarnMsg))
{
sendmsg = string.Join(",", new string[] { WarnMsg });
//lineOperation.msgData["zh"] = lineOperation.msg;
//lineOperation.msgData["en"] = lineOperation.msg;
//lineOperation.msgData["jp"] = lineOperation.msg;
//lineOperation.status = (int)StoreStatus.Warning;
}
else if (!RobotManage.isRunning)
{
sendmsg = crc.GetString("Res0001", "设备未启动");
}
lineOperation.msg = sendmsg;
lineOperation.msg = lineOperation.msg.Trim().Trim(',');
boxStatus.msg = lineOperation.msg;
lineOperation.msgEn = lineOperation.msg;
lineOperation.msgJp = lineOperation.msg;
//lineOperation.boxStatus.Add(StoreID, boxStatus);
lineOperation.data= lineOperation.data.Union(UnifiedDataHandler.PrintNgMESData()).ToDictionary(x=>x.Key,x=>x.Value);
lineOperation.showLogs = StepInformation(out Dictionary<string,string> dic);
lineOperation.data =lineOperation.data.Union(dic).ToDictionary(x => x.Key, x => x.Value);
return lineOperation;
}
private static string api_communication = "service/equipment/communication"; //流水线状态通信接口
public static string GetPostApi()
{
var host = server;
if (!host.StartsWith("http://"))
{
host = "http://" + host;
}
if (!host.EndsWith("/"))
{
host = host + "/";
}
return host + api_communication;
}
public static string GetPostApi(string api)
{
var host = server;
if (!host.StartsWith("http://"))
{
host = "http://" + host;
}
if (!host.EndsWith("/"))
{
host = host + "/";
}
if (api.StartsWith("/"))
api = api.Substring(1);
return host + api;
}
int laststatus = 0;
public void SendLineStatus()
{
try
{
lock (serverclock)
{
bool printlog = false;
DateTime time = DateTime.Now;
//构建发送给服务器的对象
Operation lineOperation = getLineBoxStatus();
if (lineOperation.status != laststatus)
{
laststatus = lineOperation.status;
printlog = true;
}
Operation resultOperation = HttpHelper.Post(GetPostApi(), lineOperation, 700, printlog);
//LogUtil.info(JsonHelper.SerializeObject(resultOperation.data));
ResultProcess(resultOperation);
TimeSpan span = DateTime.Now - time;
if (span.TotalMilliseconds > 700)
{
LogUtil.info(StoreName + "TimerProcess[" + span.TotalMilliseconds + "]");
}
}
}
catch (Exception ex)
{
LogUtil.error("SendLineStatus",ex);
}
}
public bool SendAgvLog(string msg)
{
if (!server.ToLower().StartsWith("http"))
{
return false;
}
lock (serverclock)
{
Operation operation = getLineBoxStatus();
//operation.logList = new List<AgvLogInfo>
//{
// new AgvLogInfo(msg)
//};
//LogUtil.info(JsonHelper.SerializeObject(operation));
Operation resultOperation = HttpHelper.Post(GetPostApi(), operation, 5000);
if (resultOperation == null)
{
LogUtil.info($"SendAgvLog error:{msg}");
return false;
}
LogUtil.info($"SendAgvLog success:{msg}");
}
return true;
}
string mimoCid = ConfigHelper.Config.Get("MIMO_CID", "01");
public bool OpMIMO(string msg, Dictionary<string, string> operate)
{
if (!server.ToLower().StartsWith("http"))
{
return false;
}
lock (serverclock)
{
Operation operation = getLineBoxStatus();
//operation.mimoOpMap = new Dictionary<string, Dictionary<string, string>>();
//Dictionary<string, string> op = new Dictionary<string, string>
//{
// { "Y10", "open" }
//};
//operation.mimoOpMap.Add("1", op);
//operation.mimoOpMap.Add(mimoCid, operate);
LogUtil.info(JsonHelper.SerializeObject(operation));
Operation resultOperation = HttpHelper.Post(GetPostApi(), operation, 1000);
if (resultOperation == null)
{
LogUtil.info($"OpMIMO error:{msg}");
return false;
}
LogUtil.info($"OpMIMO success:{msg}");
}
return true;
}
public int queueTaskCount = -1;
ConcurrentQueue<string> commandResultMsg = new ConcurrentQueue<string>();
void ResultProcess(Operation resultOperation)
{
//发送状态信息到服务器
if (resultOperation == null)
{
//判断服务端是否返回出库操作
return;
}
if (resultOperation.op.Equals(1))
{
var barcode = "";
if (resultOperation.data.ContainsKey("code"))
barcode = resultOperation.data["code"];
//ReviceInStoreProcess(barcode, resultOperation);
}
else if (resultOperation.op.Equals(2))
{
//ReviceOutStoreProcess(resultOperation);
}
else if (resultOperation.op.Equals(5))
{
//ProcessHumidityCMD(resultOperation);
}
//ProcessHumidityCMD(resultOperation);
if (resultOperation.data != null)
{
Dictionary<string, string> dataMap = resultOperation.data;
//折叠门开启状态
if (dataMap.ContainsKey("X16") && dataMap["X16"].Equals("1"))
{
AGVManager.StringDoorIsOpen = true;
}
else
{
AGVManager.StringDoorIsOpen = false;
}
if (dataMap.ContainsKey(ParamDefine.SendEmptyShelf) && dataMap[ParamDefine.SendEmptyShelf].Equals(ParamDefine.doit))
{
AGVManager.CreateAGVTask(STDTaskType.NS200_UNLOAD_B1);
LogUtil.info(StoreName + "收到服务器命令:贴标机 SendEmptyShelf=doit");
}
else if (dataMap.ContainsKey(ParamDefine.SendFullShelf) && dataMap[ParamDefine.SendFullShelf].Equals(ParamDefine.doit))
{
AGVManager.CreateAGVTask(STDTaskType.NS200_UNLOAD_B2);
LogUtil.info(StoreName + "收到服务器命令:贴标机 SendFullShelf=doit");
}
else if (dataMap.ContainsKey(ParamDefine.NeedEmptySHelf) && dataMap[ParamDefine.NeedEmptySHelf].Equals(ParamDefine.doit))
{
AGVManager.CreateAGVTask(STDTaskType.NS200_LOAD_B2);
LogUtil.info(StoreName + "收到服务器命令:贴标机 NeedEmptySHelf=doit");
}
else if (dataMap.ContainsKey(ParamDefine.NeedFullShelf) && dataMap[ParamDefine.NeedFullShelf].Equals(ParamDefine.doit))
{
AGVManager.CreateAGVTask(STDTaskType.NS200_LOAD_B1);
LogUtil.info(StoreName + "收到服务器命令:贴标机 NeedFullShelf=doit");
}
}
//if (resultOperation.mimoOpMap != null)
//{
// foreach (var item in resultOperation.mimoOpMap)
// {
// Dictionary<string, string> dataMap = item.Value;
// if (dataMap.ContainsKey(ParamDefine.SendEmptyShelf) && dataMap[ParamDefine.SendEmptyShelf].Equals(ParamDefine.doit))
// {
// AGVManager.CreateAGVTask(STDTaskType.MIMO_UNLOAD);
// LogUtil.info(StoreName + $"收到服务器命令:MIMO({item.Key}) SendEmptyShelf=doit");
// }
// else if (dataMap.ContainsKey(ParamDefine.NeedEmptySHelf) && dataMap[ParamDefine.NeedEmptySHelf].Equals(ParamDefine.doit))
// {
// LogUtil.info(StoreName + $"收到服务器命令:MIMO({item.Key}) NeedEmptySHelf=doit 不执行");
// }
// else if (dataMap.ContainsKey(ParamDefine.SendFullShelf) && dataMap[ParamDefine.SendFullShelf].Equals(ParamDefine.doit))
// {
// AGVManager.CreateAGVTask(STDTaskType.MIMO_UNLOAD);
// LogUtil.info(StoreName + $"收到服务器命令:MIMO({item.Key}) SendFullShelf=doit");
// }
// else if (dataMap.ContainsKey(ParamDefine.NeedFullShelf) && dataMap[ParamDefine.NeedFullShelf].Equals(ParamDefine.doit))
// {
// AGVManager.CreateAGVTask(STDTaskType.MIMO_LOAD);
// LogUtil.info(StoreName + $"收到服务器命令:MIMO({item.Key}) NeedFullShelf=doit");
// }
// }
//}
}
public Dictionary<string, string> GetAgvBtnStatus()
{
//mimo呼叫agv按钮
Dictionary<string, string> map = new Dictionary<string, string>();
map.Add(ParamDefine.SendEmptyShelf, ParamDefine.disable);
map.Add(ParamDefine.SendFullShelf, ParamDefine.disable);
map.Add(ParamDefine.NeedEmptySHelf, ParamDefine.disable);
map.Add(ParamDefine.NeedFullShelf, ParamDefine.disable);
if (!RobotManage.isRunning)
{
return map;
}
if (RobotManage.mainMachine.IOValue(IO_Type.RightEnd_Check).Equals(IO_VALUE.LOW))
{
map[ParamDefine.NeedFullShelf] = ParamDefine.enable;
}
else
//if (RobotManage.mainMachine.IOValue(IO_Type.RightEnd_Check).Equals(IO_VALUE.HIGH)
// && RobotManage.mainMachine.RightMoveInfo.MoveStep == MoveStep.Wait)
map[ParamDefine.SendEmptyShelf] = ParamDefine.enable;
//if (RobotManage.mainMachine.IOValue(IO_Type.LeftEnd_Check).Equals(IO_VALUE.HIGH)
// && RobotManage.mainMachine.LeftMoveInfo.MoveStep == MoveStep.Wait)
if (RobotManage.mainMachine.IOValue(IO_Type.LeftEnd_Check).Equals(IO_VALUE.HIGH))
map[ParamDefine.SendFullShelf] = ParamDefine.enable;
else
map[ParamDefine.NeedEmptySHelf] = ParamDefine.enable;
return map;
}
private static string GetAddr(string addr, Dictionary<string, string> paramsMap)
{
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;
}
/// <summary>
/// 获取模板信息
/// </summary>
/// <returns></returns>
public List<EquipMsg> StepInformation(out Dictionary<string,string> dic)
{
dic = new Dictionary<string,string>();
List<EquipMsg> equipMsgs = new List<EquipMsg>();
try
{
PropertyInfo[] properties = typeof(DeviceLibrary.MainMachine).GetProperties();
foreach (var property in properties)
{
if (property.PropertyType == typeof(MoveInfo))
{
EquipMsg msg = new EquipMsg();
MoveInfo addressValue = (MoveInfo)property.GetValue(RobotManage.mainMachine);
if (addressValue.Name != crc.GetString("Res0053", "重置"))
{
//msg.msgCode = lang;
msg.module = addressValue.Name;
//msg.msg = addressValue.MoveStep.ToString();
//msg.msgParams = new string[1] { addressValue.lastmsg };
//msg.msgParams = new string[] { addressValue.GetStateStr() };
msg.msg = addressValue.GetStateStr();
equipMsgs.Add(msg);
if (addressValue.Name== crc.GetString("Res0039", "右侧取料"))
{
dic.Add("inShelfType", ShelfType.smallReel.ToString());
dic.Add("inShelfStatus", UnifiedDataHandler.ShelfStatus(true,Convert.ToInt32(addressValue.MoveStep)));
}
if (addressValue.Name== crc.GetString("Res0051", "左侧放料"))
{
dic.Add("outShelfType", ShelfType.smallReel.ToString());
dic.Add("outShelfStatus", UnifiedDataHandler.ShelfStatus(false, Convert.ToInt32(addressValue.MoveStep)));
}
}
}
}
var printstatus = PrinterHelper.LastPrintStatus;
EquipMsg equip = new EquipMsg()
{
module = crc.GetString("Res0216", "打印机"),
msg = (printstatus == CustPrinterStatus.Unknown) ? "Wait" : printstatus.ToString(),
};
}
catch (Exception ex)
{
LogUtil.error($"获取模块信息出错:{ex}!");
}
return equipMsgs;
}
}
public class ResultData
{
//{"code":0,"msg":"ok","data":"7"}
public int code { get; set; }
public string msg { get; set; }
public Dictionary<string, string> data { get; set; }
}
/// <summary>
///1=设备联机(正常就绪)(入库后,BOX恢复原始状态)(出库后,移载装置恢复原始状态),
///2=急停,3=故障,4=警告,5=调试
/// 6=入库执行中,7=入仓完成,8=入仓失败
/// 9=出库执行,10=出仓完成,11=出库失败
/// </summary>
public enum StoreStatus
{
None = 0,
/// <summary>
/// 1=设备联机(正常就绪)(入库后,BOX恢复原始状态)(出库后,移载装置恢复原始状态),
/// </summary>
StoreOnline = 1,
/// <summary>
///2=急停中
/// </summary>
SuddenStop = 2,
/// <summary>
/// 3=故障中
/// </summary>
InTrouble = 3,
/// <summary>
/// 4=警告
/// </summary>
Warning = 4,
/// <summary>
/// 5=设备调试中
/// </summary>
Debugging = 5,
/// <summary>
/// 6=入库执行中
/// </summary>
InStoreExecute = 6,
/// <summary>
/// 7= 入仓位完成(料仓Box把料盘放入对应的库位中,装置还未恢复原始状态)
/// </summary>
InStoreEnd = 7,
/// <summary>
/// 8=入库失败
/// </summary>
InStoreFaild = 8,
/// <summary>
/// 9=出库执行中",
/// </summary>
OutStoreExecute = 9,
/// <summary>
///10= 出仓位完成( 料盘已经放到Box门口)
/// </summary>
OutStoreBoxEnd = 10,
/// <summary>
///11=出库完成
/// </summary>
OutStoreEnd = 11,
/// <summary>
/// 12=移栽出库移栽过程中(移栽完成后变成OnLine)
/// </summary>
OutMoveExecute = 12,
/// <summary>
/// 重置中(原点返回和重置都发此状态)
/// </summary>
ResetMove = 13,
/// <summary>
/// 扫码入库失败
/// </summary>
InStoreError = 14,
}
}