Common.cs
15.2 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
using System;
using System.Collections.Generic;
using System.Net.Sockets;
using System.Runtime.InteropServices;
using System.Threading;
namespace AGVControl
{
/// <summary>
/// 公共参数
/// </summary>
public static class Common
{
/// <summary>
/// 节点信息
/// </summary>
public static List<ClientNode> nodeInfo;
/// <summary>
/// 小车信息
/// </summary>
public static List<Agv_Info> agvInfo;
public static List<string> linePlace;
public static System.Windows.Forms.TextBox logTextBox;
public static BLL.AgvServer server;
public static BLL.Control control;
public static BLL.MiR_API mir;
public static BLL.WebService web;
public static string itsHttp;
public static log4net.ILog log;
public static Dictionary<string, string> agvMission;
public static Dictionary<string, string> agvProductionLine;
public static System.Configuration.Configuration appConfig;
public static readonly string CONFIG_PATH = AppDomain.CurrentDomain.BaseDirectory + "Config\\";
private static string msg = "";
private static int logCnt = 0;
public static void LogInfo(string text, bool isShow = true)
{
if (logTextBox.InvokeRequired)
{
logTextBox.Invoke(new Action(() => LogInfo(text)));
return;
}
if (msg.Equals(text))//连续重复的日志只打印一次
return;
logCnt++;
if (logCnt > 100)
{
logCnt = 0;
msg = "";
}
log.Info(text);
if (isShow)
{
msg += string.Format("[{0}] {1}\r\n", DateTime.Now.ToString("HH:mm:ss"), text);
logTextBox.Text = msg;
}
}
public static void ReadLinePlace()
{
if (!System.IO.File.Exists(CONFIG_PATH + "LinePlace.txt"))
return;
string[] s = System.IO.File.ReadAllLines(CONFIG_PATH + "LinePlace.txt");
linePlace = new List<string>();
linePlace.AddRange(s);
}
public static bool AddLinePlace(string nodeName)
{
int idx = Common.nodeInfo.FindIndex(s => s.Name == nodeName);
if (idx > -1)
{
if (Common.linePlace.Contains(nodeName))
{
LogInfo("AddLinePlace 节点" + nodeName + "已存在任务队列,无需重复添加");
return false;
}
Common.linePlace.Add(nodeName);
System.IO.File.WriteAllLines(CONFIG_PATH + "LinePlace.txt", linePlace);
return true;
}
else
{
log.Error("AddLinePlace 失败 节点" + nodeName + "不存在");
return false;
}
}
public static void RemoveLinePlace(string nodeName)
{
Common.linePlace.Remove(nodeName);
System.IO.File.WriteAllLines(CONFIG_PATH + "LinePlace.txt", linePlace);
}
public static void CheckAGVMissionState()
{
foreach (Agv_Info agv in agvInfo)
{
Thread.Sleep(50);
bool rtn = Common.mir.Get_Register(agv, 20, out int regValue);
if (rtn)
{
agv.GetPlace(regValue);
Common.LogInfo(string.Format("软件开启:{0} Get_Register PLC{1}={2} Place={3} PlaceState={4}", agv.Name, 20, regValue, agv.Place, agv.PlaceState));
if (!agv.Place.Equals(""))
{
int idx = nodeInfo.FindIndex(s => s.Name == agv.Place);
if (idx > -1)
{
nodeInfo[idx].AgvName = agv.Name;
}
agv.TaskSend = "Move" + agv.Place;
}
}
else
{
Common.log.Error("CheckAGVMissionState 获取PLC20失败");
}
}
}
}
public static class API
{
[DllImport("user32.dll", EntryPoint = "ShowWindow", CharSet = CharSet.Auto)]
public static extern int ShowWindow(IntPtr hwnd, int nCmdShow);
[DllImport("user32.dll ", SetLastError = true)]
public static extern void SwitchToThisWindow(IntPtr hWnd, bool fAltTab);
public const int SW_RESTORE = 9;
}
/// <summary>
/// 客户端
/// </summary>
public class Client
{
/// <summary>
/// 循环
/// </summary>
public bool Loop;
/// <summary>
/// IP地址
/// </summary>
public string IP;
/// <summary>
/// 是否连接
/// </summary>
public bool IsConn;
/// <summary>
/// 节点名称
/// </summary>
public List<string> nodeName;
/// <summary>
/// 套接字
/// </summary>
public Socket Socket;
/// <summary>
/// 接收数据线程
/// </summary>
public Thread ListenNet;
}
/// <summary>
/// 客户端的节点
/// </summary>
public class ClientNode
{
private string rfid = "00";
/// <summary>
/// 节点名称
/// </summary>
public string Name { set; get; }
/// <summary>
/// IP地址
/// </summary>
public string IP { set; get; }
/// <summary>
/// RFID
/// </summary>
public string RFID
{
set
{
if (value.Length < 2)
rfid = value.PadLeft(2, '0');
else
rfid = value;
}
get
{
return rfid;
}
}
/// <summary>
/// 动作
/// </summary>
public ClientAction Action { set; get; }
/// <summary>
/// 小车名称
/// </summary>
public string AgvName { set; get; }
/// <summary>
/// 在线
/// </summary>
public bool Online { set; get; }
/// <summary>
/// 是否可用
/// </summary>
public bool IsUse { set; get; }
/// <summary>
/// 客户端节点
/// </summary>
/// <param name="name"></param>
/// <param name="ip"></param>
/// <param name="isUse"></param>
public ClientNode(string name, string ip, bool isUse)
{
Name = name;
IP = ip;
RFID = rfid;
Action = ClientAction.None;
AgvName = "";
Online = false;
IsUse = isUse;
}
/// <summary>
/// 客户端节点
/// </summary>
/// <param name="name"></param>
/// <param name="rfid"></param>
/// <param name="action"></param>
/// <param name="level"></param>
public ClientNode(string name, string rfid = "", ClientAction action = ClientAction.None)
{
Name = name;
RFID = rfid;
Action = action;
AgvName = "";
Online = false;
IsUse = false;
}
/// <summary>
/// 所有属性的文本形式
/// </summary>
/// <returns></returns>
public string ToText()
{
string s = string.Format("Name={0}, Action={1}, RFID={2}", Name, Action, RFID);
return s;
}
public string[] ToRow()
{
//节点,IP,动作,RFID,AGV名称,在线,调用,清除AGV
string[] s = new string[8];
s[0] = Name;
s[1] = IP;
if (Online)
{
s[2] = Action.ToString();
s[3] = RFID;
s[4] = AgvName;
}
s[5] = Online.ToString();
s[6] = IsUse.ToString();
s[7] = "Clear";
return s;
}
/// <summary>
/// 脱机
/// </summary>
public void Offline()
{
RFID = "00";
Action = ClientAction.None;
AgvName = "";
Online = false;
}
}
public class Agv_Info
{
/// <summary>
/// 小车名称
/// </summary>
public string Name { private set; get; }
/// <summary>
/// 小车在FLEET中的ID号
/// </summary>
public string ID { private set; get; }
/// <summary>
/// IP地址
/// </summary>
public string IP { private set; get; }
/// <summary>
/// 授权码
/// </summary>
public string Authorization { private set; get; }
/// <summary>
/// 当前架子的RFID
/// </summary>
public string RFID { set; get; }
/// <summary>
/// 是否在线
/// </summary>
public bool IsCon { set; get; }
/// <summary>
/// 是否可用
/// </summary>
public bool IsUse { set; get; }
/// <summary>
/// 地点
/// </summary>
public string Place { set; get; }
/// <summary>
/// 地点的状态
/// </summary>
public PlaceState PlaceState { private set; get; }
/// <summary>
/// 小车的状态ID,(从小车获取)
/// </summary>
public int StateID { private set; get; }
/// <summary>
/// 小车的状态,(从小车获取)
/// </summary>
public string StateText { private set; get; }
/// <summary>
/// 电量百分比,(从小车获取)
/// </summary>
public int Battery { private set; get; }
/// <summary>
/// 当前任务文本,(从小车获取)
/// </summary>
public string MissionText { set; get; }
/// <summary>
/// 下一个地点
/// </summary>
public string NextPlace { set; get; }
/// <summary>
/// 关门,用于执行一次
/// </summary>
//public bool CloseDoor { set; get; }
/// <summary>
/// 任务发送
/// </summary>
public string TaskSend { set; get; }
/// <summary>
/// 闲置等待时间,用于充电
/// </summary>
//public int WaitTime { set; get; }
/// <summary>
/// 临时待机位,用于执行一次
/// </summary>
//public bool StandbyTemp { set; get; }
public struct DockingStru
{
public DateTime startTime;
public bool IsDocking;
}
/// <summary>
/// 停靠信息
/// </summary>
public DockingStru DockingInfo;
public Agv_Info(string id, string name, string ip, string authorization, bool isUse)
{
ID = id;
Name = name;
IP = ip;
Authorization = authorization;
RFID = "";
IsCon = false;
IsUse = isUse;
Place = "";
TaskSend = "";
NextPlace = "";
DockingInfo.IsDocking = false;
DockingInfo.startTime = DateTime.Now;
}
public bool SetState(int stateID, string stateText, int battery, string missionText)
{
bool isChange = false;
if (!StateID.Equals(stateID) || !StateText.Equals(stateText) || !Battery.Equals(battery) || !MissionText.Equals(missionText))
isChange = true;
StateID = stateID;
StateText = stateText;
Battery = battery;
MissionText = missionText;
return isChange;
}
public void GetPlace(int value)
{
if (value < 1000)
{
Place = "";
PlaceState = PlaceState.None;
TaskSend = "";
}
else
{
int a = value / 1000;
int b = (value - a * 1000) / 10;
int c = value - a * 1000 - b * 10;
if (c.Equals(1) || c.Equals(3) || c.Equals(5))
TaskSend = "";
Place = (char)(64 + a) + b.ToString();
PlaceState = (PlaceState)c;
}
}
public string[] ToRow()
{
//AGV名称,IP,AGV状态,任务状态,地点,后续任务,在线,电量,调用,清除缓存
List<string> s = new List<string>();
s.Add(Name);
s.Add(IP);
if (IsCon)
{
s.Add(StateText);
s.Add(MissionText);
s.Add(Place);
s.Add(NextPlace);
}
else
{
s.AddRange(new List<string> { "", "", "", "" });
}
s.Add(IsCon.ToString());
s.Add(Battery + "%");
s.Add(IsUse.ToString());
s.Add("Clear");
return s.ToArray();
}
}
/// <summary>
/// 客户端的动作
/// </summary>
public enum ClientAction : byte
{
/// <summary>
/// 没有动作
/// </summary>
None = 0,
/// <summary>
/// 需要7寸D料架
/// </summary>
NeedD = 1,
/// <summary>
/// 需要大尺寸C料架
/// </summary>
NeedC = 2,
/// <summary>
/// 需要进入料架
/// </summary>
NeedEnter = 3,
/// <summary>
/// 需要出去料架
/// </summary>
NeedLeave = 4,
/// <summary>
/// 需要进入离开料架
/// </summary>
NeedEnterLeave = 5,
/// <summary>
/// 准备进入,服务器发送
/// </summary>
ReadyEnter = 6,
/// <summary>
/// 可以进入料架
/// </summary>
MayEnter = 7,
/// <summary>
/// 完成进入料架
/// </summary>
FinishEnter = 8,
/// <summary>
/// 准备离开,服务器发送
/// </summary>
ReadyLeave = 9,
/// <summary>
/// 可以出去料架
/// </summary>
MayLeave = 10,
/// <summary>
/// 完成出去料架
/// </summary>
FinishLeave = 11,
}
/// <summary>
/// 地点状态
/// </summary>
public enum PlaceState
{
/// <summary>
/// 没有任务
/// </summary>
None = 0,
/// <summary>
/// 小车移动任务
/// </summary>
Move = 1,
/// <summary>
/// 小车移动任务完成
/// </summary>
MoveFinish = 2,
/// <summary>
/// 小车Enter任务
/// </summary>
Enter = 3,
/// <summary>
/// 小车Enter任务完成
/// </summary>
EnterFinish = 4,
/// <summary>
/// 小车Leave任务
/// </summary>
Leave = 5,
/// <summary>
/// 小车Leave任务完成
/// </summary>
LeaveFinish = 6,
/// <summary>
/// 出错
/// </summary>
Error = 9
}
}