AgvClient.cs
5.4 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
using AGVLib;
using OnlineStore.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace OnlineStore.DeviceLibrary
{
public class AgvClient
{
public static bool CurrCancelState = false;
private static string ServerIp = ConfigAppSettings.GetValue(Setting_Init.AgvServerIp, "127.0.0.1");
private static Client agvClient;
public static List<Node> NodeList = new List<Node>();
private static bool isInit = false;
static int port = 12000;
public static void Init()
{
try
{
LogUtil.info("开始 Init agvclient");
if (!isInit)
{
isInit = true;
agvClient = new Client();
agvClient.ReceivedEvent += AgvClient_ReceivedEvent;
agvClient.ConnectedEvent += AgvClient_ConnectedEvent;
}
LogUtil.info(" 开始 agvClient.Connect");
agvClient.Connect(ServerIp, port);
foreach (var item in NodeList)
{
SetToNone(item.id);
}
}
catch (Exception ex)
{
LogUtil.error("初始化agvClient " + ServerIp + " 出错:", ex);
}
}
static bool connectStr = false;
private static void AgvClient_ConnectedEvent(bool status)
{
if (status != connectStr)
{
connectStr = status;
LogUtil.info($"连接AGV服务器结果:{ServerIp}:{port}" + (status ? "已连接" : "断开"));
}
}
public delegate void ReceiveServerInfoHandlerInfo(Node node);
public static event ReceiveServerInfoHandlerInfo RecvServerInfo;
private static void AgvClient_ReceivedEvent(Node node)
{
RecvServerInfo?.Invoke(node);
}
public static void SetCancelState(bool isCancel)
{
if (agvClient == null)
{
return;
}
CurrCancelState = isCancel;
agvClient.Shielded = CurrCancelState;
}
public static void SetStatus(int id, NodeStatus nodeStatus, string shelfId = "", NodeLevel level = NodeLevel.None, bool isMust = false)
{
if (agvClient == null)
{
return;
}
Node node = getNode(id);
//if (!isMust && level.Equals(NodeLevel.None))
//{
// if (node!=null)
// {
// // NodeStatus currA = node.status; //相同状态就设置一次
// if (node.status.Equals(nodeStatus) && node.shelf_id.Equals(shelfId))
// {
// return;
// }
// }
//}
//string mark = "";
node.status = nodeStatus;
node.shelf_id = shelfId;
node.level = level;
agvClient.SetStatus(node);
UpdateAction(id, node.status);
}
static Node getNode(int id)
{
return NodeList.Find(s => s.id.Equals(id));
}
public static bool MayEnter(int id, string shelfId = "")
{
Node node = getNode(id);
SetStatus(id, NodeStatus.MayEnter, shelfId);
return true;
}
public static bool MayLeave(int id, string shelfId = "")
{
Node node = getNode(id);
SetStatus(id, NodeStatus.MayLeave, shelfId);
return true;
}
public static bool SetToNone(int id, string shelfId = "")
{
Node node = getNode(id);
SetStatus(id, NodeStatus.None, shelfId);
return true;
}
public static bool SetToComplete(int id, string shelfId = "")
{
Node node = getNode(id);
SetStatus(id, NodeStatus.Complete, shelfId);
return true;
}
public static bool FinishEnter(int id, string shelfId = "")
{
SetStatus(id, NodeStatus.FinishEnter, shelfId);
return true;
}
public static bool FinishLeave(int id, string shelfId = "")
{
Node node = getNode(id);
SetStatus(id, NodeStatus.FinishLeave, shelfId);
return true;
}
public static bool NeedLeave(int id, string shelf_id = "")
{
SetStatus(id, NodeStatus.NeedLeave, shelf_id);
return true;
}
public static NodeStatus GetAction(int id)
{
Node node = getNode(id);
if (node != null)
{
return node.status;
}
return NodeStatus.None;
}
public static void UpdateAction(int id, NodeStatus action)
{
Node node = getNode(id);
if (node != null)
{
node.status = action;
}
}
public static void Dispose()
{
try
{
if (agvClient != null)
{
agvClient.Close();
}
}
catch (Exception ex)
{
LogUtil.error("释放 agvClient " + ServerIp + " 出错:", ex);
}
}
}
}