Common.cs
24.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
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
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
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 System.Windows.Forms.TextBox logTextBox;
public static System.Windows.Forms.DataGridView missionView;
public static BLL.AgvServer server;
public static BLL.Control control;
public static BLL.MiR_API mir;
public static BLL.WebService web;
public static ChargeStatus chargeStatus;
public static string itsHttp;
public static log4net.ILog log;
public static Dictionary<string, string> agvMission;
public static Dictionary<string, string> showNameMissionName;
public static Dictionary<string, string> agvProductionLine;
public static System.Configuration.Configuration appConfig;
public static MissionManager missionManager;
public static readonly string CONFIG_PATH = AppDomain.CurrentDomain.BaseDirectory + "Config\\";
/// <summary>
/// E21 E22 是否允许离开/进入
/// </summary>
public static bool IsAllowLeaveOrEnter = false;
private static List<string> msg = new List<string>();
private static string preLog = "";
public static void LogInfo(string text, bool isShow = true)
{
if (logTextBox.InvokeRequired)
{
logTextBox.Invoke(new Action(() => LogInfo(text, isShow)));
return;
}
if (preLog.Equals(text))//连续重复的日志只打印一次
return;
preLog = text;
if (msg.Count > 25)
{
msg.RemoveRange(0,10);
}
log.Info(text);
string tmpStr="";
if (isShow)
{
msg.Add(string.Format("[{0}] {1}\r\n", DateTime.Now.ToString("HH:mm:ss"), text));
msg.ForEach(s=> tmpStr+= s);
logTextBox.Text = tmpStr;
}
}
public static void ReadLinePlace()
{
if (!System.IO.File.Exists(CONFIG_PATH + "LinePlace.txt"))
{
File.Create(CONFIG_PATH + "LinePlace.txt");
return;
}
string[] s = System.IO.File.ReadAllLines(CONFIG_PATH + "LinePlace.txt");
for (int i = 0; i < s.Count(); i++)
{
string[] mission = s[i].Split(',');
if (mission.Length != 2)
continue;
missionManager.missionList.Add(new MissionStru(mission[0], mission[1]));
}
}
public static bool AddLinePlace(string nodeName)
{
int idx = Common.nodeInfo.FindIndex(s => s.Name == nodeName);
if (idx > -1)
{
Common.missionManager.missionList.Add(new MissionStru(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),nodeName));
using (System.IO.StreamWriter file = new System.IO.StreamWriter(CONFIG_PATH + "LinePlace.txt"))
{
foreach (var item in Common.missionManager.missionList)
{
file.WriteLine(string.Format("{0},{1}",item.CreateTime, item.NodeName));
}
}
return true;
}
else
{
log.Error("AddLinePlace 失败 节点" + nodeName + "不存在");
return false;
}
}
public static bool DelLinePlace(string nodeName)
{
int idx = Common.nodeInfo.FindIndex(s => s.Name == nodeName);
if (idx > -1)
{
idx = Common.missionManager.missionList.FindIndex(s => s.NodeName == nodeName);
if(idx>-1)
{
Common.missionManager.missionList.RemoveAt(idx);
using (System.IO.StreamWriter file = new System.IO.StreamWriter(CONFIG_PATH + "LinePlace.txt"))
{
foreach (var item in Common.missionManager.missionList)
{
file.WriteLine(string.Format("{0},{1}", item.CreateTime, item.NodeName));
}
}
return true;
}
log.Error("DelLinePlace 失败 节点任务" + nodeName + "不存在");
return false;
}
else
{
log.Error("DelLinePlace 失败 节点" + nodeName + "不存在");
return false;
}
}
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.log.Debug(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.Debug("CheckAGVMissionState 获取PLC20失败");
}
}
}
public static void GetNodesPosition()
{
Agv_Info agv = agvInfo[0];
foreach (ClientNode clientNode in nodeInfo)
{
Thread.Sleep(50);
bool rtn = Common.mir.Get_Node_Pos(agv, clientNode, out BLL.MirPosition mirPosition);
if (rtn)
{
clientNode.position.X = mirPosition.pos_x;
clientNode.position.Y = mirPosition.pos_y;
Common.log.Debug(string.Format("软件开启:{0} 获取节点位置({1},{2})", clientNode.Name, clientNode.position.X, clientNode.position.Y));
}
else
{
Common.log.Debug("GetNodesPosition 获取节点位置失败");
}
}
}
}
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>
/// 节点位置的guid
/// </summary>
public string Pos_guid { set; get; }
/// <summary>
/// 节点位置名称
/// </summary>
public string Pos_name { get; set; }
/// <summary>
/// 节点位置坐标
/// </summary>
public PositionStru position;
public string AliceName { get; set; }
/// <summary>
/// 客户端节点
/// </summary>
/// <param name="name"></param>
/// <param name="ip"></param>
/// <param name="isUse"></param>
public ClientNode(string name, string ip, string aliceName, string pos_name, string pos_guid, bool isUse)
{
Name = name;
IP = ip;
AliceName = aliceName;
RFID = rfid;
Action = ClientAction.None;
AgvName = "";
Online = false;
IsUse = isUse;
this.Pos_name = pos_name;
this.Pos_guid = pos_guid;
}
/// <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[9];
s[0] = AliceName;
s[1] = IP;
if (Online)
{
s[2] = string.Format("({0},{1})", position.X.ToString("f2"), position.Y.ToString("f2"));
s[3] = Action.ToString();
s[4] = RFID;
s[5] = AgvName;
}
s[6] = Online?"在线":"离线";
s[7] = IsUse?"是":"否";
s[8] = "清除";
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>
/// IO模块GUID
/// </summary>
public string IOID { 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 string PlaceAliceName { set; get; } = "";
/// <summary>
/// 地点的状态
/// </summary>
public PlaceState PlaceState { private set; get; }
/// <summary>
/// 小车的状态ID,(从小车获取)
/// </summary>
public int StateID { private set; get; }
/// <summary>
/// 负载
/// </summary>
public bool IsExistShelf { private set; get; } = false;
/// <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 string NextPlaceAliceName { 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 class clsPosition
{
public double orientation { get; set; }
public double x { get; set; }
public double y { get; set; }
}
/// <summary>
/// 当前位置
/// </summary>
public clsPosition Position;
/// <summary>
/// 任务内容
/// </summary>
public string Msg { get; set; }
public Agv_Info(string id, string name, string ip, string authorization, string ioID, bool isUse,string rfid)
{
ID = id;
Name = name;
IP = ip;
Authorization = authorization;
IOID = ioID;
RFID = rfid;
IsCon = false;
IsUse = isUse;
Place = "";
TaskSend = "";
NextPlace = "";
Msg = "";
DockingInfo.IsDocking = false;
DockingInfo.startTime = DateTime.Now;
Position = new Agv_Info.clsPosition();
}
DateTime IoStartTime = DateTime.MaxValue;
int IoLastTime = 5000;
public bool SetState(int stateID, string stateText, int battery, string missionText, clsPosition position, bool[] input, bool[] output)
{
bool isChange = false;
bool preShelfState = IsExistShelf;
if (input != null && input.Length == 4)
{
bool reachIoValue = input[3].Equals(true);//input[2] &&
if (reachIoValue)
{
//满足给定的IO值,计算持续时间
if (IoStartTime == DateTime.MaxValue)
{
IoStartTime = DateTime.Now;
}
if (IoLastTime > 0)
{
//持续时间
TimeSpan lastTimeSpan = DateTime.Now - IoStartTime;
IsExistShelf = (lastTimeSpan.TotalMilliseconds >= IoLastTime);
}
else
{
IsExistShelf = true;
}
}
else
{
//重新计时
IoStartTime = DateTime.MinValue;
IsExistShelf = input[3];
}
}
if (!StateID.Equals(stateID) || !StateText.Equals(stateText)
|| !MissionText.Equals(missionText) || !IsExistShelf.Equals(preShelfState)) //!Battery.Equals(battery)
isChange = true;
StateID = stateID;
StateText = stateText;
Battery = battery;
MissionText = missionText;
Position = position;
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))//Move,Enter,Leave
TaskSend = "";
Place = (char)(64 + a) + b.ToString();
if(Common.nodeInfo !=null && Common.nodeInfo.Count !=0)
{
ClientNode node = Common.nodeInfo.Find(s => s.Name == Place);
if (node != null)
PlaceAliceName = node.AliceName;
else
PlaceAliceName = "";
}
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(RFID);
s.Add(Msg);
s.Add(MissionText);
s.Add(PlaceAliceName);
s.Add(PlaceState.ToString());
if (Common.nodeInfo != null && Common.nodeInfo.Count != 0)
{
ClientNode node = Common.nodeInfo.Find(q => q.Name == NextPlace);
if (node != null)
NextPlaceAliceName = node.AliceName;
else
NextPlaceAliceName = "";
}
s.Add(NextPlaceAliceName);
}
else
{
s.AddRange(new List<string> { "", "","", "", "", "", "" });
}
s.Add(IsExistShelf ? "满载" : "空载");
s.Add(IsCon?"在线":"离线");
s.Add(Battery + "%");
s.Add(IsUse?"是":"否");
s.Add("清空");
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
}
public struct PositionStru
{
public double X;
public double Y;
}
public class ChargeStatus
{
/// <summary>
/// 1号充电桩的AGV名称
/// </summary>
public string charge3 = "";
/// <summary>
/// 1号充电桩的AGV名称
/// </summary>
public string charge4 = "";
/// <summary>
/// 1号充电桩的AGV名称
/// </summary>
public string charge5 = "";
/// <summary>
/// 1号充电桩的AGV名称
/// </summary>
public string charge6 = "";
/// <summary>
/// 充电等待时间(s)
/// </summary>
public int chargeWait = 0;
/// <summary>
/// 充电最大电量,小于该值等待指定时间去充电
/// </summary>
public int chargeMax;
/// <summary>
/// 充电最小电量,小于该值直接去充电
/// </summary>
public int chargeMin;
/// <summary>
/// 两车充电间隔时间(ms)
/// </summary>
public long chargeInterval;
private bool _autoCharge;
public bool AutoCharge
{
set
{
_autoCharge = value;
Common.appConfig.AppSettings.Settings["AutoCharge"].Value = value.ToString();
Common.appConfig.Save();
}
get
{
return _autoCharge;
}
}
public ChargeStatus()
{
_autoCharge = Convert.ToBoolean(Common.appConfig.AppSettings.Settings["AutoCharge"].Value);
chargeWait = Convert.ToInt32(Common.appConfig.AppSettings.Settings["ChargeWait"].Value);
string s = Common.appConfig.AppSettings.Settings["ChargeThreshold"].Value;
string[] arr = s.Split(',');
chargeMin = Convert.ToInt32(arr[0]);
chargeMax = Convert.ToInt32(arr[1]);
chargeInterval = 0;
}
}
/// <summary>
/// 任务信息结构
/// </summary>
public struct MissionStru
{
public string NodeName;
public string CreateTime;
/// <summary>
/// 任务结构
/// </summary>
/// <param name="dateTime">创建时间</param>
/// <param name="name">节点名称</param>
public MissionStru(string dateTime, string name)
{
NodeName = name;
CreateTime = dateTime;
}
}
/// <summary>
/// 任务管理
/// </summary>
public class MissionManager
{
/// <summary>
/// 任务列表
/// </summary>
public List<MissionStru> missionList;
/// <summary>
/// 主动执行空架任务的小车名称
/// </summary>
public string AGV_Name_EmptyTask { get; set; } = "";
public MissionManager()
{
missionList = new List<MissionStru>();
}
}
}